2 * Sone - Identity.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;
27 * A Web of Trust identity.
29 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
31 public class Identity {
33 /** The Web of Trust connector. */
34 protected final WebOfTrustConnector webOfTrustConnector;
36 /** The ID of the identity. */
37 private final String id;
39 /** The nickname of the identity. */
40 private final String nickname;
42 /** The request URI of the identity. */
43 private final String requestUri;
45 /** The contexts of the identity. */
46 protected final Set<String> contexts = Collections.synchronizedSet(new HashSet<String>());
48 /** Whether the contexts have already been loaded. */
49 private volatile boolean contextsLoaded = false;
51 /** The properties of the identity. */
52 private final Map<String, String> properties = Collections.synchronizedMap(new HashMap<String, String>());
55 * Creates a new identity.
57 * @param webOfTrustConnector
58 * The Web of Trust connector
60 * The ID of the identity
62 * The nickname of the identity
64 * The request URI of the identity
66 public Identity(WebOfTrustConnector webOfTrustConnector, String id, String nickname, String requestUri) {
67 this.webOfTrustConnector = webOfTrustConnector;
69 this.nickname = nickname;
70 this.requestUri = requestUri;
78 * Returns the ID of the identity.
80 * @return The ID of the identity
82 public String getId() {
87 * Returns the nickname of the identity.
89 * @return The nickname of the identity
91 public String getNickname() {
96 * Returns the request URI of the identity.
98 * @return The request URI of the identity
100 public String getRequestUri() {
105 * Returns the contexts of the identity.
107 * @return The contexts of the identity
108 * @throws PluginException
109 * if an error occured communicating with the Web of Trust
112 public Set<String> getContexts() throws PluginException {
113 return getContexts(false);
117 * Returns the contexts of the identity.
120 * {@code true} to force a reload of the contexts
121 * @return The contexts of the identity
122 * @throws PluginException
123 * if an error occured communicating with the Web of Trust
126 public Set<String> getContexts(boolean forceReload) throws PluginException {
127 if (!contextsLoaded || forceReload) {
128 Set<String> contexts = webOfTrustConnector.loadIdentityContexts(this);
129 contextsLoaded = true;
130 this.contexts.clear();
131 this.contexts.addAll(contexts);
133 return Collections.unmodifiableSet(contexts);
137 * Returns whether the identity contains the given context.
140 * The context to check for
141 * @return {@code true} if this identity has the given context,
142 * {@code false} otherwise
144 public boolean hasContext(String context) {
145 return contexts.contains(context);
149 * Returns the properties of the identity.
151 * @return The properties of the identity
153 public Map<String, String> getProperties() {
154 return Collections.unmodifiableMap(properties);
158 * Returns the value of the property with the given name.
161 * The name of the property
162 * @return The value of the property, or {@code null} if there is no such
165 public String getProperty(String name) {
166 return properties.get(name);
170 * Sets the property with the given name to the given value.
173 * The name of the property to set
175 * The new value of the property
177 public void setProperty(String name, String value) {
178 properties.put(name, value);
179 /* TODO - set property. */
183 * Removes the property with the given name.
186 * The name of the property to remove
188 public void removeProperty(String name) {
189 properties.remove(name);
190 /* TODO - remove property. */
201 public int hashCode() {
202 return id.hashCode();
209 public boolean equals(Object object) {
210 if (!(object instanceof Identity)) {
213 Identity identity = (Identity) object;
214 return identity.id.equals(id);