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.core.SoneUri.create;
23 import static net.pterodactylus.sone.data.Album.FLATTENER;
24 import static net.pterodactylus.sone.data.Album.IMAGES;
25 import static net.pterodactylus.sone.template.SoneAccessor.getNiceName;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.Comparator;
30 import java.util.List;
33 import javax.annotation.Nullable;
35 import net.pterodactylus.sone.core.Options;
36 import net.pterodactylus.sone.database.AlbumBuilder;
37 import net.pterodactylus.sone.database.PostBuilder;
38 import net.pterodactylus.sone.database.PostReplyBuilder;
39 import net.pterodactylus.sone.freenet.wot.Identity;
40 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
42 import freenet.keys.FreenetURI;
44 import com.google.common.base.Function;
45 import com.google.common.base.Predicate;
46 import com.google.common.collect.ComparisonChain;
47 import com.google.common.primitives.Ints;
50 * A Sone defines everything about a user: her profile, her status updates, her
51 * replies, her likes and dislikes, etc.
53 * @author <a href="mailto:bombe@pterodactylus.net">David āBombeā Roden</a>
55 public interface Sone extends Identified, Fingerprintable, Comparable<Sone> {
58 * Enumeration for the possible states of a {@link Sone}.
60 * @author <a href="mailto:bombe@pterodactylus.net">David āBombeā Roden</a>
62 public enum SoneStatus {
64 /** The Sone is unknown, i.e. not yet downloaded. */
67 /** The Sone is idle, i.e. not being downloaded or inserted. */
70 /** The Sone is currently being inserted. */
73 /** The Sone is currently being downloaded. */
78 * The possible values for the āshow custom avatarsā option.
80 * @author <a href="mailto:bombe@pterodactylus.net">David āBombeā Roden</a>
82 public static enum ShowCustomAvatars {
84 /** Never show custom avatars. */
87 /** Only show custom avatars of followed Sones. */
90 /** Only show custom avatars of Sones you manually trust. */
93 /** Only show custom avatars of automatically trusted Sones. */
96 /** Always show custom avatars. */
101 /** comparator that sorts Sones by their nice name. */
102 public static final Comparator<Sone> NICE_NAME_COMPARATOR = new Comparator<Sone>() {
105 public int compare(Sone leftSone, Sone rightSone) {
106 return ComparisonChain.start()
107 .compare(getNiceName(leftSone).toLowerCase(), getNiceName(rightSone).toLowerCase())
108 .compare(leftSone.getId(), rightSone.getId())
114 public static final Function<Sone, String> TO_NICE_NAME = new Function<Sone, String>() {
116 public String apply(Sone sone) {
117 return (sone == null) ? null : getNiceName(sone);
121 /** Comparator that sorts Sones by last activity (least recent active first). */
122 public static final Comparator<Sone> LAST_ACTIVITY_COMPARATOR = new Comparator<Sone>() {
125 public int compare(Sone firstSone, Sone secondSone) {
126 return (int) Math.min(Integer.MAX_VALUE, Math.max(Integer.MIN_VALUE, secondSone.getTime() - firstSone.getTime()));
130 /** Comparator that sorts Sones by numbers of posts (descending). */
131 public static final Comparator<Sone> POST_COUNT_COMPARATOR = new Comparator<Sone>() {
137 public int compare(Sone leftSone, Sone rightSone) {
138 return (leftSone.getPosts().size() != rightSone.getPosts().size()) ? (rightSone.getPosts().size() - leftSone.getPosts().size()) : (rightSone.getReplies().size() - leftSone.getReplies().size());
142 /** Comparator that sorts Sones by number of images (descending). */
143 public static final Comparator<Sone> IMAGE_COUNT_COMPARATOR = new Comparator<Sone>() {
149 public int compare(Sone leftSone, Sone rightSone) {
150 int rightSoneImageCount = from(asList(rightSone.getRootAlbum())).transformAndConcat(FLATTENER).transformAndConcat(IMAGES).size();
151 int leftSoneImageCount = from(asList(leftSone.getRootAlbum())).transformAndConcat(FLATTENER).transformAndConcat(IMAGES).size();
152 /* sort descending. */
153 return Ints.compare(rightSoneImageCount, leftSoneImageCount);
157 /** Filter to remove Sones that have not been downloaded. */
158 public static final Predicate<Sone> EMPTY_SONE_FILTER = new Predicate<Sone>() {
161 public boolean apply(Sone sone) {
162 return (sone == null) ? false : sone.getTime() != 0;
166 /** Filter that matches all {@link Sone#isLocal() local Sones}. */
167 public static final Predicate<Sone> LOCAL_SONE_FILTER = new Predicate<Sone>() {
170 public boolean apply(Sone sone) {
171 return (sone == null) ? false : sone.isLocal();
176 /** Filter that matches Sones that have at least one album. */
177 public static final Predicate<Sone> HAS_ALBUM_FILTER = new Predicate<Sone>() {
180 public boolean apply(Sone sone) {
181 return (sone == null) ? false : !sone.getRootAlbum().getAlbums().isEmpty();
185 public static final Function<Sone, FreenetURI> TO_FREENET_URI = new Function<Sone, FreenetURI>() {
187 public FreenetURI apply(Sone sone) {
188 return (sone == null) ? null : create(sone.getIdentity().getRequestUri());
192 public static final Function<Sone, FreenetURI> TO_INSERT_URI = new Function<Sone, FreenetURI>() {
194 public FreenetURI apply(@Nullable Sone sone) {
195 return ((sone == null) || !sone.isLocal()) ? null : create(((OwnIdentity) sone.getIdentity()).getInsertUri());
199 public static final Function<Sone, List<Post>> TO_POSTS = new Function<Sone, List<Post>>() {
201 public List<Post> apply(Sone sone) {
202 return (sone == null) ? Collections.<Post>emptyList() : sone.getPosts();
207 * Returns the identity of this Sone.
209 * @return The identity of this Sone
211 Identity getIdentity();
214 * Returns the name of this Sone.
216 * @return The name of this Sone
221 * Returns whether this Sone is a local Sone.
223 * @return {@code true} if this Sone is a local Sone, {@code false} otherwise
228 * Returns the latest edition of this Sone.
230 * @return The latest edition of this Sone
232 long getLatestEdition();
235 * Return the time of the last inserted update of this Sone.
237 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
242 * Sets the time of the last inserted update of this Sone.
245 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
246 * @return This Sone (for method chaining)
248 Sone setTime(long time);
251 * Returns the status of this Sone.
253 * @return The status of this Sone
255 SoneStatus getStatus();
258 * Sets the new status of this Sone.
261 * The new status of this Sone
263 * @throws IllegalArgumentException
264 * if {@code status} is {@code null}
266 Sone setStatus(SoneStatus status);
269 * Returns a copy of the profile. If you want to update values in the profile
270 * of this Sone, update the values in the returned {@link Profile} and use
271 * {@link #setProfile(Profile)} to change the profile in this Sone.
273 * @return A copy of the profile
275 Profile getProfile();
278 * Sets the profile of this Sone. A copy of the given profile is stored so that
279 * subsequent modifications of the given profile are not reflected in this
285 void setProfile(Profile profile);
288 * Returns the client used by this Sone.
290 * @return The client used by this Sone, or {@code null}
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
308 Sone setKnown(boolean known);
311 * Returns all friend Sones of this Sone.
313 * @return The friend Sones of this Sone
315 Collection<String> getFriends();
318 * Returns whether this Sone has the given Sone as a friend Sone.
320 * @param friendSoneId
321 * The ID of the Sone to check for
322 * @return {@code true} if this Sone has the given Sone as a friend, {@code
325 boolean hasFriend(String friendSoneId);
328 * Adds the given Sone as a friend Sone.
331 * The friend Sone to add
332 * @return This Sone (for method chaining)
334 Sone addFriend(String friendSone);
337 * Removes the given Sone as a friend Sone.
339 * @param friendSoneId
340 * The ID of the friend Sone to remove
341 * @return This Sone (for method chaining)
343 Sone removeFriend(String friendSoneId);
346 * Returns the list of posts of this Sone, sorted by time, newest first.
348 * @return All posts of this Sone
350 List<Post> getPosts();
353 * Sets all posts of this Sone at once.
356 * The new (and only) posts of this Sone
357 * @return This Sone (for method chaining)
359 Sone setPosts(Collection<Post> posts);
362 * Adds the given post to this Sone. The post will not be added if its {@link
363 * Post#getSone() Sone} is not this Sone.
368 void addPost(Post post);
371 * Removes the given post from this Sone.
376 void removePost(Post post);
379 * Returns all replies this Sone made.
381 * @return All replies this Sone made
383 Set<PostReply> getReplies();
386 * Sets all replies of this Sone at once.
389 * The new (and only) replies of this Sone
390 * @return This Sone (for method chaining)
392 Sone setReplies(Collection<PostReply> replies);
395 * Adds a reply to this Sone. If the given reply was not made by this Sone,
396 * nothing is added to this Sone.
401 void addReply(PostReply reply);
404 * Removes a reply from this Sone.
407 * The reply to remove
409 void removeReply(PostReply reply);
412 * Returns the IDs of all liked posts.
414 * @return All liked postsā IDs
416 Set<String> getLikedPostIds();
419 * Sets the IDs of all liked posts.
421 * @param likedPostIds
422 * All liked postsā IDs
423 * @return This Sone (for method chaining)
425 Sone setLikePostIds(Set<String> likedPostIds);
428 * Returns the IDs of all liked replies.
430 * @return All liked repliesā IDs
432 Set<String> getLikedReplyIds();
435 * Sets the IDs of all liked replies.
437 * @param likedReplyIds
438 * All liked repliesā IDs
439 * @return This Sone (for method chaining)
441 Sone setLikeReplyIds(Set<String> likedReplyIds);
444 * Returns the root album that contains all visible albums of this Sone.
446 * @return The root album of this Sone
448 Album getRootAlbum();
451 * Returns Sone-specific options.
453 * @return The options of this Sone
455 Options getOptions();
458 * Sets the options of this Sone.
461 * The options of this Sone
463 /* TODO - remove this method again, maybe add an option provider */
464 void setOptions(Options options);
466 AlbumBuilder newAlbumBuilder() throws IllegalStateException;
468 PostBuilder newPostBuilder();
470 PostReplyBuilder newPostReplyBuilder(String postId) throws IllegalStateException;
476 Modifier setLatestEdition(long latestEdition);