Remove post reply management from Sone.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 7 Dec 2014 10:37:44 +0000 (11:37 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 7 Dec 2014 10:37:44 +0000 (11:37 +0100)
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/core/SoneParser.java
src/main/java/net/pterodactylus/sone/data/Sone.java
src/main/java/net/pterodactylus/sone/data/impl/AbstractSoneBuilder.java
src/main/java/net/pterodactylus/sone/data/impl/IdOnlySone.java
src/main/java/net/pterodactylus/sone/data/impl/SoneImpl.java
src/main/java/net/pterodactylus/sone/database/SoneBuilder.java
src/main/java/net/pterodactylus/sone/database/memory/MemorySoneBuilder.java

index 2e1b5fb..f39f8eb 100644 (file)
@@ -1024,7 +1024,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                final PostReply reply = postReplyBuilder.build();
                database.storePostReply(reply);
                eventBus.post(new NewPostReplyFoundEvent(reply));
-               sone.addReply(reply);
                touchConfiguration();
                localElementTicker.schedule(new MarkReplyKnown(reply), 10, TimeUnit.SECONDS);
                return reply;
@@ -1044,7 +1043,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                }
                database.removePostReply(reply);
                markReplyKnown(reply);
-               sone.removeReply(reply);
                touchConfiguration();
        }
 
index d2914da..6056a7f 100644 (file)
@@ -191,15 +191,14 @@ public class SoneParser {
                        }
                        soneBuilder.withPosts(posts);
                }
-               Sone sone = soneBuilder.build();
 
                /* parse replies. */
                SimpleXML repliesXml = soneXml.getNode("replies");
-               Set<PostReply> replies = new HashSet<PostReply>();
                if (repliesXml == null) {
                        /* TODO - mark Sone as bad. */
-                       logger.log(Level.WARNING, String.format("Downloaded Sone %s has no replies!", sone));
+                       logger.log(Level.WARNING, String.format("Downloaded Sone %s has no replies!", originalSone));
                } else {
+                       Set<PostReply> replies = new HashSet<PostReply>();
                        for (SimpleXML replyXml : repliesXml.getNodes("reply")) {
                                String replyId = replyXml.getValue("id", null);
                                String replyPostId = replyXml.getValue("post-id", null);
@@ -207,21 +206,23 @@ public class SoneParser {
                                String replyText = replyXml.getValue("text", null);
                                if ((replyId == null) || (replyPostId == null) || (replyTime == null) || (replyText == null)) {
                                        /* TODO - mark Sone as bad. */
-                                       logger.log(Level.WARNING, String.format("Downloaded reply for Sone %s with missing data! ID: %s, Post: %s, Time: %s, Text: %s", sone, replyId, replyPostId, replyTime, replyText));
+                                       logger.log(Level.WARNING, String.format("Downloaded reply for Sone %s with missing data! ID: %s, Post: %s, Time: %s, Text: %s", originalSone, replyId, replyPostId, replyTime, replyText));
                                        return null;
                                }
                                try {
                                        PostReplyBuilder postReplyBuilder = core.postReplyBuilder();
                                        /* TODO - parse time correctly. */
-                                       postReplyBuilder.withId(replyId).from(sone.getId()).to(replyPostId).withTime(Long.parseLong(replyTime)).withText(replyText);
+                                       postReplyBuilder.withId(replyId).from(originalSone.getId()).to(replyPostId).withTime(Long.parseLong(replyTime)).withText(replyText);
                                        replies.add(postReplyBuilder.build());
                                } catch (NumberFormatException nfe1) {
                                        /* TODO - mark Sone as bad. */
-                                       logger.log(Level.WARNING, String.format("Downloaded reply for Sone %s with invalid time: %s", sone, replyTime));
+                                       logger.log(Level.WARNING, String.format("Downloaded reply for Sone %s with invalid time: %s", originalSone, replyTime));
                                        return null;
                                }
                        }
+                       soneBuilder.withPostReplies(replies);
                }
+               Sone sone = soneBuilder.build();
 
                /* parse liked post IDs. */
                SimpleXML likePostIdsXml = soneXml.getNode("post-likes");
@@ -326,7 +327,6 @@ public class SoneParser {
                /* atomic setter operation on the Sone. */
                synchronized (sone) {
                        sone.setProfile(profile);
-                       sone.setReplies(replies);
                        sone.setLikePostIds(likedPostIds);
                        sone.setLikeReplyIds(likedReplyIds);
                        for (Album album : topLevelAlbums) {
index 2cf8bf6..bc90b12 100644 (file)
@@ -350,32 +350,6 @@ public interface Sone extends Identified, Fingerprintable, Comparable<Sone> {
        Set<PostReply> getReplies();
 
        /**
-        * Sets all replies of this Sone at once.
-        *
-        * @param replies
-        *              The new (and only) replies of this Sone
-        * @return This Sone (for method chaining)
-        */
-       Sone setReplies(Collection<PostReply> replies);
-
-       /**
-        * Adds a reply to this Sone. If the given reply was not made by this Sone,
-        * nothing is added to this Sone.
-        *
-        * @param reply
-        *              The reply to add
-        */
-       void addReply(PostReply reply);
-
-       /**
-        * Removes a reply from this Sone.
-        *
-        * @param reply
-        *              The reply to remove
-        */
-       void removeReply(PostReply reply);
-
-       /**
         * Returns the IDs of all liked posts.
         *
         * @return All liked posts’ IDs
index bc19d4d..a2f1333 100644 (file)
@@ -7,6 +7,7 @@ import java.util.HashSet;
 
 import net.pterodactylus.sone.data.Client;
 import net.pterodactylus.sone.data.Post;
+import net.pterodactylus.sone.data.PostReply;
 import net.pterodactylus.sone.database.SoneBuilder;
 import net.pterodactylus.sone.freenet.wot.Identity;
 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
@@ -23,6 +24,7 @@ public abstract class AbstractSoneBuilder implements SoneBuilder {
        protected long lastUpdated;
        protected Client client;
        protected final Collection<Post> posts = new HashSet<Post>();
+       protected final Collection<PostReply> postReplies = new HashSet<PostReply>();
 
        @Override
        public SoneBuilder from(Identity identity) {
@@ -55,6 +57,13 @@ public abstract class AbstractSoneBuilder implements SoneBuilder {
                return this;
        }
 
+       @Override
+       public SoneBuilder withPostReplies(Collection<PostReply> postReplies) {
+               this.postReplies.clear();
+               this.postReplies.addAll(postReplies);
+               return this;
+       }
+
        protected void validate() throws IllegalStateException {
                checkState(identity != null, "identity must not be null");
                checkState(!local || (identity instanceof OwnIdentity),
index 018ba32..0401a30 100644 (file)
@@ -126,19 +126,6 @@ public class IdOnlySone implements Sone {
        }
 
        @Override
-       public Sone setReplies(Collection<PostReply> replies) {
-               return this;
-       }
-
-       @Override
-       public void addReply(PostReply reply) {
-       }
-
-       @Override
-       public void removeReply(PostReply reply) {
-       }
-
-       @Override
        public Set<String> getLikedPostIds() {
                return emptySet();
        }
index 142f4a2..eb8bbe5 100644 (file)
@@ -98,7 +98,7 @@ public class SoneImpl implements LocalSone {
        private final Collection<Post> posts = new HashSet<Post>();
 
        /** All replies. */
-       private final Set<PostReply> replies = new CopyOnWriteArraySet<PostReply>();
+       private final Set<PostReply> replies = new HashSet<PostReply>();
 
        /** The IDs of all liked posts. */
        private final Set<String> likedPostIds = new CopyOnWriteArraySet<String>();
@@ -121,7 +121,7 @@ public class SoneImpl implements LocalSone {
         * @param local
         *              {@code true} if the Sone is a local Sone, {@code false} otherwise
         */
-       public SoneImpl(Database database, Identity identity, boolean local, long time, Client client, Collection<Post> posts) {
+       public SoneImpl(Database database, Identity identity, boolean local, long time, Client client, Collection<Post> posts, Collection<PostReply> postReplies) {
                this.database = database;
                this.id = identity.getId();
                this.identity = identity;
@@ -129,6 +129,7 @@ public class SoneImpl implements LocalSone {
                this.time = time;
                this.client = client;
                this.posts.addAll(posts);
+               this.replies.addAll(postReplies);
        }
 
        //
index 0fdeacf..5fb5fed 100644 (file)
@@ -4,6 +4,7 @@ import java.util.Collection;
 
 import net.pterodactylus.sone.data.Client;
 import net.pterodactylus.sone.data.Post;
+import net.pterodactylus.sone.data.PostReply;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.freenet.wot.Identity;
 
@@ -21,6 +22,7 @@ public interface SoneBuilder {
        SoneBuilder using(Client client);
 
        SoneBuilder withPosts(Collection<Post> posts);
+       SoneBuilder withPostReplies(Collection<PostReply> postReplies);
 
        Sone build() throws IllegalStateException;
 
index 355d2ad..3cdb120 100644 (file)
@@ -21,7 +21,7 @@ public class MemorySoneBuilder extends AbstractSoneBuilder {
        @Override
        public Sone build() throws IllegalStateException {
                validate();
-               return new SoneImpl(database, identity, local, lastUpdated, client, posts);
+               return new SoneImpl(database, identity, local, lastUpdated, client, posts, postReplies);
        }
 
 }