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 * Sets the identity of this Sone. The {@link Identity#getId() ID} of the
182 * identity has to match this Sone’s {@link #getId()}.
185 * The identity of this Sone
186 * @return This Sone (for method chaining)
187 * @throws IllegalArgumentException
188 * if the ID of the identity does not match this Sone’s ID
190 Sone setIdentity(Identity identity) throws IllegalArgumentException;
193 * Returns the name of this Sone.
195 * @return The name of this Sone
200 * Returns whether this Sone is a local Sone.
202 * @return {@code true} if this Sone is a local Sone, {@code false} otherwise
207 * Returns the request URI of this Sone.
209 * @return The request URI of this Sone
211 FreenetURI getRequestUri();
214 * Sets the request URI of this Sone.
217 * The request URI of this Sone
218 * @return This Sone (for method chaining)
220 Sone setRequestUri(FreenetURI requestUri);
223 * Returns the insert URI of this Sone.
225 * @return The insert URI of this Sone
227 FreenetURI getInsertUri();
230 * Sets the insert URI of this Sone.
233 * The insert URI of this Sone
234 * @return This Sone (for method chaining)
236 Sone setInsertUri(FreenetURI insertUri);
239 * Returns the latest edition of this Sone.
241 * @return The latest edition of this Sone
243 long getLatestEdition();
246 * Sets the latest edition of this Sone. If the given latest edition is not
247 * greater than the current latest edition, the latest edition of this Sone is
250 * @param latestEdition
251 * The latest edition of this Sone
253 void setLatestEdition(long latestEdition);
256 * Return the time of the last inserted update of this Sone.
258 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
263 * Sets the time of the last inserted update of this Sone.
266 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
267 * @return This Sone (for method chaining)
269 Sone setTime(long time);
272 * Returns the status of this Sone.
274 * @return The status of this Sone
276 SoneStatus getStatus();
279 * Sets the new status of this Sone.
282 * The new status of this Sone
284 * @throws IllegalArgumentException
285 * if {@code status} is {@code null}
287 Sone setStatus(SoneStatus status);
290 * Returns a copy of the profile. If you want to update values in the profile
291 * of this Sone, update the values in the returned {@link Profile} and use
292 * {@link #setProfile(Profile)} to change the profile in this Sone.
294 * @return A copy of the profile
296 Profile getProfile();
299 * Sets the profile of this Sone. A copy of the given profile is stored so that
300 * subsequent modifications of the given profile are not reflected in this
306 void setProfile(Profile profile);
309 * Returns the client used by this Sone.
311 * @return The client used by this Sone, or {@code null}
316 * Sets the client used by this Sone.
319 * The client used by this Sone, or {@code null}
320 * @return This Sone (for method chaining)
322 Sone setClient(Client client);
325 * Returns whether this Sone is known.
327 * @return {@code true} if this Sone is known, {@code false} otherwise
332 * Sets whether this Sone is known.
335 * {@code true} if this Sone is known, {@code false} otherwise
338 Sone setKnown(boolean known);
341 * Returns all friend Sones of this Sone.
343 * @return The friend Sones of this Sone
345 List<String> getFriends();
348 * Returns whether this Sone has the given Sone as a friend Sone.
350 * @param friendSoneId
351 * The ID of the Sone to check for
352 * @return {@code true} if this Sone has the given Sone as a friend, {@code
355 boolean hasFriend(String friendSoneId);
358 * Adds the given Sone as a friend Sone.
361 * The friend Sone to add
362 * @return This Sone (for method chaining)
364 Sone addFriend(String friendSone);
367 * Removes the given Sone as a friend Sone.
369 * @param friendSoneId
370 * The ID of the friend Sone to remove
371 * @return This Sone (for method chaining)
373 Sone removeFriend(String friendSoneId);
376 * Returns the list of posts of this Sone, sorted by time, newest first.
378 * @return All posts of this Sone
380 List<Post> getPosts();
383 * Sets all posts of this Sone at once.
386 * The new (and only) posts of this Sone
387 * @return This Sone (for method chaining)
389 Sone setPosts(Collection<Post> posts);
392 * Adds the given post to this Sone. The post will not be added if its {@link
393 * Post#getSone() Sone} is not this Sone.
398 void addPost(Post post);
401 * Removes the given post from this Sone.
406 void removePost(Post post);
409 * Returns all replies this Sone made.
411 * @return All replies this Sone made
413 Set<PostReply> getReplies();
416 * Sets all replies of this Sone at once.
419 * The new (and only) replies of this Sone
420 * @return This Sone (for method chaining)
422 Sone setReplies(Collection<PostReply> replies);
425 * Adds a reply to this Sone. If the given reply was not made by this Sone,
426 * nothing is added to this Sone.
431 void addReply(PostReply reply);
434 * Removes a reply from this Sone.
437 * The reply to remove
439 void removeReply(PostReply reply);
442 * Returns the IDs of all liked posts.
444 * @return All liked posts’ IDs
446 Set<String> getLikedPostIds();
449 * Sets the IDs of all liked posts.
451 * @param likedPostIds
452 * All liked posts’ IDs
453 * @return This Sone (for method chaining)
455 Sone setLikePostIds(Set<String> likedPostIds);
458 * Checks whether the given post ID is liked by this Sone.
462 * @return {@code true} if this Sone likes the given post, {@code false}
465 boolean isLikedPostId(String postId);
468 * Adds the given post ID to the list of posts this Sone likes.
472 * @return This Sone (for method chaining)
474 Sone addLikedPostId(String postId);
477 * Removes the given post ID from the list of posts this Sone likes.
481 * @return This Sone (for method chaining)
483 Sone removeLikedPostId(String postId);
486 * Returns the IDs of all liked replies.
488 * @return All liked replies’ IDs
490 Set<String> getLikedReplyIds();
493 * Sets the IDs of all liked replies.
495 * @param likedReplyIds
496 * All liked replies’ IDs
497 * @return This Sone (for method chaining)
499 Sone setLikeReplyIds(Set<String> likedReplyIds);
502 * Checks whether the given reply ID is liked by this Sone.
505 * The ID of the reply
506 * @return {@code true} if this Sone likes the given reply, {@code false}
509 boolean isLikedReplyId(String replyId);
512 * Adds the given reply ID to the list of replies this Sone likes.
515 * The ID of the reply
516 * @return This Sone (for method chaining)
518 Sone addLikedReplyId(String replyId);
521 * Removes the given post ID from the list of replies this Sone likes.
524 * The ID of the reply
525 * @return This Sone (for method chaining)
527 Sone removeLikedReplyId(String replyId);
530 * Returns the root album that contains all visible albums of this Sone.
532 * @return The root album of this Sone
534 Album getRootAlbum();
537 * Returns Sone-specific options.
539 * @return The options of this Sone
541 Options getOptions();
544 * Sets the options of this Sone.
547 * The options of this Sone
549 /* TODO - remove this method again, maybe add an option provider */
550 void setOptions(Options options);
552 AlbumBuilder newAlbumBuilder() throws IllegalStateException;
554 PostBuilder newPostBuilder();
556 PostReplyBuilder newPostReplyBuilder(String postId) throws IllegalStateException;