Add function that converts a Sone into its insert URI.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / Sone.java
index acf9031..bca7895 100644 (file)
@@ -22,6 +22,7 @@ import static java.util.Arrays.asList;
 import static net.pterodactylus.sone.core.SoneUri.create;
 import static net.pterodactylus.sone.data.Album.FLATTENER;
 import static net.pterodactylus.sone.data.Album.IMAGES;
+import static net.pterodactylus.sone.template.SoneAccessor.getNiceName;
 
 import java.util.Collection;
 import java.util.Collections;
@@ -29,17 +30,21 @@ import java.util.Comparator;
 import java.util.List;
 import java.util.Set;
 
+import javax.annotation.Nullable;
+
 import net.pterodactylus.sone.core.Options;
 import net.pterodactylus.sone.database.AlbumBuilder;
 import net.pterodactylus.sone.database.PostBuilder;
 import net.pterodactylus.sone.database.PostReplyBuilder;
 import net.pterodactylus.sone.freenet.wot.Identity;
-import net.pterodactylus.sone.template.SoneAccessor;
+import net.pterodactylus.sone.freenet.wot.OwnIdentity;
+
+import freenet.keys.FreenetURI;
 
 import com.google.common.base.Function;
 import com.google.common.base.Predicate;
+import com.google.common.collect.ComparisonChain;
 import com.google.common.primitives.Ints;
-import freenet.keys.FreenetURI;
 
 /**
  * A Sone defines everything about a user: her profile, her status updates, her
@@ -98,15 +103,21 @@ public interface Sone extends Identified, Fingerprintable, Comparable<Sone> {
 
                @Override
                public int compare(Sone leftSone, Sone rightSone) {
-                       int diff = SoneAccessor.getNiceName(leftSone).compareToIgnoreCase(SoneAccessor.getNiceName(rightSone));
-                       if (diff != 0) {
-                               return diff;
-                       }
-                       return leftSone.getId().compareToIgnoreCase(rightSone.getId());
+                       return ComparisonChain.start()
+                                       .compare(getNiceName(leftSone).toLowerCase(), getNiceName(rightSone).toLowerCase())
+                                       .compare(leftSone.getId(), rightSone.getId())
+                                       .result();
                }
 
        };
 
+       public static final Function<Sone, String> TO_NICE_NAME = new Function<Sone, String>() {
+               @Override
+               public String apply(Sone sone) {
+                       return (sone == null) ? null : getNiceName(sone);
+               }
+       };
+
        /** Comparator that sorts Sones by last activity (least recent active first). */
        public static final Comparator<Sone> LAST_ACTIVITY_COMPARATOR = new Comparator<Sone>() {
 
@@ -178,6 +189,13 @@ public interface Sone extends Identified, Fingerprintable, Comparable<Sone> {
                }
        };
 
+       public static final Function<Sone, FreenetURI> TO_INSERT_URI = new Function<Sone, FreenetURI>() {
+               @Override
+               public FreenetURI apply(@Nullable Sone sone) {
+                       return ((sone == null) || !sone.isLocal()) ? null : create(((OwnIdentity) sone.getIdentity()).getInsertUri());
+               }
+       };
+
        public static final Function<Sone, List<Post>> TO_POSTS = new Function<Sone, List<Post>>() {
                @Override
                public List<Post> apply(Sone sone) {
@@ -407,34 +425,6 @@ public interface Sone extends Identified, Fingerprintable, Comparable<Sone> {
        Sone setLikePostIds(Set<String> likedPostIds);
 
        /**
-        * Checks whether the given post ID is liked by this Sone.
-        *
-        * @param postId
-        *              The ID of the post
-        * @return {@code true} if this Sone likes the given post, {@code false}
-        *         otherwise
-        */
-       boolean isLikedPostId(String postId);
-
-       /**
-        * Adds the given post ID to the list of posts this Sone likes.
-        *
-        * @param postId
-        *              The ID of the post
-        * @return This Sone (for method chaining)
-        */
-       Sone addLikedPostId(String postId);
-
-       /**
-        * Removes the given post ID from the list of posts this Sone likes.
-        *
-        * @param postId
-        *              The ID of the post
-        * @return This Sone (for method chaining)
-        */
-       Sone removeLikedPostId(String postId);
-
-       /**
         * Returns the IDs of all liked replies.
         *
         * @return All liked replies’ IDs
@@ -451,34 +441,6 @@ public interface Sone extends Identified, Fingerprintable, Comparable<Sone> {
        Sone setLikeReplyIds(Set<String> likedReplyIds);
 
        /**
-        * Checks whether the given reply ID is liked by this Sone.
-        *
-        * @param replyId
-        *              The ID of the reply
-        * @return {@code true} if this Sone likes the given reply, {@code false}
-        *         otherwise
-        */
-       boolean isLikedReplyId(String replyId);
-
-       /**
-        * Adds the given reply ID to the list of replies this Sone likes.
-        *
-        * @param replyId
-        *              The ID of the reply
-        * @return This Sone (for method chaining)
-        */
-       Sone addLikedReplyId(String replyId);
-
-       /**
-        * Removes the given post ID from the list of replies this Sone likes.
-        *
-        * @param replyId
-        *              The ID of the reply
-        * @return This Sone (for method chaining)
-        */
-       Sone removeLikedReplyId(String replyId);
-
-       /**
         * Returns the root album that contains all visible albums of this Sone.
         *
         * @return The root album of this Sone