Extract Identity interfaces, move functionality to identity classes.
[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 /**
27  * A Web of Trust identity.
28  *
29  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
30  */
31 public class DefaultIdentity implements 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 DefaultIdentity(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          * {@inheritDoc}
70          */
71         @Override
72         public String getId() {
73                 return id;
74         }
75
76         /**
77          * {@inheritDoc}
78          */
79         @Override
80         public String getNickname() {
81                 return nickname;
82         }
83
84         /**
85          * {@inheritDoc}
86          */
87         @Override
88         public String getRequestUri() {
89                 return requestUri;
90         }
91
92         /**
93          * {@inheritDoc}
94          */
95         @Override
96         public Set<String> getContexts() {
97                 return Collections.unmodifiableSet(contexts);
98         }
99
100         /**
101          * Sets the contexts of this identity.
102          * <p>
103          * This method is only called by the {@link IdentityManager}.
104          *
105          * @param contexts
106          *            The contexts to set
107          */
108         void setContextsPrivate(Set<String> contexts) {
109                 this.contexts.clear();
110                 this.contexts.addAll(contexts);
111         }
112
113         /**
114          * {@inheritDoc}
115          */
116         @Override
117         public boolean hasContext(String context) {
118                 return contexts.contains(context);
119         }
120
121         /**
122          * Adds the given context to this identity.
123          * <p>
124          * This method is only called by the {@link IdentityManager}.
125          *
126          * @param context
127          *            The context to add
128          */
129         void addContextPrivate(String context) {
130                 contexts.add(context);
131         }
132
133         /**
134          * Removes the given context from this identity.
135          * <p>
136          * This method is only called by the {@link IdentityManager}.
137          *
138          * @param context
139          *            The context to remove
140          */
141         public void removeContextPrivate(String context) {
142                 contexts.remove(context);
143         }
144
145         /**
146          * {@inheritDoc}
147          */
148         @Override
149         public Map<String, String> getProperties() {
150                 synchronized (properties) {
151                         return Collections.unmodifiableMap(properties);
152                 }
153         }
154
155         /**
156          * Sets all properties of this identity.
157          * <p>
158          * This method is only called by the {@link IdentityManager}.
159          *
160          * @param properties
161          *            The new properties of this identity
162          */
163         void setPropertiesPrivate(Map<String, String> properties) {
164                 synchronized (this.properties) {
165                         this.properties.clear();
166                         this.properties.putAll(properties);
167                 }
168         }
169
170         /**
171          * Sets the property with the given name to the given value.
172          * <p>
173          * This method is only called by the {@link IdentityManager}.
174          *
175          * @param name
176          *            The name of the property
177          * @param value
178          *            The value of the property
179          */
180         void setPropertyPrivate(String name, String value) {
181                 synchronized (properties) {
182                         properties.put(name, value);
183                 }
184         }
185
186         /**
187          * {@inheritDoc}
188          */
189         @Override
190         public String getProperty(String name) {
191                 synchronized (properties) {
192                         return properties.get(name);
193                 }
194         }
195
196         /**
197          * Removes the property with the given name.
198          * <p>
199          * This method is only called by the {@link IdentityManager}.
200          *
201          * @param name
202          *            The name of the property to remove
203          */
204         void removePropertyPrivate(String name) {
205                 synchronized (properties) {
206                         properties.remove(name);
207                 }
208         }
209
210         //
211         // OBJECT METHODS
212         //
213
214         /**
215          * {@inheritDoc}
216          */
217         @Override
218         public int hashCode() {
219                 return id.hashCode();
220         }
221
222         /**
223          * {@inheritDoc}
224          */
225         @Override
226         public boolean equals(Object object) {
227                 if (!(object instanceof DefaultIdentity)) {
228                         return false;
229                 }
230                 DefaultIdentity identity = (DefaultIdentity) object;
231                 return identity.id.equals(id);
232         }
233
234         /**
235          * {@inheritDoc}
236          */
237         @Override
238         public String toString() {
239                 return getClass().getSimpleName() + "[id=" + id + ",nickname=" + nickname + ",contexts=" + contexts + ",properties=" + properties + "]";
240         }
241
242 }