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