Use marker variable for whether the contexts have been loaded.
[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.
106          *
107          * @return The contexts of the identity
108          * @throws PluginException
109          *             if an error occured communicating with the Web of Trust
110          *             plugin
111          */
112         public Set<String> getContexts() throws PluginException {
113                 return getContexts(false);
114         }
115
116         /**
117          * Returns the contexts of the identity.
118          *
119          * @param forceReload
120          *            {@code true} to force a reload of the contexts
121          * @return The contexts of the identity
122          * @throws PluginException
123          *             if an error occured communicating with the Web of Trust
124          *             plugin
125          */
126         public Set<String> getContexts(boolean forceReload) throws PluginException {
127                 if (!contextsLoaded || forceReload) {
128                         Set<String> contexts = webOfTrustConnector.loadIdentityContexts(this);
129                         contextsLoaded = true;
130                         this.contexts.clear();
131                         this.contexts.addAll(contexts);
132                 }
133                 return Collections.unmodifiableSet(contexts);
134         }
135
136         /**
137          * Returns whether the identity contains the given context.
138          *
139          * @param context
140          *            The context to check for
141          * @return {@code true} if this identity has the given context,
142          *         {@code false} otherwise
143          */
144         public boolean hasContext(String context) {
145                 return contexts.contains(context);
146         }
147
148         /**
149          * Returns the properties of the identity.
150          *
151          * @return The properties of the identity
152          */
153         public Map<String, String> getProperties() {
154                 return Collections.unmodifiableMap(properties);
155         }
156
157         /**
158          * Returns the value of the property with the given name.
159          *
160          * @param name
161          *            The name of the property
162          * @return The value of the property, or {@code null} if there is no such
163          *         property
164          */
165         public String getProperty(String name) {
166                 return properties.get(name);
167         }
168
169         /**
170          * Sets the property with the given name to the given value.
171          *
172          * @param name
173          *            The name of the property to set
174          * @param value
175          *            The new value of the property
176          */
177         public void setProperty(String name, String value) {
178                 properties.put(name, value);
179                 /* TODO - set property. */
180         }
181
182         /**
183          * Removes the property with the given name.
184          *
185          * @param name
186          *            The name of the property to remove
187          */
188         public void removeProperty(String name) {
189                 properties.remove(name);
190                 /* TODO - remove property. */
191         }
192
193         //
194         // OBJECT METHODS
195         //
196
197         /**
198          * {@inheritDoc}
199          */
200         @Override
201         public int hashCode() {
202                 return id.hashCode();
203         }
204
205         /**
206          * {@inheritDoc}
207          */
208         @Override
209         public boolean equals(Object object) {
210                 if (!(object instanceof Identity)) {
211                         return false;
212                 }
213                 Identity identity = (Identity) object;
214                 return identity.id.equals(id);
215         }
216
217 }