2 * Sone - Identity.java - Copyright © 2010 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.freenet.wot;
20 import java.util.Collections;
21 import java.util.HashMap;
26 * A Web of Trust identity.
28 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
30 public class Identity {
32 /** The Web of Trust connector. */
33 protected final WebOfTrustConnector webOfTrustConnector;
35 /** The ID of the identity. */
36 private final String id;
38 /** The nickname of the identity. */
39 private final String nickname;
41 /** The request URI of the identity. */
42 private final String requestUri;
44 /** The properties of the identity. */
45 protected final Map<String, String> properties = Collections.synchronizedMap(new HashMap<String, String>());
48 * Creates a new identity.
50 * @param webOfTrustConnector
51 * The Web of Trust connector
53 * The ID of the identity
55 * The nickname of the identity
57 * The request URI of the identity
59 public Identity(WebOfTrustConnector webOfTrustConnector, String id, String nickname, String requestUri) {
60 this.webOfTrustConnector = webOfTrustConnector;
62 this.nickname = nickname;
63 this.requestUri = requestUri;
71 * Returns the ID of the identity.
73 * @return The ID of the identity
75 public String getId() {
80 * Returns the nickname of the identity.
82 * @return The nickname of the identity
84 public String getNickname() {
89 * Returns the request URI of the identity.
91 * @return The request URI of the identity
93 public String getRequestUri() {
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
102 * @return The contexts of the identity
103 * @throws PluginException
104 * if an error occured communicating with the Web of Trust
107 public Set<String> getContexts() throws PluginException {
108 return webOfTrustConnector.loadIdentityContexts(this);
112 * Returns whether the identity contains the given 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
122 public boolean hasContext(String context) throws PluginException {
123 return getContexts().contains(context);
127 * Returns the properties of the identity.
129 * @return The properties of the identity
131 public Map<String, String> getProperties() {
132 return Collections.unmodifiableMap(properties);
136 * Returns the value of the property with the given name.
139 * The name of the property
140 * @return The value of the property, or {@code null} if there is no such
143 public String getProperty(String name) {
144 return properties.get(name);
155 public int hashCode() {
156 return id.hashCode();
163 public boolean equals(Object object) {
164 if (!(object instanceof Identity)) {
167 Identity identity = (Identity) object;
168 return identity.id.equals(id);