2 * Sone - Sone.java - Copyright © 2010–2020 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.template.SoneAccessor;
37 import freenet.keys.FreenetURI;
39 import com.google.common.base.Function;
40 import com.google.common.primitives.Ints;
43 * A Sone defines everything about a user: her profile, her status updates, her
44 * replies, her likes and dislikes, etc.
46 public interface Sone extends Identified, Fingerprintable, Comparable<Sone> {
49 * Enumeration for the possible states of a {@link Sone}.
51 public enum SoneStatus {
53 /** The Sone is unknown, i.e. not yet downloaded. */
56 /** The Sone is idle, i.e. not being downloaded or inserted. */
59 /** The Sone is currently being inserted. */
62 /** The Sone is currently being downloaded. */
66 /** comparator that sorts Sones by their nice name. */
67 public static final Comparator<Sone> NICE_NAME_COMPARATOR = new Comparator<Sone>() {
70 public int compare(Sone leftSone, Sone rightSone) {
71 int diff = SoneAccessor.getNiceName(leftSone).compareToIgnoreCase(SoneAccessor.getNiceName(rightSone));
75 return leftSone.getId().compareToIgnoreCase(rightSone.getId());
80 /** Comparator that sorts Sones by last activity (least recent active first). */
81 public static final Comparator<Sone> LAST_ACTIVITY_COMPARATOR = new Comparator<Sone>() {
84 public int compare(Sone firstSone, Sone secondSone) {
85 return (int) Math.min(Integer.MAX_VALUE, Math.max(Integer.MIN_VALUE, secondSone.getTime() - firstSone.getTime()));
89 /** Comparator that sorts Sones by numbers of posts (descending). */
90 public static final Comparator<Sone> POST_COUNT_COMPARATOR = new Comparator<Sone>() {
96 public int compare(Sone leftSone, Sone rightSone) {
97 return (leftSone.getPosts().size() != rightSone.getPosts().size()) ? (rightSone.getPosts().size() - leftSone.getPosts().size()) : (rightSone.getReplies().size() - leftSone.getReplies().size());
101 /** Comparator that sorts Sones by number of images (descending). */
102 public static final Comparator<Sone> IMAGE_COUNT_COMPARATOR = new Comparator<Sone>() {
108 public int compare(Sone leftSone, Sone rightSone) {
109 int rightSoneImageCount = from(asList(rightSone.getRootAlbum())).transformAndConcat(FLATTENER).transformAndConcat(IMAGES).size();
110 int leftSoneImageCount = from(asList(leftSone.getRootAlbum())).transformAndConcat(FLATTENER).transformAndConcat(IMAGES).size();
111 /* sort descending. */
112 return Ints.compare(rightSoneImageCount, leftSoneImageCount);
116 public static final Function<Sone, List<Album>> toAllAlbums = new Function<Sone, List<Album>>() {
118 public List<Album> apply(@Nullable Sone sone) {
119 return (sone == null) ? Collections.<Album>emptyList() : FLATTENER.apply(
120 sone.getRootAlbum());
124 public static final Function<Sone, List<Image>> toAllImages = new Function<Sone, List<Image>>() {
126 public List<Image> apply(@Nullable Sone sone) {
127 return (sone == null) ? Collections.<Image>emptyList() :
128 from(FLATTENER.apply(sone.getRootAlbum()))
129 .transformAndConcat(IMAGES).toList();
134 * Returns the identity of this Sone.
136 * @return The identity of this Sone
139 Identity getIdentity();
142 * Returns the name of this Sone.
144 * @return The name of this Sone
150 * Returns whether this Sone is a local Sone.
152 * @return {@code true} if this Sone is a local Sone, {@code false} otherwise
157 * Returns the request URI of this Sone.
159 * @return The request URI of this Sone
162 FreenetURI getRequestUri();
165 * Returns the latest edition of this Sone.
167 * @return The latest edition of this Sone
169 long getLatestEdition();
172 * Sets the latest edition of this Sone. If the given latest edition is not
173 * greater than the current latest edition, the latest edition of this Sone is
176 * @param latestEdition
177 * The latest edition of this Sone
179 void setLatestEdition(long latestEdition);
182 * Return the time of the last inserted update of this Sone.
184 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
189 * Sets the time of the last inserted update of this Sone.
192 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
193 * @return This Sone (for method chaining)
196 Sone setTime(long time);
199 * Returns the status of this Sone.
201 * @return The status of this Sone
204 SoneStatus getStatus();
207 * Sets the new status of this Sone.
210 * The new status of this Sone
212 * @throws IllegalArgumentException
213 * if {@code status} is {@code null}
216 Sone setStatus(@Nonnull SoneStatus status);
219 * Returns a copy of the profile. If you want to update values in the profile
220 * of this Sone, update the values in the returned {@link Profile} and use
221 * {@link #setProfile(Profile)} to change the profile in this Sone.
223 * @return A copy of the profile
226 Profile getProfile();
229 * Sets the profile of this Sone. A copy of the given profile is stored so that
230 * subsequent modifications of the given profile are not reflected in this
236 void setProfile(@Nonnull Profile profile);
239 * Returns the client used by this Sone.
241 * @return The client used by this Sone, or {@code null}
247 * Sets the client used by this Sone.
250 * The client used by this Sone, or {@code null}
251 * @return This Sone (for method chaining)
254 Sone setClient(@Nullable Client client);
257 * Returns whether this Sone is known.
259 * @return {@code true} if this Sone is known, {@code false} otherwise
264 * Sets whether this Sone is known.
267 * {@code true} if this Sone is known, {@code false} otherwise
271 Sone setKnown(boolean known);
274 * Returns all friend Sones of this Sone.
276 * @return The friend Sones of this Sone
279 Collection<String> getFriends();
282 * Returns whether this Sone has the given Sone as a friend Sone.
284 * @param friendSoneId
285 * The ID of the Sone to check for
286 * @return {@code true} if this Sone has the given Sone as a friend, {@code
289 boolean hasFriend(@Nonnull String friendSoneId);
292 * Returns the list of posts of this Sone, sorted by time, newest first.
294 * @return All posts of this Sone
297 List<Post> getPosts();
300 * Sets all posts of this Sone at once.
303 * The new (and only) posts of this Sone
304 * @return This Sone (for method chaining)
307 Sone setPosts(@Nonnull Collection<Post> posts);
310 * Adds the given post to this Sone. The post will not be added if its {@link
311 * Post#getSone() Sone} is not this Sone.
316 void addPost(@Nonnull Post post);
319 * Removes the given post from this Sone.
324 void removePost(@Nonnull Post post);
327 * Returns all replies this Sone made.
329 * @return All replies this Sone made
332 Set<PostReply> getReplies();
335 * Sets all replies of this Sone at once.
338 * The new (and only) replies of this Sone
339 * @return This Sone (for method chaining)
342 Sone setReplies(@Nonnull Collection<PostReply> replies);
345 * Adds a reply to this Sone. If the given reply was not made by this Sone,
346 * nothing is added to this Sone.
351 void addReply(@Nonnull PostReply reply);
354 * Removes a reply from this Sone.
357 * The reply to remove
359 void removeReply(@Nonnull PostReply reply);
362 * Returns the IDs of all liked posts.
364 * @return All liked posts’ IDs
367 Set<String> getLikedPostIds();
370 * Sets the IDs of all liked posts.
372 * @param likedPostIds
373 * All liked posts’ IDs
374 * @return This Sone (for method chaining)
377 Sone setLikePostIds(@Nonnull Set<String> likedPostIds);
380 * Checks whether the given post ID is liked by this Sone.
384 * @return {@code true} if this Sone likes the given post, {@code false}
387 boolean isLikedPostId(@Nonnull String postId);
390 * Adds the given post ID to the list of posts this Sone likes.
394 * @return This Sone (for method chaining)
397 Sone addLikedPostId(@Nonnull String postId);
400 * Removes the given post ID from the list of posts this Sone likes.
405 void removeLikedPostId(@Nonnull String postId);
408 * Returns the IDs of all liked replies.
410 * @return All liked replies’ IDs
413 Set<String> getLikedReplyIds();
416 * Sets the IDs of all liked replies.
418 * @param likedReplyIds
419 * All liked replies’ IDs
420 * @return This Sone (for method chaining)
423 Sone setLikeReplyIds(@Nonnull Set<String> likedReplyIds);
426 * Checks whether the given reply ID is liked by this Sone.
429 * The ID of the reply
430 * @return {@code true} if this Sone likes the given reply, {@code false}
433 boolean isLikedReplyId(@Nonnull String replyId);
436 * Adds the given reply ID to the list of replies this Sone likes.
439 * The ID of the reply
440 * @return This Sone (for method chaining)
443 Sone addLikedReplyId(@Nonnull String replyId);
446 * Removes the given post ID from the list of replies this Sone likes.
449 * The ID of the reply
451 void removeLikedReplyId(@Nonnull String replyId);
454 * Returns the root album that contains all visible albums of this Sone.
456 * @return The root album of this Sone
459 Album getRootAlbum();
462 * Returns Sone-specific options.
464 * @return The options of this Sone
467 SoneOptions getOptions();
470 * Sets the options of this Sone.
473 * The options of this Sone
475 /* TODO - remove this method again, maybe add an option provider */
476 void setOptions(@Nonnull SoneOptions options);