2 * Sone - DefaultIdentity.java - Copyright © 2010–2019 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 public class DefaultIdentity implements Identity {
32 /** The ID of the identity. */
33 private final String id;
35 /** The nickname of the identity. */
36 private final String nickname;
38 /** The request URI of the identity. */
39 private final String requestUri;
41 /** The contexts of the identity. */
42 private final Set<String> contexts = Collections.synchronizedSet(new HashSet<String>());
44 /** The properties of the identity. */
45 private final Map<String, String> properties = Collections.synchronizedMap(new HashMap<String, String>());
48 private final Map<OwnIdentity, Trust> trustCache = Collections.synchronizedMap(new HashMap<OwnIdentity, Trust>());
51 * Creates a new identity.
54 * The ID of the identity
56 * The nickname of the identity
58 * The request URI of the identity
60 public DefaultIdentity(String id, String nickname, String requestUri) {
62 this.nickname = nickname;
63 this.requestUri = requestUri;
71 public String getId() {
76 public String getNickname() {
81 public String getRequestUri() {
86 public Set<String> getContexts() {
87 return Collections.unmodifiableSet(contexts);
91 public boolean hasContext(String context) {
92 return contexts.contains(context);
96 public void setContexts(Collection<String> contexts) {
97 this.contexts.clear();
98 this.contexts.addAll(contexts);
102 public Identity addContext(String context) {
103 contexts.add(context);
108 public Identity removeContext(String context) {
109 contexts.remove(context);
114 public Map<String, String> getProperties() {
115 return Collections.unmodifiableMap(properties);
119 public void setProperties(Map<String, String> properties) {
120 this.properties.clear();
121 this.properties.putAll(properties);
125 public String getProperty(String name) {
126 return properties.get(name);
130 public Identity setProperty(String name, String value) {
131 properties.put(name, value);
136 public Identity removeProperty(String name) {
137 properties.remove(name);
142 public Trust getTrust(OwnIdentity ownIdentity) {
143 return trustCache.get(ownIdentity);
147 public Identity setTrust(OwnIdentity ownIdentity, Trust trust) {
148 trustCache.put(ownIdentity, trust);
153 public Identity removeTrust(OwnIdentity ownIdentity) {
154 trustCache.remove(ownIdentity);
163 public int hashCode() {
164 return getId().hashCode();
168 public boolean equals(Object object) {
169 if (!(object instanceof Identity)) {
172 Identity identity = (Identity) object;
173 return identity.getId().equals(getId());
177 public String toString() {
178 return getClass().getSimpleName() + "[id=" + id + ",nickname=" + nickname + ",contexts=" + contexts + ",properties=" + properties + "]";