2 * Sone - DefaultIdentity.java - Copyright © 2010–2013 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.Collection;
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.HashSet;
28 * A Web of Trust identity.
30 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
32 public class DefaultIdentity implements Identity {
34 /** The ID of the identity. */
35 private final String id;
37 /** The nickname of the identity. */
38 private final String nickname;
40 /** The request URI of the identity. */
41 private final String requestUri;
43 /** The contexts of the identity. */
44 private final Set<String> contexts = Collections.synchronizedSet(new HashSet<String>());
46 /** The properties of the identity. */
47 private final Map<String, String> properties = Collections.synchronizedMap(new HashMap<String, String>());
50 private final Map<OwnIdentity, Trust> trustCache = Collections.synchronizedMap(new HashMap<OwnIdentity, Trust>());
53 * Creates a new identity.
56 * The ID of the identity
58 * The nickname of the identity
60 * The request URI of the identity
62 public DefaultIdentity(String id, String nickname, String requestUri) {
64 this.nickname = nickname;
65 this.requestUri = requestUri;
76 public String getId() {
84 public String getNickname() {
92 public String getRequestUri() {
100 public Set<String> getContexts() {
101 return Collections.unmodifiableSet(contexts);
108 public boolean hasContext(String context) {
109 return contexts.contains(context);
116 public void setContexts(Collection<String> contexts) {
117 this.contexts.clear();
118 this.contexts.addAll(contexts);
125 public void addContext(String context) {
126 contexts.add(context);
133 public void removeContext(String context) {
134 contexts.remove(context);
141 public Map<String, String> getProperties() {
142 return Collections.unmodifiableMap(properties);
149 public void setProperties(Map<String, String> properties) {
150 this.properties.clear();
151 this.properties.putAll(properties);
158 public String getProperty(String name) {
159 return properties.get(name);
166 public void setProperty(String name, String value) {
167 properties.put(name, value);
174 public void removeProperty(String name) {
175 properties.remove(name);
182 public Trust getTrust(OwnIdentity ownIdentity) {
183 return trustCache.get(ownIdentity);
190 public void setTrust(OwnIdentity ownIdentity, Trust trust) {
191 trustCache.put(ownIdentity, trust);
198 public void removeTrust(OwnIdentity ownIdentity) {
199 trustCache.remove(ownIdentity);
210 public int hashCode() {
211 return getId().hashCode();
218 public boolean equals(Object object) {
219 if (!(object instanceof Identity)) {
222 Identity identity = (Identity) object;
223 return identity.getId().equals(getId());
230 public String toString() {
231 return getClass().getSimpleName() + "[id=" + id + ",nickname=" + nickname + ",contexts=" + contexts + ",properties=" + properties + "]";