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.Cache;
28 import net.pterodactylus.util.cache.CacheException;
29 import net.pterodactylus.util.cache.CacheItem;
30 import net.pterodactylus.util.cache.DefaultCacheItem;
31 import net.pterodactylus.util.cache.MemoryCache;
32 import net.pterodactylus.util.cache.ValueRetriever;
35 * A Web of Trust identity.
37 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39 public class DefaultIdentity implements Identity {
41 /** The web of trust connector. */
42 private final WebOfTrustConnector webOfTrustConnector;
44 /** The ID of the identity. */
45 private final String id;
47 /** The nickname of the identity. */
48 private final String nickname;
50 /** The request URI of the identity. */
51 private final String requestUri;
53 /** The contexts of the identity. */
54 private final Set<String> contexts = Collections.synchronizedSet(new HashSet<String>());
56 /** The properties of the identity. */
57 private final Map<String, String> properties = Collections.synchronizedMap(new HashMap<String, String>());
60 private final Cache<OwnIdentity, Trust> trustCache = new MemoryCache<OwnIdentity, Trust>(new ValueRetriever<OwnIdentity, Trust>() {
63 @SuppressWarnings("synthetic-access")
64 public CacheItem<Trust> retrieve(OwnIdentity ownIdentity) throws CacheException {
66 return new DefaultCacheItem<Trust>(webOfTrustConnector.getTrust(ownIdentity, DefaultIdentity.this));
67 } catch (PluginException pe1) {
68 throw new CacheException("Could not retrieve trust for OwnIdentity: " + ownIdentity, pe1);
75 * Creates a new identity.
77 * @param webOfTrustConnector
78 * The web of trust connector
80 * The ID of the identity
82 * The nickname of the identity
84 * The request URI of the identity
86 public DefaultIdentity(WebOfTrustConnector webOfTrustConnector, String id, String nickname, String requestUri) {
87 this.webOfTrustConnector = webOfTrustConnector;
89 this.nickname = nickname;
90 this.requestUri = requestUri;
101 public String getId() {
109 public String getNickname() {
117 public String getRequestUri() {
125 public Set<String> getContexts() {
126 return Collections.unmodifiableSet(contexts);
130 * Sets the contexts of this identity.
132 * This method is only called by the {@link IdentityManager}.
135 * The contexts to set
137 void setContextsPrivate(Set<String> contexts) {
138 this.contexts.clear();
139 this.contexts.addAll(contexts);
146 public boolean hasContext(String context) {
147 return contexts.contains(context);
151 * Adds the given context to this identity.
153 * This method is only called by the {@link IdentityManager}.
158 void addContextPrivate(String context) {
159 contexts.add(context);
163 * Removes the given context from this identity.
165 * This method is only called by the {@link IdentityManager}.
168 * The context to remove
170 public void removeContextPrivate(String context) {
171 contexts.remove(context);
178 public Map<String, String> getProperties() {
179 synchronized (properties) {
180 return Collections.unmodifiableMap(properties);
185 * Sets all properties of this identity.
187 * This method is only called by the {@link IdentityManager}.
190 * The new properties of this identity
192 void setPropertiesPrivate(Map<String, String> properties) {
193 synchronized (this.properties) {
194 this.properties.clear();
195 this.properties.putAll(properties);
200 * Sets the property with the given name to the given value.
202 * This method is only called by the {@link IdentityManager}.
205 * The name of the property
207 * The value of the property
209 void setPropertyPrivate(String name, String value) {
210 synchronized (properties) {
211 properties.put(name, value);
219 public String getProperty(String name) {
220 synchronized (properties) {
221 return properties.get(name);
226 * Removes the property with the given name.
228 * This method is only called by the {@link IdentityManager}.
231 * The name of the property to remove
233 void removePropertyPrivate(String name) {
234 synchronized (properties) {
235 properties.remove(name);
243 public Trust getTrust(OwnIdentity ownIdentity) throws WebOfTrustException {
245 return trustCache.get(ownIdentity);
246 } catch (CacheException ce1) {
247 throw new WebOfTrustException("Could not get trust for OwnIdentity: " + ownIdentity, ce1);
259 public int hashCode() {
260 return id.hashCode();
267 public boolean equals(Object object) {
268 if (!(object instanceof DefaultIdentity)) {
271 DefaultIdentity identity = (DefaultIdentity) object;
272 return identity.id.equals(id);
279 public String toString() {
280 return getClass().getSimpleName() + "[id=" + id + ",nickname=" + nickname + ",contexts=" + contexts + ",properties=" + properties + "]";