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