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 ID of the identity. */
34 private final String id;
36 /** The nickname of the identity. */
37 private final String nickname;
39 /** The request URI of the identity. */
40 private final String requestUri;
42 /** The contexts of the identity. */
43 private final Set<String> contexts = Collections.synchronizedSet(new HashSet<String>());
45 /** The properties of the identity. */
46 private final Map<String, String> properties = Collections.synchronizedMap(new HashMap<String, String>());
49 * Creates a new identity.
52 * The ID of the identity
54 * The nickname of the identity
56 * The request URI of the identity
58 public Identity(String id, String nickname, String requestUri) {
60 this.nickname = nickname;
61 this.requestUri = requestUri;
69 * Returns the ID of the identity.
71 * @return The ID of the identity
73 public String getId() {
78 * Returns the nickname of the identity.
80 * @return The nickname of the identity
82 public String getNickname() {
87 * Returns the request URI of the identity.
89 * @return The request URI of the identity
91 public String getRequestUri() {
96 * Returns all contexts of this identity.
98 * @return All contexts of this identity
100 public Set<String> getContexts() {
101 return Collections.unmodifiableSet(contexts);
105 * Sets all contexts of this identity.
107 * This method is only called by the {@link IdentityManager}.
110 * All contexts of the identity
112 void setContexts(Set<String> contexts) {
113 this.contexts.clear();
114 this.contexts.addAll(contexts);
118 * Returns whether this identity has the given context.
121 * The context to check for
122 * @return {@code true} if this identity has the given context,
123 * {@code false} otherwise
125 public boolean hasContext(String context) {
126 return contexts.contains(context);
130 * Adds the given context to this identity.
132 * This method is only called by the {@link IdentityManager}.
137 void addContext(String context) {
138 contexts.add(context);
142 * Removes the given context from this identity.
144 * This method is only called by the {@link IdentityManager}.
147 * The context to remove
149 void removeContext(String context) {
150 contexts.remove(context);
154 * Returns all properties of this identity.
156 * @return All properties of this identity
158 public Map<String, String> getProperties() {
159 synchronized (properties) {
160 return Collections.unmodifiableMap(properties);
165 * Sets all properties of this identity.
167 * This method is only called by the {@link IdentityManager}.
170 * The new properties of this identity
172 void setProperties(Map<String, String> properties) {
173 synchronized (this.properties) {
174 this.properties.clear();
175 this.properties.putAll(properties);
180 * Sets the property with the given name to the given value.
182 * This method is only called by the {@link IdentityManager}.
185 * The name of the property
187 * The value of the property
189 void setProperty(String name, String value) {
190 synchronized (properties) {
191 properties.put(name, value);
196 * Returns the value of the property with the given name.
199 * The name of the property
200 * @return The value of the property
202 public String getProperty(String name) {
203 synchronized (properties) {
204 return properties.get(name);
209 * Removes the property with the given name.
211 * This method is only called by the {@link IdentityManager}.
214 * The name of the property to remove
216 void removeProperty(String name) {
217 synchronized (properties) {
218 properties.remove(name);
230 public int hashCode() {
231 return id.hashCode();
238 public boolean equals(Object object) {
239 if (!(object instanceof Identity)) {
242 Identity identity = (Identity) object;
243 return identity.id.equals(id);
250 public String toString() {
251 return getClass().getSimpleName() + "[id=" + id + ",nickname=" + nickname + ",contexts=" + contexts + ",properties=" + properties + "]";