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