Store the “known” status of a post in the database.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 5 Mar 2014 06:14:47 +0000 (07:14 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 5 Mar 2014 06:14:47 +0000 (07:14 +0100)
Also, don’t allow a post to be set unknown from the Post
interface because there’s no use for it.

src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/data/Post.java
src/main/java/net/pterodactylus/sone/data/impl/DefaultPost.java
src/main/java/net/pterodactylus/sone/database/PostDatabase.java
src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java

index 0107a22..cb791dc 100644 (file)
@@ -759,7 +759,7 @@ public class Core extends AbstractService implements SoneProvider {
                                        continue;
                                }
                                if (newPost.getTime() < getSoneFollowingTime(sone)) {
-                                       newPost.setKnown(true);
+                                       newPost.setKnown();
                                } else if (!newPost.isKnown()) {
                                        eventBus.post(new NewPostFoundEvent(newPost));
                                }
@@ -1071,7 +1071,7 @@ public class Core extends AbstractService implements SoneProvider {
                }
                database.storePosts(sone, posts);
                for (Post post : posts) {
-                       post.setKnown(true);
+                       post.setKnown();
                }
                database.storePostReplies(sone, replies);
                for (PostReply reply : replies) {
@@ -1106,7 +1106,7 @@ public class Core extends AbstractService implements SoneProvider {
         *              The post to mark as known
         */
        public void markPostKnown(Post post) {
-               post.setKnown(true);
+               post.setKnown();
                eventBus.post(new MarkPostKnownEvent(post));
                touchConfiguration();
                for (PostReply reply : post.getReplies()) {
index 1fe2fa7..bd3986f 100644 (file)
@@ -119,11 +119,9 @@ public interface Post extends Identified {
        /**
         * Sets whether this post is known.
         *
-        * @param known
-        *            {@code true} if this post is known, {@code false} otherwise
         * @return This post
         */
-       public Post setKnown(boolean known);
+       public Post setKnown();
 
        void like(Sone localSone);
        void unlike(Sone localSone);
index a1204f9..a560185 100644 (file)
@@ -55,9 +55,6 @@ public class DefaultPost implements Post {
        /** The text of the post. */
        private final String text;
 
-       /** Whether the post is known. */
-       private volatile boolean known;
-
        /**
         * Creates a new post.
         *
@@ -119,12 +116,12 @@ public class DefaultPost implements Post {
 
        @Override
        public boolean isKnown() {
-               return known;
+               return database.isPostKnown(this);
        }
 
        @Override
-       public DefaultPost setKnown(boolean known) {
-               this.known = known;
+       public DefaultPost setKnown() {
+               database.setPostKnown(this);
                return this;
        }
 
index d8956db..8fa4000 100644 (file)
@@ -100,6 +100,8 @@ public interface PostDatabase {
         */
        void removePosts(Sone sone);
 
+       void setPostKnown(Post post);
+       boolean isPostKnown(Post post);
        void likePost(Post post, Sone localSone);
        void unlikePost(Post post, Sone localSone);
        boolean isLiked(Post post, Sone sone);
index edadf61..4bf6643 100644 (file)
@@ -309,6 +309,39 @@ public class MemoryDatabase extends AbstractService implements Database {
                }
        }
 
+       /**
+        * Returns whether the given post is known.
+        *
+        * @param post
+        *              The post
+        * @return {@code true} if the post is known, {@code false} otherwise
+        */
+       @Override
+       public boolean isPostKnown(Post post) {
+               lock.readLock().lock();
+               try {
+                       return knownPosts.contains(post.getId());
+               } finally {
+                       lock.readLock().unlock();
+               }
+       }
+
+       /**
+        * Sets whether the given post is known.
+        *
+        * @param post
+        *              The post
+        */
+       @Override
+       public void setPostKnown(Post post) {
+               lock.writeLock().lock();
+               try {
+                       knownPosts.add(post.getId());
+               } finally {
+                       lock.writeLock().unlock();
+               }
+       }
+
        @Override
        public void likePost(Post post, Sone localSone) {
                lock.writeLock().lock();
@@ -766,47 +799,6 @@ public class MemoryDatabase extends AbstractService implements Database {
        }
 
        //
-       // PACKAGE-PRIVATE METHODS
-       //
-
-       /**
-        * Returns whether the given post is known.
-        *
-        * @param post
-        *              The post
-        * @return {@code true} if the post is known, {@code false} otherwise
-        */
-       boolean isPostKnown(Post post) {
-               lock.readLock().lock();
-               try {
-                       return knownPosts.contains(post.getId());
-               } finally {
-                       lock.readLock().unlock();
-               }
-       }
-
-       /**
-        * Sets whether the given post is known.
-        *
-        * @param post
-        *              The post
-        * @param known
-        *              {@code true} if the post is known, {@code false} otherwise
-        */
-       void setPostKnown(Post post, boolean known) {
-               lock.writeLock().lock();
-               try {
-                       if (known) {
-                               knownPosts.add(post.getId());
-                       } else {
-                               knownPosts.remove(post.getId());
-                       }
-               } finally {
-                       lock.writeLock().unlock();
-               }
-       }
-
-       //
        // PRIVATE METHODS
        //