🔥 Remove getInsertUri() method from Sone interface
[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 latest edition of this Sone.
196          *
197          * @return The latest edition of this Sone
198          */
199         long getLatestEdition();
200
201         /**
202          * Sets the latest edition of this Sone. If the given latest edition is not
203          * greater than the current latest edition, the latest edition of this Sone is
204          * not changed.
205          *
206          * @param latestEdition
207          *              The latest edition of this Sone
208          */
209         void setLatestEdition(long latestEdition);
210
211         /**
212          * Return the time of the last inserted update of this Sone.
213          *
214          * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
215          */
216         long getTime();
217
218         /**
219          * Sets the time of the last inserted update of this Sone.
220          *
221          * @param time
222          *              The time of the update (in milliseconds since Jan 1, 1970 UTC)
223          * @return This Sone (for method chaining)
224          */
225         @Nonnull
226         Sone setTime(long time);
227
228         /**
229          * Returns the status of this Sone.
230          *
231          * @return The status of this Sone
232          */
233         @Nonnull
234         SoneStatus getStatus();
235
236         /**
237          * Sets the new status of this Sone.
238          *
239          * @param status
240          *              The new status of this Sone
241          * @return This Sone
242          * @throws IllegalArgumentException
243          *              if {@code status} is {@code null}
244          */
245         @Nonnull
246         Sone setStatus(@Nonnull SoneStatus status);
247
248         /**
249          * Returns a copy of the profile. If you want to update values in the profile
250          * of this Sone, update the values in the returned {@link Profile} and use
251          * {@link #setProfile(Profile)} to change the profile in this Sone.
252          *
253          * @return A copy of the profile
254          */
255         @Nonnull
256         Profile getProfile();
257
258         /**
259          * Sets the profile of this Sone. A copy of the given profile is stored so that
260          * subsequent modifications of the given profile are not reflected in this
261          * Sone!
262          *
263          * @param profile
264          *              The profile to set
265          */
266         void setProfile(@Nonnull Profile profile);
267
268         /**
269          * Returns the client used by this Sone.
270          *
271          * @return The client used by this Sone, or {@code null}
272          */
273         @Nullable
274         Client getClient();
275
276         /**
277          * Sets the client used by this Sone.
278          *
279          * @param client
280          *              The client used by this Sone, or {@code null}
281          * @return This Sone (for method chaining)
282          */
283         @Nonnull
284         Sone setClient(@Nullable Client client);
285
286         /**
287          * Returns whether this Sone is known.
288          *
289          * @return {@code true} if this Sone is known, {@code false} otherwise
290          */
291         boolean isKnown();
292
293         /**
294          * Sets whether this Sone is known.
295          *
296          * @param known
297          *              {@code true} if this Sone is known, {@code false} otherwise
298          * @return This Sone
299          */
300         @Nonnull
301         Sone setKnown(boolean known);
302
303         /**
304          * Returns all friend Sones of this Sone.
305          *
306          * @return The friend Sones of this Sone
307          */
308         @Nonnull
309         Collection<String> getFriends();
310
311         /**
312          * Returns whether this Sone has the given Sone as a friend Sone.
313          *
314          * @param friendSoneId
315          *              The ID of the Sone to check for
316          * @return {@code true} if this Sone has the given Sone as a friend, {@code
317          *         false} otherwise
318          */
319         boolean hasFriend(@Nonnull String friendSoneId);
320
321         /**
322          * Returns the list of posts of this Sone, sorted by time, newest first.
323          *
324          * @return All posts of this Sone
325          */
326         @Nonnull
327         List<Post> getPosts();
328
329         /**
330          * Sets all posts of this Sone at once.
331          *
332          * @param posts
333          *              The new (and only) posts of this Sone
334          * @return This Sone (for method chaining)
335          */
336         @Nonnull
337         Sone setPosts(@Nonnull Collection<Post> posts);
338
339         /**
340          * Adds the given post to this Sone. The post will not be added if its {@link
341          * Post#getSone() Sone} is not this Sone.
342          *
343          * @param post
344          *              The post to add
345          */
346         void addPost(@Nonnull Post post);
347
348         /**
349          * Removes the given post from this Sone.
350          *
351          * @param post
352          *              The post to remove
353          */
354         void removePost(@Nonnull Post post);
355
356         /**
357          * Returns all replies this Sone made.
358          *
359          * @return All replies this Sone made
360          */
361         @Nonnull
362         Set<PostReply> getReplies();
363
364         /**
365          * Sets all replies of this Sone at once.
366          *
367          * @param replies
368          *              The new (and only) replies of this Sone
369          * @return This Sone (for method chaining)
370          */
371         @Nonnull
372         Sone setReplies(@Nonnull Collection<PostReply> replies);
373
374         /**
375          * Adds a reply to this Sone. If the given reply was not made by this Sone,
376          * nothing is added to this Sone.
377          *
378          * @param reply
379          *              The reply to add
380          */
381         void addReply(@Nonnull PostReply reply);
382
383         /**
384          * Removes a reply from this Sone.
385          *
386          * @param reply
387          *              The reply to remove
388          */
389         void removeReply(@Nonnull PostReply reply);
390
391         /**
392          * Returns the IDs of all liked posts.
393          *
394          * @return All liked posts’ IDs
395          */
396         @Nonnull
397         Set<String> getLikedPostIds();
398
399         /**
400          * Sets the IDs of all liked posts.
401          *
402          * @param likedPostIds
403          *              All liked posts’ IDs
404          * @return This Sone (for method chaining)
405          */
406         @Nonnull
407         Sone setLikePostIds(@Nonnull 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(@Nonnull 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         @Nonnull
427         Sone addLikedPostId(@Nonnull String postId);
428
429         /**
430          * Removes the given post ID from the list of posts this Sone likes.
431          *
432          * @param postId
433          *              The ID of the post
434          */
435         void removeLikedPostId(@Nonnull String postId);
436
437         /**
438          * Returns the IDs of all liked replies.
439          *
440          * @return All liked replies’ IDs
441          */
442         @Nonnull
443         Set<String> getLikedReplyIds();
444
445         /**
446          * Sets the IDs of all liked replies.
447          *
448          * @param likedReplyIds
449          *              All liked replies’ IDs
450          * @return This Sone (for method chaining)
451          */
452         @Nonnull
453         Sone setLikeReplyIds(@Nonnull Set<String> likedReplyIds);
454
455         /**
456          * Checks whether the given reply ID is liked by this Sone.
457          *
458          * @param replyId
459          *              The ID of the reply
460          * @return {@code true} if this Sone likes the given reply, {@code false}
461          *         otherwise
462          */
463         boolean isLikedReplyId(@Nonnull String replyId);
464
465         /**
466          * Adds the given reply ID to the list of replies this Sone likes.
467          *
468          * @param replyId
469          *              The ID of the reply
470          * @return This Sone (for method chaining)
471          */
472         @Nonnull
473         Sone addLikedReplyId(@Nonnull String replyId);
474
475         /**
476          * Removes the given post ID from the list of replies this Sone likes.
477          *
478          * @param replyId
479          *              The ID of the reply
480          */
481         void removeLikedReplyId(@Nonnull String replyId);
482
483         /**
484          * Returns the root album that contains all visible albums of this Sone.
485          *
486          * @return The root album of this Sone
487          */
488         @Nonnull
489         Album getRootAlbum();
490
491         /**
492          * Returns Sone-specific options.
493          *
494          * @return The options of this Sone
495          */
496         @Nonnull
497         SoneOptions getOptions();
498
499         /**
500          * Sets the options of this Sone.
501          *
502          * @param options
503          *              The options of this Sone
504          */
505         /* TODO - remove this method again, maybe add an option provider */
506         void setOptions(@Nonnull SoneOptions options);
507
508 }