From 6abff3e875d253a011c73574f2a6012a28af279a Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 5 Nov 2013 21:48:25 +0100 Subject: [PATCH] Move isLikedPostId from Sone to Post. --- src/main/java/net/pterodactylus/sone/data/Post.java | 1 + src/main/java/net/pterodactylus/sone/data/Sone.java | 10 ---------- .../java/net/pterodactylus/sone/data/impl/DefaultPost.java | 5 +++++ .../java/net/pterodactylus/sone/database/PostDatabase.java | 1 + .../net/pterodactylus/sone/database/memory/MemoryDatabase.java | 9 +++++++++ .../java/net/pterodactylus/sone/template/PostAccessor.java | 2 +- 6 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/data/Post.java b/src/main/java/net/pterodactylus/sone/data/Post.java index 474f9d1..36db3a3 100644 --- a/src/main/java/net/pterodactylus/sone/data/Post.java +++ b/src/main/java/net/pterodactylus/sone/data/Post.java @@ -128,6 +128,7 @@ public interface Post extends Identified { public void like(Sone localSone); public void unlike(Sone localSone); + boolean isLiked(Sone sone); Set getLikes(); List getReplies(); diff --git a/src/main/java/net/pterodactylus/sone/data/Sone.java b/src/main/java/net/pterodactylus/sone/data/Sone.java index e340bdf..44929c6 100644 --- a/src/main/java/net/pterodactylus/sone/data/Sone.java +++ b/src/main/java/net/pterodactylus/sone/data/Sone.java @@ -408,16 +408,6 @@ public interface Sone extends Identified, Fingerprintable, Comparable { Sone setLikePostIds(Set 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 diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultPost.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultPost.java index 587add9..a1204f9 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultPost.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultPost.java @@ -139,6 +139,11 @@ public class DefaultPost implements Post { } @Override + public boolean isLiked(Sone sone) { + return database.isLiked(this, sone); + } + + @Override public Set getLikes() { return database.getLikes(this); } diff --git a/src/main/java/net/pterodactylus/sone/database/PostDatabase.java b/src/main/java/net/pterodactylus/sone/database/PostDatabase.java index a25f245..d8956db 100644 --- a/src/main/java/net/pterodactylus/sone/database/PostDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/PostDatabase.java @@ -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 getLikes(Post post); } diff --git a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java index 975b8ab..bdc247e 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java @@ -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 getLikes(Post post) { lock.readLock().lock(); diff --git a/src/main/java/net/pterodactylus/sone/template/PostAccessor.java b/src/main/java/net/pterodactylus/sone/template/PostAccessor.java index c3f9de3..26a05db 100644 --- a/src/main/java/net/pterodactylus/sone/template/PostAccessor.java +++ b/src/main/java/net/pterodactylus/sone/template/PostAccessor.java @@ -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")) { -- 2.7.4