c461c8b89dfa9fd1a162ebaaf29e28c9bf5cb2dd
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / wot / DefaultOwnIdentity.java
1 /*
2  * Sone - DefaultOwnIdentity.java - Copyright © 2010–2012 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.Map;
21 import java.util.Map.Entry;
22 import java.util.Set;
23
24 import net.pterodactylus.util.validation.Validation;
25
26 /**
27  * An own identity is an identity that the owner of the node has full control
28  * over.
29  *
30  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
31  */
32 public class DefaultOwnIdentity extends DefaultIdentity implements OwnIdentity {
33
34         /** The identity manager. */
35         private final WebOfTrustConnector webOfTrustConnector;
36
37         /** The insert URI of the identity. */
38         private final String insertUri;
39
40         /**
41          * Creates a new own identity.
42          *
43          * @param webOfTrustConnector
44          *            The identity manager
45          * @param id
46          *            The ID of the identity
47          * @param nickname
48          *            The nickname of the identity
49          * @param requestUri
50          *            The request URI of the identity
51          * @param insertUri
52          *            The insert URI of the identity
53          */
54         public DefaultOwnIdentity(WebOfTrustConnector webOfTrustConnector, String id, String nickname, String requestUri, String insertUri) {
55                 super(webOfTrustConnector, id, nickname, requestUri);
56                 this.webOfTrustConnector = webOfTrustConnector;
57                 this.insertUri = insertUri;
58         }
59
60         /**
61          * Copy constructor for an own identity.
62          *
63          * @param webOfTrustConnector
64          *            The web of trust connector
65          * @param ownIdentity
66          *            The own identity to copy
67          */
68         public DefaultOwnIdentity(WebOfTrustConnector webOfTrustConnector, OwnIdentity ownIdentity) {
69                 super(webOfTrustConnector, ownIdentity.getId(), ownIdentity.getNickname(), ownIdentity.getRequestUri());
70                 this.webOfTrustConnector = webOfTrustConnector;
71                 this.insertUri = ownIdentity.getInsertUri();
72                 setContextsPrivate(ownIdentity.getContexts());
73                 setPropertiesPrivate(ownIdentity.getProperties());
74         }
75
76         //
77         // ACCESSORS
78         //
79
80         /**
81          * {@inheritDoc}
82          */
83         @Override
84         public String getInsertUri() {
85                 return insertUri;
86         }
87
88         /**
89          * {@inheritDoc}
90          */
91         @Override
92         public void addContext(String context) throws WebOfTrustException {
93                 webOfTrustConnector.addContext(this, context);
94                 addContextPrivate(context);
95         }
96
97         /**
98          * {@inheritDoc}
99          */
100         @Override
101         public void removeContext(String context) throws WebOfTrustException {
102                 webOfTrustConnector.removeContext(this, context);
103                 removeContextPrivate(context);
104         }
105
106         /**
107          * {@inheritDoc}
108          */
109         @Override
110         public void setContexts(Set<String> contexts) throws WebOfTrustException {
111                 for (String context : getContexts()) {
112                         if (!contexts.contains(context)) {
113                                 webOfTrustConnector.removeContext(this, context);
114                         }
115                 }
116                 for (String context : contexts) {
117                         if (!getContexts().contains(context)) {
118                                 webOfTrustConnector.addContext(this, context);
119                         }
120                 }
121                 setContextsPrivate(contexts);
122         }
123
124         /**
125          * {@inheritDoc}
126          */
127         @Override
128         public void setProperty(String name, String value) throws WebOfTrustException {
129                 webOfTrustConnector.setProperty(this, name, value);
130                 setPropertyPrivate(name, value);
131         }
132
133         /**
134          * {@inheritDoc}
135          */
136         @Override
137         public void removeProperty(String name) throws WebOfTrustException {
138                 webOfTrustConnector.removeProperty(this, name);
139                 removePropertyPrivate(name);
140         }
141
142         /**
143          * {@inheritDoc}
144          */
145         @Override
146         public void setProperties(Map<String, String> properties) throws WebOfTrustException {
147                 for (Entry<String, String> oldProperty : getProperties().entrySet()) {
148                         if (!properties.containsKey(oldProperty.getKey())) {
149                                 webOfTrustConnector.removeProperty(this, oldProperty.getKey());
150                         } else {
151                                 webOfTrustConnector.setProperty(this, oldProperty.getKey(), properties.get(oldProperty.getKey()));
152                         }
153                 }
154                 for (Entry<String, String> newProperty : properties.entrySet()) {
155                         if (!getProperties().containsKey(newProperty.getKey())) {
156                                 webOfTrustConnector.setProperty(this, newProperty.getKey(), newProperty.getValue());
157                         }
158                 }
159                 setPropertiesPrivate(properties);
160         }
161
162         /**
163          * {@inheritDoc}
164          */
165         @Override
166         public void setTrust(Identity target, int trustValue, String comment) throws WebOfTrustException {
167                 Validation.begin().isNotNull("Trust Target", target).isNotNull("Trust Comment", comment).isLessOrEqual("Trust Value", trustValue, 100).isGreaterOrEqual("Trust Value", trustValue, -100).check();
168                 webOfTrustConnector.setTrust(this, target, trustValue, comment);
169                 if (target instanceof DefaultIdentity) {
170                         ((DefaultIdentity) target).setTrustPrivate(this, new Trust(trustValue, trustValue, 0));
171                 }
172         }
173
174         /**
175          * {@inheritDoc}
176          */
177         @Override
178         public void removeTrust(Identity target) throws WebOfTrustException {
179                 Validation.begin().isNotNull("Trust Target", target).check();
180                 webOfTrustConnector.removeTrust(this, target);
181                 if (target instanceof DefaultIdentity) {
182                         ((DefaultIdentity) target).setTrustPrivate(this, new Trust(null, null, null));
183                 }
184         }
185
186         //
187         // OBJECT METHODS
188         //
189
190         /**
191          * {@inheritDoc}
192          */
193         @Override
194         public int hashCode() {
195                 /* The hash of DefaultIdentity is fine. */
196                 return super.hashCode();
197         }
198
199         /**
200          * {@inheritDoc}
201          */
202         @Override
203         public boolean equals(Object object) {
204                 /* The ID of the superclass is still enough. */
205                 return super.equals(object);
206         }
207
208 }