Store a reply’s known status in the reply itself.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 27 Jan 2012 09:10:12 +0000 (10:10 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 27 Jan 2012 09:28:37 +0000 (10:28 +0100)
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/data/Reply.java
src/main/java/net/pterodactylus/sone/template/ReplyAccessor.java
src/main/java/net/pterodactylus/sone/web/WebInterface.java

index a4fca35..f979284 100644 (file)
@@ -149,9 +149,6 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
        /** All replies. */
        private Map<String, PostReply> replies = new HashMap<String, PostReply>();
 
-       /** All new replies. */
-       private Set<String> newReplies = new HashSet<String>();
-
        /** All known replies. */
        private Set<String> knownReplies = new HashSet<String>();
 
@@ -649,20 +646,6 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
        }
 
        /**
-        * Returns whether the reply with the given ID is new.
-        *
-        * @param replyId
-        *            The ID of the reply to check
-        * @return {@code true} if the reply is considered to be new, {@code false}
-        *         otherwise
-        */
-       public boolean isNewReply(String replyId) {
-               synchronized (newReplies) {
-                       return !knownReplies.contains(replyId) && newReplies.contains(replyId);
-               }
-       }
-
-       /**
         * Returns all Sones that have liked the given post.
         *
         * @param post
@@ -1209,15 +1192,17 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                                        }
                                }
                                Set<PostReply> storedReplies = storedSone.getReplies();
-                               synchronized (newReplies) {
+                               synchronized (knownReplies) {
                                        for (PostReply reply : sone.getReplies()) {
                                                reply.setSone(storedSone);
                                                if (!storedReplies.contains(reply)) {
                                                        if (reply.getTime() < getSoneFollowingTime(sone)) {
+                                                               reply.setKnown(true);
                                                                knownReplies.add(reply.getId());
                                                        } else if (!knownReplies.contains(reply.getId())) {
-                                                               newReplies.add(reply.getId());
                                                                coreListenerManager.fireNewReplyFound(reply);
+                                                       } else {
+                                                               reply.setKnown(true);
                                                        }
                                                }
                                                replies.put(reply.getId(), reply);
@@ -1545,7 +1530,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                                knownPosts.add(post.getId());
                        }
                }
-               synchronized (newReplies) {
+               synchronized (knownReplies) {
                        for (PostReply reply : replies) {
                                knownReplies.add(reply.getId());
                        }
@@ -1756,8 +1741,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                synchronized (replies) {
                        replies.put(reply.getId(), reply);
                }
-               synchronized (newReplies) {
-                       newReplies.add(reply.getId());
+               synchronized (knownReplies) {
                        coreListenerManager.fireNewReplyFound(reply);
                }
                sone.addReply(reply);
@@ -1790,7 +1774,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                synchronized (replies) {
                        replies.remove(reply.getId());
                }
-               synchronized (newReplies) {
+               synchronized (knownReplies) {
                        markReplyKnown(reply);
                        knownReplies.remove(reply.getId());
                }
@@ -1806,8 +1790,8 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
         *            The reply to mark as known
         */
        public void markReplyKnown(PostReply reply) {
-               synchronized (newReplies) {
-                       newReplies.remove(reply.getId());
+               reply.setKnown(true);
+               synchronized (knownReplies) {
                        if (knownReplies.add(reply.getId())) {
                                coreListenerManager.fireMarkReplyKnown(reply);
                                touchConfiguration();
@@ -2221,7 +2205,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
 
                        /* save known replies. */
                        int replyCounter = 0;
-                       synchronized (newReplies) {
+                       synchronized (knownReplies) {
                                for (String knownReplyId : knownReplies) {
                                        configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").setValue(knownReplyId);
                                }
@@ -2364,7 +2348,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                        if (knownReplyId == null) {
                                break;
                        }
-                       synchronized (newReplies) {
+                       synchronized (knownReplies) {
                                knownReplies.add(knownReplyId);
                        }
                }
@@ -2502,10 +2486,9 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                        }
                }
                synchronized (replies) {
-                       synchronized (newReplies) {
+                       synchronized (knownReplies) {
                                for (PostReply reply : sone.getReplies()) {
                                        replies.remove(reply.getId());
-                                       newReplies.remove(reply.getId());
                                        coreListenerManager.fireReplyRemoved(reply);
                                }
                        }
index 780fe69..15d8287 100644 (file)
@@ -69,6 +69,9 @@ public abstract class Reply<T extends Reply<T>> {
        /** The text of the reply. */
        private volatile String text;
 
+       /** Whether the reply is known. */
+       private volatile boolean known;
+
        /**
         * Creates a new reply with the given ID.
         *
@@ -187,6 +190,28 @@ public abstract class Reply<T extends Reply<T>> {
                return (T) this;
        }
 
+       /**
+        * Returns whether this reply is known.
+        *
+        * @return {@code true} if this reply is known, {@code false} otherwise
+        */
+       public boolean isKnown() {
+               return known;
+       }
+
+       /**
+        * Sets whether this reply is known.
+        *
+        * @param known
+        *            {@code true} if this reply is known, {@code false} otherwise
+        * @return This reply
+        */
+       @SuppressWarnings("unchecked")
+       public T setKnown(boolean known) {
+               this.known = known;
+               return (T) this;
+       }
+
        //
        // OBJECT METHODS
        //
index 24bcfd6..7b89713 100644 (file)
@@ -58,7 +58,7 @@ public class ReplyAccessor extends ReflectionAccessor {
                        Sone currentSone = (Sone) templateContext.get("currentSone");
                        return (currentSone != null) && (currentSone.isLikedReplyId(reply.getId()));
                } else if (member.equals("new")) {
-                       return core.isNewReply(reply.getId());
+                       return !reply.isKnown();
                } else if (member.equals("loaded")) {
                        return reply.getSone() != null;
                }
index a007222..ba12765 100644 (file)
@@ -888,7 +888,7 @@ public class WebInterface implements CoreListener {
                if (!getMentionedSones(reply.getText()).isEmpty()) {
                        boolean isMentioned = false;
                        for (PostReply existingReply : getCore().getReplies(reply.getPost())) {
-                               isMentioned |= getCore().isNewReply(reply.getId()) && !getMentionedSones(existingReply.getText()).isEmpty();
+                               isMentioned |= !reply.isKnown() && !getMentionedSones(existingReply.getText()).isEmpty();
                        }
                        if (!isMentioned) {
                                mentionNotification.remove(reply.getPost());