Turn (Own)Identity into mere caches of values from the WebOfTrust.
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / wot / DefaultIdentity.java
1 /*
2  * Sone - DefaultIdentity.java - Copyright © 2010–2012 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.sone.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.Set;
26
27 /**
28  * A Web of Trust identity.
29  *
30  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
31  */
32 public class DefaultIdentity implements Identity {
33
34         /** The ID of the identity. */
35         private final String id;
36
37         /** The nickname of the identity. */
38         private final String nickname;
39
40         /** The request URI of the identity. */
41         private final String requestUri;
42
43         /** The contexts of the identity. */
44         private final Set<String> contexts = Collections.synchronizedSet(new HashSet<String>());
45
46         /** The properties of the identity. */
47         private final Map<String, String> properties = Collections.synchronizedMap(new HashMap<String, String>());
48
49         /** Cached trust. */
50         private final Map<OwnIdentity, Trust> trustCache = Collections.synchronizedMap(new HashMap<OwnIdentity, Trust>());
51
52         /**
53          * Creates a new identity.
54          *
55          * @param id
56          *            The ID of the identity
57          * @param nickname
58          *            The nickname of the identity
59          * @param requestUri
60          *            The request URI of the identity
61          */
62         public DefaultIdentity(String id, String nickname, String requestUri) {
63                 this.id = id;
64                 this.nickname = nickname;
65                 this.requestUri = requestUri;
66         }
67
68         //
69         // ACCESSORS
70         //
71
72         /**
73          * {@inheritDoc}
74          */
75         @Override
76         public String getId() {
77                 return id;
78         }
79
80         /**
81          * {@inheritDoc}
82          */
83         @Override
84         public String getNickname() {
85                 return nickname;
86         }
87
88         /**
89          * {@inheritDoc}
90          */
91         @Override
92         public String getRequestUri() {
93                 return requestUri;
94         }
95
96         /**
97          * {@inheritDoc}
98          */
99         @Override
100         public Set<String> getContexts() {
101                 return Collections.unmodifiableSet(contexts);
102         }
103
104         /**
105          * {@inheritDoc}
106          */
107         @Override
108         public boolean hasContext(String context) {
109                 return contexts.contains(context);
110         }
111
112         /**
113          * {@inheritDoc}
114          */
115         @Override
116         public void setContexts(Collection<String> contexts) {
117                 this.contexts.clear();
118                 this.contexts.addAll(contexts);
119         }
120
121         /**
122          * {@inheritDoc}
123          */
124         @Override
125         public void addContext(String context) {
126                 contexts.add(context);
127         }
128
129         /**
130          * {@inheritDoc}
131          */
132         @Override
133         public void removeContext(String context) {
134                 contexts.remove(context);
135         }
136
137         /**
138          * {@inheritDoc}
139          */
140         @Override
141         public Map<String, String> getProperties() {
142                 return Collections.unmodifiableMap(properties);
143         }
144
145         /**
146          * {@inheritDoc}
147          */
148         @Override
149         public void setProperties(Map<String, String> properties) {
150                 this.properties.clear();
151                 this.properties.putAll(properties);
152         }
153
154         /**
155          * {@inheritDoc}
156          */
157         @Override
158         public String getProperty(String name) {
159                 return properties.get(name);
160         }
161
162         /**
163          * {@inheritDoc}
164          */
165         @Override
166         public void setProperty(String name, String value) {
167                 properties.put(name, value);
168         }
169
170         /**
171          * {@inheritDoc}
172          */
173         @Override
174         public void removeProperty(String name) {
175                 properties.remove(name);
176         }
177
178         /**
179          * {@inheritDoc}
180          */
181         @Override
182         public Trust getTrust(OwnIdentity ownIdentity) {
183                 return trustCache.get(ownIdentity);
184         }
185
186         /**
187          * {@inheritDoc}
188          */
189         @Override
190         public void setTrust(OwnIdentity ownIdentity, Trust trust) {
191                 trustCache.put(ownIdentity, trust);
192         }
193
194         /**
195          * {@inheritDoc}
196          */
197         @Override
198         public void removeTrust(OwnIdentity ownIdentity) {
199                 trustCache.remove(ownIdentity);
200         }
201
202         //
203         // OBJECT METHODS
204         //
205
206         /**
207          * {@inheritDoc}
208          */
209         @Override
210         public int hashCode() {
211                 return id.hashCode();
212         }
213
214         /**
215          * {@inheritDoc}
216          */
217         @Override
218         public boolean equals(Object object) {
219                 if (!(object instanceof DefaultIdentity)) {
220                         return false;
221                 }
222                 DefaultIdentity identity = (DefaultIdentity) object;
223                 return identity.id.equals(id);
224         }
225
226         /**
227          * {@inheritDoc}
228          */
229         @Override
230         public String toString() {
231                 return getClass().getSimpleName() + "[id=" + id + ",nickname=" + nickname + ",contexts=" + contexts + ",properties=" + properties + "]";
232         }
233
234 }