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