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 net.pterodactylus.sone.core.Options;
34 import net.pterodactylus.sone.database.AlbumBuilder;
35 import net.pterodactylus.sone.database.PostBuilder;
36 import net.pterodactylus.sone.database.PostReplyBuilder;
37 import net.pterodactylus.sone.freenet.wot.Identity;
39 import freenet.keys.FreenetURI;
41 import com.google.common.base.Function;
42 import com.google.common.base.Predicate;
43 import com.google.common.collect.ComparisonChain;
44 import com.google.common.primitives.Ints;
47 * A Sone defines everything about a user: her profile, her status updates, her
48 * replies, her likes and dislikes, etc.
50 * @author <a href="mailto:bombe@pterodactylus.net">David āBombeā Roden</a>
52 public interface Sone extends Identified, Fingerprintable, Comparable<Sone> {
55 * Enumeration for the possible states of a {@link Sone}.
57 * @author <a href="mailto:bombe@pterodactylus.net">David āBombeā Roden</a>
59 public enum SoneStatus {
61 /** The Sone is unknown, i.e. not yet downloaded. */
64 /** The Sone is idle, i.e. not being downloaded or inserted. */
67 /** The Sone is currently being inserted. */
70 /** The Sone is currently being downloaded. */
75 * The possible values for the āshow custom avatarsā option.
77 * @author <a href="mailto:bombe@pterodactylus.net">David āBombeā Roden</a>
79 public static enum ShowCustomAvatars {
81 /** Never show custom avatars. */
84 /** Only show custom avatars of followed Sones. */
87 /** Only show custom avatars of Sones you manually trust. */
90 /** Only show custom avatars of automatically trusted Sones. */
93 /** Always show custom avatars. */
98 /** comparator that sorts Sones by their nice name. */
99 public static final Comparator<Sone> NICE_NAME_COMPARATOR = new Comparator<Sone>() {
102 public int compare(Sone leftSone, Sone rightSone) {
103 return ComparisonChain.start()
104 .compare(getNiceName(leftSone).toLowerCase(), getNiceName(rightSone).toLowerCase())
105 .compare(leftSone.getId(), rightSone.getId())
111 public static final Function<Sone, String> TO_NICE_NAME = new Function<Sone, String>() {
113 public String apply(Sone sone) {
114 return (sone == null) ? null : getNiceName(sone);
118 /** Comparator that sorts Sones by last activity (least recent active first). */
119 public static final Comparator<Sone> LAST_ACTIVITY_COMPARATOR = new Comparator<Sone>() {
122 public int compare(Sone firstSone, Sone secondSone) {
123 return (int) Math.min(Integer.MAX_VALUE, Math.max(Integer.MIN_VALUE, secondSone.getTime() - firstSone.getTime()));
127 /** Comparator that sorts Sones by numbers of posts (descending). */
128 public static final Comparator<Sone> POST_COUNT_COMPARATOR = new Comparator<Sone>() {
134 public int compare(Sone leftSone, Sone rightSone) {
135 return (leftSone.getPosts().size() != rightSone.getPosts().size()) ? (rightSone.getPosts().size() - leftSone.getPosts().size()) : (rightSone.getReplies().size() - leftSone.getReplies().size());
139 /** Comparator that sorts Sones by number of images (descending). */
140 public static final Comparator<Sone> IMAGE_COUNT_COMPARATOR = new Comparator<Sone>() {
146 public int compare(Sone leftSone, Sone rightSone) {
147 int rightSoneImageCount = from(asList(rightSone.getRootAlbum())).transformAndConcat(FLATTENER).transformAndConcat(IMAGES).size();
148 int leftSoneImageCount = from(asList(leftSone.getRootAlbum())).transformAndConcat(FLATTENER).transformAndConcat(IMAGES).size();
149 /* sort descending. */
150 return Ints.compare(rightSoneImageCount, leftSoneImageCount);
154 /** Filter to remove Sones that have not been downloaded. */
155 public static final Predicate<Sone> EMPTY_SONE_FILTER = new Predicate<Sone>() {
158 public boolean apply(Sone sone) {
159 return (sone == null) ? false : sone.getTime() != 0;
163 /** Filter that matches all {@link Sone#isLocal() local Sones}. */
164 public static final Predicate<Sone> LOCAL_SONE_FILTER = new Predicate<Sone>() {
167 public boolean apply(Sone sone) {
168 return (sone == null) ? false : sone.isLocal();
173 /** Filter that matches Sones that have at least one album. */
174 public static final Predicate<Sone> HAS_ALBUM_FILTER = new Predicate<Sone>() {
177 public boolean apply(Sone sone) {
178 return (sone == null) ? false : !sone.getRootAlbum().getAlbums().isEmpty();
182 public static final Function<Sone, FreenetURI> TO_FREENET_URI = new Function<Sone, FreenetURI>() {
184 public FreenetURI apply(Sone sone) {
185 return (sone == null) ? null : create(sone.getIdentity().getRequestUri());
189 public static final Function<Sone, List<Post>> TO_POSTS = new Function<Sone, List<Post>>() {
191 public List<Post> apply(Sone sone) {
192 return (sone == null) ? Collections.<Post>emptyList() : sone.getPosts();
197 * Returns the identity of this Sone.
199 * @return The identity of this Sone
201 Identity getIdentity();
204 * Returns the name of this Sone.
206 * @return The name of this Sone
211 * Returns whether this Sone is a local Sone.
213 * @return {@code true} if this Sone is a local Sone, {@code false} otherwise
218 * Returns the latest edition of this Sone.
220 * @return The latest edition of this Sone
222 long getLatestEdition();
225 * Return the time of the last inserted update of this Sone.
227 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
232 * Sets the time of the last inserted update of this Sone.
235 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
236 * @return This Sone (for method chaining)
238 Sone setTime(long time);
241 * Returns the status of this Sone.
243 * @return The status of this Sone
245 SoneStatus getStatus();
248 * Sets the new status of this Sone.
251 * The new status of this Sone
253 * @throws IllegalArgumentException
254 * if {@code status} is {@code null}
256 Sone setStatus(SoneStatus status);
259 * Returns a copy of the profile. If you want to update values in the profile
260 * of this Sone, update the values in the returned {@link Profile} and use
261 * {@link #setProfile(Profile)} to change the profile in this Sone.
263 * @return A copy of the profile
265 Profile getProfile();
268 * Sets the profile of this Sone. A copy of the given profile is stored so that
269 * subsequent modifications of the given profile are not reflected in this
275 void setProfile(Profile profile);
278 * Returns the client used by this Sone.
280 * @return The client used by this Sone, or {@code null}
285 * Returns whether this Sone is known.
287 * @return {@code true} if this Sone is known, {@code false} otherwise
292 * Sets whether this Sone is known.
295 * {@code true} if this Sone is known, {@code false} otherwise
298 Sone setKnown(boolean known);
301 * Returns all friend Sones of this Sone.
303 * @return The friend Sones of this Sone
305 Collection<String> getFriends();
308 * Returns whether this Sone has the given Sone as a friend Sone.
310 * @param friendSoneId
311 * The ID of the Sone to check for
312 * @return {@code true} if this Sone has the given Sone as a friend, {@code
315 boolean hasFriend(String friendSoneId);
318 * Adds the given Sone as a friend Sone.
321 * The friend Sone to add
322 * @return This Sone (for method chaining)
324 Sone addFriend(String friendSone);
327 * Removes the given Sone as a friend Sone.
329 * @param friendSoneId
330 * The ID of the friend Sone to remove
331 * @return This Sone (for method chaining)
333 Sone removeFriend(String friendSoneId);
336 * Returns the list of posts of this Sone, sorted by time, newest first.
338 * @return All posts of this Sone
340 List<Post> getPosts();
343 * Sets all posts of this Sone at once.
346 * The new (and only) posts of this Sone
347 * @return This Sone (for method chaining)
349 Sone setPosts(Collection<Post> posts);
352 * Adds the given post to this Sone. The post will not be added if its {@link
353 * Post#getSone() Sone} is not this Sone.
358 void addPost(Post post);
361 * Removes the given post from this Sone.
366 void removePost(Post post);
369 * Returns all replies this Sone made.
371 * @return All replies this Sone made
373 Set<PostReply> getReplies();
376 * Sets all replies of this Sone at once.
379 * The new (and only) replies of this Sone
380 * @return This Sone (for method chaining)
382 Sone setReplies(Collection<PostReply> replies);
385 * Adds a reply to this Sone. If the given reply was not made by this Sone,
386 * nothing is added to this Sone.
391 void addReply(PostReply reply);
394 * Removes a reply from this Sone.
397 * The reply to remove
399 void removeReply(PostReply reply);
402 * Returns the IDs of all liked posts.
404 * @return All liked postsā IDs
406 Set<String> getLikedPostIds();
409 * Sets the IDs of all liked posts.
411 * @param likedPostIds
412 * All liked postsā IDs
413 * @return This Sone (for method chaining)
415 Sone setLikePostIds(Set<String> likedPostIds);
418 * Returns the IDs of all liked replies.
420 * @return All liked repliesā IDs
422 Set<String> getLikedReplyIds();
425 * Sets the IDs of all liked replies.
427 * @param likedReplyIds
428 * All liked repliesā IDs
429 * @return This Sone (for method chaining)
431 Sone setLikeReplyIds(Set<String> likedReplyIds);
434 * Returns the root album that contains all visible albums of this Sone.
436 * @return The root album of this Sone
438 Album getRootAlbum();
441 * Returns Sone-specific options.
443 * @return The options of this Sone
445 Options getOptions();
448 * Sets the options of this Sone.
451 * The options of this Sone
453 /* TODO - remove this method again, maybe add an option provider */
454 void setOptions(Options options);
456 AlbumBuilder newAlbumBuilder() throws IllegalStateException;
458 PostBuilder newPostBuilder();
460 PostReplyBuilder newPostReplyBuilder(String postId) throws IllegalStateException;
466 Modifier setLatestEdition(long latestEdition);