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;
36 import freenet.keys.FreenetURI;
38 import com.google.common.base.Function;
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 public interface Sone extends Identified, Fingerprintable, Comparable<Sone> {
48 * Enumeration for the possible states of a {@link Sone}.
50 public enum SoneStatus {
52 /** The Sone is unknown, i.e. not yet downloaded. */
55 /** The Sone is idle, i.e. not being downloaded or inserted. */
58 /** The Sone is currently being inserted. */
61 /** The Sone is currently being downloaded. */
65 /** Comparator that sorts Sones by numbers of posts (descending). */
66 public static final Comparator<Sone> POST_COUNT_COMPARATOR = new Comparator<Sone>() {
72 public int compare(Sone leftSone, Sone rightSone) {
73 return (leftSone.getPosts().size() != rightSone.getPosts().size()) ? (rightSone.getPosts().size() - leftSone.getPosts().size()) : (rightSone.getReplies().size() - leftSone.getReplies().size());
77 /** Comparator that sorts Sones by number of images (descending). */
78 public static final Comparator<Sone> IMAGE_COUNT_COMPARATOR = new Comparator<Sone>() {
84 public int compare(Sone leftSone, Sone rightSone) {
85 int rightSoneImageCount = from(asList(rightSone.getRootAlbum())).transformAndConcat(FLATTENER).transformAndConcat(IMAGES).size();
86 int leftSoneImageCount = from(asList(leftSone.getRootAlbum())).transformAndConcat(FLATTENER).transformAndConcat(IMAGES).size();
87 /* sort descending. */
88 return Ints.compare(rightSoneImageCount, leftSoneImageCount);
92 public static final Function<Sone, List<Album>> toAllAlbums = new Function<Sone, List<Album>>() {
94 public List<Album> apply(@Nullable Sone sone) {
95 return (sone == null) ? Collections.<Album>emptyList() : FLATTENER.apply(
100 public static final Function<Sone, List<Image>> toAllImages = new Function<Sone, List<Image>>() {
102 public List<Image> apply(@Nullable Sone sone) {
103 return (sone == null) ? Collections.<Image>emptyList() :
104 from(FLATTENER.apply(sone.getRootAlbum()))
105 .transformAndConcat(IMAGES).toList();
110 * Returns the identity of this Sone.
112 * @return The identity of this Sone
115 Identity getIdentity();
118 * Returns the name of this Sone.
120 * @return The name of this Sone
126 * Returns whether this Sone is a local Sone.
128 * @return {@code true} if this Sone is a local Sone, {@code false} otherwise
133 * Returns the request URI of this Sone.
135 * @return The request URI of this Sone
138 FreenetURI getRequestUri();
141 * Returns the latest edition of this Sone.
143 * @return The latest edition of this Sone
145 long getLatestEdition();
148 * Sets the latest edition of this Sone. If the given latest edition is not
149 * greater than the current latest edition, the latest edition of this Sone is
152 * @param latestEdition
153 * The latest edition of this Sone
155 void setLatestEdition(long latestEdition);
158 * Return the time of the last inserted update of this Sone.
160 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
165 * Sets the time of the last inserted update of this Sone.
168 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
169 * @return This Sone (for method chaining)
172 Sone setTime(long time);
175 * Returns the status of this Sone.
177 * @return The status of this Sone
180 SoneStatus getStatus();
183 * Sets the new status of this Sone.
186 * The new status of this Sone
188 * @throws IllegalArgumentException
189 * if {@code status} is {@code null}
192 Sone setStatus(@Nonnull SoneStatus status);
195 * Returns a copy of the profile. If you want to update values in the profile
196 * of this Sone, update the values in the returned {@link Profile} and use
197 * {@link #setProfile(Profile)} to change the profile in this Sone.
199 * @return A copy of the profile
202 Profile getProfile();
205 * Sets the profile of this Sone. A copy of the given profile is stored so that
206 * subsequent modifications of the given profile are not reflected in this
212 void setProfile(@Nonnull Profile profile);
215 * Returns the client used by this Sone.
217 * @return The client used by this Sone, or {@code null}
223 * Sets the client used by this Sone.
226 * The client used by this Sone, or {@code null}
227 * @return This Sone (for method chaining)
230 Sone setClient(@Nullable Client client);
233 * Returns whether this Sone is known.
235 * @return {@code true} if this Sone is known, {@code false} otherwise
240 * Sets whether this Sone is known.
243 * {@code true} if this Sone is known, {@code false} otherwise
247 Sone setKnown(boolean known);
250 * Returns all friend Sones of this Sone.
252 * @return The friend Sones of this Sone
255 Collection<String> getFriends();
258 * Returns whether this Sone has the given Sone as a friend Sone.
260 * @param friendSoneId
261 * The ID of the Sone to check for
262 * @return {@code true} if this Sone has the given Sone as a friend, {@code
265 boolean hasFriend(@Nonnull String friendSoneId);
268 * Returns the list of posts of this Sone, sorted by time, newest first.
270 * @return All posts of this Sone
273 List<Post> getPosts();
276 * Sets all posts of this Sone at once.
279 * The new (and only) posts of this Sone
280 * @return This Sone (for method chaining)
283 Sone setPosts(@Nonnull Collection<Post> posts);
286 * Adds the given post to this Sone. The post will not be added if its {@link
287 * Post#getSone() Sone} is not this Sone.
292 void addPost(@Nonnull Post post);
295 * Removes the given post from this Sone.
300 void removePost(@Nonnull Post post);
303 * Returns all replies this Sone made.
305 * @return All replies this Sone made
308 Set<PostReply> getReplies();
311 * Sets all replies of this Sone at once.
314 * The new (and only) replies of this Sone
315 * @return This Sone (for method chaining)
318 Sone setReplies(@Nonnull Collection<PostReply> replies);
321 * Adds a reply to this Sone. If the given reply was not made by this Sone,
322 * nothing is added to this Sone.
327 void addReply(@Nonnull PostReply reply);
330 * Removes a reply from this Sone.
333 * The reply to remove
335 void removeReply(@Nonnull PostReply reply);
338 * Returns the IDs of all liked posts.
340 * @return All liked posts’ IDs
343 Set<String> getLikedPostIds();
346 * Sets the IDs of all liked posts.
348 * @param likedPostIds
349 * All liked posts’ IDs
350 * @return This Sone (for method chaining)
353 Sone setLikePostIds(@Nonnull Set<String> likedPostIds);
356 * Checks whether the given post ID is liked by this Sone.
360 * @return {@code true} if this Sone likes the given post, {@code false}
363 boolean isLikedPostId(@Nonnull String postId);
366 * Adds the given post ID to the list of posts this Sone likes.
370 * @return This Sone (for method chaining)
373 Sone addLikedPostId(@Nonnull String postId);
376 * Removes the given post ID from the list of posts this Sone likes.
381 void removeLikedPostId(@Nonnull String postId);
384 * Returns the IDs of all liked replies.
386 * @return All liked replies’ IDs
389 Set<String> getLikedReplyIds();
392 * Sets the IDs of all liked replies.
394 * @param likedReplyIds
395 * All liked replies’ IDs
396 * @return This Sone (for method chaining)
399 Sone setLikeReplyIds(@Nonnull Set<String> likedReplyIds);
402 * Checks whether the given reply ID is liked by this Sone.
405 * The ID of the reply
406 * @return {@code true} if this Sone likes the given reply, {@code false}
409 boolean isLikedReplyId(@Nonnull String replyId);
412 * Adds the given reply ID to the list of replies this Sone likes.
415 * The ID of the reply
416 * @return This Sone (for method chaining)
419 Sone addLikedReplyId(@Nonnull String replyId);
422 * Removes the given post ID from the list of replies this Sone likes.
425 * The ID of the reply
427 void removeLikedReplyId(@Nonnull String replyId);
430 * Returns the root album that contains all visible albums of this Sone.
432 * @return The root album of this Sone
435 Album getRootAlbum();
438 * Returns Sone-specific options.
440 * @return The options of this Sone
443 SoneOptions getOptions();
446 * Sets the options of this Sone.
449 * The options of this Sone
451 /* TODO - remove this method again, maybe add an option provider */
452 void setOptions(@Nonnull SoneOptions options);