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