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;
26 import net.pterodactylus.sone.freenet.plugin.PluginException;
27 import net.pterodactylus.util.cache.CacheException;
28 import net.pterodactylus.util.cache.CacheItem;
29 import net.pterodactylus.util.cache.DefaultCacheItem;
30 import net.pterodactylus.util.cache.MemoryCache;
31 import net.pterodactylus.util.cache.ValueRetriever;
32 import net.pterodactylus.util.cache.WritableCache;
33 import net.pterodactylus.util.collection.TimedMap;
36 * A Web of Trust identity.
38 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40 public class DefaultIdentity implements Identity {
42 /** The web of trust connector. */
43 private final WebOfTrustConnector webOfTrustConnector;
45 /** The ID of the identity. */
46 private final String id;
48 /** The nickname of the identity. */
49 private final String nickname;
51 /** The request URI of the identity. */
52 private final String requestUri;
54 /** The contexts of the identity. */
55 private final Set<String> contexts = Collections.synchronizedSet(new HashSet<String>());
57 /** The properties of the identity. */
58 private final Map<String, String> properties = Collections.synchronizedMap(new HashMap<String, String>());
61 private final WritableCache<OwnIdentity, Trust> trustCache = new MemoryCache<OwnIdentity, Trust>(new ValueRetriever<OwnIdentity, Trust>() {
64 @SuppressWarnings("synthetic-access")
65 public CacheItem<Trust> retrieve(OwnIdentity ownIdentity) throws CacheException {
67 return new DefaultCacheItem<Trust>(webOfTrustConnector.getTrust(ownIdentity, DefaultIdentity.this));
68 } catch (PluginException pe1) {
69 throw new CacheException("Could not retrieve trust for OwnIdentity: " + ownIdentity, pe1);
73 }, new TimedMap<OwnIdentity, CacheItem<Trust>>(60000));
76 * Creates a new identity.
78 * @param webOfTrustConnector
79 * The web of trust connector
81 * The ID of the identity
83 * The nickname of the identity
85 * The request URI of the identity
87 public DefaultIdentity(WebOfTrustConnector webOfTrustConnector, String id, String nickname, String requestUri) {
88 this.webOfTrustConnector = webOfTrustConnector;
90 this.nickname = nickname;
91 this.requestUri = requestUri;
102 public String getId() {
110 public String getNickname() {
118 public String getRequestUri() {
126 public Set<String> getContexts() {
127 return Collections.unmodifiableSet(contexts);
131 * Sets the contexts of this identity.
133 * This method is only called by the {@link IdentityManager}.
136 * The contexts to set
138 void setContextsPrivate(Set<String> contexts) {
139 this.contexts.clear();
140 this.contexts.addAll(contexts);
147 public boolean hasContext(String context) {
148 return contexts.contains(context);
152 * Adds the given context to this identity.
154 * This method is only called by the {@link IdentityManager}.
159 void addContextPrivate(String context) {
160 contexts.add(context);
164 * Removes the given context from this identity.
166 * This method is only called by the {@link IdentityManager}.
169 * The context to remove
171 public void removeContextPrivate(String context) {
172 contexts.remove(context);
179 public Map<String, String> getProperties() {
180 synchronized (properties) {
181 return Collections.unmodifiableMap(properties);
186 * Sets all properties of this identity.
188 * This method is only called by the {@link IdentityManager}.
191 * The new properties of this identity
193 void setPropertiesPrivate(Map<String, String> properties) {
194 synchronized (this.properties) {
195 this.properties.clear();
196 this.properties.putAll(properties);
201 * Sets the property with the given name to the given value.
203 * This method is only called by the {@link IdentityManager}.
206 * The name of the property
208 * The value of the property
210 void setPropertyPrivate(String name, String value) {
211 synchronized (properties) {
212 properties.put(name, value);
220 public String getProperty(String name) {
221 synchronized (properties) {
222 return properties.get(name);
227 * Removes the property with the given name.
229 * This method is only called by the {@link IdentityManager}.
232 * The name of the property to remove
234 void removePropertyPrivate(String name) {
235 synchronized (properties) {
236 properties.remove(name);
244 public Trust getTrust(OwnIdentity ownIdentity) throws WebOfTrustException {
246 return trustCache.get(ownIdentity);
247 } catch (CacheException ce1) {
248 throw new WebOfTrustException("Could not get trust for OwnIdentity: " + ownIdentity, ce1);
253 * Sets the trust received for this identity by the given own identity.
256 * The own identity that gives the trust
258 * The trust received for this identity
260 void setTrustPrivate(OwnIdentity ownIdentity, Trust trust) {
261 trustCache.put(ownIdentity, trust);
272 public int hashCode() {
273 return id.hashCode();
280 public boolean equals(Object object) {
281 if (!(object instanceof DefaultIdentity)) {
284 DefaultIdentity identity = (DefaultIdentity) object;
285 return identity.id.equals(id);
292 public String toString() {
293 return getClass().getSimpleName() + "[id=" + id + ",nickname=" + nickname + ",contexts=" + contexts + ",properties=" + properties + "]";