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 freenet.keys.FreenetURI;
40 import com.google.common.base.Function;
41 import com.google.common.base.Predicate;
42 import com.google.common.primitives.Ints;
45 * A Sone defines everything about a user: her profile, her status updates, her
46 * replies, her likes and dislikes, etc.
48 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
50 public interface Sone extends Identified, Fingerprintable, Comparable<Sone> {
53 * Enumeration for the possible states of a {@link Sone}.
55 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
57 public enum SoneStatus {
59 /** The Sone is unknown, i.e. not yet downloaded. */
62 /** The Sone is idle, i.e. not being downloaded or inserted. */
65 /** The Sone is currently being inserted. */
68 /** The Sone is currently being downloaded. */
73 * The possible values for the “show custom avatars” option.
75 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
77 public static enum ShowCustomAvatars {
79 /** Never show custom avatars. */
82 /** Only show custom avatars of followed Sones. */
85 /** Only show custom avatars of Sones you manually trust. */
88 /** Only show custom avatars of automatically trusted Sones. */
91 /** Always show custom avatars. */
96 /** comparator that sorts Sones by their nice name. */
97 public static final Comparator<Sone> NICE_NAME_COMPARATOR = new Comparator<Sone>() {
100 public int compare(Sone leftSone, Sone rightSone) {
101 int diff = SoneAccessor.getNiceName(leftSone).compareToIgnoreCase(SoneAccessor.getNiceName(rightSone));
105 return leftSone.getId().compareToIgnoreCase(rightSone.getId());
110 /** Comparator that sorts Sones by last activity (least recent active first). */
111 public static final Comparator<Sone> LAST_ACTIVITY_COMPARATOR = new Comparator<Sone>() {
114 public int compare(Sone firstSone, Sone secondSone) {
115 return (int) Math.min(Integer.MAX_VALUE, Math.max(Integer.MIN_VALUE, secondSone.getTime() - firstSone.getTime()));
119 /** Comparator that sorts Sones by numbers of posts (descending). */
120 public static final Comparator<Sone> POST_COUNT_COMPARATOR = new Comparator<Sone>() {
126 public int compare(Sone leftSone, Sone rightSone) {
127 return (leftSone.getPosts().size() != rightSone.getPosts().size()) ? (rightSone.getPosts().size() - leftSone.getPosts().size()) : (rightSone.getReplies().size() - leftSone.getReplies().size());
131 /** Comparator that sorts Sones by number of images (descending). */
132 public static final Comparator<Sone> IMAGE_COUNT_COMPARATOR = new Comparator<Sone>() {
138 public int compare(Sone leftSone, Sone rightSone) {
139 int rightSoneImageCount = from(asList(rightSone.getRootAlbum())).transformAndConcat(FLATTENER).transformAndConcat(IMAGES).size();
140 int leftSoneImageCount = from(asList(leftSone.getRootAlbum())).transformAndConcat(FLATTENER).transformAndConcat(IMAGES).size();
141 /* sort descending. */
142 return Ints.compare(rightSoneImageCount, leftSoneImageCount);
146 /** Filter to remove Sones that have not been downloaded. */
147 public static final Predicate<Sone> EMPTY_SONE_FILTER = new Predicate<Sone>() {
150 public boolean apply(Sone sone) {
151 return (sone != null) && (sone.getTime() != 0);
155 /** Filter that matches all {@link Sone#isLocal() local Sones}. */
156 public static final Predicate<Sone> LOCAL_SONE_FILTER = new Predicate<Sone>() {
159 public boolean apply(Sone sone) {
160 return (sone != null) && (sone.getIdentity() instanceof OwnIdentity);
165 /** Filter that matches Sones that have at least one album. */
166 public static final Predicate<Sone> HAS_ALBUM_FILTER = new Predicate<Sone>() {
169 public boolean apply(Sone sone) {
170 return (sone != null) && !sone.getRootAlbum().getAlbums().isEmpty();
174 public static final Function<Sone, String> toSoneXmlUri =
175 new Function<Sone, String>() {
178 public String apply(@Nullable Sone input) {
179 return input.getRequestUri()
180 .setMetaString(new String[] { "sone.xml" })
185 public static final Function<Sone, List<Album>> toAllAlbums = new Function<Sone, List<Album>>() {
187 public List<Album> apply(@Nullable Sone sone) {
188 return (sone == null) ? Collections.<Album>emptyList() : FLATTENER.apply(
189 sone.getRootAlbum());
193 public static final Function<Sone, List<Image>> toAllImages = new Function<Sone, List<Image>>() {
195 public List<Image> apply(@Nullable Sone sone) {
196 return (sone == null) ? Collections.<Image>emptyList() :
197 from(FLATTENER.apply(sone.getRootAlbum()))
198 .transformAndConcat(IMAGES).toList();
203 * Returns the identity of this Sone.
205 * @return The identity of this Sone
207 Identity getIdentity();
210 * Returns the name of this Sone.
212 * @return The name of this Sone
217 * Returns whether this Sone is a local Sone.
219 * @return {@code true} if this Sone is a local Sone, {@code false} otherwise
224 * Returns the request URI of this Sone.
226 * @return The request URI of this Sone
228 FreenetURI getRequestUri();
231 * Sets the request URI of this Sone.
234 * The request URI of this Sone
235 * @return This Sone (for method chaining)
237 Sone setRequestUri(FreenetURI requestUri);
240 * Returns the insert URI of this Sone.
242 * @return The insert URI of this Sone
244 FreenetURI getInsertUri();
247 * Returns the latest edition of this Sone.
249 * @return The latest edition of this Sone
251 long getLatestEdition();
254 * Sets the latest edition of this Sone. If the given latest edition is not
255 * greater than the current latest edition, the latest edition of this Sone is
258 * @param latestEdition
259 * The latest edition of this Sone
261 void setLatestEdition(long latestEdition);
264 * Return the time of the last inserted update of this Sone.
266 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
271 * Sets the time of the last inserted update of this Sone.
274 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
275 * @return This Sone (for method chaining)
277 Sone setTime(long time);
280 * Returns the status of this Sone.
282 * @return The status of this Sone
284 SoneStatus getStatus();
287 * Sets the new status of this Sone.
290 * The new status of this Sone
292 * @throws IllegalArgumentException
293 * if {@code status} is {@code null}
295 Sone setStatus(SoneStatus status);
298 * Returns a copy of the profile. If you want to update values in the profile
299 * of this Sone, update the values in the returned {@link Profile} and use
300 * {@link #setProfile(Profile)} to change the profile in this Sone.
302 * @return A copy of the profile
304 Profile getProfile();
307 * Sets the profile of this Sone. A copy of the given profile is stored so that
308 * subsequent modifications of the given profile are not reflected in this
314 void setProfile(Profile profile);
317 * Returns the client used by this Sone.
319 * @return The client used by this Sone, or {@code null}
324 * Sets the client used by this Sone.
327 * The client used by this Sone, or {@code null}
328 * @return This Sone (for method chaining)
330 Sone setClient(Client client);
333 * Returns whether this Sone is known.
335 * @return {@code true} if this Sone is known, {@code false} otherwise
340 * Sets whether this Sone is known.
343 * {@code true} if this Sone is known, {@code false} otherwise
346 Sone setKnown(boolean known);
349 * Returns all friend Sones of this Sone.
351 * @return The friend Sones of this Sone
353 List<String> getFriends();
356 * Returns whether this Sone has the given Sone as a friend Sone.
358 * @param friendSoneId
359 * The ID of the Sone to check for
360 * @return {@code true} if this Sone has the given Sone as a friend, {@code
363 boolean hasFriend(String friendSoneId);
366 * Adds the given Sone as a friend Sone.
369 * The friend Sone to add
370 * @return This Sone (for method chaining)
372 Sone addFriend(String friendSone);
375 * Removes the given Sone as a friend Sone.
377 * @param friendSoneId
378 * The ID of the friend Sone to remove
379 * @return This Sone (for method chaining)
381 Sone removeFriend(String friendSoneId);
384 * Returns the list of posts of this Sone, sorted by time, newest first.
386 * @return All posts of this Sone
388 List<Post> getPosts();
391 * Sets all posts of this Sone at once.
394 * The new (and only) posts of this Sone
395 * @return This Sone (for method chaining)
397 Sone setPosts(Collection<Post> posts);
400 * Adds the given post to this Sone. The post will not be added if its {@link
401 * Post#getSone() Sone} is not this Sone.
406 void addPost(Post post);
409 * Removes the given post from this Sone.
414 void removePost(Post post);
417 * Returns all replies this Sone made.
419 * @return All replies this Sone made
421 Set<PostReply> getReplies();
424 * Sets all replies of this Sone at once.
427 * The new (and only) replies of this Sone
428 * @return This Sone (for method chaining)
430 Sone setReplies(Collection<PostReply> replies);
433 * Adds a reply to this Sone. If the given reply was not made by this Sone,
434 * nothing is added to this Sone.
439 void addReply(PostReply reply);
442 * Removes a reply from this Sone.
445 * The reply to remove
447 void removeReply(PostReply reply);
450 * Returns the IDs of all liked posts.
452 * @return All liked posts’ IDs
454 Set<String> getLikedPostIds();
457 * Sets the IDs of all liked posts.
459 * @param likedPostIds
460 * All liked posts’ IDs
461 * @return This Sone (for method chaining)
463 Sone setLikePostIds(Set<String> likedPostIds);
466 * Checks whether the given post ID is liked by this Sone.
470 * @return {@code true} if this Sone likes the given post, {@code false}
473 boolean isLikedPostId(String postId);
476 * Adds the given post ID to the list of posts this Sone likes.
480 * @return This Sone (for method chaining)
482 Sone addLikedPostId(String postId);
485 * Removes the given post ID from the list of posts this Sone likes.
489 * @return This Sone (for method chaining)
491 Sone removeLikedPostId(String postId);
494 * Returns the IDs of all liked replies.
496 * @return All liked replies’ IDs
498 Set<String> getLikedReplyIds();
501 * Sets the IDs of all liked replies.
503 * @param likedReplyIds
504 * All liked replies’ IDs
505 * @return This Sone (for method chaining)
507 Sone setLikeReplyIds(Set<String> likedReplyIds);
510 * Checks whether the given reply ID is liked by this Sone.
513 * The ID of the reply
514 * @return {@code true} if this Sone likes the given reply, {@code false}
517 boolean isLikedReplyId(String replyId);
520 * Adds the given reply ID to the list of replies this Sone likes.
523 * The ID of the reply
524 * @return This Sone (for method chaining)
526 Sone addLikedReplyId(String replyId);
529 * Removes the given post ID from the list of replies this Sone likes.
532 * The ID of the reply
533 * @return This Sone (for method chaining)
535 Sone removeLikedReplyId(String replyId);
538 * Returns the root album that contains all visible albums of this Sone.
540 * @return The root album of this Sone
542 Album getRootAlbum();
545 * Returns Sone-specific options.
547 * @return The options of this Sone
549 SoneOptions getOptions();
552 * Sets the options of this Sone.
555 * The options of this Sone
557 /* TODO - remove this method again, maybe add an option provider */
558 void setOptions(SoneOptions options);