Add modifier to post replies to mark them known.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 16 Oct 2013 19:32:23 +0000 (21:32 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:25:28 +0000 (22:25 +0100)
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/data/Reply.java
src/main/java/net/pterodactylus/sone/data/impl/DefaultPostReply.java
src/main/java/net/pterodactylus/sone/data/impl/DefaultReply.java
src/main/java/net/pterodactylus/sone/database/PostReplyDatabase.java
src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java

index 518859c..03091e3 100644 (file)
@@ -839,7 +839,7 @@ public class Core extends AbstractService implements SoneProvider {
                                        continue;
                                }
                                if (reply.getTime() < getSoneFollowingTime(sone)) {
-                                       reply.setKnown(true);
+                                       reply.modify().setKnown().update();
                                } else if (!reply.isKnown()) {
                                        eventBus.post(new NewPostReplyFoundEvent(reply));
                                }
@@ -1134,7 +1134,7 @@ public class Core extends AbstractService implements SoneProvider {
                }
                database.storePostReplies(sone, replies);
                for (PostReply reply : replies) {
-                       reply.setKnown(true);
+                       reply.modify().setKnown().update();
                }
 
                logger.info(String.format("Sone loaded successfully: %s", sone));
@@ -1234,7 +1234,7 @@ public class Core extends AbstractService implements SoneProvider {
         */
        public void markReplyKnown(PostReply reply) {
                boolean previouslyKnown = reply.isKnown();
-               reply.setKnown(true);
+               reply.modify().setKnown().update();
                eventBus.post(new MarkPostReplyKnownEvent(reply));
                if (!previouslyKnown) {
                        touchConfiguration();
index c229d04..2c2a7bd 100644 (file)
@@ -91,13 +91,13 @@ public interface Reply<T extends Reply<T>> extends Identified {
         */
        public boolean isKnown();
 
-       /**
-        * Sets whether this reply is known.
-        *
-        * @param known
-        *            {@code true} if this reply is known, {@code false} otherwise
-        * @return This reply
-        */
-       public T setKnown(boolean known);
+       Modifier<T> modify();
+
+       interface Modifier<T> {
+
+               Modifier<T> setKnown();
+               T update();
+
+       }
 
 }
index ef275ff..705586b 100644 (file)
@@ -58,6 +58,11 @@ public class DefaultPostReply extends DefaultReply<PostReply> implements PostRep
        //
 
        @Override
+       public boolean isKnown() {
+               return database.isPostReplyKnown(this);
+       }
+
+       @Override
        public String getPostId() {
                return postId;
        }
@@ -67,4 +72,25 @@ public class DefaultPostReply extends DefaultReply<PostReply> implements PostRep
                return database.getPost(postId);
        }
 
+       @Override
+       public Modifier<PostReply> modify() {
+               return new Modifier<PostReply>() {
+                       private boolean known = isKnown();
+
+                       @Override
+                       public Modifier<PostReply> setKnown() {
+                               known = true;
+                               return this;
+                       }
+
+                       @Override
+                       public PostReply update() {
+                               if (known) {
+                                       database.setPostReplyKnown(DefaultPostReply.this);
+                               }
+                               return DefaultPostReply.this;
+                       }
+               };
+       }
+
 }
index 655d114..3d32dcb 100644 (file)
@@ -44,9 +44,6 @@ public abstract class DefaultReply<T extends Reply<T>> implements Reply<T> {
        /** The text of the reply. */
        private final String text;
 
-       /** Whether the reply is known. */
-       private volatile boolean known;
-
        /**
         * Creates a new reply.
         *
@@ -88,18 +85,6 @@ public abstract class DefaultReply<T extends Reply<T>> implements Reply<T> {
                return text;
        }
 
-       @Override
-       public boolean isKnown() {
-               return known;
-       }
-
-       @Override
-       @SuppressWarnings("unchecked")
-       public T setKnown(boolean known) {
-               this.known = known;
-               return (T) this;
-       }
-
        //
        // OBJECT METHODS
        //
index 2f320d2..1b8fbe5 100644 (file)
@@ -50,6 +50,9 @@ public interface PostReplyDatabase {
         */
        List<PostReply> getReplies(String postId);
 
+       boolean isPostReplyKnown(PostReply postReply);
+       void setPostReplyKnown(PostReply postReply);
+
        /**
         * Stores the given post reply.
         *
index 704509f..8cff1be 100644 (file)
@@ -346,6 +346,33 @@ public class MemoryDatabase extends AbstractService implements Database {
        // POSTREPLYSTORE METHODS
        //
 
+       /**
+        * Returns whether the given post reply is known.
+        *
+        * @param postReply
+        *              The post reply
+        * @return {@code true} if the given post reply is known, {@code false}
+        *         otherwise
+        */
+       public boolean isPostReplyKnown(PostReply postReply) {
+               lock.readLock().lock();
+               try {
+                       return knownPostReplies.contains(postReply.getId());
+               } finally {
+                       lock.readLock().unlock();
+               }
+       }
+
+       @Override
+       public void setPostReplyKnown(PostReply postReply) {
+               lock.writeLock().lock();
+               try {
+                       knownPostReplies.add(postReply.getId());
+               } finally {
+                       lock.writeLock().unlock();
+               }
+       }
+
        @Override
        public void storePostReply(PostReply postReply) {
                lock.writeLock().lock();
@@ -630,44 +657,6 @@ public class MemoryDatabase extends AbstractService implements Database {
                }
        }
 
-       /**
-        * Returns whether the given post reply is known.
-        *
-        * @param postReply
-        *              The post reply
-        * @return {@code true} if the given post reply is known, {@code false}
-        *         otherwise
-        */
-       boolean isPostReplyKnown(PostReply postReply) {
-               lock.readLock().lock();
-               try {
-                       return knownPostReplies.contains(postReply.getId());
-               } finally {
-                       lock.readLock().unlock();
-               }
-       }
-
-       /**
-        * Sets whether the given post reply is known.
-        *
-        * @param postReply
-        *              The post reply
-        * @param known
-        *              {@code true} if the post reply is known, {@code false} otherwise
-        */
-       void setPostReplyKnown(PostReply postReply, boolean known) {
-               lock.writeLock().lock();
-               try {
-                       if (known) {
-                               knownPostReplies.add(postReply.getId());
-                       } else {
-                               knownPostReplies.remove(postReply.getId());
-                       }
-               } finally {
-                       lock.writeLock().unlock();
-               }
-       }
-
        //
        // PRIVATE METHODS
        //