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