2 * Sone - Sone.java - Copyright © 2010–2019 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 public interface Sone extends Identified, Fingerprintable, Comparable<Sone> {
51 * Enumeration for the possible states of a {@link Sone}.
53 public enum SoneStatus {
55 /** The Sone is unknown, i.e. not yet downloaded. */
58 /** The Sone is idle, i.e. not being downloaded or inserted. */
61 /** The Sone is currently being inserted. */
64 /** The Sone is currently being downloaded. */
68 /** comparator that sorts Sones by their nice name. */
69 public static final Comparator<Sone> NICE_NAME_COMPARATOR = new Comparator<Sone>() {
72 public int compare(Sone leftSone, Sone rightSone) {
73 int diff = SoneAccessor.getNiceName(leftSone).compareToIgnoreCase(SoneAccessor.getNiceName(rightSone));
77 return leftSone.getId().compareToIgnoreCase(rightSone.getId());
82 /** Comparator that sorts Sones by last activity (least recent active first). */
83 public static final Comparator<Sone> LAST_ACTIVITY_COMPARATOR = new Comparator<Sone>() {
86 public int compare(Sone firstSone, Sone secondSone) {
87 return (int) Math.min(Integer.MAX_VALUE, Math.max(Integer.MIN_VALUE, secondSone.getTime() - firstSone.getTime()));
91 /** Comparator that sorts Sones by numbers of posts (descending). */
92 public static final Comparator<Sone> POST_COUNT_COMPARATOR = new Comparator<Sone>() {
98 public int compare(Sone leftSone, Sone rightSone) {
99 return (leftSone.getPosts().size() != rightSone.getPosts().size()) ? (rightSone.getPosts().size() - leftSone.getPosts().size()) : (rightSone.getReplies().size() - leftSone.getReplies().size());
103 /** Comparator that sorts Sones by number of images (descending). */
104 public static final Comparator<Sone> IMAGE_COUNT_COMPARATOR = new Comparator<Sone>() {
110 public int compare(Sone leftSone, Sone rightSone) {
111 int rightSoneImageCount = from(asList(rightSone.getRootAlbum())).transformAndConcat(FLATTENER).transformAndConcat(IMAGES).size();
112 int leftSoneImageCount = from(asList(leftSone.getRootAlbum())).transformAndConcat(FLATTENER).transformAndConcat(IMAGES).size();
113 /* sort descending. */
114 return Ints.compare(rightSoneImageCount, leftSoneImageCount);
118 /** Filter to remove Sones that have not been downloaded. */
119 public static final Predicate<Sone> EMPTY_SONE_FILTER = new Predicate<Sone>() {
122 public boolean apply(Sone sone) {
123 return (sone != null) && (sone.getTime() != 0);
127 /** Filter that matches all {@link Sone#isLocal() local Sones}. */
128 public static final Predicate<Sone> LOCAL_SONE_FILTER = new Predicate<Sone>() {
131 public boolean apply(Sone sone) {
132 return (sone != null) && (sone.getIdentity() instanceof OwnIdentity);
137 /** Filter that matches Sones that have at least one album. */
138 public static final Predicate<Sone> HAS_ALBUM_FILTER = new Predicate<Sone>() {
141 public boolean apply(Sone sone) {
142 return (sone != null) && !sone.getRootAlbum().getAlbums().isEmpty();
146 public static final Function<Sone, List<Album>> toAllAlbums = new Function<Sone, List<Album>>() {
148 public List<Album> apply(@Nullable Sone sone) {
149 return (sone == null) ? Collections.<Album>emptyList() : FLATTENER.apply(
150 sone.getRootAlbum());
154 public static final Function<Sone, List<Image>> toAllImages = new Function<Sone, List<Image>>() {
156 public List<Image> apply(@Nullable Sone sone) {
157 return (sone == null) ? Collections.<Image>emptyList() :
158 from(FLATTENER.apply(sone.getRootAlbum()))
159 .transformAndConcat(IMAGES).toList();
164 * Returns the identity of this Sone.
166 * @return The identity of this Sone
169 Identity getIdentity();
172 * Returns the name of this Sone.
174 * @return The name of this Sone
180 * Returns whether this Sone is a local Sone.
182 * @return {@code true} if this Sone is a local Sone, {@code false} otherwise
187 * Returns the request URI of this Sone.
189 * @return The request URI of this Sone
192 FreenetURI getRequestUri();
195 * Returns the insert URI of this Sone.
197 * @return The insert URI of this Sone
200 FreenetURI getInsertUri();
203 * Returns the latest edition of this Sone.
205 * @return The latest edition of this Sone
207 long getLatestEdition();
210 * Sets the latest edition of this Sone. If the given latest edition is not
211 * greater than the current latest edition, the latest edition of this Sone is
214 * @param latestEdition
215 * The latest edition of this Sone
217 void setLatestEdition(long latestEdition);
220 * Return the time of the last inserted update of this Sone.
222 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
227 * Sets the time of the last inserted update of this Sone.
230 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
231 * @return This Sone (for method chaining)
234 Sone setTime(long time);
237 * Returns the status of this Sone.
239 * @return The status of this Sone
242 SoneStatus getStatus();
245 * Sets the new status of this Sone.
248 * The new status of this Sone
250 * @throws IllegalArgumentException
251 * if {@code status} is {@code null}
254 Sone setStatus(@Nonnull SoneStatus status);
257 * Returns a copy of the profile. If you want to update values in the profile
258 * of this Sone, update the values in the returned {@link Profile} and use
259 * {@link #setProfile(Profile)} to change the profile in this Sone.
261 * @return A copy of the profile
264 Profile getProfile();
267 * Sets the profile of this Sone. A copy of the given profile is stored so that
268 * subsequent modifications of the given profile are not reflected in this
274 void setProfile(@Nonnull Profile profile);
277 * Returns the client used by this Sone.
279 * @return The client used by this Sone, or {@code null}
285 * Sets the client used by this Sone.
288 * The client used by this Sone, or {@code null}
289 * @return This Sone (for method chaining)
292 Sone setClient(@Nullable Client client);
295 * Returns whether this Sone is known.
297 * @return {@code true} if this Sone is known, {@code false} otherwise
302 * Sets whether this Sone is known.
305 * {@code true} if this Sone is known, {@code false} otherwise
309 Sone setKnown(boolean known);
312 * Returns all friend Sones of this Sone.
314 * @return The friend Sones of this Sone
317 Collection<String> getFriends();
320 * Returns whether this Sone has the given Sone as a friend Sone.
322 * @param friendSoneId
323 * The ID of the Sone to check for
324 * @return {@code true} if this Sone has the given Sone as a friend, {@code
327 boolean hasFriend(@Nonnull String friendSoneId);
330 * Returns the list of posts of this Sone, sorted by time, newest first.
332 * @return All posts of this Sone
335 List<Post> getPosts();
338 * Sets all posts of this Sone at once.
341 * The new (and only) posts of this Sone
342 * @return This Sone (for method chaining)
345 Sone setPosts(@Nonnull Collection<Post> posts);
348 * Adds the given post to this Sone. The post will not be added if its {@link
349 * Post#getSone() Sone} is not this Sone.
354 void addPost(@Nonnull Post post);
357 * Removes the given post from this Sone.
362 void removePost(@Nonnull Post post);
365 * Returns all replies this Sone made.
367 * @return All replies this Sone made
370 Set<PostReply> getReplies();
373 * Sets all replies of this Sone at once.
376 * The new (and only) replies of this Sone
377 * @return This Sone (for method chaining)
380 Sone setReplies(@Nonnull Collection<PostReply> replies);
383 * Adds a reply to this Sone. If the given reply was not made by this Sone,
384 * nothing is added to this Sone.
389 void addReply(@Nonnull PostReply reply);
392 * Removes a reply from this Sone.
395 * The reply to remove
397 void removeReply(@Nonnull PostReply reply);
400 * Returns the IDs of all liked posts.
402 * @return All liked posts’ IDs
405 Set<String> getLikedPostIds();
408 * Sets the IDs of all liked posts.
410 * @param likedPostIds
411 * All liked posts’ IDs
412 * @return This Sone (for method chaining)
415 Sone setLikePostIds(@Nonnull Set<String> likedPostIds);
418 * Checks whether the given post ID is liked by this Sone.
422 * @return {@code true} if this Sone likes the given post, {@code false}
425 boolean isLikedPostId(@Nonnull String postId);
428 * Adds the given post ID to the list of posts this Sone likes.
432 * @return This Sone (for method chaining)
435 Sone addLikedPostId(@Nonnull String postId);
438 * Removes the given post ID from the list of posts this Sone likes.
443 void removeLikedPostId(@Nonnull String postId);
446 * Returns the IDs of all liked replies.
448 * @return All liked replies’ IDs
451 Set<String> getLikedReplyIds();
454 * Sets the IDs of all liked replies.
456 * @param likedReplyIds
457 * All liked replies’ IDs
458 * @return This Sone (for method chaining)
461 Sone setLikeReplyIds(@Nonnull Set<String> likedReplyIds);
464 * Checks whether the given reply ID is liked by this Sone.
467 * The ID of the reply
468 * @return {@code true} if this Sone likes the given reply, {@code false}
471 boolean isLikedReplyId(@Nonnull String replyId);
474 * Adds the given reply ID to the list of replies this Sone likes.
477 * The ID of the reply
478 * @return This Sone (for method chaining)
481 Sone addLikedReplyId(@Nonnull String replyId);
484 * Removes the given post ID from the list of replies this Sone likes.
487 * The ID of the reply
489 void removeLikedReplyId(@Nonnull String replyId);
492 * Returns the root album that contains all visible albums of this Sone.
494 * @return The root album of this Sone
497 Album getRootAlbum();
500 * Returns Sone-specific options.
502 * @return The options of this Sone
505 SoneOptions getOptions();
508 * Sets the options of this Sone.
511 * The options of this Sone
513 /* TODO - remove this method again, maybe add an option provider */
514 void setOptions(@Nonnull SoneOptions options);