Store contexts and properties in identities, allow modification only via IdentityManager.
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / wot / Identity.java
index c3053ae..5816b47 100644 (file)
@@ -40,7 +40,7 @@ public class Identity {
        private final String requestUri;
 
        /** The contexts of the identity. */
-       protected final Set<String> contexts = Collections.synchronizedSet(new HashSet<String>());
+       private final Set<String> contexts = Collections.synchronizedSet(new HashSet<String>());
 
        /** The properties of the identity. */
        private final Map<String, String> properties = Collections.synchronizedMap(new HashMap<String, String>());
@@ -93,16 +93,29 @@ public class Identity {
        }
 
        /**
-        * Returns the contexts of the identity.
+        * Returns all contexts of this identity.
         *
-        * @return The contexts of the identity
+        * @return All contexts of this identity
         */
        public Set<String> getContexts() {
                return Collections.unmodifiableSet(contexts);
        }
 
        /**
-        * Returns whether the identity contains the given context.
+        * Sets all contexts of this identity.
+        * <p>
+        * This method is only called by the {@link IdentityManager}.
+        *
+        * @param contexts
+        *            All contexts of the identity
+        */
+       void setContexts(Set<String> contexts) {
+               this.contexts.clear();
+               this.contexts.addAll(contexts);
+       }
+
+       /**
+        * Returns whether this identity has the given context.
         *
         * @param context
         *            The context to check for
@@ -114,48 +127,96 @@ public class Identity {
        }
 
        /**
-        * Returns the properties of the identity.
+        * Adds the given context to this identity.
+        * <p>
+        * 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.
+        * <p>
+        * 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<String, String> 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.
+        * <p>
+        * 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<String, String> properties) {
+               synchronized (this.properties) {
+                       this.properties.clear();
+                       this.properties.putAll(properties);
+               }
        }
 
        /**
         * Sets the property with the given name to the given value.
+        * <p>
+        * 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.
+        * <p>
+        * 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);
+               }
        }
 
        //
@@ -182,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 + "]";
+       }
+
 }