From 62ebf3da00001abb841c92128c4fdb57d0795ee2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 10 Nov 2010 20:29:57 +0100 Subject: [PATCH] Store friend Sones as strings, not as Sone objects. --- .../java/net/pterodactylus/sone/core/Core.java | 32 +++++++++------------- .../java/net/pterodactylus/sone/data/Sone.java | 13 ++++----- .../net/pterodactylus/sone/web/FollowSonePage.java | 7 ++--- .../java/net/pterodactylus/sone/web/IndexPage.java | 17 ++++-------- .../sone/web/ajax/FollowSoneAjaxPage.java | 5 ++-- 5 files changed, 29 insertions(+), 45 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index ad19824..450e996 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -107,11 +107,11 @@ public class Core implements IdentityListener { private Map remoteSones = new HashMap(); /** All new Sones. */ - private Set newSones = new HashSet(); + private Set newSones = new HashSet(); /** All known Sones. */ /* synchronize access on {@link #newSones}. */ - private Set knownSones = new HashSet(); + private Set knownSones = new HashSet(); /** All posts. */ private Map posts = new HashMap(); @@ -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 friends = new HashSet(); + Set friends = new HashSet(); 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); } diff --git a/src/main/java/net/pterodactylus/sone/data/Sone.java b/src/main/java/net/pterodactylus/sone/data/Sone.java index 2149a68..2bb5a3e 100644 --- a/src/main/java/net/pterodactylus/sone/data/Sone.java +++ b/src/main/java/net/pterodactylus/sone/data/Sone.java @@ -82,7 +82,7 @@ public class Sone { private volatile Profile profile; /** All friend Sones. */ - private final Set friendSones = Collections.synchronizedSet(new HashSet()); + private final Set friendSones = Collections.synchronizedSet(new HashSet()); /** All posts. */ private final Set posts = Collections.synchronizedSet(new HashSet()); @@ -289,9 +289,8 @@ public class Sone { * * @return The friend Sones of this Sone */ - public List getFriends() { - List friends = new ArrayList(friendSones); - Collections.sort(friends, NICE_NAME_COMPARATOR); + public List getFriends() { + List friends = new ArrayList(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 friends) { + public Sone setFriends(Collection 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; diff --git a/src/main/java/net/pterodactylus/sone/web/FollowSonePage.java b/src/main/java/net/pterodactylus/sone/web/FollowSonePage.java index bb90ce0..0367f6a 100644 --- a/src/main/java/net/pterodactylus/sone/web/FollowSonePage.java +++ b/src/main/java/net/pterodactylus/sone/web/FollowSonePage.java @@ -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); } } diff --git a/src/main/java/net/pterodactylus/sone/web/IndexPage.java b/src/main/java/net/pterodactylus/sone/web/IndexPage.java index 589e7aa..00ccc35 100644 --- a/src/main/java/net/pterodactylus/sone/web/IndexPage.java +++ b/src/main/java/net/pterodactylus/sone/web/IndexPage.java @@ -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 allPosts = new ArrayList(); allPosts.addAll(sone.getPosts()); - for (Sone friendSone : sone.getFriends()) { - allPosts.addAll(friendSone.getPosts()); - } - Collections.sort(allPosts, new Comparator() { - - @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); } diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/FollowSoneAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/FollowSoneAjaxPage.java index 0c83553..6d98b43 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/FollowSoneAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/FollowSoneAjaxPage.java @@ -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); } -- 2.7.4