Move isLikedPostId from Sone to Post.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 5 Nov 2013 20:48:25 +0000 (21:48 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:25:53 +0000 (22:25 +0100)
src/main/java/net/pterodactylus/sone/data/Post.java
src/main/java/net/pterodactylus/sone/data/Sone.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
src/main/java/net/pterodactylus/sone/template/PostAccessor.java

index 474f9d1..36db3a3 100644 (file)
@@ -128,6 +128,7 @@ public interface Post extends Identified {
        public void like(Sone localSone);
        public void unlike(Sone localSone);
 
+       boolean isLiked(Sone sone);
        Set<Sone> getLikes();
 
        List<PostReply> getReplies();
index e340bdf..44929c6 100644 (file)
@@ -408,16 +408,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);
-
-       /**
         * Returns the IDs of all liked replies.
         *
         * @return All liked replies’ IDs
index 587add9..a1204f9 100644 (file)
@@ -139,6 +139,11 @@ public class DefaultPost implements Post {
        }
 
        @Override
+       public boolean isLiked(Sone sone) {
+               return database.isLiked(this, sone);
+       }
+
+       @Override
        public Set<Sone> getLikes() {
                return database.getLikes(this);
        }
index a25f245..d8956db 100644 (file)
@@ -102,6 +102,7 @@ public interface PostDatabase {
 
        void likePost(Post post, Sone localSone);
        void unlikePost(Post post, Sone localSone);
+       boolean isLiked(Post post, Sone sone);
        Set<Sone> getLikes(Post post);
 
 }
index 975b8ab..bdc247e 100644 (file)
@@ -309,6 +309,15 @@ public class MemoryDatabase extends AbstractService implements Database {
                }
        }
 
+       public boolean isLiked(Post post, Sone sone) {
+               lock.readLock().lock();
+               try {
+                       return likedPostsBySone.containsEntry(sone, post);
+               } finally {
+                       lock.readLock().unlock();
+               }
+       }
+
        @Override
        public Set<Sone> getLikes(Post post) {
                lock.readLock().lock();
index c3f9de3..26a05db 100644 (file)
@@ -59,7 +59,7 @@ public class PostAccessor extends ReflectionAccessor {
                        return post.getLikes();
                } else if (member.equals("liked")) {
                        Sone currentSone = (Sone) templateContext.get("currentSone");
-                       return (currentSone != null) && (currentSone.isLikedPostId(post.getId()));
+                       return (currentSone != null) && post.isLiked(currentSone);
                } else if (member.equals("new")) {
                        return !post.isKnown();
                } else if (member.equals("bookmarked")) {