Remove @author tags
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / wot / DefaultIdentity.java
index dcafb18..8bda5de 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Sone - DefaultIdentity.java - Copyright © 2010 David Roden
+ * Sone - DefaultIdentity.java - Copyright © 2010–2016 David Roden
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 
 package net.pterodactylus.sone.freenet.wot;
 
+import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
 
-import net.pterodactylus.sone.freenet.plugin.PluginException;
-import net.pterodactylus.util.cache.CacheException;
-import net.pterodactylus.util.cache.CacheItem;
-import net.pterodactylus.util.cache.DefaultCacheItem;
-import net.pterodactylus.util.cache.MemoryCache;
-import net.pterodactylus.util.cache.ValueRetriever;
-import net.pterodactylus.util.cache.WritableCache;
-
 /**
  * A Web of Trust identity.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
 public class DefaultIdentity implements Identity {
 
-       /** The web of trust connector. */
-       private final WebOfTrustConnector webOfTrustConnector;
-
        /** The ID of the identity. */
        private final String id;
 
@@ -57,25 +45,11 @@ public class DefaultIdentity implements Identity {
        private final Map<String, String> properties = Collections.synchronizedMap(new HashMap<String, String>());
 
        /** Cached trust. */
-       private final WritableCache<OwnIdentity, Trust> trustCache = new MemoryCache<OwnIdentity, Trust>(new ValueRetriever<OwnIdentity, Trust>() {
-
-               @Override
-               @SuppressWarnings("synthetic-access")
-               public CacheItem<Trust> retrieve(OwnIdentity ownIdentity) throws CacheException {
-                       try {
-                               return new DefaultCacheItem<Trust>(webOfTrustConnector.getTrust(ownIdentity, DefaultIdentity.this));
-                       } catch (PluginException pe1) {
-                               throw new CacheException("Could not retrieve trust for OwnIdentity: " + ownIdentity, pe1);
-                       }
-               }
-
-       });
+       private final Map<OwnIdentity, Trust> trustCache = Collections.synchronizedMap(new HashMap<OwnIdentity, Trust>());
 
        /**
         * Creates a new identity.
         *
-        * @param webOfTrustConnector
-        *            The web of trust connector
         * @param id
         *            The ID of the identity
         * @param nickname
@@ -83,8 +57,7 @@ public class DefaultIdentity implements Identity {
         * @param requestUri
         *            The request URI of the identity
         */
-       public DefaultIdentity(WebOfTrustConnector webOfTrustConnector, String id, String nickname, String requestUri) {
-               this.webOfTrustConnector = webOfTrustConnector;
+       public DefaultIdentity(String id, String nickname, String requestUri) {
                this.id = id;
                this.nickname = nickname;
                this.requestUri = requestUri;
@@ -94,199 +67,112 @@ public class DefaultIdentity implements Identity {
        // ACCESSORS
        //
 
-       /**
-        * {@inheritDoc}
-        */
        @Override
        public String getId() {
                return id;
        }
 
-       /**
-        * {@inheritDoc}
-        */
        @Override
        public String getNickname() {
                return nickname;
        }
 
-       /**
-        * {@inheritDoc}
-        */
        @Override
        public String getRequestUri() {
                return requestUri;
        }
 
-       /**
-        * {@inheritDoc}
-        */
        @Override
        public Set<String> getContexts() {
                return Collections.unmodifiableSet(contexts);
        }
 
-       /**
-        * Sets the contexts of this identity.
-        * <p>
-        * This method is only called by the {@link IdentityManager}.
-        *
-        * @param contexts
-        *            The contexts to set
-        */
-       void setContextsPrivate(Set<String> contexts) {
-               this.contexts.clear();
-               this.contexts.addAll(contexts);
-       }
-
-       /**
-        * {@inheritDoc}
-        */
        @Override
        public boolean hasContext(String context) {
                return contexts.contains(context);
        }
 
-       /**
-        * Adds the given context to this identity.
-        * <p>
-        * This method is only called by the {@link IdentityManager}.
-        *
-        * @param context
-        *            The context to add
-        */
-       void addContextPrivate(String context) {
+       @Override
+       public void setContexts(Collection<String> contexts) {
+               this.contexts.clear();
+               this.contexts.addAll(contexts);
+       }
+
+       @Override
+       public Identity addContext(String context) {
                contexts.add(context);
+               return this;
        }
 
-       /**
-        * Removes the given context from this identity.
-        * <p>
-        * This method is only called by the {@link IdentityManager}.
-        *
-        * @param context
-        *            The context to remove
-        */
-       public void removeContextPrivate(String context) {
+       @Override
+       public Identity removeContext(String context) {
                contexts.remove(context);
+               return this;
        }
 
-       /**
-        * {@inheritDoc}
-        */
        @Override
        public Map<String, String> getProperties() {
-               synchronized (properties) {
-                       return Collections.unmodifiableMap(properties);
-               }
+               return Collections.unmodifiableMap(properties);
        }
 
-       /**
-        * Sets all properties of this identity.
-        * <p>
-        * This method is only called by the {@link IdentityManager}.
-        *
-        * @param properties
-        *            The new properties of this identity
-        */
-       void setPropertiesPrivate(Map<String, String> properties) {
-               synchronized (this.properties) {
-                       this.properties.clear();
-                       this.properties.putAll(properties);
-               }
+       @Override
+       public void setProperties(Map<String, String> 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
-        * @param value
-        *            The value of the property
-        */
-       void setPropertyPrivate(String name, String value) {
-               synchronized (properties) {
-                       properties.put(name, value);
-               }
+       @Override
+       public String getProperty(String name) {
+               return properties.get(name);
        }
 
-       /**
-        * {@inheritDoc}
-        */
        @Override
-       public String getProperty(String name) {
-               synchronized (properties) {
-                       return properties.get(name);
-               }
+       public Identity setProperty(String name, String value) {
+               properties.put(name, value);
+               return this;
        }
 
-       /**
-        * 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
-        */
-       void removePropertyPrivate(String name) {
-               synchronized (properties) {
-                       properties.remove(name);
-               }
+       @Override
+       public Identity removeProperty(String name) {
+               properties.remove(name);
+               return this;
        }
 
-       /**
-        * {@inheritDoc}
-        */
        @Override
-       public Trust getTrust(OwnIdentity ownIdentity) throws WebOfTrustException {
-               try {
-                       return trustCache.get(ownIdentity);
-               } catch (CacheException ce1) {
-                       throw new WebOfTrustException("Could not get trust for OwnIdentity: " + ownIdentity, ce1);
-               }
+       public Trust getTrust(OwnIdentity ownIdentity) {
+               return trustCache.get(ownIdentity);
        }
 
-       /**
-        * Sets the trust received for this identity by the given own identity.
-        *
-        * @param ownIdentity
-        *            The own identity that gives the trust
-        * @param trust
-        *            The trust received for this identity
-        */
-       void setTrustPrivate(OwnIdentity ownIdentity, Trust trust) {
+       @Override
+       public Identity setTrust(OwnIdentity ownIdentity, Trust trust) {
                trustCache.put(ownIdentity, trust);
+               return this;
+       }
+
+       @Override
+       public Identity removeTrust(OwnIdentity ownIdentity) {
+               trustCache.remove(ownIdentity);
+               return this;
        }
 
        //
        // OBJECT METHODS
        //
 
-       /**
-        * {@inheritDoc}
-        */
        @Override
        public int hashCode() {
-               return id.hashCode();
+               return getId().hashCode();
        }
 
-       /**
-        * {@inheritDoc}
-        */
        @Override
        public boolean equals(Object object) {
-               if (!(object instanceof DefaultIdentity)) {
+               if (!(object instanceof Identity)) {
                        return false;
                }
-               DefaultIdentity identity = (DefaultIdentity) object;
-               return identity.id.equals(id);
+               Identity identity = (Identity) object;
+               return identity.getId().equals(getId());
        }
 
-       /**
-        * {@inheritDoc}
-        */
        @Override
        public String toString() {
                return getClass().getSimpleName() + "[id=" + id + ",nickname=" + nickname + ",contexts=" + contexts + ",properties=" + properties + "]";