Don’t cache contexts.
[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.Map;
23 import java.util.Set;
24
25 /**
26  * A Web of Trust identity.
27  *
28  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
29  */
30 public class Identity {
31
32         /** The Web of Trust connector. */
33         protected final WebOfTrustConnector webOfTrustConnector;
34
35         /** The ID of the identity. */
36         private final String id;
37
38         /** The nickname of the identity. */
39         private final String nickname;
40
41         /** The request URI of the identity. */
42         private final String requestUri;
43
44         /** The properties of the identity. */
45         protected final Map<String, String> properties = Collections.synchronizedMap(new HashMap<String, String>());
46
47         /**
48          * Creates a new identity.
49          *
50          * @param webOfTrustConnector
51          *            The Web of Trust connector
52          * @param id
53          *            The ID of the identity
54          * @param nickname
55          *            The nickname of the identity
56          * @param requestUri
57          *            The request URI of the identity
58          */
59         public Identity(WebOfTrustConnector webOfTrustConnector, String id, String nickname, String requestUri) {
60                 this.webOfTrustConnector = webOfTrustConnector;
61                 this.id = id;
62                 this.nickname = nickname;
63                 this.requestUri = requestUri;
64         }
65
66         //
67         // ACCESSORS
68         //
69
70         /**
71          * Returns the ID of the identity.
72          *
73          * @return The ID of the identity
74          */
75         public String getId() {
76                 return id;
77         }
78
79         /**
80          * Returns the nickname of the identity.
81          *
82          * @return The nickname of the identity
83          */
84         public String getNickname() {
85                 return nickname;
86         }
87
88         /**
89          * Returns the request URI of the identity.
90          *
91          * @return The request URI of the identity
92          */
93         public String getRequestUri() {
94                 return requestUri;
95         }
96
97         /**
98          * Returns the contexts of the identity. If the contexts have not been
99          * loaded yet, they will be loaded. If loading the contexts fails, an empty
100          * set is returned.
101          *
102          * @return The contexts of the identity
103          * @throws PluginException
104          *             if an error occured communicating with the Web of Trust
105          *             plugin
106          */
107         public Set<String> getContexts() throws PluginException {
108                 return webOfTrustConnector.loadIdentityContexts(this);
109         }
110
111         /**
112          * Returns whether the identity contains the given context.
113          *
114          * @param context
115          *            The context to check for
116          * @return {@code true} if this identity has the given context,
117          *         {@code false} otherwise
118          * @throws PluginException
119          *             if an error occured communicating with the Web of Trust
120          *             plugin
121          */
122         public boolean hasContext(String context) throws PluginException {
123                 return getContexts().contains(context);
124         }
125
126         /**
127          * Returns the properties of the identity.
128          *
129          * @return The properties of the identity
130          */
131         public Map<String, String> getProperties() {
132                 return Collections.unmodifiableMap(properties);
133         }
134
135         /**
136          * Returns the value of the property with the given name.
137          *
138          * @param name
139          *            The name of the property
140          * @return The value of the property, or {@code null} if there is no such
141          *         property
142          */
143         public String getProperty(String name) {
144                 return properties.get(name);
145         }
146
147         //
148         // OBJECT METHODS
149         //
150
151         /**
152          * {@inheritDoc}
153          */
154         @Override
155         public int hashCode() {
156                 return id.hashCode();
157         }
158
159         /**
160          * {@inheritDoc}
161          */
162         @Override
163         public boolean equals(Object object) {
164                 if (!(object instanceof Identity)) {
165                         return false;
166                 }
167                 Identity identity = (Identity) object;
168                 return identity.id.equals(id);
169         }
170
171 }