X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ffreenet%2Fwot%2FIdentity.java;h=5816b47772e0e750117c6e92c3fb69630cb2ff4f;hb=6f8f8c0321e2ac8bff6746a97742126727ac6778;hp=2909936ac2c65ccff8f6c3ed324a8bc59382fb28;hpb=da6c0a57ac23f13e25a2347237ef3d9ac52ac332;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/freenet/wot/Identity.java b/src/main/java/net/pterodactylus/sone/freenet/wot/Identity.java index 2909936..5816b47 100644 --- a/src/main/java/net/pterodactylus/sone/freenet/wot/Identity.java +++ b/src/main/java/net/pterodactylus/sone/freenet/wot/Identity.java @@ -30,9 +30,6 @@ import java.util.Set; */ public class Identity { - /** The Web of Trust connector. */ - protected final WebOfTrustConnector webOfTrustConnector; - /** The ID of the identity. */ private final String id; @@ -43,7 +40,7 @@ public class Identity { private final String requestUri; /** The contexts of the identity. */ - protected final Set contexts = Collections.synchronizedSet(new HashSet()); + private final Set contexts = Collections.synchronizedSet(new HashSet()); /** The properties of the identity. */ private final Map properties = Collections.synchronizedMap(new HashMap()); @@ -51,8 +48,6 @@ public class Identity { /** * Creates a new identity. * - * @param webOfTrustConnector - * The Web of Trust connector * @param id * The ID of the identity * @param nickname @@ -60,8 +55,7 @@ public class Identity { * @param requestUri * The request URI of the identity */ - public Identity(WebOfTrustConnector webOfTrustConnector, String id, String nickname, String requestUri) { - this.webOfTrustConnector = webOfTrustConnector; + public Identity(String id, String nickname, String requestUri) { this.id = id; this.nickname = nickname; this.requestUri = requestUri; @@ -99,38 +93,29 @@ public class Identity { } /** - * Returns the contexts of the identity. + * Returns all contexts of this identity. * - * @return The contexts of the identity - * @throws PluginException - * if an error occured communicating with the Web of Trust - * plugin + * @return All contexts of this identity */ - public Set getContexts() throws PluginException { - return getContexts(false); + public Set getContexts() { + return Collections.unmodifiableSet(contexts); } /** - * Returns the contexts of the identity. + * Sets all contexts of this identity. + *

+ * This method is only called by the {@link IdentityManager}. * - * @param forceReload - * {@code true} to force a reload of the contexts - * @return The contexts of the identity - * @throws PluginException - * if an error occured communicating with the Web of Trust - * plugin - */ - public Set getContexts(boolean forceReload) throws PluginException { - if (contexts.isEmpty() || forceReload) { - Set contexts = webOfTrustConnector.loadIdentityContexts(this); - this.contexts.clear(); - this.contexts.addAll(contexts); - } - return Collections.unmodifiableSet(contexts); + * @param contexts + * All contexts of the identity + */ + void setContexts(Set contexts) { + this.contexts.clear(); + this.contexts.addAll(contexts); } /** - * Returns whether the identity contains the given context. + * Returns whether this identity has the given context. * * @param context * The context to check for @@ -142,48 +127,96 @@ public class Identity { } /** - * Returns the properties of the identity. + * Adds the given context to this identity. + *

+ * This method is only called by the {@link IdentityManager}. * - * @return The properties of the identity + * @param context + * The context to add + */ + void addContext(String context) { + contexts.add(context); + } + + /** + * Removes the given context from this identity. + *

+ * This method is only called by the {@link IdentityManager}. + * + * @param context + * The context to remove + */ + void removeContext(String context) { + contexts.remove(context); + } + + /** + * Returns all properties of this identity. + * + * @return All properties of this identity */ public Map getProperties() { - return Collections.unmodifiableMap(properties); + synchronized (properties) { + return Collections.unmodifiableMap(properties); + } } /** - * Returns the value of the property with the given name. + * Sets all properties of this identity. + *

+ * This method is only called by the {@link IdentityManager}. * - * @param name - * The name of the property - * @return The value of the property, or {@code null} if there is no such - * property + * @param properties + * The new properties of this identity */ - public String getProperty(String name) { - return properties.get(name); + void setProperties(Map properties) { + synchronized (this.properties) { + this.properties.clear(); + this.properties.putAll(properties); + } } /** * Sets the property with the given name to the given value. + *

+ * This method is only called by the {@link IdentityManager}. * * @param name - * The name of the property to set + * The name of the property * @param value - * The new value of the property + * The value of the property */ - public void setProperty(String name, String value) { - properties.put(name, value); - /* TODO - set property. */ + void setProperty(String name, String value) { + synchronized (properties) { + properties.put(name, value); + } + } + + /** + * Returns the value of the property with the given name. + * + * @param name + * The name of the property + * @return The value of the property + */ + public String getProperty(String name) { + synchronized (properties) { + return properties.get(name); + } } /** * Removes the property with the given name. + *

+ * This method is only called by the {@link IdentityManager}. * * @param name * The name of the property to remove */ - public void removeProperty(String name) { - properties.remove(name); - /* TODO - remove property. */ + void removeProperty(String name) { + synchronized (properties) { + properties.remove(name); + } } // @@ -210,4 +243,12 @@ public class Identity { return identity.id.equals(id); } + /** + * {@inheritDoc} + */ + @Override + public String toString() { + return getClass().getSimpleName() + "[id=" + id + ",nickname=" + nickname + ",contexts=" + contexts + ",properties=" + properties + "]"; + } + }