Implement Identity.getContexts().
[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         /** The properties of the identity. */
49         private final Map<String, String> properties = Collections.synchronizedMap(new HashMap<String, String>());
50
51         /**
52          * Creates a new identity.
53          *
54          * @param webOfTrustConnector
55          *            The Web of Trust connector
56          * @param id
57          *            The ID of the identity
58          * @param nickname
59          *            The nickname of the identity
60          * @param requestUri
61          *            The request URI of the identity
62          */
63         public Identity(WebOfTrustConnector webOfTrustConnector, String id, String nickname, String requestUri) {
64                 this.webOfTrustConnector = webOfTrustConnector;
65                 this.id = id;
66                 this.nickname = nickname;
67                 this.requestUri = requestUri;
68         }
69
70         //
71         // ACCESSORS
72         //
73
74         /**
75          * Returns the ID of the identity.
76          *
77          * @return The ID of the identity
78          */
79         public String getId() {
80                 return id;
81         }
82
83         /**
84          * Returns the nickname of the identity.
85          *
86          * @return The nickname of the identity
87          */
88         public String getNickname() {
89                 return nickname;
90         }
91
92         /**
93          * Returns the request URI of the identity.
94          *
95          * @return The request URI of the identity
96          */
97         public String getRequestUri() {
98                 return requestUri;
99         }
100
101         /**
102          * Returns the contexts of the identity.
103          *
104          * @return The contexts of the identity
105          * @throws PluginException
106          *             if an error occured communicating with the Web of Trust
107          *             plugin
108          */
109         public Set<String> getContexts() throws PluginException {
110                 return getContexts(false);
111         }
112
113         /**
114          * Returns the contexts of the identity.
115          *
116          * @param forceReload
117          *            {@code true} to force a reload of the contexts
118          * @return The contexts of the identity
119          * @throws PluginException
120          *             if an error occured communicating with the Web of Trust
121          *             plugin
122          */
123         public Set<String> getContexts(boolean forceReload) throws PluginException {
124                 if (contexts.isEmpty() || forceReload) {
125                         Set<String> contexts = webOfTrustConnector.loadIdentityContexts(this);
126                         this.contexts.clear();
127                         this.contexts.addAll(contexts);
128                 }
129                 return Collections.unmodifiableSet(contexts);
130         }
131
132         /**
133          * Returns whether the identity contains the given context.
134          *
135          * @param context
136          *            The context to check for
137          * @return {@code true} if this identity has the given context,
138          *         {@code false} otherwise
139          */
140         public boolean hasContext(String context) {
141                 return contexts.contains(context);
142         }
143
144         /**
145          * Returns the properties of the identity.
146          *
147          * @return The properties of the identity
148          */
149         public Map<String, String> getProperties() {
150                 return Collections.unmodifiableMap(properties);
151         }
152
153         /**
154          * Returns the value of the property with the given name.
155          *
156          * @param name
157          *            The name of the property
158          * @return The value of the property, or {@code null} if there is no such
159          *         property
160          */
161         public String getProperty(String name) {
162                 return properties.get(name);
163         }
164
165         /**
166          * Sets the property with the given name to the given value.
167          *
168          * @param name
169          *            The name of the property to set
170          * @param value
171          *            The new value of the property
172          */
173         public void setProperty(String name, String value) {
174                 properties.put(name, value);
175                 /* TODO - set property. */
176         }
177
178         /**
179          * Removes the property with the given name.
180          *
181          * @param name
182          *            The name of the property to remove
183          */
184         public void removeProperty(String name) {
185                 properties.remove(name);
186                 /* TODO - remove property. */
187         }
188
189         //
190         // OBJECT METHODS
191         //
192
193         /**
194          * {@inheritDoc}
195          */
196         @Override
197         public int hashCode() {
198                 return id.hashCode();
199         }
200
201         /**
202          * {@inheritDoc}
203          */
204         @Override
205         public boolean equals(Object object) {
206                 if (!(object instanceof Identity)) {
207                         return false;
208                 }
209                 Identity identity = (Identity) object;
210                 return identity.id.equals(id);
211         }
212
213 }