2 * Sone - DefaultIdentity.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 DefaultIdentity implements 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 DefaultIdentity(String id, String nickname, String requestUri) {
60 this.nickname = nickname;
61 this.requestUri = requestUri;
72 public String getId() {
80 public String getNickname() {
88 public String getRequestUri() {
96 public Set<String> getContexts() {
97 return Collections.unmodifiableSet(contexts);
101 * Sets the contexts of this identity.
103 * This method is only called by the {@link IdentityManager}.
106 * The contexts to set
108 void setContextsPrivate(Set<String> contexts) {
109 this.contexts.clear();
110 this.contexts.addAll(contexts);
117 public boolean hasContext(String context) {
118 return contexts.contains(context);
122 * Adds the given context to this identity.
124 * This method is only called by the {@link IdentityManager}.
129 void addContextPrivate(String context) {
130 contexts.add(context);
134 * Removes the given context from this identity.
136 * This method is only called by the {@link IdentityManager}.
139 * The context to remove
141 public void removeContextPrivate(String context) {
142 contexts.remove(context);
149 public Map<String, String> getProperties() {
150 synchronized (properties) {
151 return Collections.unmodifiableMap(properties);
156 * Sets all properties of this identity.
158 * This method is only called by the {@link IdentityManager}.
161 * The new properties of this identity
163 void setPropertiesPrivate(Map<String, String> properties) {
164 synchronized (this.properties) {
165 this.properties.clear();
166 this.properties.putAll(properties);
171 * Sets the property with the given name to the given value.
173 * This method is only called by the {@link IdentityManager}.
176 * The name of the property
178 * The value of the property
180 void setPropertyPrivate(String name, String value) {
181 synchronized (properties) {
182 properties.put(name, value);
190 public String getProperty(String name) {
191 synchronized (properties) {
192 return properties.get(name);
197 * Removes the property with the given name.
199 * This method is only called by the {@link IdentityManager}.
202 * The name of the property to remove
204 void removePropertyPrivate(String name) {
205 synchronized (properties) {
206 properties.remove(name);
218 public int hashCode() {
219 return id.hashCode();
226 public boolean equals(Object object) {
227 if (!(object instanceof DefaultIdentity)) {
230 DefaultIdentity identity = (DefaultIdentity) object;
231 return identity.id.equals(id);
238 public String toString() {
239 return getClass().getSimpleName() + "[id=" + id + ",nickname=" + nickname + ",contexts=" + contexts + ",properties=" + properties + "]";