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