2 * Sone - DefaultIdentity.java - Copyright © 2010–2012 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.Logger;
27 import net.pterodactylus.util.logging.Logging;
30 * A Web of Trust identity.
32 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34 public class DefaultIdentity implements Identity {
37 private static final Logger logger = Logging.getLogger(DefaultIdentity.class);
39 /** The web of trust connector. */
40 private final WebOfTrustConnector webOfTrustConnector;
42 /** The ID of the identity. */
43 private final String id;
45 /** The nickname of the identity. */
46 private final String nickname;
48 /** The request URI of the identity. */
49 private final String requestUri;
51 /** The contexts of the identity. */
52 private final Set<String> contexts = Collections.synchronizedSet(new HashSet<String>());
54 /** The properties of the identity. */
55 private final Map<String, String> properties = Collections.synchronizedMap(new HashMap<String, String>());
58 /* synchronize on itself. */
59 private final Map<OwnIdentity, Trust> trustCache = new HashMap<OwnIdentity, Trust>();
62 * Creates a new identity.
64 * @param webOfTrustConnector
65 * The web of trust connector
67 * The ID of the identity
69 * The nickname of the identity
71 * The request URI of the identity
73 public DefaultIdentity(WebOfTrustConnector webOfTrustConnector, String id, String nickname, String requestUri) {
74 this.webOfTrustConnector = webOfTrustConnector;
76 this.nickname = nickname;
77 this.requestUri = requestUri;
88 public String getId() {
96 public String getNickname() {
104 public String getRequestUri() {
112 public Set<String> getContexts() {
113 return Collections.unmodifiableSet(contexts);
117 * Sets the contexts of this identity.
119 * This method is only called by the {@link IdentityManager}.
122 * The contexts to set
124 void setContextsPrivate(Set<String> contexts) {
125 this.contexts.clear();
126 this.contexts.addAll(contexts);
133 public boolean hasContext(String context) {
134 return contexts.contains(context);
138 * Adds the given context to this identity.
140 * This method is only called by the {@link IdentityManager}.
145 void addContextPrivate(String context) {
146 contexts.add(context);
150 * Removes the given context from this identity.
152 * This method is only called by the {@link IdentityManager}.
155 * The context to remove
157 public void removeContextPrivate(String context) {
158 contexts.remove(context);
165 public Map<String, String> getProperties() {
166 synchronized (properties) {
167 return Collections.unmodifiableMap(properties);
172 * Sets all properties of this identity.
174 * This method is only called by the {@link IdentityManager}.
177 * The new properties of this identity
179 void setPropertiesPrivate(Map<String, String> properties) {
180 synchronized (this.properties) {
181 this.properties.clear();
182 this.properties.putAll(properties);
187 * Sets the property with the given name to the given value.
189 * This method is only called by the {@link IdentityManager}.
192 * The name of the property
194 * The value of the property
196 void setPropertyPrivate(String name, String value) {
197 synchronized (properties) {
198 properties.put(name, value);
206 public String getProperty(String name) {
207 synchronized (properties) {
208 return properties.get(name);
213 * Removes the property with the given name.
215 * This method is only called by the {@link IdentityManager}.
218 * The name of the property to remove
220 void removePropertyPrivate(String name) {
221 synchronized (properties) {
222 properties.remove(name);
230 public Trust getTrust(OwnIdentity ownIdentity) {
231 synchronized (trustCache) {
232 return trustCache.get(ownIdentity);
237 * Sets the trust received for this identity by the given own identity.
240 * The own identity that gives the trust
242 * The trust received for this identity
244 public void setTrust(OwnIdentity ownIdentity, Trust trust) {
245 synchronized (trustCache) {
246 trustCache.put(ownIdentity, trust);
258 public int hashCode() {
259 return id.hashCode();
266 public boolean equals(Object object) {
267 if (!(object instanceof DefaultIdentity)) {
270 DefaultIdentity identity = (DefaultIdentity) object;
271 return identity.id.equals(id);
278 public String toString() {
279 return getClass().getSimpleName() + "[id=" + id + ",nickname=" + nickname + ",contexts=" + contexts + ",properties=" + properties + "]";