2 * Sone - DefaultIdentity.java - Copyright © 2010–2016 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;
73 public String getId() {
78 public String getNickname() {
83 public String getRequestUri() {
88 public Set<String> getContexts() {
89 return Collections.unmodifiableSet(contexts);
93 public boolean hasContext(String context) {
94 return contexts.contains(context);
98 public void setContexts(Collection<String> contexts) {
99 this.contexts.clear();
100 this.contexts.addAll(contexts);
104 public Identity addContext(String context) {
105 contexts.add(context);
110 public Identity removeContext(String context) {
111 contexts.remove(context);
116 public Map<String, String> getProperties() {
117 return Collections.unmodifiableMap(properties);
121 public void setProperties(Map<String, String> properties) {
122 this.properties.clear();
123 this.properties.putAll(properties);
127 public String getProperty(String name) {
128 return properties.get(name);
132 public Identity setProperty(String name, String value) {
133 properties.put(name, value);
138 public Identity removeProperty(String name) {
139 properties.remove(name);
144 public Trust getTrust(OwnIdentity ownIdentity) {
145 return trustCache.get(ownIdentity);
149 public Identity setTrust(OwnIdentity ownIdentity, Trust trust) {
150 trustCache.put(ownIdentity, trust);
155 public Identity removeTrust(OwnIdentity ownIdentity) {
156 trustCache.remove(ownIdentity);
165 public int hashCode() {
166 return getId().hashCode();
170 public boolean equals(Object object) {
171 if (!(object instanceof Identity)) {
174 Identity identity = (Identity) object;
175 return identity.getId().equals(getId());
179 public String toString() {
180 return getClass().getSimpleName() + "[id=" + id + ",nickname=" + nickname + ",contexts=" + contexts + ",properties=" + properties + "]";