X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FCore.java;h=450e996b383cd4e501021829cd9d08185581081f;hb=62ebf3da00001abb841c92128c4fdb57d0795ee2;hp=93891ab3e4af5613d2a820a9c8989d31f4bfee6f;hpb=19a2cb003837648db4d708eac8971343b5026257;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 93891ab..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(); @@ -207,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); } /** @@ -288,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); } @@ -337,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; } } @@ -538,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); @@ -716,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. */ @@ -742,7 +780,7 @@ public class Core implements IdentityListener { sone.setModificationCounter(soneModificationCounter); } synchronized (newSones) { - for (Sone friend : friends) { + for (String friend : friends) { knownSones.add(friend); } } @@ -819,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); @@ -1003,7 +1040,7 @@ public class Core implements IdentityListener { break; } synchronized (newSones) { - knownSones.add(getRemoteSone(knownSoneId)); + knownSones.add(knownSoneId); } } } @@ -1021,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); }