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