Remove @author tags
[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 public class DefaultIdentity implements Identity {
31
32         /** The ID of the identity. */
33         private final String id;
34
35         /** The nickname of the identity. */
36         private final String nickname;
37
38         /** The request URI of the identity. */
39         private final String requestUri;
40
41         /** The contexts of the identity. */
42         private final Set<String> contexts = Collections.synchronizedSet(new HashSet<String>());
43
44         /** The properties of the identity. */
45         private final Map<String, String> properties = Collections.synchronizedMap(new HashMap<String, String>());
46
47         /** Cached trust. */
48         private final Map<OwnIdentity, Trust> trustCache = Collections.synchronizedMap(new HashMap<OwnIdentity, Trust>());
49
50         /**
51          * Creates a new identity.
52          *
53          * @param id
54          *            The ID of the identity
55          * @param nickname
56          *            The nickname of the identity
57          * @param requestUri
58          *            The request URI of the identity
59          */
60         public DefaultIdentity(String id, String nickname, String requestUri) {
61                 this.id = id;
62                 this.nickname = nickname;
63                 this.requestUri = requestUri;
64         }
65
66         //
67         // ACCESSORS
68         //
69
70         @Override
71         public String getId() {
72                 return id;
73         }
74
75         @Override
76         public String getNickname() {
77                 return nickname;
78         }
79
80         @Override
81         public String getRequestUri() {
82                 return requestUri;
83         }
84
85         @Override
86         public Set<String> getContexts() {
87                 return Collections.unmodifiableSet(contexts);
88         }
89
90         @Override
91         public boolean hasContext(String context) {
92                 return contexts.contains(context);
93         }
94
95         @Override
96         public void setContexts(Collection<String> contexts) {
97                 this.contexts.clear();
98                 this.contexts.addAll(contexts);
99         }
100
101         @Override
102         public Identity addContext(String context) {
103                 contexts.add(context);
104                 return this;
105         }
106
107         @Override
108         public Identity removeContext(String context) {
109                 contexts.remove(context);
110                 return this;
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 Identity setProperty(String name, String value) {
131                 properties.put(name, value);
132                 return this;
133         }
134
135         @Override
136         public Identity removeProperty(String name) {
137                 properties.remove(name);
138                 return this;
139         }
140
141         @Override
142         public Trust getTrust(OwnIdentity ownIdentity) {
143                 return trustCache.get(ownIdentity);
144         }
145
146         @Override
147         public Identity setTrust(OwnIdentity ownIdentity, Trust trust) {
148                 trustCache.put(ownIdentity, trust);
149                 return this;
150         }
151
152         @Override
153         public Identity removeTrust(OwnIdentity ownIdentity) {
154                 trustCache.remove(ownIdentity);
155                 return this;
156         }
157
158         //
159         // OBJECT METHODS
160         //
161
162         @Override
163         public int hashCode() {
164                 return getId().hashCode();
165         }
166
167         @Override
168         public boolean equals(Object object) {
169                 if (!(object instanceof Identity)) {
170                         return false;
171                 }
172                 Identity identity = (Identity) object;
173                 return identity.getId().equals(getId());
174         }
175
176         @Override
177         public String toString() {
178                 return getClass().getSimpleName() + "[id=" + id + ",nickname=" + nickname + ",contexts=" + contexts + ",properties=" + properties + "]";
179         }
180
181 }