Move enum to more appropriate class
[Sone.git] / src / main / java / net / pterodactylus / sone / data / Sone.java
1 /*
2  * Sone - Sone.java - Copyright © 2010–2016 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.data;
19
20 import static com.google.common.collect.FluentIterable.from;
21 import static java.util.Arrays.asList;
22 import static net.pterodactylus.sone.data.Album.FLATTENER;
23 import static net.pterodactylus.sone.data.Album.IMAGES;
24
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.Comparator;
28 import java.util.List;
29 import java.util.Set;
30
31 import javax.annotation.Nonnull;
32 import javax.annotation.Nullable;
33
34 import net.pterodactylus.sone.freenet.wot.Identity;
35 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
36 import net.pterodactylus.sone.template.SoneAccessor;
37
38 import freenet.keys.FreenetURI;
39
40 import com.google.common.base.Function;
41 import com.google.common.base.Predicate;
42 import com.google.common.primitives.Ints;
43
44 /**
45  * A Sone defines everything about a user: her profile, her status updates, her
46  * replies, her likes and dislikes, etc.
47  *
48  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
49  */
50 public interface Sone extends Identified, Fingerprintable, Comparable<Sone> {
51
52         /**
53          * Enumeration for the possible states of a {@link Sone}.
54          *
55          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
56          */
57         public enum SoneStatus {
58
59                 /** The Sone is unknown, i.e. not yet downloaded. */
60                 unknown,
61
62                 /** The Sone is idle, i.e. not being downloaded or inserted. */
63                 idle,
64
65                 /** The Sone is currently being inserted. */
66                 inserting,
67
68                 /** The Sone is currently being downloaded. */
69                 downloading,
70         }
71
72         /** comparator that sorts Sones by their nice name. */
73         public static final Comparator<Sone> NICE_NAME_COMPARATOR = new Comparator<Sone>() {
74
75                 @Override
76                 public int compare(Sone leftSone, Sone rightSone) {
77                         int diff = SoneAccessor.getNiceName(leftSone).compareToIgnoreCase(SoneAccessor.getNiceName(rightSone));
78                         if (diff != 0) {
79                                 return diff;
80                         }
81                         return leftSone.getId().compareToIgnoreCase(rightSone.getId());
82                 }
83
84         };
85
86         /** Comparator that sorts Sones by last activity (least recent active first). */
87         public static final Comparator<Sone> LAST_ACTIVITY_COMPARATOR = new Comparator<Sone>() {
88
89                 @Override
90                 public int compare(Sone firstSone, Sone secondSone) {
91                         return (int) Math.min(Integer.MAX_VALUE, Math.max(Integer.MIN_VALUE, secondSone.getTime() - firstSone.getTime()));
92                 }
93         };
94
95         /** Comparator that sorts Sones by numbers of posts (descending). */
96         public static final Comparator<Sone> POST_COUNT_COMPARATOR = new Comparator<Sone>() {
97
98                 /**
99                  * {@inheritDoc}
100                  */
101                 @Override
102                 public int compare(Sone leftSone, Sone rightSone) {
103                         return (leftSone.getPosts().size() != rightSone.getPosts().size()) ? (rightSone.getPosts().size() - leftSone.getPosts().size()) : (rightSone.getReplies().size() - leftSone.getReplies().size());
104                 }
105         };
106
107         /** Comparator that sorts Sones by number of images (descending). */
108         public static final Comparator<Sone> IMAGE_COUNT_COMPARATOR = new Comparator<Sone>() {
109
110                 /**
111                  * {@inheritDoc}
112                  */
113                 @Override
114                 public int compare(Sone leftSone, Sone rightSone) {
115                         int rightSoneImageCount = from(asList(rightSone.getRootAlbum())).transformAndConcat(FLATTENER).transformAndConcat(IMAGES).size();
116                         int leftSoneImageCount = from(asList(leftSone.getRootAlbum())).transformAndConcat(FLATTENER).transformAndConcat(IMAGES).size();
117                         /* sort descending. */
118                         return Ints.compare(rightSoneImageCount, leftSoneImageCount);
119                 }
120         };
121
122         /** Filter to remove Sones that have not been downloaded. */
123         public static final Predicate<Sone> EMPTY_SONE_FILTER = new Predicate<Sone>() {
124
125                 @Override
126                 public boolean apply(Sone sone) {
127                         return (sone != null) && (sone.getTime() != 0);
128                 }
129         };
130
131         /** Filter that matches all {@link Sone#isLocal() local Sones}. */
132         public static final Predicate<Sone> LOCAL_SONE_FILTER = new Predicate<Sone>() {
133
134                 @Override
135                 public boolean apply(Sone sone) {
136                         return (sone != null) && (sone.getIdentity() instanceof OwnIdentity);
137                 }
138
139         };
140
141         /** Filter that matches Sones that have at least one album. */
142         public static final Predicate<Sone> HAS_ALBUM_FILTER = new Predicate<Sone>() {
143
144                 @Override
145                 public boolean apply(Sone sone) {
146                         return (sone != null) && !sone.getRootAlbum().getAlbums().isEmpty();
147                 }
148         };
149
150         public static final Function<Sone, List<Album>> toAllAlbums = new Function<Sone, List<Album>>() {
151                 @Override
152                 public List<Album> apply(@Nullable Sone sone) {
153                         return (sone == null) ? Collections.<Album>emptyList() : FLATTENER.apply(
154                                         sone.getRootAlbum());
155                 }
156         };
157
158         public static final Function<Sone, List<Image>> toAllImages = new Function<Sone, List<Image>>() {
159                 @Override
160                 public List<Image> apply(@Nullable Sone sone) {
161                         return (sone == null) ? Collections.<Image>emptyList() :
162                                         from(FLATTENER.apply(sone.getRootAlbum()))
163                                                         .transformAndConcat(IMAGES).toList();
164                 }
165         };
166
167         /**
168          * Returns the identity of this Sone.
169          *
170          * @return The identity of this Sone
171          */
172         Identity getIdentity();
173
174         /**
175          * Returns the name of this Sone.
176          *
177          * @return The name of this Sone
178          */
179         String getName();
180
181         /**
182          * Returns whether this Sone is a local Sone.
183          *
184          * @return {@code true} if this Sone is a local Sone, {@code false} otherwise
185          */
186         boolean isLocal();
187
188         /**
189          * Returns the request URI of this Sone.
190          *
191          * @return The request URI of this Sone
192          */
193         FreenetURI getRequestUri();
194
195         /**
196          * Returns the insert URI of this Sone.
197          *
198          * @return The insert URI of this Sone
199          */
200         FreenetURI getInsertUri();
201
202         /**
203          * Returns the latest edition of this Sone.
204          *
205          * @return The latest edition of this Sone
206          */
207         long getLatestEdition();
208
209         /**
210          * Sets the latest edition of this Sone. If the given latest edition is not
211          * greater than the current latest edition, the latest edition of this Sone is
212          * not changed.
213          *
214          * @param latestEdition
215          *              The latest edition of this Sone
216          */
217         void setLatestEdition(long latestEdition);
218
219         /**
220          * Return the time of the last inserted update of this Sone.
221          *
222          * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
223          */
224         long getTime();
225
226         /**
227          * Sets the time of the last inserted update of this Sone.
228          *
229          * @param time
230          *              The time of the update (in milliseconds since Jan 1, 1970 UTC)
231          * @return This Sone (for method chaining)
232          */
233         Sone setTime(long time);
234
235         /**
236          * Returns the status of this Sone.
237          *
238          * @return The status of this Sone
239          */
240         SoneStatus getStatus();
241
242         /**
243          * Sets the new status of this Sone.
244          *
245          * @param status
246          *              The new status of this Sone
247          * @return This Sone
248          * @throws IllegalArgumentException
249          *              if {@code status} is {@code null}
250          */
251         Sone setStatus(SoneStatus status);
252
253         /**
254          * Returns a copy of the profile. If you want to update values in the profile
255          * of this Sone, update the values in the returned {@link Profile} and use
256          * {@link #setProfile(Profile)} to change the profile in this Sone.
257          *
258          * @return A copy of the profile
259          */
260         Profile getProfile();
261
262         /**
263          * Sets the profile of this Sone. A copy of the given profile is stored so that
264          * subsequent modifications of the given profile are not reflected in this
265          * Sone!
266          *
267          * @param profile
268          *              The profile to set
269          */
270         void setProfile(Profile profile);
271
272         /**
273          * Returns the client used by this Sone.
274          *
275          * @return The client used by this Sone, or {@code null}
276          */
277         Client getClient();
278
279         /**
280          * Sets the client used by this Sone.
281          *
282          * @param client
283          *              The client used by this Sone, or {@code null}
284          * @return This Sone (for method chaining)
285          */
286         Sone setClient(Client client);
287
288         /**
289          * Returns whether this Sone is known.
290          *
291          * @return {@code true} if this Sone is known, {@code false} otherwise
292          */
293         boolean isKnown();
294
295         /**
296          * Sets whether this Sone is known.
297          *
298          * @param known
299          *              {@code true} if this Sone is known, {@code false} otherwise
300          * @return This Sone
301          */
302         Sone setKnown(boolean known);
303
304         /**
305          * Returns all friend Sones of this Sone.
306          *
307          * @return The friend Sones of this Sone
308          */
309         Collection<String> getFriends();
310
311         /**
312          * Returns whether this Sone has the given Sone as a friend Sone.
313          *
314          * @param friendSoneId
315          *              The ID of the Sone to check for
316          * @return {@code true} if this Sone has the given Sone as a friend, {@code
317          *         false} otherwise
318          */
319         boolean hasFriend(String friendSoneId);
320
321         /**
322          * Returns the list of posts of this Sone, sorted by time, newest first.
323          *
324          * @return All posts of this Sone
325          */
326         List<Post> getPosts();
327
328         /**
329          * Sets all posts of this Sone at once.
330          *
331          * @param posts
332          *              The new (and only) posts of this Sone
333          * @return This Sone (for method chaining)
334          */
335         Sone setPosts(Collection<Post> posts);
336
337         /**
338          * Adds the given post to this Sone. The post will not be added if its {@link
339          * Post#getSone() Sone} is not this Sone.
340          *
341          * @param post
342          *              The post to add
343          */
344         void addPost(Post post);
345
346         /**
347          * Removes the given post from this Sone.
348          *
349          * @param post
350          *              The post to remove
351          */
352         void removePost(Post post);
353
354         /**
355          * Returns all replies this Sone made.
356          *
357          * @return All replies this Sone made
358          */
359         Set<PostReply> getReplies();
360
361         /**
362          * Sets all replies of this Sone at once.
363          *
364          * @param replies
365          *              The new (and only) replies of this Sone
366          * @return This Sone (for method chaining)
367          */
368         Sone setReplies(Collection<PostReply> replies);
369
370         /**
371          * Adds a reply to this Sone. If the given reply was not made by this Sone,
372          * nothing is added to this Sone.
373          *
374          * @param reply
375          *              The reply to add
376          */
377         void addReply(PostReply reply);
378
379         /**
380          * Removes a reply from this Sone.
381          *
382          * @param reply
383          *              The reply to remove
384          */
385         void removeReply(PostReply reply);
386
387         /**
388          * Returns the IDs of all liked posts.
389          *
390          * @return All liked posts’ IDs
391          */
392         Set<String> getLikedPostIds();
393
394         /**
395          * Sets the IDs of all liked posts.
396          *
397          * @param likedPostIds
398          *              All liked posts’ IDs
399          * @return This Sone (for method chaining)
400          */
401         Sone setLikePostIds(Set<String> likedPostIds);
402
403         /**
404          * Checks whether the given post ID is liked by this Sone.
405          *
406          * @param postId
407          *              The ID of the post
408          * @return {@code true} if this Sone likes the given post, {@code false}
409          *         otherwise
410          */
411         boolean isLikedPostId(String postId);
412
413         /**
414          * Adds the given post ID to the list of posts this Sone likes.
415          *
416          * @param postId
417          *              The ID of the post
418          * @return This Sone (for method chaining)
419          */
420         Sone addLikedPostId(String postId);
421
422         /**
423          * Removes the given post ID from the list of posts this Sone likes.
424          *
425          * @param postId
426          *              The ID of the post
427          * @return This Sone (for method chaining)
428          */
429         Sone removeLikedPostId(String postId);
430
431         /**
432          * Returns the IDs of all liked replies.
433          *
434          * @return All liked replies’ IDs
435          */
436         Set<String> getLikedReplyIds();
437
438         /**
439          * Sets the IDs of all liked replies.
440          *
441          * @param likedReplyIds
442          *              All liked replies’ IDs
443          * @return This Sone (for method chaining)
444          */
445         Sone setLikeReplyIds(Set<String> likedReplyIds);
446
447         /**
448          * Checks whether the given reply ID is liked by this Sone.
449          *
450          * @param replyId
451          *              The ID of the reply
452          * @return {@code true} if this Sone likes the given reply, {@code false}
453          *         otherwise
454          */
455         boolean isLikedReplyId(String replyId);
456
457         /**
458          * Adds the given reply ID to the list of replies this Sone likes.
459          *
460          * @param replyId
461          *              The ID of the reply
462          * @return This Sone (for method chaining)
463          */
464         Sone addLikedReplyId(String replyId);
465
466         /**
467          * Removes the given post ID from the list of replies this Sone likes.
468          *
469          * @param replyId
470          *              The ID of the reply
471          * @return This Sone (for method chaining)
472          */
473         Sone removeLikedReplyId(String replyId);
474
475         /**
476          * Returns the root album that contains all visible albums of this Sone.
477          *
478          * @return The root album of this Sone
479          */
480         Album getRootAlbum();
481
482         /**
483          * Returns Sone-specific options.
484          *
485          * @return The options of this Sone
486          */
487         SoneOptions getOptions();
488
489         /**
490          * Sets the options of this Sone.
491          *
492          * @param options
493          *              The options of this Sone
494          */
495         /* TODO - remove this method again, maybe add an option provider */
496         void setOptions(SoneOptions options);
497
498 }