433516fceedef4491612fb4fb7bea3583d02b7da
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / wot / DefaultIdentity.java
1 /*
2  * Sone - DefaultIdentity.java - Copyright © 2010–2016 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 Identity addContext(String context) {
105                 contexts.add(context);
106                 return this;
107         }
108
109         @Override
110         public Identity removeContext(String context) {
111                 contexts.remove(context);
112                 return this;
113         }
114
115         @Override
116         public Map<String, String> getProperties() {
117                 return Collections.unmodifiableMap(properties);
118         }
119
120         @Override
121         public void setProperties(Map<String, String> properties) {
122                 this.properties.clear();
123                 this.properties.putAll(properties);
124         }
125
126         @Override
127         public String getProperty(String name) {
128                 return properties.get(name);
129         }
130
131         @Override
132         public Identity setProperty(String name, String value) {
133                 properties.put(name, value);
134                 return this;
135         }
136
137         @Override
138         public Identity removeProperty(String name) {
139                 properties.remove(name);
140                 return this;
141         }
142
143         @Override
144         public Trust getTrust(OwnIdentity ownIdentity) {
145                 return trustCache.get(ownIdentity);
146         }
147
148         @Override
149         public Identity setTrust(OwnIdentity ownIdentity, Trust trust) {
150                 trustCache.put(ownIdentity, trust);
151                 return this;
152         }
153
154         @Override
155         public Identity removeTrust(OwnIdentity ownIdentity) {
156                 trustCache.remove(ownIdentity);
157                 return this;
158         }
159
160         //
161         // OBJECT METHODS
162         //
163
164         @Override
165         public int hashCode() {
166                 return getId().hashCode();
167         }
168
169         @Override
170         public boolean equals(Object object) {
171                 if (!(object instanceof Identity)) {
172                         return false;
173                 }
174                 Identity identity = (Identity) object;
175                 return identity.getId().equals(getId());
176         }
177
178         @Override
179         public String toString() {
180                 return getClass().getSimpleName() + "[id=" + id + ",nickname=" + nickname + ",contexts=" + contexts + ",properties=" + properties + "]";
181         }
182
183 }