3e03f79a0c863904a1a12115acce724d607446a7
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / wot / DefaultIdentity.java
1 /*
2  * Sone - DefaultIdentity.java - Copyright © 2010–2013 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.Collection;
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.Map;
25 import java.util.Set;
26
27 /**
28  * A Web of Trust identity.
29  *
30  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
31  */
32 public class DefaultIdentity implements Identity {
33
34         /** The ID of the identity. */
35         private final String id;
36
37         /** The nickname of the identity. */
38         private final String nickname;
39
40         /** The request URI of the identity. */
41         private final String requestUri;
42
43         /** The contexts of the identity. */
44         private final Set<String> contexts = Collections.synchronizedSet(new HashSet<String>());
45
46         /** The properties of the identity. */
47         private final Map<String, String> properties = Collections.synchronizedMap(new HashMap<String, String>());
48
49         /** Cached trust. */
50         private final Map<OwnIdentity, Trust> trustCache = Collections.synchronizedMap(new HashMap<OwnIdentity, Trust>());
51
52         /**
53          * Creates a new identity.
54          *
55          * @param id
56          *            The ID of the identity
57          * @param nickname
58          *            The nickname of the identity
59          * @param requestUri
60          *            The request URI of the identity
61          */
62         public DefaultIdentity(String id, String nickname, String requestUri) {
63                 this.id = id;
64                 this.nickname = nickname;
65                 this.requestUri = requestUri;
66         }
67
68         //
69         // ACCESSORS
70         //
71
72         @Override
73         public String getId() {
74                 return id;
75         }
76
77         @Override
78         public String getNickname() {
79                 return nickname;
80         }
81
82         @Override
83         public String getRequestUri() {
84                 return requestUri;
85         }
86
87         @Override
88         public Set<String> getContexts() {
89                 return Collections.unmodifiableSet(contexts);
90         }
91
92         @Override
93         public boolean hasContext(String context) {
94                 return contexts.contains(context);
95         }
96
97         @Override
98         public void setContexts(Collection<String> contexts) {
99                 this.contexts.clear();
100                 this.contexts.addAll(contexts);
101         }
102
103         @Override
104         public void addContext(String context) {
105                 contexts.add(context);
106         }
107
108         @Override
109         public void removeContext(String context) {
110                 contexts.remove(context);
111         }
112
113         @Override
114         public Map<String, String> getProperties() {
115                 return Collections.unmodifiableMap(properties);
116         }
117
118         @Override
119         public void setProperties(Map<String, String> properties) {
120                 this.properties.clear();
121                 this.properties.putAll(properties);
122         }
123
124         @Override
125         public String getProperty(String name) {
126                 return properties.get(name);
127         }
128
129         @Override
130         public void setProperty(String name, String value) {
131                 properties.put(name, value);
132         }
133
134         @Override
135         public void removeProperty(String name) {
136                 properties.remove(name);
137         }
138
139         @Override
140         public Trust getTrust(OwnIdentity ownIdentity) {
141                 return trustCache.get(ownIdentity);
142         }
143
144         @Override
145         public void setTrust(OwnIdentity ownIdentity, Trust trust) {
146                 trustCache.put(ownIdentity, trust);
147         }
148
149         @Override
150         public void removeTrust(OwnIdentity ownIdentity) {
151                 trustCache.remove(ownIdentity);
152         }
153
154         //
155         // OBJECT METHODS
156         //
157
158         @Override
159         public int hashCode() {
160                 return getId().hashCode();
161         }
162
163         @Override
164         public boolean equals(Object object) {
165                 if (!(object instanceof Identity)) {
166                         return false;
167                 }
168                 Identity identity = (Identity) object;
169                 return identity.getId().equals(getId());
170         }
171
172         @Override
173         public String toString() {
174                 return getClass().getSimpleName() + "[id=" + id + ",nickname=" + nickname + ",contexts=" + contexts + ",properties=" + properties + "]";
175         }
176
177 }