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 protected 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. If the contexts have not been
106 * loaded yet, they will be loaded. If loading the contexts fails, an empty
109 * @return The contexts of the identity
111 public Set<String> getContexts() {
113 return getContexts(false);
114 } catch (PluginException pe1) {
115 return Collections.emptySet();
120 * Returns the contexts of the identity.
123 * {@code true} to force a reload of the contexts
124 * @return The contexts of the identity
125 * @throws PluginException
126 * if an error occured communicating with the Web of Trust
129 public Set<String> getContexts(boolean forceReload) throws PluginException {
130 if (!contextsLoaded || forceReload) {
131 Set<String> contexts = webOfTrustConnector.loadIdentityContexts(this);
132 contextsLoaded = true;
133 this.contexts.clear();
134 this.contexts.addAll(contexts);
136 return Collections.unmodifiableSet(contexts);
140 * Returns whether the identity contains the given context.
143 * The context to check for
144 * @return {@code true} if this identity has the given context,
145 * {@code false} otherwise
147 public boolean hasContext(String context) {
148 return getContexts().contains(context);
152 * Returns the properties of the identity.
154 * @return The properties of the identity
156 public Map<String, String> getProperties() {
157 return Collections.unmodifiableMap(properties);
161 * Returns the value of the property with the given name.
164 * The name of the property
165 * @return The value of the property, or {@code null} if there is no such
168 public String getProperty(String name) {
169 return properties.get(name);
180 public int hashCode() {
181 return id.hashCode();
188 public boolean equals(Object object) {
189 if (!(object instanceof Identity)) {
192 Identity identity = (Identity) object;
193 return identity.id.equals(id);