From 888268b3742d0fca33bc50fc42f6956f3142e503 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 16 Oct 2013 21:32:23 +0200 Subject: [PATCH] Add modifier to post replies to mark them known. --- .../java/net/pterodactylus/sone/core/Core.java | 6 +- .../java/net/pterodactylus/sone/data/Reply.java | 16 +++--- .../sone/data/impl/DefaultPostReply.java | 26 +++++++++ .../pterodactylus/sone/data/impl/DefaultReply.java | 15 ----- .../sone/database/PostReplyDatabase.java | 3 + .../sone/database/memory/MemoryDatabase.java | 65 +++++++++------------- 6 files changed, 67 insertions(+), 64 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 518859c..03091e3 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -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(); diff --git a/src/main/java/net/pterodactylus/sone/data/Reply.java b/src/main/java/net/pterodactylus/sone/data/Reply.java index c229d04..2c2a7bd 100644 --- a/src/main/java/net/pterodactylus/sone/data/Reply.java +++ b/src/main/java/net/pterodactylus/sone/data/Reply.java @@ -91,13 +91,13 @@ public interface Reply> 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 modify(); + + interface Modifier { + + Modifier setKnown(); + T update(); + + } } diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostReply.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostReply.java index ef275ff..705586b 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostReply.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostReply.java @@ -58,6 +58,11 @@ public class DefaultPostReply extends DefaultReply 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 implements PostRep return database.getPost(postId); } + @Override + public Modifier modify() { + return new Modifier() { + private boolean known = isKnown(); + + @Override + public Modifier setKnown() { + known = true; + return this; + } + + @Override + public PostReply update() { + if (known) { + database.setPostReplyKnown(DefaultPostReply.this); + } + return DefaultPostReply.this; + } + }; + } + } diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultReply.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultReply.java index 655d114..3d32dcb 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultReply.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultReply.java @@ -44,9 +44,6 @@ public abstract class DefaultReply> implements Reply { /** 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> implements Reply { return text; } - @Override - public boolean isKnown() { - return known; - } - - @Override - @SuppressWarnings("unchecked") - public T setKnown(boolean known) { - this.known = known; - return (T) this; - } - // // OBJECT METHODS // diff --git a/src/main/java/net/pterodactylus/sone/database/PostReplyDatabase.java b/src/main/java/net/pterodactylus/sone/database/PostReplyDatabase.java index 2f320d2..1b8fbe5 100644 --- a/src/main/java/net/pterodactylus/sone/database/PostReplyDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/PostReplyDatabase.java @@ -50,6 +50,9 @@ public interface PostReplyDatabase { */ List getReplies(String postId); + boolean isPostReplyKnown(PostReply postReply); + void setPostReplyKnown(PostReply postReply); + /** * Stores the given post reply. * 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 704509f..8cff1be 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java @@ -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 // -- 2.7.4