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