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 /** The properties of the identity. */
49 private final Map<String, String> properties = Collections.synchronizedMap(new HashMap<String, String>());
52 * Creates a new identity.
54 * @param webOfTrustConnector
55 * The Web of Trust connector
57 * The ID of the identity
59 * The nickname of the identity
61 * The request URI of the identity
63 public Identity(WebOfTrustConnector webOfTrustConnector, String id, String nickname, String requestUri) {
64 this.webOfTrustConnector = webOfTrustConnector;
66 this.nickname = nickname;
67 this.requestUri = requestUri;
75 * Returns the ID of the identity.
77 * @return The ID of the identity
79 public String getId() {
84 * Returns the nickname of the identity.
86 * @return The nickname of the identity
88 public String getNickname() {
93 * Returns the request URI of the identity.
95 * @return The request URI of the identity
97 public String getRequestUri() {
102 * Returns the contexts of the identity.
104 * @return The contexts of the identity
105 * @throws PluginException
106 * if an error occured communicating with the Web of Trust
109 public Set<String> getContexts() throws PluginException {
110 return getContexts(false);
114 * Returns the contexts of the identity.
117 * {@code true} to force a reload of the contexts
118 * @return The contexts of the identity
119 * @throws PluginException
120 * if an error occured communicating with the Web of Trust
123 public Set<String> getContexts(boolean forceReload) throws PluginException {
124 if (contexts.isEmpty() || forceReload) {
125 Set<String> contexts = webOfTrustConnector.loadIdentityContexts(this);
126 this.contexts.clear();
127 this.contexts.addAll(contexts);
129 return Collections.unmodifiableSet(contexts);
133 * Returns whether the identity contains the given context.
136 * The context to check for
137 * @return {@code true} if this identity has the given context,
138 * {@code false} otherwise
140 public boolean hasContext(String context) {
141 return contexts.contains(context);
145 * Returns the properties of the identity.
147 * @return The properties of the identity
149 public Map<String, String> getProperties() {
150 return Collections.unmodifiableMap(properties);
154 * Returns the value of the property with the given name.
157 * The name of the property
158 * @return The value of the property, or {@code null} if there is no such
161 public String getProperty(String name) {
162 return properties.get(name);
166 * Sets the property with the given name to the given value.
169 * The name of the property to set
171 * The new value of the property
173 public void setProperty(String name, String value) {
174 properties.put(name, value);
175 /* TODO - set property. */
179 * Removes the property with the given name.
182 * The name of the property to remove
184 public void removeProperty(String name) {
185 properties.remove(name);
186 /* TODO - remove property. */
197 public int hashCode() {
198 return id.hashCode();
205 public boolean equals(Object object) {
206 if (!(object instanceof Identity)) {
209 Identity identity = (Identity) object;
210 return identity.id.equals(id);