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.freenet.wot;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.HashSet;
25 import java.util.Objects;
29 * A Web of Trust identity.
31 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
33 public class DefaultIdentity implements Identity {
35 private final String id;
36 private final String nickname;
37 private final String requestUri;
38 private final Set<String> contexts = Collections.synchronizedSet(new HashSet<>());
39 private final Map<String, String> properties = Collections.synchronizedMap(new HashMap<>());
40 private final Map<OwnIdentity, Trust> trustCache = Collections.synchronizedMap(new HashMap<>());
42 public DefaultIdentity(String id, String nickname, String requestUri) {
44 this.nickname = nickname;
45 this.requestUri = requestUri;
49 public String getId() {
54 public String getNickname() {
59 public String getRequestUri() {
64 public Set<String> getContexts() {
65 return Collections.unmodifiableSet(contexts);
69 public boolean hasContext(String context) {
70 return contexts.contains(context);
74 public void setContexts(Collection<String> contexts) {
75 this.contexts.clear();
76 this.contexts.addAll(contexts);
80 public Identity addContext(String context) {
81 contexts.add(context);
86 public Identity removeContext(String context) {
87 contexts.remove(context);
92 public Map<String, String> getProperties() {
93 return Collections.unmodifiableMap(properties);
97 public void setProperties(Map<String, String> properties) {
98 this.properties.clear();
99 this.properties.putAll(properties);
103 public String getProperty(String name) {
104 return properties.get(name);
108 public Identity setProperty(String name, String value) {
109 properties.put(name, value);
114 public Identity removeProperty(String name) {
115 properties.remove(name);
120 public Trust getTrust(OwnIdentity ownIdentity) {
121 return trustCache.get(ownIdentity);
125 public Identity setTrust(OwnIdentity ownIdentity, Trust trust) {
126 trustCache.put(ownIdentity, trust);
131 public Identity removeTrust(OwnIdentity ownIdentity) {
132 trustCache.remove(ownIdentity);
137 public int hashCode() {
138 return Objects.hash(getId());
142 public boolean equals(Object object) {
143 if (!(object instanceof Identity)) {
146 Identity identity = (Identity) object;
147 return Objects.equals(getId(), identity.getId());
151 public String toString() {
152 return getClass().getSimpleName() + "[id=" + id + ",nickname=" + nickname + ",contexts=" + contexts + ",properties=" + properties + "]";