Add Freenet plugin and WoT subprojects
[fwot.git] / wot / src / main / java / net / pterodactylus / freenet / wot / DefaultIdentity.java
1 /*
2  * Sone - DefaultIdentity.java - Copyright © 2010–2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.freenet.wot;
19
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.Map;
25 import java.util.Objects;
26 import java.util.Set;
27
28 /**
29  * A Web of Trust identity.
30  *
31  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
32  */
33 public class DefaultIdentity implements Identity {
34
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<>());
41
42         public DefaultIdentity(String id, String nickname, String requestUri) {
43                 this.id = id;
44                 this.nickname = nickname;
45                 this.requestUri = requestUri;
46         }
47
48         @Override
49         public String getId() {
50                 return id;
51         }
52
53         @Override
54         public String getNickname() {
55                 return nickname;
56         }
57
58         @Override
59         public String getRequestUri() {
60                 return requestUri;
61         }
62
63         @Override
64         public Set<String> getContexts() {
65                 return Collections.unmodifiableSet(contexts);
66         }
67
68         @Override
69         public boolean hasContext(String context) {
70                 return contexts.contains(context);
71         }
72
73         @Override
74         public void setContexts(Collection<String> contexts) {
75                 this.contexts.clear();
76                 this.contexts.addAll(contexts);
77         }
78
79         @Override
80         public Identity addContext(String context) {
81                 contexts.add(context);
82                 return this;
83         }
84
85         @Override
86         public Identity removeContext(String context) {
87                 contexts.remove(context);
88                 return this;
89         }
90
91         @Override
92         public Map<String, String> getProperties() {
93                 return Collections.unmodifiableMap(properties);
94         }
95
96         @Override
97         public void setProperties(Map<String, String> properties) {
98                 this.properties.clear();
99                 this.properties.putAll(properties);
100         }
101
102         @Override
103         public String getProperty(String name) {
104                 return properties.get(name);
105         }
106
107         @Override
108         public Identity setProperty(String name, String value) {
109                 properties.put(name, value);
110                 return this;
111         }
112
113         @Override
114         public Identity removeProperty(String name) {
115                 properties.remove(name);
116                 return this;
117         }
118
119         @Override
120         public Trust getTrust(OwnIdentity ownIdentity) {
121                 return trustCache.get(ownIdentity);
122         }
123
124         @Override
125         public Identity setTrust(OwnIdentity ownIdentity, Trust trust) {
126                 trustCache.put(ownIdentity, trust);
127                 return this;
128         }
129
130         @Override
131         public Identity removeTrust(OwnIdentity ownIdentity) {
132                 trustCache.remove(ownIdentity);
133                 return this;
134         }
135
136         @Override
137         public int hashCode() {
138                 return Objects.hash(getId());
139         }
140
141         @Override
142         public boolean equals(Object object) {
143                 if (!(object instanceof Identity)) {
144                         return false;
145                 }
146                 Identity identity = (Identity) object;
147                 return Objects.equals(getId(), identity.getId());
148         }
149
150         @Override
151         public String toString() {
152                 return getClass().getSimpleName() + "[id=" + id + ",nickname=" + nickname + ",contexts=" + contexts + ",properties=" + properties + "]";
153         }
154
155 }