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