Store contexts and properties in identities, allow modification only via IdentityManager.
[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 ID of the identity. */
34         private final String id;
35
36         /** The nickname of the identity. */
37         private final String nickname;
38
39         /** The request URI of the identity. */
40         private final String requestUri;
41
42         /** The contexts of the identity. */
43         private final Set<String> contexts = Collections.synchronizedSet(new HashSet<String>());
44
45         /** The properties of the identity. */
46         private final Map<String, String> properties = Collections.synchronizedMap(new HashMap<String, String>());
47
48         /**
49          * Creates a new identity.
50          *
51          * @param id
52          *            The ID of the identity
53          * @param nickname
54          *            The nickname of the identity
55          * @param requestUri
56          *            The request URI of the identity
57          */
58         public Identity(String id, String nickname, String requestUri) {
59                 this.id = id;
60                 this.nickname = nickname;
61                 this.requestUri = requestUri;
62         }
63
64         //
65         // ACCESSORS
66         //
67
68         /**
69          * Returns the ID of the identity.
70          *
71          * @return The ID of the identity
72          */
73         public String getId() {
74                 return id;
75         }
76
77         /**
78          * Returns the nickname of the identity.
79          *
80          * @return The nickname of the identity
81          */
82         public String getNickname() {
83                 return nickname;
84         }
85
86         /**
87          * Returns the request URI of the identity.
88          *
89          * @return The request URI of the identity
90          */
91         public String getRequestUri() {
92                 return requestUri;
93         }
94
95         /**
96          * Returns all contexts of this identity.
97          *
98          * @return All contexts of this identity
99          */
100         public Set<String> getContexts() {
101                 return Collections.unmodifiableSet(contexts);
102         }
103
104         /**
105          * Sets all contexts of this identity.
106          * <p>
107          * This method is only called by the {@link IdentityManager}.
108          *
109          * @param contexts
110          *            All contexts of the identity
111          */
112         void setContexts(Set<String> contexts) {
113                 this.contexts.clear();
114                 this.contexts.addAll(contexts);
115         }
116
117         /**
118          * Returns whether this identity has the given context.
119          *
120          * @param context
121          *            The context to check for
122          * @return {@code true} if this identity has the given context,
123          *         {@code false} otherwise
124          */
125         public boolean hasContext(String context) {
126                 return contexts.contains(context);
127         }
128
129         /**
130          * Adds the given context to this identity.
131          * <p>
132          * This method is only called by the {@link IdentityManager}.
133          *
134          * @param context
135          *            The context to add
136          */
137         void addContext(String context) {
138                 contexts.add(context);
139         }
140
141         /**
142          * Removes the given context from this identity.
143          * <p>
144          * This method is only called by the {@link IdentityManager}.
145          *
146          * @param context
147          *            The context to remove
148          */
149         void removeContext(String context) {
150                 contexts.remove(context);
151         }
152
153         /**
154          * Returns all properties of this identity.
155          *
156          * @return All properties of this identity
157          */
158         public Map<String, String> getProperties() {
159                 synchronized (properties) {
160                         return Collections.unmodifiableMap(properties);
161                 }
162         }
163
164         /**
165          * Sets all properties of this identity.
166          * <p>
167          * This method is only called by the {@link IdentityManager}.
168          *
169          * @param properties
170          *            The new properties of this identity
171          */
172         void setProperties(Map<String, String> properties) {
173                 synchronized (this.properties) {
174                         this.properties.clear();
175                         this.properties.putAll(properties);
176                 }
177         }
178
179         /**
180          * Sets the property with the given name to the given value.
181          * <p>
182          * This method is only called by the {@link IdentityManager}.
183          *
184          * @param name
185          *            The name of the property
186          * @param value
187          *            The value of the property
188          */
189         void setProperty(String name, String value) {
190                 synchronized (properties) {
191                         properties.put(name, value);
192                 }
193         }
194
195         /**
196          * Returns the value of the property with the given name.
197          *
198          * @param name
199          *            The name of the property
200          * @return The value of the property
201          */
202         public String getProperty(String name) {
203                 synchronized (properties) {
204                         return properties.get(name);
205                 }
206         }
207
208         /**
209          * Removes the property with the given name.
210          * <p>
211          * This method is only called by the {@link IdentityManager}.
212          *
213          * @param name
214          *            The name of the property to remove
215          */
216         void removeProperty(String name) {
217                 synchronized (properties) {
218                         properties.remove(name);
219                 }
220         }
221
222         //
223         // OBJECT METHODS
224         //
225
226         /**
227          * {@inheritDoc}
228          */
229         @Override
230         public int hashCode() {
231                 return id.hashCode();
232         }
233
234         /**
235          * {@inheritDoc}
236          */
237         @Override
238         public boolean equals(Object object) {
239                 if (!(object instanceof Identity)) {
240                         return false;
241                 }
242                 Identity identity = (Identity) object;
243                 return identity.id.equals(id);
244         }
245
246         /**
247          * {@inheritDoc}
248          */
249         @Override
250         public String toString() {
251                 return getClass().getSimpleName() + "[id=" + id + ",nickname=" + nickname + ",contexts=" + contexts + ",properties=" + properties + "]";
252         }
253
254 }