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.freenet.wot.Identity;
33 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
34 import net.pterodactylus.sone.template.SoneAccessor;
36 import freenet.keys.FreenetURI;
38 import com.google.common.base.Predicate;
39 import com.google.common.primitives.Ints;
42 * A Sone defines everything about a user: her profile, her status updates, her
43 * replies, her likes and dislikes, etc.
45 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
47 public interface Sone extends Identified, Fingerprintable, Comparable<Sone> {
50 * Enumeration for the possible states of a {@link Sone}.
52 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
54 public enum SoneStatus {
56 /** The Sone is unknown, i.e. not yet downloaded. */
59 /** The Sone is idle, i.e. not being downloaded or inserted. */
62 /** The Sone is currently being inserted. */
65 /** The Sone is currently being downloaded. */
70 * The possible values for the “show custom avatars” option.
72 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
74 public static enum ShowCustomAvatars {
76 /** Never show custom avatars. */
79 /** Only show custom avatars of followed Sones. */
82 /** Only show custom avatars of Sones you manually trust. */
85 /** Only show custom avatars of automatically trusted Sones. */
88 /** Always show custom avatars. */
93 /** comparator that sorts Sones by their nice name. */
94 public static final Comparator<Sone> NICE_NAME_COMPARATOR = new Comparator<Sone>() {
97 public int compare(Sone leftSone, Sone rightSone) {
98 int diff = SoneAccessor.getNiceName(leftSone).compareToIgnoreCase(SoneAccessor.getNiceName(rightSone));
102 return leftSone.getId().compareToIgnoreCase(rightSone.getId());
107 /** Comparator that sorts Sones by last activity (least recent active first). */
108 public static final Comparator<Sone> LAST_ACTIVITY_COMPARATOR = new Comparator<Sone>() {
111 public int compare(Sone firstSone, Sone secondSone) {
112 return (int) Math.min(Integer.MAX_VALUE, Math.max(Integer.MIN_VALUE, secondSone.getTime() - firstSone.getTime()));
116 /** Comparator that sorts Sones by numbers of posts (descending). */
117 public static final Comparator<Sone> POST_COUNT_COMPARATOR = new Comparator<Sone>() {
123 public int compare(Sone leftSone, Sone rightSone) {
124 return (leftSone.getPosts().size() != rightSone.getPosts().size()) ? (rightSone.getPosts().size() - leftSone.getPosts().size()) : (rightSone.getReplies().size() - leftSone.getReplies().size());
128 /** Comparator that sorts Sones by number of images (descending). */
129 public static final Comparator<Sone> IMAGE_COUNT_COMPARATOR = new Comparator<Sone>() {
135 public int compare(Sone leftSone, Sone rightSone) {
136 int rightSoneImageCount = from(asList(rightSone.getRootAlbum())).transformAndConcat(FLATTENER).transformAndConcat(IMAGES).size();
137 int leftSoneImageCount = from(asList(leftSone.getRootAlbum())).transformAndConcat(FLATTENER).transformAndConcat(IMAGES).size();
138 /* sort descending. */
139 return Ints.compare(rightSoneImageCount, leftSoneImageCount);
143 /** Filter to remove Sones that have not been downloaded. */
144 public static final Predicate<Sone> EMPTY_SONE_FILTER = new Predicate<Sone>() {
147 public boolean apply(Sone sone) {
148 return (sone == null) ? false : sone.getTime() != 0;
152 /** Filter that matches all {@link Sone#isLocal() local Sones}. */
153 public static final Predicate<Sone> LOCAL_SONE_FILTER = new Predicate<Sone>() {
156 public boolean apply(Sone sone) {
157 return (sone == null) ? false : sone.getIdentity() instanceof OwnIdentity;
162 /** Filter that matches Sones that have at least one album. */
163 public static final Predicate<Sone> HAS_ALBUM_FILTER = new Predicate<Sone>() {
166 public boolean apply(Sone sone) {
167 return (sone == null) ? false : !sone.getRootAlbum().getAlbums().isEmpty();
172 * Returns the identity of this Sone.
174 * @return The identity of this Sone
176 Identity getIdentity();
179 * Sets the identity of this Sone. The {@link Identity#getId() ID} of the
180 * identity has to match this Sone’s {@link #getId()}.
183 * The identity of this Sone
184 * @return This Sone (for method chaining)
185 * @throws IllegalArgumentException
186 * if the ID of the identity does not match this Sone’s ID
188 Sone setIdentity(Identity identity) throws IllegalArgumentException;
191 * Returns the name of this Sone.
193 * @return The name of this Sone
198 * Returns whether this Sone is a local Sone.
200 * @return {@code true} if this Sone is a local Sone, {@code false} otherwise
205 * Returns the request URI of this Sone.
207 * @return The request URI of this Sone
209 FreenetURI getRequestUri();
212 * Sets the request URI of this Sone.
215 * The request URI of this Sone
216 * @return This Sone (for method chaining)
218 Sone setRequestUri(FreenetURI requestUri);
221 * Returns the insert URI of this Sone.
223 * @return The insert URI of this Sone
225 FreenetURI getInsertUri();
228 * Sets the insert URI of this Sone.
231 * The insert URI of this Sone
232 * @return This Sone (for method chaining)
234 Sone setInsertUri(FreenetURI insertUri);
237 * Returns the latest edition of this Sone.
239 * @return The latest edition of this Sone
241 long getLatestEdition();
244 * Sets the latest edition of this Sone. If the given latest edition is not
245 * greater than the current latest edition, the latest edition of this Sone is
248 * @param latestEdition
249 * The latest edition of this Sone
251 void setLatestEdition(long latestEdition);
254 * Return the time of the last inserted update of this Sone.
256 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
261 * Sets the time of the last inserted update of this Sone.
264 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
265 * @return This Sone (for method chaining)
267 Sone setTime(long time);
270 * Returns the status of this Sone.
272 * @return The status of this Sone
274 SoneStatus getStatus();
277 * Sets the new status of this Sone.
280 * The new status of this Sone
282 * @throws IllegalArgumentException
283 * if {@code status} is {@code null}
285 Sone setStatus(SoneStatus status);
288 * Returns a copy of the profile. If you want to update values in the profile
289 * of this Sone, update the values in the returned {@link Profile} and use
290 * {@link #setProfile(Profile)} to change the profile in this Sone.
292 * @return A copy of the profile
294 Profile getProfile();
297 * Sets the profile of this Sone. A copy of the given profile is stored so that
298 * subsequent modifications of the given profile are not reflected in this
304 void setProfile(Profile profile);
307 * Returns the client used by this Sone.
309 * @return The client used by this Sone, or {@code null}
314 * Sets the client used by this Sone.
317 * The client used by this Sone, or {@code null}
318 * @return This Sone (for method chaining)
320 Sone setClient(Client client);
323 * Returns whether this Sone is known.
325 * @return {@code true} if this Sone is known, {@code false} otherwise
330 * Sets whether this Sone is known.
333 * {@code true} if this Sone is known, {@code false} otherwise
336 Sone setKnown(boolean known);
339 * Returns all friend Sones of this Sone.
341 * @return The friend Sones of this Sone
343 List<String> getFriends();
346 * Returns whether this Sone has the given Sone as a friend Sone.
348 * @param friendSoneId
349 * The ID of the Sone to check for
350 * @return {@code true} if this Sone has the given Sone as a friend, {@code
353 boolean hasFriend(String friendSoneId);
356 * Adds the given Sone as a friend Sone.
359 * The friend Sone to add
360 * @return This Sone (for method chaining)
362 Sone addFriend(String friendSone);
365 * Removes the given Sone as a friend Sone.
367 * @param friendSoneId
368 * The ID of the friend Sone to remove
369 * @return This Sone (for method chaining)
371 Sone removeFriend(String friendSoneId);
374 * Returns the list of posts of this Sone, sorted by time, newest first.
376 * @return All posts of this Sone
378 List<Post> getPosts();
381 * Sets all posts of this Sone at once.
384 * The new (and only) posts of this Sone
385 * @return This Sone (for method chaining)
387 Sone setPosts(Collection<Post> posts);
390 * Adds the given post to this Sone. The post will not be added if its {@link
391 * Post#getSone() Sone} is not this Sone.
396 void addPost(Post post);
399 * Removes the given post from this Sone.
404 void removePost(Post post);
407 * Returns all replies this Sone made.
409 * @return All replies this Sone made
411 Set<PostReply> getReplies();
414 * Sets all replies of this Sone at once.
417 * The new (and only) replies of this Sone
418 * @return This Sone (for method chaining)
420 Sone setReplies(Collection<PostReply> replies);
423 * Adds a reply to this Sone. If the given reply was not made by this Sone,
424 * nothing is added to this Sone.
429 void addReply(PostReply reply);
432 * Removes a reply from this Sone.
435 * The reply to remove
437 void removeReply(PostReply reply);
440 * Returns the IDs of all liked posts.
442 * @return All liked posts’ IDs
444 Set<String> getLikedPostIds();
447 * Sets the IDs of all liked posts.
449 * @param likedPostIds
450 * All liked posts’ IDs
451 * @return This Sone (for method chaining)
453 Sone setLikePostIds(Set<String> likedPostIds);
456 * Checks whether the given post ID is liked by this Sone.
460 * @return {@code true} if this Sone likes the given post, {@code false}
463 boolean isLikedPostId(String postId);
466 * Adds the given post ID to the list of posts this Sone likes.
470 * @return This Sone (for method chaining)
472 Sone addLikedPostId(String postId);
475 * Removes the given post ID from the list of posts this Sone likes.
479 * @return This Sone (for method chaining)
481 Sone removeLikedPostId(String postId);
484 * Returns the IDs of all liked replies.
486 * @return All liked replies’ IDs
488 Set<String> getLikedReplyIds();
491 * Sets the IDs of all liked replies.
493 * @param likedReplyIds
494 * All liked replies’ IDs
495 * @return This Sone (for method chaining)
497 Sone setLikeReplyIds(Set<String> likedReplyIds);
500 * Checks whether the given reply ID is liked by this Sone.
503 * The ID of the reply
504 * @return {@code true} if this Sone likes the given reply, {@code false}
507 boolean isLikedReplyId(String replyId);
510 * Adds the given reply ID to the list of replies this Sone likes.
513 * The ID of the reply
514 * @return This Sone (for method chaining)
516 Sone addLikedReplyId(String replyId);
519 * Removes the given post ID from the list of replies this Sone likes.
522 * The ID of the reply
523 * @return This Sone (for method chaining)
525 Sone removeLikedReplyId(String replyId);
528 * Returns the root album that contains all visible albums of this Sone.
530 * @return The root album of this Sone
532 Album getRootAlbum();
535 * Returns Sone-specific options.
537 * @return The options of this Sone
539 Options getOptions();
542 * Sets the options of this Sone.
545 * The options of this Sone
547 /* TODO - remove this method again, maybe add an option provider */
548 void setOptions(Options options);
550 AlbumBuilder newAlbumBuilder();