Small performance improvements.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index 8cf8834..00e7deb 100644 (file)
@@ -494,20 +494,38 @@ public class Core implements IdentityListener, UpdateListener {
        }
 
        /**
-        * Returns whether the given Sone is a new Sone. After this check, the Sone
-        * is marked as known, i.e. a second call with the same parameters will
-        * always yield {@code false}.
+        * Returns whether the Sone with the given ID is a new Sone. After this
+        * check, the Sone is marked as known, i.e. a second call with the same
+        * parameters will always yield {@code false}.
         *
-        * @param sone
-        *            The sone to check for
+        * @param soneId
+        *            The ID of the sone to check for
         * @return {@code true} if the given Sone is new, false otherwise
         */
-       public boolean isNewSone(Sone sone) {
+       public boolean isNewSone(String soneId) {
+               return isNewSone(soneId, true);
+       }
+
+       /**
+        * Returns whether the Sone with the given ID is a new Sone. The Sone will
+        * be marked as known if {@code markAsKnown} is {@code true}, otherwise the
+        * Sone will keep its current “new” state.
+        *
+        * @param soneId
+        *            The ID of the sone to check for
+        * @param markAsKnown
+        *            {@code true} to mark the Sone as known in any case,
+        *            {@code false} to not mark it as known
+        * @return {@code true} if the given Sone is new, false otherwise
+        */
+       public boolean isNewSone(String soneId, boolean markAsKnown) {
                synchronized (newSones) {
-                       boolean isNew = !knownSones.contains(sone.getId()) && newSones.remove(sone.getId());
-                       knownSones.add(sone.getId());
-                       if (isNew) {
-                               coreListenerManager.fireMarkSoneKnown(sone);
+                       boolean isNew = !knownSones.contains(soneId) && newSones.contains(soneId);
+                       if (markAsKnown) {
+                               Sone sone = getSone(soneId, false);
+                               if (sone != null) {
+                                       markSoneKnown(sone);
+                               }
                        }
                        return isNew;
                }
@@ -1029,10 +1047,11 @@ public class Core implements IdentityListener, UpdateListener {
                                                }
                                        }
                                }
+                               List<Post> storedPosts = storedSone.getPosts();
                                synchronized (newPosts) {
                                        for (Post post : sone.getPosts()) {
-                                               post.setSone(getSone(post.getSone().getId()));
-                                               if (!storedSone.getPosts().contains(post) && !knownPosts.contains(post.getId())) {
+                                               post.setSone(storedSone);
+                                               if (!storedPosts.contains(post) && !knownPosts.contains(post.getId())) {
                                                        newPosts.add(post.getId());
                                                        coreListenerManager.fireNewPostFound(post);
                                                }
@@ -1049,10 +1068,11 @@ public class Core implements IdentityListener, UpdateListener {
                                                }
                                        }
                                }
+                               Set<Reply> storedReplies = sone.getReplies();
                                synchronized (newReplies) {
                                        for (Reply reply : sone.getReplies()) {
-                                               reply.setSone(getSone(reply.getSone().getId()));
-                                               if (!storedSone.getReplies().contains(reply) && !knownReplies.contains(reply.getId())) {
+                                               reply.setSone(storedSone);
+                                               if (!storedReplies.contains(reply) && !knownReplies.contains(reply.getId())) {
                                                        newReplies.add(reply.getId());
                                                        coreListenerManager.fireNewReplyFound(reply);
                                                }
@@ -1134,7 +1154,7 @@ public class Core implements IdentityListener, UpdateListener {
        public void markSoneKnown(Sone sone) {
                synchronized (newSones) {
                        if (newSones.remove(sone.getId())) {
-                               knownPosts.add(sone.getId());
+                               knownSones.add(sone.getId());
                                coreListenerManager.fireMarkSoneKnown(sone);
                                saveConfiguration();
                        }