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