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