continue;
}
if (reply.getTime() < getSoneFollowingTime(sone)) {
- reply.setKnown(true);
+ reply.modify().setKnown().update();
} else if (!reply.isKnown()) {
eventBus.post(new NewPostReplyFoundEvent(reply));
}
}
database.storePostReplies(sone, replies);
for (PostReply reply : replies) {
- reply.setKnown(true);
+ reply.modify().setKnown().update();
}
logger.info(String.format("Sone loaded successfully: %s", sone));
*/
public void markReplyKnown(PostReply reply) {
boolean previouslyKnown = reply.isKnown();
- reply.setKnown(true);
+ reply.modify().setKnown().update();
eventBus.post(new MarkPostReplyKnownEvent(reply));
if (!previouslyKnown) {
touchConfiguration();
*/
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();
+
+ }
}
//
@Override
+ public boolean isKnown() {
+ return database.isPostReplyKnown(this);
+ }
+
+ @Override
public String getPostId() {
return postId;
}
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;
+ }
+ };
+ }
+
}
/** The text of the reply. */
private final String text;
- /** Whether the reply is known. */
- private volatile boolean known;
-
/**
* Creates a new 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
//
*/
List<PostReply> getReplies(String postId);
+ boolean isPostReplyKnown(PostReply postReply);
+ void setPostReplyKnown(PostReply postReply);
+
/**
* Stores the given post reply.
*
// 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();
}
}
- /**
- * 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
//