f200022b78b4f11895cb7e36e509230a1a90fa24
[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.Collections;
21 import java.util.HashMap;
22 import java.util.HashSet;
23 import java.util.Map;
24 import java.util.Set;
25 import java.util.logging.Logger;
26
27 import net.pterodactylus.util.logging.Logging;
28
29 /**
30  * A Web of Trust identity.
31  *
32  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
33  */
34 public class DefaultIdentity implements Identity {
35
36         /** The logger. */
37         private static final Logger logger = Logging.getLogger(DefaultIdentity.class);
38
39         /** The web of trust connector. */
40         private final WebOfTrustConnector webOfTrustConnector;
41
42         /** The ID of the identity. */
43         private final String id;
44
45         /** The nickname of the identity. */
46         private final String nickname;
47
48         /** The request URI of the identity. */
49         private final String requestUri;
50
51         /** The contexts of the identity. */
52         private final Set<String> contexts = Collections.synchronizedSet(new HashSet<String>());
53
54         /** The properties of the identity. */
55         private final Map<String, String> properties = Collections.synchronizedMap(new HashMap<String, String>());
56
57         /** Cached trust. */
58         /* synchronize on itself. */
59         private final Map<OwnIdentity, Trust> trustCache = new HashMap<OwnIdentity, Trust>();
60
61         /**
62          * Creates a new identity.
63          *
64          * @param webOfTrustConnector
65          *            The web of trust connector
66          * @param id
67          *            The ID of the identity
68          * @param nickname
69          *            The nickname of the identity
70          * @param requestUri
71          *            The request URI of the identity
72          */
73         public DefaultIdentity(WebOfTrustConnector webOfTrustConnector, String id, String nickname, String requestUri) {
74                 this.webOfTrustConnector = webOfTrustConnector;
75                 this.id = id;
76                 this.nickname = nickname;
77                 this.requestUri = requestUri;
78         }
79
80         //
81         // ACCESSORS
82         //
83
84         /**
85          * {@inheritDoc}
86          */
87         @Override
88         public String getId() {
89                 return id;
90         }
91
92         /**
93          * {@inheritDoc}
94          */
95         @Override
96         public String getNickname() {
97                 return nickname;
98         }
99
100         /**
101          * {@inheritDoc}
102          */
103         @Override
104         public String getRequestUri() {
105                 return requestUri;
106         }
107
108         /**
109          * {@inheritDoc}
110          */
111         @Override
112         public Set<String> getContexts() {
113                 return Collections.unmodifiableSet(contexts);
114         }
115
116         /**
117          * Sets the contexts of this identity.
118          * <p>
119          * This method is only called by the {@link IdentityManager}.
120          *
121          * @param contexts
122          *            The contexts to set
123          */
124         void setContextsPrivate(Set<String> contexts) {
125                 this.contexts.clear();
126                 this.contexts.addAll(contexts);
127         }
128
129         /**
130          * {@inheritDoc}
131          */
132         @Override
133         public boolean hasContext(String context) {
134                 return contexts.contains(context);
135         }
136
137         /**
138          * Adds the given context to this identity.
139          * <p>
140          * This method is only called by the {@link IdentityManager}.
141          *
142          * @param context
143          *            The context to add
144          */
145         void addContextPrivate(String context) {
146                 contexts.add(context);
147         }
148
149         /**
150          * Removes the given context from this identity.
151          * <p>
152          * This method is only called by the {@link IdentityManager}.
153          *
154          * @param context
155          *            The context to remove
156          */
157         public void removeContextPrivate(String context) {
158                 contexts.remove(context);
159         }
160
161         /**
162          * {@inheritDoc}
163          */
164         @Override
165         public Map<String, String> getProperties() {
166                 synchronized (properties) {
167                         return Collections.unmodifiableMap(properties);
168                 }
169         }
170
171         /**
172          * Sets all properties of this identity.
173          * <p>
174          * This method is only called by the {@link IdentityManager}.
175          *
176          * @param properties
177          *            The new properties of this identity
178          */
179         void setPropertiesPrivate(Map<String, String> properties) {
180                 synchronized (this.properties) {
181                         this.properties.clear();
182                         this.properties.putAll(properties);
183                 }
184         }
185
186         /**
187          * Sets the property with the given name to the given value.
188          * <p>
189          * This method is only called by the {@link IdentityManager}.
190          *
191          * @param name
192          *            The name of the property
193          * @param value
194          *            The value of the property
195          */
196         void setPropertyPrivate(String name, String value) {
197                 synchronized (properties) {
198                         properties.put(name, value);
199                 }
200         }
201
202         /**
203          * {@inheritDoc}
204          */
205         @Override
206         public String getProperty(String name) {
207                 synchronized (properties) {
208                         return properties.get(name);
209                 }
210         }
211
212         /**
213          * Removes the property with the given name.
214          * <p>
215          * This method is only called by the {@link IdentityManager}.
216          *
217          * @param name
218          *            The name of the property to remove
219          */
220         void removePropertyPrivate(String name) {
221                 synchronized (properties) {
222                         properties.remove(name);
223                 }
224         }
225
226         /**
227          * {@inheritDoc}
228          */
229         @Override
230         public Trust getTrust(OwnIdentity ownIdentity) {
231                 synchronized (trustCache) {
232                         return trustCache.get(ownIdentity);
233                 }
234         }
235
236         /**
237          * Sets the trust received for this identity by the given own identity.
238          *
239          * @param ownIdentity
240          *            The own identity that gives the trust
241          * @param trust
242          *            The trust received for this identity
243          */
244         public void setTrust(OwnIdentity ownIdentity, Trust trust) {
245                 synchronized (trustCache) {
246                         trustCache.put(ownIdentity, trust);
247                 }
248         }
249
250         //
251         // OBJECT METHODS
252         //
253
254         /**
255          * {@inheritDoc}
256          */
257         @Override
258         public int hashCode() {
259                 return id.hashCode();
260         }
261
262         /**
263          * {@inheritDoc}
264          */
265         @Override
266         public boolean equals(Object object) {
267                 if (!(object instanceof DefaultIdentity)) {
268                         return false;
269                 }
270                 DefaultIdentity identity = (DefaultIdentity) object;
271                 return identity.id.equals(id);
272         }
273
274         /**
275          * {@inheritDoc}
276          */
277         @Override
278         public String toString() {
279                 return getClass().getSimpleName() + "[id=" + id + ",nickname=" + nickname + ",contexts=" + contexts + ",properties=" + properties + "]";
280         }
281
282 }