Store friend Sones as strings, not as Sone objects.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 10 Nov 2010 19:29:57 +0000 (20:29 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 10 Nov 2010 19:50:12 +0000 (20:50 +0100)
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/data/Sone.java
src/main/java/net/pterodactylus/sone/web/FollowSonePage.java
src/main/java/net/pterodactylus/sone/web/IndexPage.java
src/main/java/net/pterodactylus/sone/web/ajax/FollowSoneAjaxPage.java

index ad19824..450e996 100644 (file)
@@ -107,11 +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<Sone> knownSones = new HashSet<Sone>();
+       private Set<String> knownSones = new HashSet<String>();
 
        /** All posts. */
        private Map<String, Post> posts = new HashMap<String, Post>();
@@ -380,8 +380,8 @@ public class Core implements IdentityListener {
         */
        public boolean isNewSone(Sone sone) {
                synchronized (newSones) {
-                       boolean isNew = !knownSones.contains(sone) && newSones.remove(sone);
-                       knownSones.add(sone);
+                       boolean isNew = !knownSones.contains(sone.getId()) && newSones.remove(sone.getId());
+                       knownSones.add(sone.getId());
                        return isNew;
                }
        }
@@ -581,7 +581,7 @@ public class Core implements IdentityListener {
                        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);
@@ -759,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. */
@@ -785,7 +780,7 @@ public class Core implements IdentityListener {
                        sone.setModificationCounter(soneModificationCounter);
                }
                synchronized (newSones) {
-                       for (Sone friend : friends) {
+                       for (String friend : friends) {
                                knownSones.add(friend);
                        }
                }
@@ -862,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);
 
@@ -1046,7 +1040,7 @@ public class Core implements IdentityListener {
                                break;
                        }
                        synchronized (newSones) {
-                               knownSones.add(getRemoteSone(knownSoneId));
+                               knownSones.add(knownSoneId);
                        }
                }
        }
@@ -1064,8 +1058,8 @@ public class Core implements IdentityListener {
                        /* save known Sones. */
                        int soneCounter = 0;
                        synchronized (newSones) {
-                               for (Sone knownSone : knownSones) {
-                                       configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").setValue(knownSone.getId());
+                               for (String knownSoneId : knownSones) {
+                                       configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").setValue(knownSoneId);
                                }
                                configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null);
                        }
index 2149a68..2bb5a3e 100644 (file)
@@ -82,7 +82,7 @@ public class Sone {
        private volatile Profile profile;
 
        /** All friend Sones. */
-       private final Set<Sone> friendSones = Collections.synchronizedSet(new HashSet<Sone>());
+       private final Set<String> friendSones = Collections.synchronizedSet(new HashSet<String>());
 
        /** All posts. */
        private final Set<Post> posts = Collections.synchronizedSet(new HashSet<Post>());
@@ -289,9 +289,8 @@ public class Sone {
         *
         * @return The friend Sones of this Sone
         */
-       public List<Sone> getFriends() {
-               List<Sone> friends = new ArrayList<Sone>(friendSones);
-               Collections.sort(friends, NICE_NAME_COMPARATOR);
+       public List<String> getFriends() {
+               List<String> friends = new ArrayList<String>(friendSones);
                return friends;
        }
 
@@ -302,7 +301,7 @@ public class Sone {
         *            The new (and only) friends of this Sone
         * @return This Sone (for method chaining)
         */
-       public Sone setFriends(Collection<Sone> friends) {
+       public Sone setFriends(Collection<String> friends) {
                friendSones.clear();
                friendSones.addAll(friends);
                return this;
@@ -327,8 +326,8 @@ public class Sone {
         *            The friend Sone to add
         * @return This Sone (for method chaining)
         */
-       public Sone addFriend(Sone friendSone) {
-               if (!friendSone.equals(this)) {
+       public Sone addFriend(String friendSone) {
+               if (!friendSone.equals(id)) {
                        friendSones.add(friendSone);
                }
                return this;
index bb90ce0..0367f6a 100644 (file)
@@ -52,11 +52,8 @@ public class FollowSonePage extends SoneTemplatePage {
                        String soneId = request.getHttpRequest().getPartAsStringFailsafe("sone", 44);
                        String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 64);
                        Sone currentSone = getCurrentSone(request.getToadletContext());
-                       Sone sone = webInterface.getCore().getSone(soneId);
-                       if (!sone.equals(currentSone)) {
-                               currentSone.addFriend(sone);
-                               webInterface.getCore().saveSone(currentSone);
-                       }
+                       currentSone.addFriend(soneId);
+                       webInterface.getCore().saveSone(currentSone);
                        throw new RedirectException(returnPage);
                }
        }
index 589e7aa..00ccc35 100644 (file)
@@ -19,7 +19,6 @@ package net.pterodactylus.sone.web;
 
 import java.util.ArrayList;
 import java.util.Collections;
-import java.util.Comparator;
 import java.util.List;
 
 import net.pterodactylus.sone.data.Post;
@@ -57,17 +56,13 @@ public class IndexPage extends SoneTemplatePage {
                Sone sone = getCurrentSone(request.getToadletContext());
                List<Post> allPosts = new ArrayList<Post>();
                allPosts.addAll(sone.getPosts());
-               for (Sone friendSone : sone.getFriends()) {
-                       allPosts.addAll(friendSone.getPosts());
-               }
-               Collections.sort(allPosts, new Comparator<Post>() {
-
-                       @Override
-                       public int compare(Post leftPost, Post rightPost) {
-                               return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightPost.getTime() - leftPost.getTime()));
+               for (String friendSoneId : sone.getFriends()) {
+                       if (!webInterface.getCore().hasSone(friendSoneId)) {
+                               continue;
                        }
-
-               });
+                       allPosts.addAll(webInterface.getCore().getSone(friendSoneId).getPosts());
+               }
+               Collections.sort(allPosts, Post.TIME_COMPARATOR);
                template.set("posts", allPosts);
        }
 
index 0c83553..6d98b43 100644 (file)
@@ -44,15 +44,14 @@ public class FollowSoneAjaxPage extends JsonPage {
        @Override
        protected JsonObject createJsonObject(Request request) {
                String soneId = request.getHttpRequest().getParam("sone");
-               Sone sone = webInterface.getCore().getSone(soneId);
-               if (sone == null) {
+               if (!webInterface.getCore().hasSone(soneId)) {
                        return new JsonObject().put("success", false).put("error", "invalid-sone-id");
                }
                Sone currentSone = getCurrentSone(request.getToadletContext());
                if (currentSone == null) {
                        return new JsonObject().put("success", false).put("error", "auth-required");
                }
-               currentSone.addFriend(sone);
+               currentSone.addFriend(soneId);
                webInterface.getCore().saveSone(currentSone);
                return new JsonObject().put("success", true);
        }