2 * Sone - DefaultOwnIdentity.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;
21 import java.util.Map.Entry;
24 import net.pterodactylus.util.validation.Validation;
27 * An own identity is an identity that the owner of the node has full control
30 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
32 public class DefaultOwnIdentity extends DefaultIdentity implements OwnIdentity {
34 /** The identity manager. */
35 private final WebOfTrustConnector webOfTrustConnector;
37 /** The insert URI of the identity. */
38 private final String insertUri;
41 * Creates a new own identity.
43 * @param webOfTrustConnector
44 * The identity manager
46 * The ID of the identity
48 * The nickname of the identity
50 * The request URI of the identity
52 * The insert URI of the identity
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;
61 * Copy constructor for an own identity.
63 * @param webOfTrustConnector
64 * The web of trust connector
66 * The own identity to copy
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();
82 public String getInsertUri() {
90 public void addContext(String context) throws WebOfTrustException {
91 webOfTrustConnector.addContext(this, context);
92 addContextPrivate(context);
99 public void removeContext(String context) throws WebOfTrustException {
100 webOfTrustConnector.removeContext(this, context);
101 removeContextPrivate(context);
108 public void setContexts(Set<String> contexts) throws WebOfTrustException {
109 for (String context : getContexts()) {
110 if (!contexts.contains(context)) {
111 webOfTrustConnector.removeContext(this, context);
114 for (String context : contexts) {
115 if (!getContexts().contains(context)) {
116 webOfTrustConnector.addContext(this, context);
119 setContextsPrivate(contexts);
126 public void setProperty(String name, String value) throws WebOfTrustException {
127 webOfTrustConnector.setProperty(this, name, value);
128 setPropertyPrivate(name, value);
135 public void removeProperty(String name) throws WebOfTrustException {
136 webOfTrustConnector.removeProperty(this, name);
137 removePropertyPrivate(name);
144 public void setProperties(Map<String, String> properties) throws WebOfTrustException {
145 for (Entry<String, String> oldProperty : getProperties().entrySet()) {
146 if (!properties.containsKey(oldProperty.getKey())) {
147 webOfTrustConnector.removeProperty(this, oldProperty.getKey());
149 webOfTrustConnector.setProperty(this, oldProperty.getKey(), properties.get(oldProperty.getKey()));
152 for (Entry<String, String> newProperty : properties.entrySet()) {
153 if (!getProperties().containsKey(newProperty.getKey())) {
154 webOfTrustConnector.setProperty(this, newProperty.getKey(), newProperty.getValue());
157 setPropertiesPrivate(properties);
164 public void setTrust(Identity target, int trustValue, String comment) throws WebOfTrustException {
165 Validation.begin().isNotNull("Trust Target", target).isNotNull("Trust Comment", comment).isLessOrEqual("Trust Value", trustValue, 100).isGreaterOrEqual("Trust Value", trustValue, -100).check();
166 webOfTrustConnector.setTrust(this, target, trustValue, comment);
167 if (target instanceof DefaultIdentity) {
168 ((DefaultIdentity) target).setTrustPrivate(this, new Trust(trustValue, trustValue, 0));
176 public void removeTrust(Identity target) throws WebOfTrustException {
177 Validation.begin().isNotNull("Trust Target", target).check();
178 webOfTrustConnector.removeTrust(this, target);
179 if (target instanceof DefaultIdentity) {
180 ((DefaultIdentity) target).setTrustPrivate(this, new Trust(null, null, null));
192 public int hashCode() {
193 /* The hash of DefaultIdentity is fine. */
194 return super.hashCode();
201 public boolean equals(Object object) {
202 /* The ID of the superclass is still enough. */
203 return super.equals(object);