Add method to check for the newness of a Sone.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index af1523a..055ede4 100644 (file)
@@ -106,6 +106,9 @@ public class Core implements IdentityListener {
        /* synchronize access on this on itself. */
        private Map<String, Sone> remoteSones = new HashMap<String, Sone>();
 
+       /** All new Sones. */
+       private Set<Sone> newSones = new HashSet<Sone>();
+
        /** All posts. */
        private Map<String, Post> posts = new HashMap<String, Post>();
 
@@ -320,6 +323,21 @@ public class Core implements IdentityListener {
        }
 
        /**
+        * 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}.
+        *
+        * @param sone
+        *            The sone to check for
+        * @return {@code true} if the given Sone is new, false otherwise
+        */
+       public boolean isNewSone(Sone sone) {
+               synchronized (newSones) {
+                       return newSones.remove(sone);
+               }
+       }
+
+       /**
         * Returns the post with the given ID.
         *
         * @param postId
@@ -508,9 +526,15 @@ public class Core implements IdentityListener {
                        return null;
                }
                synchronized (remoteSones) {
+                       boolean newSone = !isRemoteSone(identity.getId());
                        final Sone sone = getRemoteSone(identity.getId()).setIdentity(identity);
                        sone.setRequestUri(getSoneUri(identity.getRequestUri()));
                        sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0));
+                       if (newSone) {
+                               synchronized (newSones) {
+                                       newSones.add(sone);
+                               }
+                       }
                        remoteSones.put(identity.getId(), sone);
                        soneDownloader.addSone(sone);
                        setSoneStatus(sone, SoneStatus.unknown);