Always at least try to load the contexts before using the empty set.
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / wot / Identity.java
1 /*
2  * Sone - Identity.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 /**
27  * A Web of Trust identity.
28  *
29  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
30  */
31 public class Identity {
32
33         /** The Web of Trust connector. */
34         protected final WebOfTrustConnector webOfTrustConnector;
35
36         /** The ID of the identity. */
37         private final String id;
38
39         /** The nickname of the identity. */
40         private final String nickname;
41
42         /** The request URI of the identity. */
43         private final String requestUri;
44
45         /** The contexts of the identity. */
46         protected final Set<String> contexts = Collections.synchronizedSet(new HashSet<String>());
47
48         /** Whether the contexts have already been loaded. */
49         private volatile boolean contextsLoaded = false;
50
51         /** The properties of the identity. */
52         private final Map<String, String> properties = Collections.synchronizedMap(new HashMap<String, String>());
53
54         /**
55          * Creates a new identity.
56          *
57          * @param webOfTrustConnector
58          *            The Web of Trust connector
59          * @param id
60          *            The ID of the identity
61          * @param nickname
62          *            The nickname of the identity
63          * @param requestUri
64          *            The request URI of the identity
65          */
66         public Identity(WebOfTrustConnector webOfTrustConnector, String id, String nickname, String requestUri) {
67                 this.webOfTrustConnector = webOfTrustConnector;
68                 this.id = id;
69                 this.nickname = nickname;
70                 this.requestUri = requestUri;
71         }
72
73         //
74         // ACCESSORS
75         //
76
77         /**
78          * Returns the ID of the identity.
79          *
80          * @return The ID of the identity
81          */
82         public String getId() {
83                 return id;
84         }
85
86         /**
87          * Returns the nickname of the identity.
88          *
89          * @return The nickname of the identity
90          */
91         public String getNickname() {
92                 return nickname;
93         }
94
95         /**
96          * Returns the request URI of the identity.
97          *
98          * @return The request URI of the identity
99          */
100         public String getRequestUri() {
101                 return requestUri;
102         }
103
104         /**
105          * Returns the contexts of the identity. If the contexts have not been
106          * loaded yet, they will be loaded. If loading the contexts fails, an empty
107          * set is returned.
108          *
109          * @return The contexts of the identity
110          */
111         public Set<String> getContexts() {
112                 try {
113                         return getContexts(false);
114                 } catch (PluginException pe1) {
115                         return Collections.emptySet();
116                 }
117         }
118
119         /**
120          * Returns the contexts of the identity.
121          *
122          * @param forceReload
123          *            {@code true} to force a reload of the contexts
124          * @return The contexts of the identity
125          * @throws PluginException
126          *             if an error occured communicating with the Web of Trust
127          *             plugin
128          */
129         public Set<String> getContexts(boolean forceReload) throws PluginException {
130                 if (!contextsLoaded || forceReload) {
131                         Set<String> contexts = webOfTrustConnector.loadIdentityContexts(this);
132                         contextsLoaded = true;
133                         this.contexts.clear();
134                         this.contexts.addAll(contexts);
135                 }
136                 return Collections.unmodifiableSet(contexts);
137         }
138
139         /**
140          * Returns whether the identity contains the given context.
141          *
142          * @param context
143          *            The context to check for
144          * @return {@code true} if this identity has the given context,
145          *         {@code false} otherwise
146          */
147         public boolean hasContext(String context) {
148                 return getContexts().contains(context);
149         }
150
151         /**
152          * Returns the properties of the identity.
153          *
154          * @return The properties of the identity
155          */
156         public Map<String, String> getProperties() {
157                 return Collections.unmodifiableMap(properties);
158         }
159
160         /**
161          * Returns the value of the property with the given name.
162          *
163          * @param name
164          *            The name of the property
165          * @return The value of the property, or {@code null} if there is no such
166          *         property
167          */
168         public String getProperty(String name) {
169                 return properties.get(name);
170         }
171
172         /**
173          * Sets the property with the given name to the given value.
174          *
175          * @param name
176          *            The name of the property to set
177          * @param value
178          *            The new value of the property
179          */
180         public void setProperty(String name, String value) {
181                 properties.put(name, value);
182                 /* TODO - set property. */
183         }
184
185         /**
186          * Removes the property with the given name.
187          *
188          * @param name
189          *            The name of the property to remove
190          */
191         public void removeProperty(String name) {
192                 properties.remove(name);
193                 /* TODO - remove property. */
194         }
195
196         //
197         // OBJECT METHODS
198         //
199
200         /**
201          * {@inheritDoc}
202          */
203         @Override
204         public int hashCode() {
205                 return id.hashCode();
206         }
207
208         /**
209          * {@inheritDoc}
210          */
211         @Override
212         public boolean equals(Object object) {
213                 if (!(object instanceof Identity)) {
214                         return false;
215                 }
216                 Identity identity = (Identity) object;
217                 return identity.id.equals(id);
218         }
219
220 }