X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdata%2FSone.java;h=8b2367be94fbe01ca919c215199e1f738f83b7a4;hb=1f6a99de95988a0299faff512ab457ae1172dc46;hp=ba5ef68d896d6dfcd6757214ffc7cb9fa1aa1923;hpb=985f25a5434ba6ba57cbafc347cbc49e67359d3f;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/data/Sone.java b/src/main/java/net/pterodactylus/sone/data/Sone.java index ba5ef68..8b2367b 100644 --- a/src/main/java/net/pterodactylus/sone/data/Sone.java +++ b/src/main/java/net/pterodactylus/sone/data/Sone.java @@ -27,13 +27,15 @@ import freenet.keys.FreenetURI; /** * A Sone defines everything about a user: the {@link User} itself, her profile, * her status updates. + *

+ * Operations that modify the Sone need to synchronize on the Sone in question. * * @author David ‘Bombe’ Roden */ public class Sone { /** A GUID for this Sone. */ - private final UUID id = UUID.randomUUID(); + private final UUID id; /** The name of this Sone. */ private final String name; @@ -48,21 +50,28 @@ public class Sone { /** All friend Sones. */ private final Set friendSones = new HashSet(); + /** Modification count. */ + private volatile long modificationCounter = 0; + /** * Creates a new Sone. * + * @param id + * The ID of this Sone * @param name * The name of the Sone * @param requestUri * The request URI of the Sone */ - public Sone(String name, FreenetURI requestUri) { - this(name, requestUri, null); + public Sone(UUID id, String name, FreenetURI requestUri) { + this(id, name, requestUri, null); } /** * Creates a new Sone. * + * @param id + * The ID of this Sone * @param name * The name of the Sone * @param requestUri @@ -70,7 +79,8 @@ public class Sone { * @param insertUri * The insert URI of the Sone */ - public Sone(String name, FreenetURI requestUri, FreenetURI insertUri) { + public Sone(UUID id, String name, FreenetURI requestUri, FreenetURI insertUri) { + this.id = id; this.name = name; this.requestUri = requestUri; this.insertUri = insertUri; @@ -144,8 +154,10 @@ public class Sone { * The friend Sone to add * @return This Sone (for method chaining) */ - public Sone addFriendSone(Sone friendSone) { - friendSones.add(friendSone); + public synchronized Sone addFriendSone(Sone friendSone) { + if (friendSones.add(friendSone)) { + modificationCounter++; + } return this; } @@ -156,11 +168,32 @@ public class Sone { * The friend Sone to remove * @return This Sone (for method chaining) */ - public Sone removeFriendSone(Sone friendSone) { - friendSones.remove(friendSone); + public synchronized Sone removeFriendSone(Sone friendSone) { + if (friendSones.remove(friendSone)) { + modificationCounter++; + } return this; } + /** + * Returns the modification counter. + * + * @return The modification counter + */ + public synchronized long getModificationCounter() { + return modificationCounter; + } + + /** + * Sets the modification counter. + * + * @param modificationCounter + * The new modification counter + */ + public synchronized void setModificationCounter(long modificationCounter) { + this.modificationCounter = modificationCounter; + } + // // OBJECT METHODS // @@ -173,4 +206,15 @@ public class Sone { return id.hashCode(); } + /** + * {@inheritDoc} + */ + @Override + public boolean equals(Object object) { + if (!(object instanceof Sone)) { + return false; + } + return ((Sone) object).id.equals(id); + } + }