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 number of images (descending). */
66 public static final Comparator<Sone> IMAGE_COUNT_COMPARATOR = new Comparator<Sone>() {
72 public int compare(Sone leftSone, Sone rightSone) {
73 int rightSoneImageCount = from(asList(rightSone.getRootAlbum())).transformAndConcat(FLATTENER).transformAndConcat(IMAGES).size();
74 int leftSoneImageCount = from(asList(leftSone.getRootAlbum())).transformAndConcat(FLATTENER).transformAndConcat(IMAGES).size();
75 /* sort descending. */
76 return Ints.compare(rightSoneImageCount, leftSoneImageCount);
80 public static final Function<Sone, List<Album>> toAllAlbums = new Function<Sone, List<Album>>() {
82 public List<Album> apply(@Nullable Sone sone) {
83 return (sone == null) ? Collections.<Album>emptyList() : FLATTENER.apply(
88 public static final Function<Sone, List<Image>> toAllImages = new Function<Sone, List<Image>>() {
90 public List<Image> apply(@Nullable Sone sone) {
91 return (sone == null) ? Collections.<Image>emptyList() :
92 from(FLATTENER.apply(sone.getRootAlbum()))
93 .transformAndConcat(IMAGES).toList();
98 * Returns the identity of this Sone.
100 * @return The identity of this Sone
103 Identity getIdentity();
106 * Returns the name of this Sone.
108 * @return The name of this Sone
114 * Returns whether this Sone is a local Sone.
116 * @return {@code true} if this Sone is a local Sone, {@code false} otherwise
121 * Returns the request URI of this Sone.
123 * @return The request URI of this Sone
126 FreenetURI getRequestUri();
129 * Returns the latest edition of this Sone.
131 * @return The latest edition of this Sone
133 long getLatestEdition();
136 * Sets the latest edition of this Sone. If the given latest edition is not
137 * greater than the current latest edition, the latest edition of this Sone is
140 * @param latestEdition
141 * The latest edition of this Sone
143 void setLatestEdition(long latestEdition);
146 * Return the time of the last inserted update of this Sone.
148 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
153 * Sets the time of the last inserted update of this Sone.
156 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
157 * @return This Sone (for method chaining)
160 Sone setTime(long time);
163 * Returns the status of this Sone.
165 * @return The status of this Sone
168 SoneStatus getStatus();
171 * Sets the new status of this Sone.
174 * The new status of this Sone
176 * @throws IllegalArgumentException
177 * if {@code status} is {@code null}
180 Sone setStatus(@Nonnull SoneStatus status);
183 * Returns a copy of the profile. If you want to update values in the profile
184 * of this Sone, update the values in the returned {@link Profile} and use
185 * {@link #setProfile(Profile)} to change the profile in this Sone.
187 * @return A copy of the profile
190 Profile getProfile();
193 * Sets the profile of this Sone. A copy of the given profile is stored so that
194 * subsequent modifications of the given profile are not reflected in this
200 void setProfile(@Nonnull Profile profile);
203 * Returns the client used by this Sone.
205 * @return The client used by this Sone, or {@code null}
211 * Sets the client used by this Sone.
214 * The client used by this Sone, or {@code null}
215 * @return This Sone (for method chaining)
218 Sone setClient(@Nullable Client client);
221 * Returns whether this Sone is known.
223 * @return {@code true} if this Sone is known, {@code false} otherwise
228 * Sets whether this Sone is known.
231 * {@code true} if this Sone is known, {@code false} otherwise
235 Sone setKnown(boolean known);
238 * Returns all friend Sones of this Sone.
240 * @return The friend Sones of this Sone
243 Collection<String> getFriends();
246 * Returns whether this Sone has the given Sone as a friend Sone.
248 * @param friendSoneId
249 * The ID of the Sone to check for
250 * @return {@code true} if this Sone has the given Sone as a friend, {@code
253 boolean hasFriend(@Nonnull String friendSoneId);
256 * Returns the list of posts of this Sone, sorted by time, newest first.
258 * @return All posts of this Sone
261 List<Post> getPosts();
264 * Sets all posts of this Sone at once.
267 * The new (and only) posts of this Sone
268 * @return This Sone (for method chaining)
271 Sone setPosts(@Nonnull Collection<Post> posts);
274 * Adds the given post to this Sone. The post will not be added if its {@link
275 * Post#getSone() Sone} is not this Sone.
280 void addPost(@Nonnull Post post);
283 * Removes the given post from this Sone.
288 void removePost(@Nonnull Post post);
291 * Returns all replies this Sone made.
293 * @return All replies this Sone made
296 Set<PostReply> getReplies();
299 * Sets all replies of this Sone at once.
302 * The new (and only) replies of this Sone
303 * @return This Sone (for method chaining)
306 Sone setReplies(@Nonnull Collection<PostReply> replies);
309 * Adds a reply to this Sone. If the given reply was not made by this Sone,
310 * nothing is added to this Sone.
315 void addReply(@Nonnull PostReply reply);
318 * Removes a reply from this Sone.
321 * The reply to remove
323 void removeReply(@Nonnull PostReply reply);
326 * Returns the IDs of all liked posts.
328 * @return All liked posts’ IDs
331 Set<String> getLikedPostIds();
334 * Sets the IDs of all liked posts.
336 * @param likedPostIds
337 * All liked posts’ IDs
338 * @return This Sone (for method chaining)
341 Sone setLikePostIds(@Nonnull Set<String> likedPostIds);
344 * Checks whether the given post ID is liked by this Sone.
348 * @return {@code true} if this Sone likes the given post, {@code false}
351 boolean isLikedPostId(@Nonnull String postId);
354 * Adds the given post ID to the list of posts this Sone likes.
358 * @return This Sone (for method chaining)
361 Sone addLikedPostId(@Nonnull String postId);
364 * Removes the given post ID from the list of posts this Sone likes.
369 void removeLikedPostId(@Nonnull String postId);
372 * Returns the IDs of all liked replies.
374 * @return All liked replies’ IDs
377 Set<String> getLikedReplyIds();
380 * Sets the IDs of all liked replies.
382 * @param likedReplyIds
383 * All liked replies’ IDs
384 * @return This Sone (for method chaining)
387 Sone setLikeReplyIds(@Nonnull Set<String> likedReplyIds);
390 * Checks whether the given reply ID is liked by this Sone.
393 * The ID of the reply
394 * @return {@code true} if this Sone likes the given reply, {@code false}
397 boolean isLikedReplyId(@Nonnull String replyId);
400 * Adds the given reply ID to the list of replies this Sone likes.
403 * The ID of the reply
404 * @return This Sone (for method chaining)
407 Sone addLikedReplyId(@Nonnull String replyId);
410 * Removes the given post ID from the list of replies this Sone likes.
413 * The ID of the reply
415 void removeLikedReplyId(@Nonnull String replyId);
418 * Returns the root album that contains all visible albums of this Sone.
420 * @return The root album of this Sone
423 Album getRootAlbum();
426 * Returns Sone-specific options.
428 * @return The options of this Sone
431 SoneOptions getOptions();
434 * Sets the options of this Sone.
437 * The options of this Sone
439 /* TODO - remove this method again, maybe add an option provider */
440 void setOptions(@Nonnull SoneOptions options);