Store friend Sones as strings, not as Sone objects.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index 055ede4..450e996 100644 (file)
@@ -107,7 +107,11 @@ public class Core implements IdentityListener {
        private Map<String, Sone> remoteSones = new HashMap<String, Sone>();
 
        /** All new Sones. */
-       private Set<Sone> newSones = new HashSet<Sone>();
+       private Set<String> newSones = new HashSet<String>();
+
+       /** All known Sones. */
+       /* synchronize access on {@link #newSones}. */
+       private Set<String> knownSones = new HashSet<String>();
 
        /** All posts. */
        private Map<String, Post> posts = new HashMap<String, Post>();
@@ -203,10 +207,39 @@ public class Core implements IdentityListener {
         *         Sone
         */
        public Sone getSone(String id) {
+               return getSone(id, true);
+       }
+
+       /**
+        * Returns the Sone with the given ID, regardless whether it’s local or
+        * remote.
+        *
+        * @param id
+        *            The ID of the Sone to get
+        * @param create
+        *            {@code true} to create a new Sone if none exists,
+        *            {@code false} to return {@code null} if a Sone with the given
+        *            ID does not exist
+        * @return The Sone with the given ID, or {@code null} if there is no such
+        *         Sone
+        */
+       public Sone getSone(String id, boolean create) {
                if (isLocalSone(id)) {
                        return getLocalSone(id);
                }
-               return getRemoteSone(id);
+               return getRemoteSone(id, create);
+       }
+
+       /**
+        * Checks whether the core knows a Sone with the given ID.
+        *
+        * @param id
+        *            The ID of the Sone
+        * @return {@code true} if there is a Sone with the given ID, {@code false}
+        *         otherwise
+        */
+       public boolean hasSone(String id) {
+               return isLocalSone(id) || isRemoteSone(id);
        }
 
        /**
@@ -284,9 +317,23 @@ public class Core implements IdentityListener {
         * @return The Sone with the given ID
         */
        public Sone getRemoteSone(String id) {
+               return getRemoteSone(id, true);
+       }
+
+       /**
+        * Returns the remote Sone with the given ID.
+        *
+        * @param id
+        *            The ID of the remote Sone to get
+        * @param create
+        *            {@code true} to always create a Sone, {@code false} to return
+        *            {@code null} if no Sone with the given ID exists
+        * @return The Sone with the given ID
+        */
+       public Sone getRemoteSone(String id, boolean create) {
                synchronized (remoteSones) {
                        Sone sone = remoteSones.get(id);
-                       if (sone == null) {
+                       if ((sone == null) && create) {
                                sone = new Sone(id);
                                remoteSones.put(id, sone);
                        }
@@ -333,7 +380,9 @@ public class Core implements IdentityListener {
         */
        public boolean isNewSone(Sone sone) {
                synchronized (newSones) {
-                       return newSones.remove(sone);
+                       boolean isNew = !knownSones.contains(sone.getId()) && newSones.remove(sone.getId());
+                       knownSones.add(sone.getId());
+                       return isNew;
                }
        }
 
@@ -526,13 +575,13 @@ public class Core implements IdentityListener {
                        return null;
                }
                synchronized (remoteSones) {
-                       boolean newSone = !isRemoteSone(identity.getId());
                        final Sone sone = getRemoteSone(identity.getId()).setIdentity(identity);
+                       boolean newSone = sone.getRequestUri() == null;
                        sone.setRequestUri(getSoneUri(identity.getRequestUri()));
                        sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0));
                        if (newSone) {
                                synchronized (newSones) {
-                                       newSones.add(sone);
+                                       newSones.add(sone.getId());
                                }
                        }
                        remoteSones.put(identity.getId(), sone);
@@ -710,18 +759,13 @@ public class Core implements IdentityListener {
                }
 
                /* load friends. */
-               Set<Sone> friends = new HashSet<Sone>();
+               Set<String> friends = new HashSet<String>();
                while (true) {
                        String friendId = configuration.getStringValue(sonePrefix + "/Friends/" + friends.size() + "/ID").getValue(null);
                        if (friendId == null) {
                                break;
                        }
-                       Boolean friendLocal = configuration.getBooleanValue(sonePrefix + "/Friends/" + friends.size() + "/Local").getValue(null);
-                       if (friendLocal == null) {
-                               logger.log(Level.WARNING, "Invalid friend found, aborting load!");
-                               return;
-                       }
-                       friends.add(friendLocal ? getLocalSone(friendId) : getRemoteSone(friendId));
+                       friends.add(friendId);
                }
 
                /* if we’re still here, Sone was loaded successfully. */
@@ -735,6 +779,11 @@ public class Core implements IdentityListener {
                        sone.setFriends(friends);
                        sone.setModificationCounter(soneModificationCounter);
                }
+               synchronized (newSones) {
+                       for (String friend : friends) {
+                               knownSones.add(friend);
+                       }
+               }
        }
 
        /**
@@ -808,9 +857,8 @@ public class Core implements IdentityListener {
 
                        /* save friends. */
                        int friendCounter = 0;
-                       for (Sone friend : sone.getFriends()) {
-                               configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(friend.getId());
-                               configuration.getBooleanValue(sonePrefix + "/Friends/" + friendCounter++ + "/Local").setValue(friend.getInsertUri() != null);
+                       for (String friend : sone.getFriends()) {
+                               configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(friend);
                        }
                        configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null);
 
@@ -984,6 +1032,17 @@ public class Core implements IdentityListener {
 
                options.getIntegerOption("InsertionDelay").set(configuration.getIntValue("Option/InsertionDelay").getValue(null));
 
+               /* load known Sones. */
+               int soneCounter = 0;
+               while (true) {
+                       String knownSoneId = configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").getValue(null);
+                       if (knownSoneId == null) {
+                               break;
+                       }
+                       synchronized (newSones) {
+                               knownSones.add(knownSoneId);
+                       }
+               }
        }
 
        /**
@@ -995,6 +1054,16 @@ public class Core implements IdentityListener {
                        configuration.getIntValue("Option/InsertionDelay").setValue(options.getIntegerOption("InsertionDelay").getReal());
                        configuration.getBooleanValue("Option/ClearOnNextRestart").setValue(options.getBooleanOption("ClearOnNextRestart").getReal());
                        configuration.getBooleanValue("Option/ReallyClearOnNextRestart").setValue(options.getBooleanOption("ReallyClearOnNextRestart").getReal());
+
+                       /* save known Sones. */
+                       int soneCounter = 0;
+                       synchronized (newSones) {
+                               for (String knownSoneId : knownSones) {
+                                       configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").setValue(knownSoneId);
+                               }
+                               configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null);
+                       }
+
                } catch (ConfigurationException ce1) {
                        logger.log(Level.SEVERE, "Could not store configuration!", ce1);
                }