2 * Sone - DefaultIdentity.java - Copyright © 2010 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.freenet.wot;
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.HashSet;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
28 import net.pterodactylus.sone.freenet.plugin.PluginException;
29 import net.pterodactylus.util.cache.CacheException;
30 import net.pterodactylus.util.cache.CacheItem;
31 import net.pterodactylus.util.cache.DefaultCacheItem;
32 import net.pterodactylus.util.cache.MemoryCache;
33 import net.pterodactylus.util.cache.ValueRetriever;
34 import net.pterodactylus.util.cache.WritableCache;
35 import net.pterodactylus.util.collection.TimedMap;
36 import net.pterodactylus.util.logging.Logging;
39 * A Web of Trust identity.
41 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43 public class DefaultIdentity implements Identity {
46 private static final Logger logger = Logging.getLogger(DefaultIdentity.class);
48 /** The web of trust connector. */
49 private final WebOfTrustConnector webOfTrustConnector;
51 /** The ID of the identity. */
52 private final String id;
54 /** The nickname of the identity. */
55 private final String nickname;
57 /** The request URI of the identity. */
58 private final String requestUri;
60 /** The contexts of the identity. */
61 private final Set<String> contexts = Collections.synchronizedSet(new HashSet<String>());
63 /** The properties of the identity. */
64 private final Map<String, String> properties = Collections.synchronizedMap(new HashMap<String, String>());
67 private final WritableCache<OwnIdentity, Trust> trustCache = new MemoryCache<OwnIdentity, Trust>(new ValueRetriever<OwnIdentity, Trust>() {
70 @SuppressWarnings("synthetic-access")
71 public CacheItem<Trust> retrieve(OwnIdentity ownIdentity) throws CacheException {
73 return new DefaultCacheItem<Trust>(webOfTrustConnector.getTrust(ownIdentity, DefaultIdentity.this));
74 } catch (PluginException pe1) {
75 throw new CacheException("Could not retrieve trust for OwnIdentity: " + ownIdentity, pe1);
79 }, new TimedMap<OwnIdentity, CacheItem<Trust>>(60000));
82 * Creates a new identity.
84 * @param webOfTrustConnector
85 * The web of trust connector
87 * The ID of the identity
89 * The nickname of the identity
91 * The request URI of the identity
93 public DefaultIdentity(WebOfTrustConnector webOfTrustConnector, String id, String nickname, String requestUri) {
94 this.webOfTrustConnector = webOfTrustConnector;
96 this.nickname = nickname;
97 this.requestUri = requestUri;
108 public String getId() {
116 public String getNickname() {
124 public String getRequestUri() {
132 public Set<String> getContexts() {
133 return Collections.unmodifiableSet(contexts);
137 * Sets the contexts of this identity.
139 * This method is only called by the {@link IdentityManager}.
142 * The contexts to set
144 void setContextsPrivate(Set<String> contexts) {
145 this.contexts.clear();
146 this.contexts.addAll(contexts);
153 public boolean hasContext(String context) {
154 return contexts.contains(context);
158 * Adds the given context to this identity.
160 * This method is only called by the {@link IdentityManager}.
165 void addContextPrivate(String context) {
166 contexts.add(context);
170 * Removes the given context from this identity.
172 * This method is only called by the {@link IdentityManager}.
175 * The context to remove
177 public void removeContextPrivate(String context) {
178 contexts.remove(context);
185 public Map<String, String> getProperties() {
186 synchronized (properties) {
187 return Collections.unmodifiableMap(properties);
192 * Sets all properties of this identity.
194 * This method is only called by the {@link IdentityManager}.
197 * The new properties of this identity
199 void setPropertiesPrivate(Map<String, String> properties) {
200 synchronized (this.properties) {
201 this.properties.clear();
202 this.properties.putAll(properties);
207 * Sets the property with the given name to the given value.
209 * This method is only called by the {@link IdentityManager}.
212 * The name of the property
214 * The value of the property
216 void setPropertyPrivate(String name, String value) {
217 synchronized (properties) {
218 properties.put(name, value);
226 public String getProperty(String name) {
227 synchronized (properties) {
228 return properties.get(name);
233 * Removes the property with the given name.
235 * This method is only called by the {@link IdentityManager}.
238 * The name of the property to remove
240 void removePropertyPrivate(String name) {
241 synchronized (properties) {
242 properties.remove(name);
250 public Trust getTrust(OwnIdentity ownIdentity) {
252 return trustCache.get(ownIdentity);
253 } catch (CacheException ce1) {
254 logger.log(Level.WARNING, "Could not get trust for OwnIdentity: " + ownIdentity, ce1);
260 * Sets the trust received for this identity by the given own identity.
263 * The own identity that gives the trust
265 * The trust received for this identity
267 void setTrustPrivate(OwnIdentity ownIdentity, Trust trust) {
268 trustCache.put(ownIdentity, trust);
279 public int hashCode() {
280 return id.hashCode();
287 public boolean equals(Object object) {
288 if (!(object instanceof DefaultIdentity)) {
291 DefaultIdentity identity = (DefaultIdentity) object;
292 return identity.id.equals(id);
299 public String toString() {
300 return getClass().getSimpleName() + "[id=" + id + ",nickname=" + nickname + ",contexts=" + contexts + ",properties=" + properties + "]";