🔥 Remove unnecessary imports
[Sone.git] / src / main / java / net / pterodactylus / sone / data / impl / SoneImpl.java
index 439f4c9..f2f2ea6 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Sone - SoneImpl.java - Copyright Â© 2010–2016 David Roden
+ * Sone - SoneImpl.java - Copyright Â© 2010–2020 David Roden
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -19,7 +19,11 @@ package net.pterodactylus.sone.data.impl;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 import static java.lang.String.format;
+import static java.nio.charset.StandardCharsets.UTF_8;
 import static java.util.logging.Logger.getLogger;
+import static net.pterodactylus.sone.data.PostKt.newestPostFirst;
+import static net.pterodactylus.sone.data.ReplyKt.newestReplyFirst;
+import static net.pterodactylus.sone.data.SoneKt.*;
 
 import java.net.MalformedURLException;
 import java.util.ArrayList;
@@ -35,17 +39,16 @@ import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 
 import net.pterodactylus.sone.data.Album;
+import net.pterodactylus.sone.data.AlbumKt;
 import net.pterodactylus.sone.data.Client;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.PostReply;
 import net.pterodactylus.sone.data.Profile;
-import net.pterodactylus.sone.data.Reply;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.data.SoneOptions;
 import net.pterodactylus.sone.data.SoneOptions.DefaultSoneOptions;
 import net.pterodactylus.sone.database.Database;
 import net.pterodactylus.sone.freenet.wot.Identity;
-import net.pterodactylus.sone.freenet.wot.OwnIdentity;
 
 import freenet.keys.FreenetURI;
 
@@ -56,8 +59,6 @@ import com.google.common.hash.Hashing;
  * {@link Sone} implementation.
  * <p/>
  * Operations that modify the Sone need to synchronize on the Sone in question.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David â€˜Bombe’ Roden</a>
  */
 public class SoneImpl implements Sone {
 
@@ -95,16 +96,16 @@ public class SoneImpl implements Sone {
        private volatile boolean known;
 
        /** All posts. */
-       private final Set<Post> posts = new CopyOnWriteArraySet<Post>();
+       private final Set<Post> posts = new CopyOnWriteArraySet<>();
 
        /** All replies. */
-       private final Set<PostReply> replies = new CopyOnWriteArraySet<PostReply>();
+       private final Set<PostReply> replies = new CopyOnWriteArraySet<>();
 
        /** The IDs of all liked posts. */
-       private final Set<String> likedPostIds = new CopyOnWriteArraySet<String>();
+       private final Set<String> likedPostIds = new CopyOnWriteArraySet<>();
 
        /** The IDs of all liked replies. */
-       private final Set<String> likedReplyIds = new CopyOnWriteArraySet<String>();
+       private final Set<String> likedReplyIds = new CopyOnWriteArraySet<>();
 
        /** The root album containing all albums. */
        private final Album rootAlbum = new AlbumImpl(this);
@@ -192,26 +193,6 @@ public class SoneImpl implements Sone {
        }
 
        /**
-        * Returns the insert URI of this Sone.
-        *
-        * @return The insert URI of this Sone
-        */
-       @Nullable
-       public FreenetURI getInsertUri() {
-               if (!isLocal()) {
-                       return null;
-               }
-               try {
-                       return new FreenetURI(((OwnIdentity) getIdentity()).getInsertUri())
-                                       .setDocName("Sone")
-                                       .setMetaString(new String[0])
-                                       .setSuggestedEdition(latestEdition);
-               } catch (MalformedURLException e) {
-                       throw new IllegalStateException(format("Own identity %s's insert URI is incorrect.", getIdentity()), e);
-               }
-       }
-
-       /**
         * Returns the latest edition of this Sone.
         *
         * @return The latest edition of this Sone
@@ -383,9 +364,9 @@ public class SoneImpl implements Sone {
        public List<Post> getPosts() {
                List<Post> sortedPosts;
                synchronized (this) {
-                       sortedPosts = new ArrayList<Post>(posts);
+                       sortedPosts = new ArrayList<>(posts);
                }
-               Collections.sort(sortedPosts, Post.NEWEST_FIRST);
+               sortedPosts.sort(newestPostFirst());
                return sortedPosts;
        }
 
@@ -533,12 +514,9 @@ public class SoneImpl implements Sone {
         *
         * @param postId
         *              The ID of the post
-        * @return This Sone (for method chaining)
         */
-       @Nonnull
-       public Sone removeLikedPostId(@Nonnull String postId) {
+       public void removeLikedPostId(@Nonnull String postId) {
                likedPostIds.remove(postId);
-               return this;
        }
 
        /**
@@ -595,12 +573,9 @@ public class SoneImpl implements Sone {
         *
         * @param replyId
         *              The ID of the reply
-        * @return This Sone (for method chaining)
         */
-       @Nonnull
-       public Sone removeLikedReplyId(@Nonnull String replyId) {
+       public void removeLikedReplyId(@Nonnull String replyId) {
                likedReplyIds.remove(replyId);
-               return this;
        }
 
        /**
@@ -642,46 +617,46 @@ public class SoneImpl implements Sone {
        @Override
        public synchronized String getFingerprint() {
                Hasher hash = Hashing.sha256().newHasher();
-               hash.putString(profile.getFingerprint());
+               hash.putString(profile.getFingerprint(), UTF_8);
 
-               hash.putString("Posts(");
+               hash.putString("Posts(", UTF_8);
                for (Post post : getPosts()) {
-                       hash.putString("Post(").putString(post.getId()).putString(")");
+                       hash.putString("Post(", UTF_8).putString(post.getId(), UTF_8).putString(")", UTF_8);
                }
-               hash.putString(")");
+               hash.putString(")", UTF_8);
 
-               List<PostReply> replies = new ArrayList<PostReply>(getReplies());
-               Collections.sort(replies, Reply.TIME_COMPARATOR);
-               hash.putString("Replies(");
+               List<PostReply> replies = new ArrayList<>(getReplies());
+               replies.sort(newestReplyFirst().reversed());
+               hash.putString("Replies(", UTF_8);
                for (PostReply reply : replies) {
-                       hash.putString("Reply(").putString(reply.getId()).putString(")");
+                       hash.putString("Reply(", UTF_8).putString(reply.getId(), UTF_8).putString(")", UTF_8);
                }
-               hash.putString(")");
+               hash.putString(")", UTF_8);
 
-               List<String> likedPostIds = new ArrayList<String>(getLikedPostIds());
+               List<String> likedPostIds = new ArrayList<>(getLikedPostIds());
                Collections.sort(likedPostIds);
-               hash.putString("LikedPosts(");
+               hash.putString("LikedPosts(", UTF_8);
                for (String likedPostId : likedPostIds) {
-                       hash.putString("Post(").putString(likedPostId).putString(")");
+                       hash.putString("Post(", UTF_8).putString(likedPostId, UTF_8).putString(")", UTF_8);
                }
-               hash.putString(")");
+               hash.putString(")", UTF_8);
 
-               List<String> likedReplyIds = new ArrayList<String>(getLikedReplyIds());
+               List<String> likedReplyIds = new ArrayList<>(getLikedReplyIds());
                Collections.sort(likedReplyIds);
-               hash.putString("LikedReplies(");
+               hash.putString("LikedReplies(", UTF_8);
                for (String likedReplyId : likedReplyIds) {
-                       hash.putString("Reply(").putString(likedReplyId).putString(")");
+                       hash.putString("Reply(", UTF_8).putString(likedReplyId, UTF_8).putString(")", UTF_8);
                }
-               hash.putString(")");
+               hash.putString(")", UTF_8);
 
-               hash.putString("Albums(");
+               hash.putString("Albums(", UTF_8);
                for (Album album : rootAlbum.getAlbums()) {
-                       if (!Album.NOT_EMPTY.apply(album)) {
+                       if (!AlbumKt.notEmpty().invoke(album)) {
                                continue;
                        }
-                       hash.putString(album.getFingerprint());
+                       hash.putString(album.getFingerprint(), UTF_8);
                }
-               hash.putString(")");
+               hash.putString(")", UTF_8);
 
                return hash.hash().toString();
        }
@@ -693,7 +668,7 @@ public class SoneImpl implements Sone {
        /** {@inheritDoc} */
        @Override
        public int compareTo(Sone sone) {
-               return NICE_NAME_COMPARATOR.compare(this, sone);
+               return niceNameComparator().compare(this, sone);
        }
 
        //