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