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