2 * Sone - Sone.java - Copyright © 2010–2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.data;
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;
25 import java.util.Collection;
26 import java.util.Comparator;
27 import java.util.List;
30 import net.pterodactylus.sone.core.Options;
31 import net.pterodactylus.sone.database.AlbumBuilder;
32 import net.pterodactylus.sone.database.PostBuilder;
33 import net.pterodactylus.sone.database.PostReplyBuilder;
34 import net.pterodactylus.sone.freenet.wot.Identity;
35 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
36 import net.pterodactylus.sone.template.SoneAccessor;
38 import freenet.keys.FreenetURI;
40 import com.google.common.base.Predicate;
41 import com.google.common.primitives.Ints;
44 * A Sone defines everything about a user: her profile, her status updates, her
45 * replies, her likes and dislikes, etc.
47 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
49 public interface Sone extends Identified, Fingerprintable, Comparable<Sone> {
52 * Enumeration for the possible states of a {@link Sone}.
54 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
56 public enum SoneStatus {
58 /** The Sone is unknown, i.e. not yet downloaded. */
61 /** The Sone is idle, i.e. not being downloaded or inserted. */
64 /** The Sone is currently being inserted. */
67 /** The Sone is currently being downloaded. */
72 * The possible values for the “show custom avatars” option.
74 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
76 public static enum ShowCustomAvatars {
78 /** Never show custom avatars. */
81 /** Only show custom avatars of followed Sones. */
84 /** Only show custom avatars of Sones you manually trust. */
87 /** Only show custom avatars of automatically trusted Sones. */
90 /** Always show custom avatars. */
95 /** comparator that sorts Sones by their nice name. */
96 public static final Comparator<Sone> NICE_NAME_COMPARATOR = new Comparator<Sone>() {
99 public int compare(Sone leftSone, Sone rightSone) {
100 int diff = SoneAccessor.getNiceName(leftSone).compareToIgnoreCase(SoneAccessor.getNiceName(rightSone));
104 return leftSone.getId().compareToIgnoreCase(rightSone.getId());
109 /** Comparator that sorts Sones by last activity (least recent active first). */
110 public static final Comparator<Sone> LAST_ACTIVITY_COMPARATOR = new Comparator<Sone>() {
113 public int compare(Sone firstSone, Sone secondSone) {
114 return (int) Math.min(Integer.MAX_VALUE, Math.max(Integer.MIN_VALUE, secondSone.getTime() - firstSone.getTime()));
118 /** Comparator that sorts Sones by numbers of posts (descending). */
119 public static final Comparator<Sone> POST_COUNT_COMPARATOR = new Comparator<Sone>() {
125 public int compare(Sone leftSone, Sone rightSone) {
126 return (leftSone.getPosts().size() != rightSone.getPosts().size()) ? (rightSone.getPosts().size() - leftSone.getPosts().size()) : (rightSone.getReplies().size() - leftSone.getReplies().size());
130 /** Comparator that sorts Sones by number of images (descending). */
131 public static final Comparator<Sone> IMAGE_COUNT_COMPARATOR = new Comparator<Sone>() {
137 public int compare(Sone leftSone, Sone rightSone) {
138 int rightSoneImageCount = from(asList(rightSone.getRootAlbum())).transformAndConcat(FLATTENER).transformAndConcat(IMAGES).size();
139 int leftSoneImageCount = from(asList(leftSone.getRootAlbum())).transformAndConcat(FLATTENER).transformAndConcat(IMAGES).size();
140 /* sort descending. */
141 return Ints.compare(rightSoneImageCount, leftSoneImageCount);
145 /** Filter to remove Sones that have not been downloaded. */
146 public static final Predicate<Sone> EMPTY_SONE_FILTER = new Predicate<Sone>() {
149 public boolean apply(Sone sone) {
150 return (sone == null) ? false : sone.getTime() != 0;
154 /** Filter that matches all {@link Sone#isLocal() local Sones}. */
155 public static final Predicate<Sone> LOCAL_SONE_FILTER = new Predicate<Sone>() {
158 public boolean apply(Sone sone) {
159 return (sone == null) ? false : sone.getIdentity() instanceof OwnIdentity;
164 /** Filter that matches Sones that have at least one album. */
165 public static final Predicate<Sone> HAS_ALBUM_FILTER = new Predicate<Sone>() {
168 public boolean apply(Sone sone) {
169 return (sone == null) ? false : !sone.getRootAlbum().getAlbums().isEmpty();
174 * Returns the identity of this Sone.
176 * @return The identity of this Sone
178 Identity getIdentity();
181 * Returns the name of this Sone.
183 * @return The name of this Sone
188 * Returns whether this Sone is a local Sone.
190 * @return {@code true} if this Sone is a local Sone, {@code false} otherwise
195 * Returns the request URI of this Sone.
197 * @return The request URI of this Sone
199 FreenetURI getRequestUri();
202 * Sets the request URI of this Sone.
205 * The request URI of this Sone
206 * @return This Sone (for method chaining)
208 Sone setRequestUri(FreenetURI requestUri);
211 * Returns the insert URI of this Sone.
213 * @return The insert URI of this Sone
215 FreenetURI getInsertUri();
218 * Sets the insert URI of this Sone.
221 * The insert URI of this Sone
222 * @return This Sone (for method chaining)
224 Sone setInsertUri(FreenetURI insertUri);
227 * Returns the latest edition of this Sone.
229 * @return The latest edition of this Sone
231 long getLatestEdition();
234 * Sets the latest edition of this Sone. If the given latest edition is not
235 * greater than the current latest edition, the latest edition of this Sone is
238 * @param latestEdition
239 * The latest edition of this Sone
241 void setLatestEdition(long latestEdition);
244 * Return the time of the last inserted update of this Sone.
246 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
251 * Sets the time of the last inserted update of this Sone.
254 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
255 * @return This Sone (for method chaining)
257 Sone setTime(long time);
260 * Returns the status of this Sone.
262 * @return The status of this Sone
264 SoneStatus getStatus();
267 * Sets the new status of this Sone.
270 * The new status of this Sone
272 * @throws IllegalArgumentException
273 * if {@code status} is {@code null}
275 Sone setStatus(SoneStatus status);
278 * Returns a copy of the profile. If you want to update values in the profile
279 * of this Sone, update the values in the returned {@link Profile} and use
280 * {@link #setProfile(Profile)} to change the profile in this Sone.
282 * @return A copy of the profile
284 Profile getProfile();
287 * Sets the profile of this Sone. A copy of the given profile is stored so that
288 * subsequent modifications of the given profile are not reflected in this
294 void setProfile(Profile profile);
297 * Returns the client used by this Sone.
299 * @return The client used by this Sone, or {@code null}
304 * Sets the client used by this Sone.
307 * The client used by this Sone, or {@code null}
308 * @return This Sone (for method chaining)
310 Sone setClient(Client client);
313 * Returns whether this Sone is known.
315 * @return {@code true} if this Sone is known, {@code false} otherwise
320 * Sets whether this Sone is known.
323 * {@code true} if this Sone is known, {@code false} otherwise
326 Sone setKnown(boolean known);
329 * Returns all friend Sones of this Sone.
331 * @return The friend Sones of this Sone
333 List<String> getFriends();
336 * Returns whether this Sone has the given Sone as a friend Sone.
338 * @param friendSoneId
339 * The ID of the Sone to check for
340 * @return {@code true} if this Sone has the given Sone as a friend, {@code
343 boolean hasFriend(String friendSoneId);
346 * Adds the given Sone as a friend Sone.
349 * The friend Sone to add
350 * @return This Sone (for method chaining)
352 Sone addFriend(String friendSone);
355 * Removes the given Sone as a friend Sone.
357 * @param friendSoneId
358 * The ID of the friend Sone to remove
359 * @return This Sone (for method chaining)
361 Sone removeFriend(String friendSoneId);
364 * Returns the list of posts of this Sone, sorted by time, newest first.
366 * @return All posts of this Sone
368 List<Post> getPosts();
371 * Sets all posts of this Sone at once.
374 * The new (and only) posts of this Sone
375 * @return This Sone (for method chaining)
377 Sone setPosts(Collection<Post> posts);
380 * Adds the given post to this Sone. The post will not be added if its {@link
381 * Post#getSone() Sone} is not this Sone.
386 void addPost(Post post);
389 * Removes the given post from this Sone.
394 void removePost(Post post);
397 * Returns all replies this Sone made.
399 * @return All replies this Sone made
401 Set<PostReply> getReplies();
404 * Sets all replies of this Sone at once.
407 * The new (and only) replies of this Sone
408 * @return This Sone (for method chaining)
410 Sone setReplies(Collection<PostReply> replies);
413 * Adds a reply to this Sone. If the given reply was not made by this Sone,
414 * nothing is added to this Sone.
419 void addReply(PostReply reply);
422 * Removes a reply from this Sone.
425 * The reply to remove
427 void removeReply(PostReply reply);
430 * Returns the IDs of all liked posts.
432 * @return All liked posts’ IDs
434 Set<String> getLikedPostIds();
437 * Sets the IDs of all liked posts.
439 * @param likedPostIds
440 * All liked posts’ IDs
441 * @return This Sone (for method chaining)
443 Sone setLikePostIds(Set<String> likedPostIds);
446 * Checks whether the given post ID is liked by this Sone.
450 * @return {@code true} if this Sone likes the given post, {@code false}
453 boolean isLikedPostId(String postId);
456 * Adds the given post ID to the list of posts this Sone likes.
460 * @return This Sone (for method chaining)
462 Sone addLikedPostId(String postId);
465 * Removes the given post ID from the list of posts this Sone likes.
469 * @return This Sone (for method chaining)
471 Sone removeLikedPostId(String postId);
474 * Returns the IDs of all liked replies.
476 * @return All liked replies’ IDs
478 Set<String> getLikedReplyIds();
481 * Sets the IDs of all liked replies.
483 * @param likedReplyIds
484 * All liked replies’ IDs
485 * @return This Sone (for method chaining)
487 Sone setLikeReplyIds(Set<String> likedReplyIds);
490 * Checks whether the given reply ID is liked by this Sone.
493 * The ID of the reply
494 * @return {@code true} if this Sone likes the given reply, {@code false}
497 boolean isLikedReplyId(String replyId);
500 * Adds the given reply ID to the list of replies this Sone likes.
503 * The ID of the reply
504 * @return This Sone (for method chaining)
506 Sone addLikedReplyId(String replyId);
509 * Removes the given post ID from the list of replies this Sone likes.
512 * The ID of the reply
513 * @return This Sone (for method chaining)
515 Sone removeLikedReplyId(String replyId);
518 * Returns the root album that contains all visible albums of this Sone.
520 * @return The root album of this Sone
522 Album getRootAlbum();
525 * Returns Sone-specific options.
527 * @return The options of this Sone
529 Options getOptions();
532 * Sets the options of this Sone.
535 * The options of this Sone
537 /* TODO - remove this method again, maybe add an option provider */
538 void setOptions(Options options);
540 AlbumBuilder newAlbumBuilder() throws IllegalStateException;
542 PostBuilder newPostBuilder();
544 PostReplyBuilder newPostReplyBuilder(String postId) throws IllegalStateException;