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