Remove setIdentity() from 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.database.PostBuilder;
33 import net.pterodactylus.sone.database.PostReplyBuilder;
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, 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          * Returns the name of this Sone.
182          *
183          * @return The name of this Sone
184          */
185         String getName();
186
187         /**
188          * Returns whether this Sone is a local Sone.
189          *
190          * @return {@code true} if this Sone is a local Sone, {@code false} otherwise
191          */
192         boolean isLocal();
193
194         /**
195          * Returns the request URI of this Sone.
196          *
197          * @return The request URI of this Sone
198          */
199         FreenetURI getRequestUri();
200
201         /**
202          * Sets the request URI of this Sone.
203          *
204          * @param requestUri
205          *              The request URI of this Sone
206          * @return This Sone (for method chaining)
207          */
208         Sone setRequestUri(FreenetURI requestUri);
209
210         /**
211          * Returns the insert URI of this Sone.
212          *
213          * @return The insert URI of this Sone
214          */
215         FreenetURI getInsertUri();
216
217         /**
218          * Sets the insert URI of this Sone.
219          *
220          * @param insertUri
221          *              The insert URI of this Sone
222          * @return This Sone (for method chaining)
223          */
224         Sone setInsertUri(FreenetURI insertUri);
225
226         /**
227          * Returns the latest edition of this Sone.
228          *
229          * @return The latest edition of this Sone
230          */
231         long getLatestEdition();
232
233         /**
234          * Sets the latest edition of this Sone. If the given latest edition is not
235          * greater than the current latest edition, the latest edition of this Sone is
236          * not changed.
237          *
238          * @param latestEdition
239          *              The latest edition of this Sone
240          */
241         void setLatestEdition(long latestEdition);
242
243         /**
244          * Return the time of the last inserted update of this Sone.
245          *
246          * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
247          */
248         long getTime();
249
250         /**
251          * Sets the time of the last inserted update of this Sone.
252          *
253          * @param time
254          *              The time of the update (in milliseconds since Jan 1, 1970 UTC)
255          * @return This Sone (for method chaining)
256          */
257         Sone setTime(long time);
258
259         /**
260          * Returns the status of this Sone.
261          *
262          * @return The status of this Sone
263          */
264         SoneStatus getStatus();
265
266         /**
267          * Sets the new status of this Sone.
268          *
269          * @param status
270          *              The new status of this Sone
271          * @return This Sone
272          * @throws IllegalArgumentException
273          *              if {@code status} is {@code null}
274          */
275         Sone setStatus(SoneStatus status);
276
277         /**
278          * Returns a copy of the profile. If you want to update values in the profile
279          * of this Sone, update the values in the returned {@link Profile} and use
280          * {@link #setProfile(Profile)} to change the profile in this Sone.
281          *
282          * @return A copy of the profile
283          */
284         Profile getProfile();
285
286         /**
287          * Sets the profile of this Sone. A copy of the given profile is stored so that
288          * subsequent modifications of the given profile are not reflected in this
289          * Sone!
290          *
291          * @param profile
292          *              The profile to set
293          */
294         void setProfile(Profile profile);
295
296         /**
297          * Returns the client used by this Sone.
298          *
299          * @return The client used by this Sone, or {@code null}
300          */
301         Client getClient();
302
303         /**
304          * Sets the client used by this Sone.
305          *
306          * @param client
307          *              The client used by this Sone, or {@code null}
308          * @return This Sone (for method chaining)
309          */
310         Sone setClient(Client client);
311
312         /**
313          * Returns whether this Sone is known.
314          *
315          * @return {@code true} if this Sone is known, {@code false} otherwise
316          */
317         boolean isKnown();
318
319         /**
320          * Sets whether this Sone is known.
321          *
322          * @param known
323          *              {@code true} if this Sone is known, {@code false} otherwise
324          * @return This Sone
325          */
326         Sone setKnown(boolean known);
327
328         /**
329          * Returns all friend Sones of this Sone.
330          *
331          * @return The friend Sones of this Sone
332          */
333         List<String> getFriends();
334
335         /**
336          * Returns whether this Sone has the given Sone as a friend Sone.
337          *
338          * @param friendSoneId
339          *              The ID of the Sone to check for
340          * @return {@code true} if this Sone has the given Sone as a friend, {@code
341          *         false} otherwise
342          */
343         boolean hasFriend(String friendSoneId);
344
345         /**
346          * Adds the given Sone as a friend Sone.
347          *
348          * @param friendSone
349          *              The friend Sone to add
350          * @return This Sone (for method chaining)
351          */
352         Sone addFriend(String friendSone);
353
354         /**
355          * Removes the given Sone as a friend Sone.
356          *
357          * @param friendSoneId
358          *              The ID of the friend Sone to remove
359          * @return This Sone (for method chaining)
360          */
361         Sone removeFriend(String friendSoneId);
362
363         /**
364          * Returns the list of posts of this Sone, sorted by time, newest first.
365          *
366          * @return All posts of this Sone
367          */
368         List<Post> getPosts();
369
370         /**
371          * Sets all posts of this Sone at once.
372          *
373          * @param posts
374          *              The new (and only) posts of this Sone
375          * @return This Sone (for method chaining)
376          */
377         Sone setPosts(Collection<Post> posts);
378
379         /**
380          * Adds the given post to this Sone. The post will not be added if its {@link
381          * Post#getSone() Sone} is not this Sone.
382          *
383          * @param post
384          *              The post to add
385          */
386         void addPost(Post post);
387
388         /**
389          * Removes the given post from this Sone.
390          *
391          * @param post
392          *              The post to remove
393          */
394         void removePost(Post post);
395
396         /**
397          * Returns all replies this Sone made.
398          *
399          * @return All replies this Sone made
400          */
401         Set<PostReply> getReplies();
402
403         /**
404          * Sets all replies of this Sone at once.
405          *
406          * @param replies
407          *              The new (and only) replies of this Sone
408          * @return This Sone (for method chaining)
409          */
410         Sone setReplies(Collection<PostReply> replies);
411
412         /**
413          * Adds a reply to this Sone. If the given reply was not made by this Sone,
414          * nothing is added to this Sone.
415          *
416          * @param reply
417          *              The reply to add
418          */
419         void addReply(PostReply reply);
420
421         /**
422          * Removes a reply from this Sone.
423          *
424          * @param reply
425          *              The reply to remove
426          */
427         void removeReply(PostReply reply);
428
429         /**
430          * Returns the IDs of all liked posts.
431          *
432          * @return All liked posts’ IDs
433          */
434         Set<String> getLikedPostIds();
435
436         /**
437          * Sets the IDs of all liked posts.
438          *
439          * @param likedPostIds
440          *              All liked posts’ IDs
441          * @return This Sone (for method chaining)
442          */
443         Sone setLikePostIds(Set<String> likedPostIds);
444
445         /**
446          * Checks whether the given post ID is liked by this Sone.
447          *
448          * @param postId
449          *              The ID of the post
450          * @return {@code true} if this Sone likes the given post, {@code false}
451          *         otherwise
452          */
453         boolean isLikedPostId(String postId);
454
455         /**
456          * Adds the given post ID to the list of posts this Sone likes.
457          *
458          * @param postId
459          *              The ID of the post
460          * @return This Sone (for method chaining)
461          */
462         Sone addLikedPostId(String postId);
463
464         /**
465          * Removes the given post ID from 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 removeLikedPostId(String postId);
472
473         /**
474          * Returns the IDs of all liked replies.
475          *
476          * @return All liked replies’ IDs
477          */
478         Set<String> getLikedReplyIds();
479
480         /**
481          * Sets the IDs of all liked replies.
482          *
483          * @param likedReplyIds
484          *              All liked replies’ IDs
485          * @return This Sone (for method chaining)
486          */
487         Sone setLikeReplyIds(Set<String> likedReplyIds);
488
489         /**
490          * Checks whether the given reply ID is liked by this Sone.
491          *
492          * @param replyId
493          *              The ID of the reply
494          * @return {@code true} if this Sone likes the given reply, {@code false}
495          *         otherwise
496          */
497         boolean isLikedReplyId(String replyId);
498
499         /**
500          * Adds the given reply ID to the list of replies this Sone likes.
501          *
502          * @param replyId
503          *              The ID of the reply
504          * @return This Sone (for method chaining)
505          */
506         Sone addLikedReplyId(String replyId);
507
508         /**
509          * Removes the given post ID from 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 removeLikedReplyId(String replyId);
516
517         /**
518          * Returns the root album that contains all visible albums of this Sone.
519          *
520          * @return The root album of this Sone
521          */
522         Album getRootAlbum();
523
524         /**
525          * Returns Sone-specific options.
526          *
527          * @return The options of this Sone
528          */
529         Options getOptions();
530
531         /**
532          * Sets the options of this Sone.
533          *
534          * @param options
535          *              The options of this Sone
536          */
537         /* TODO - remove this method again, maybe add an option provider */
538         void setOptions(Options options);
539
540         AlbumBuilder newAlbumBuilder() throws IllegalStateException;
541
542         PostBuilder newPostBuilder();
543
544         PostReplyBuilder newPostReplyBuilder(String postId) throws IllegalStateException;
545
546 }