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.Collections;
27 import java.util.Comparator;
28 import java.util.List;
31 import javax.annotation.Nonnull;
32 import javax.annotation.Nullable;
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 com.google.common.base.Optional;
39 import freenet.keys.FreenetURI;
41 import com.google.common.base.Function;
42 import com.google.common.base.Predicate;
43 import com.google.common.primitives.Ints;
46 * A Sone defines everything about a user: her profile, her status updates, her
47 * replies, her likes and dislikes, etc.
49 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
51 public interface Sone extends Identified, Fingerprintable, Comparable<Sone> {
54 * Enumeration for the possible states of a {@link Sone}.
56 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
58 public enum SoneStatus {
60 /** The Sone is unknown, i.e. not yet downloaded. */
63 /** The Sone is idle, i.e. not being downloaded or inserted. */
66 /** The Sone is currently being inserted. */
69 /** The Sone is currently being downloaded. */
74 * The possible values for the “show custom avatars” option.
76 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
78 public static enum ShowCustomAvatars {
80 /** Never show custom avatars. */
83 /** Only show custom avatars of followed Sones. */
86 /** Only show custom avatars of Sones you manually trust. */
89 /** Only show custom avatars of automatically trusted Sones. */
92 /** Always show custom avatars. */
97 /** comparator that sorts Sones by their nice name. */
98 public static final Comparator<Sone> NICE_NAME_COMPARATOR = new Comparator<Sone>() {
101 public int compare(Sone leftSone, Sone rightSone) {
102 int diff = SoneAccessor.getNiceName(leftSone).compareToIgnoreCase(SoneAccessor.getNiceName(rightSone));
106 return leftSone.getId().compareToIgnoreCase(rightSone.getId());
111 /** Comparator that sorts Sones by last activity (least recent active first). */
112 public static final Comparator<Sone> LAST_ACTIVITY_COMPARATOR = new Comparator<Sone>() {
115 public int compare(Sone firstSone, Sone secondSone) {
116 return (int) Math.min(Integer.MAX_VALUE, Math.max(Integer.MIN_VALUE, secondSone.getTime() - firstSone.getTime()));
120 /** Comparator that sorts Sones by numbers of posts (descending). */
121 public static final Comparator<Sone> POST_COUNT_COMPARATOR = new Comparator<Sone>() {
127 public int compare(Sone leftSone, Sone rightSone) {
128 return (leftSone.getPosts().size() != rightSone.getPosts().size()) ? (rightSone.getPosts().size() - leftSone.getPosts().size()) : (rightSone.getReplies().size() - leftSone.getReplies().size());
132 /** Comparator that sorts Sones by number of images (descending). */
133 public static final Comparator<Sone> IMAGE_COUNT_COMPARATOR = new Comparator<Sone>() {
139 public int compare(Sone leftSone, Sone rightSone) {
140 int rightSoneImageCount = from(asList(rightSone.getRootAlbum())).transformAndConcat(FLATTENER).transformAndConcat(IMAGES).size();
141 int leftSoneImageCount = from(asList(leftSone.getRootAlbum())).transformAndConcat(FLATTENER).transformAndConcat(IMAGES).size();
142 /* sort descending. */
143 return Ints.compare(rightSoneImageCount, leftSoneImageCount);
147 /** Filter to remove Sones that have not been downloaded. */
148 public static final Predicate<Sone> EMPTY_SONE_FILTER = new Predicate<Sone>() {
151 public boolean apply(Sone sone) {
152 return (sone != null) && (sone.getTime() != 0);
156 /** Filter that matches all {@link Sone#isLocal() local Sones}. */
157 public static final Predicate<Sone> LOCAL_SONE_FILTER = new Predicate<Sone>() {
160 public boolean apply(Sone sone) {
161 return (sone != null) && (sone.getIdentity() instanceof OwnIdentity);
166 /** Filter that matches Sones that have at least one album. */
167 public static final Predicate<Sone> HAS_ALBUM_FILTER = new Predicate<Sone>() {
170 public boolean apply(Sone sone) {
171 return (sone != null) && !sone.getRootAlbum().getAlbums().isEmpty();
175 public static final Function<Sone, List<Album>> toAllAlbums = new Function<Sone, List<Album>>() {
177 public List<Album> apply(@Nullable Sone sone) {
178 return (sone == null) ? Collections.<Album>emptyList() : FLATTENER.apply(
179 sone.getRootAlbum());
183 public static final Function<Sone, List<Image>> toAllImages = new Function<Sone, List<Image>>() {
185 public List<Image> apply(@Nullable Sone sone) {
186 return (sone == null) ? Collections.<Image>emptyList() :
187 from(FLATTENER.apply(sone.getRootAlbum()))
188 .transformAndConcat(IMAGES).toList();
193 * Returns the identity of this Sone.
195 * @return The identity of this Sone
197 Identity getIdentity();
200 * Returns the name of this Sone.
202 * @return The name of this Sone
207 * Returns whether this Sone is a local Sone.
209 * @return {@code true} if this Sone is a local Sone, {@code false} otherwise
214 * Returns the request URI of this Sone.
216 * @return The request URI of this Sone
218 FreenetURI getRequestUri();
221 * Returns the insert URI of this Sone.
223 * @return The insert URI of this Sone
225 FreenetURI getInsertUri();
228 * Returns the latest edition of this Sone.
230 * @return The latest edition of this Sone
232 long getLatestEdition();
235 * Sets the latest edition of this Sone. If the given latest edition is not
236 * greater than the current latest edition, the latest edition of this Sone is
239 * @param latestEdition
240 * The latest edition of this Sone
242 void setLatestEdition(long latestEdition);
245 * Return the time of the last inserted update of this Sone.
247 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
252 * Sets the time of the last inserted update of this Sone.
255 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
256 * @return This Sone (for method chaining)
258 Sone setTime(long time);
261 * Returns the status of this Sone.
263 * @return The status of this Sone
265 SoneStatus getStatus();
268 * Sets the new status of this Sone.
271 * The new status of this Sone
273 * @throws IllegalArgumentException
274 * if {@code status} is {@code null}
276 Sone setStatus(SoneStatus status);
279 * Returns a copy of the profile. If you want to update values in the profile
280 * of this Sone, update the values in the returned {@link Profile} and use
281 * {@link #setProfile(Profile)} to change the profile in this Sone.
283 * @return A copy of the profile
285 Profile getProfile();
288 * Sets the profile of this Sone. A copy of the given profile is stored so that
289 * subsequent modifications of the given profile are not reflected in this
295 void setProfile(Profile profile);
298 * Returns the client used by this Sone.
300 * @return The client used by this Sone, or {@code null}
305 * Sets the client used by this Sone.
308 * The client used by this Sone, or {@code null}
309 * @return This Sone (for method chaining)
311 Sone setClient(Client client);
314 * Returns whether this Sone is known.
316 * @return {@code true} if this Sone is known, {@code false} otherwise
321 * Sets whether this Sone is known.
324 * {@code true} if this Sone is known, {@code false} otherwise
327 Sone setKnown(boolean known);
330 * Returns all friend Sones of this Sone.
332 * @return The friend Sones of this Sone
334 Collection<String> getFriends();
337 * Returns whether this Sone has the given Sone as a friend Sone.
339 * @param friendSoneId
340 * The ID of the Sone to check for
341 * @return {@code true} if this Sone has the given Sone as a friend, {@code
344 boolean hasFriend(String friendSoneId);
347 * Returns the list of posts of this Sone, sorted by time, newest first.
349 * @return All posts of this Sone
351 List<Post> getPosts();
354 * Sets all posts of this Sone at once.
357 * The new (and only) posts of this Sone
358 * @return This Sone (for method chaining)
360 Sone setPosts(Collection<Post> posts);
363 * Adds the given post to this Sone. The post will not be added if its {@link
364 * Post#getSone() Sone} is not this Sone.
369 void addPost(Post post);
372 * Removes the given post from this Sone.
377 void removePost(Post post);
380 * Returns all replies this Sone made.
382 * @return All replies this Sone made
384 Set<PostReply> getReplies();
387 * Sets all replies of this Sone at once.
390 * The new (and only) replies of this Sone
391 * @return This Sone (for method chaining)
393 Sone setReplies(Collection<PostReply> replies);
396 * Adds a reply to this Sone. If the given reply was not made by this Sone,
397 * nothing is added to this Sone.
402 void addReply(PostReply reply);
405 * Removes a reply from this Sone.
408 * The reply to remove
410 void removeReply(PostReply reply);
413 * Returns the IDs of all liked posts.
415 * @return All liked posts’ IDs
417 Set<String> getLikedPostIds();
420 * Sets the IDs of all liked posts.
422 * @param likedPostIds
423 * All liked posts’ IDs
424 * @return This Sone (for method chaining)
426 Sone setLikePostIds(Set<String> likedPostIds);
429 * Checks whether the given post ID is liked by this Sone.
433 * @return {@code true} if this Sone likes the given post, {@code false}
436 boolean isLikedPostId(String postId);
439 * Adds the given post ID to the list of posts this Sone likes.
443 * @return This Sone (for method chaining)
445 Sone addLikedPostId(String postId);
448 * Removes the given post ID from the list of posts this Sone likes.
452 * @return This Sone (for method chaining)
454 Sone removeLikedPostId(String postId);
457 * Returns the IDs of all liked replies.
459 * @return All liked replies’ IDs
461 Set<String> getLikedReplyIds();
464 * Sets the IDs of all liked replies.
466 * @param likedReplyIds
467 * All liked replies’ IDs
468 * @return This Sone (for method chaining)
470 Sone setLikeReplyIds(Set<String> likedReplyIds);
473 * Checks whether the given reply ID is liked by this Sone.
476 * The ID of the reply
477 * @return {@code true} if this Sone likes the given reply, {@code false}
480 boolean isLikedReplyId(String replyId);
483 * Adds the given reply ID to the list of replies this Sone likes.
486 * The ID of the reply
487 * @return This Sone (for method chaining)
489 Sone addLikedReplyId(String replyId);
492 * Removes the given post ID from the list of replies this Sone likes.
495 * The ID of the reply
496 * @return This Sone (for method chaining)
498 Sone removeLikedReplyId(String replyId);
501 * Returns the root album that contains all visible albums of this Sone.
503 * @return The root album of this Sone
505 Album getRootAlbum();
506 Optional<Image> getImageByInternalId(final String internalId);
509 * Returns Sone-specific options.
511 * @return The options of this Sone
513 SoneOptions getOptions();
516 * Sets the options of this Sone.
519 * The options of this Sone
521 /* TODO - remove this method again, maybe add an option provider */
522 void setOptions(SoneOptions options);