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