🔀 Merge branch 'release/v82'
[Sone.git] / freenet / wot / DefaultOwnIdentity.java
1 /*
2  * Sone - DefaultOwnIdentity.java - Copyright Â© 2010 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         // ACCESSORS
62         //
63
64         /**
65          * {@inheritDoc}
66          */
67         @Override
68         public String getInsertUri() {
69                 return insertUri;
70         }
71
72         /**
73          * {@inheritDoc}
74          */
75         @Override
76         public void addContext(String context) throws WebOfTrustException {
77                 webOfTrustConnector.addContext(this, context);
78                 addContextPrivate(context);
79         }
80
81         /**
82          * {@inheritDoc}
83          */
84         @Override
85         public void removeContext(String context) throws WebOfTrustException {
86                 webOfTrustConnector.removeContext(this, context);
87                 removeContextPrivate(context);
88         }
89
90         /**
91          * {@inheritDoc}
92          */
93         @Override
94         public void setContexts(Set<String> contexts) throws WebOfTrustException {
95                 for (String context : getContexts()) {
96                         if (!contexts.contains(context)) {
97                                 webOfTrustConnector.removeContext(this, context);
98                         }
99                 }
100                 for (String context : contexts) {
101                         if (!getContexts().contains(context)) {
102                                 webOfTrustConnector.addContext(this, context);
103                         }
104                 }
105                 setContextsPrivate(contexts);
106         }
107
108         /**
109          * {@inheritDoc}
110          */
111         @Override
112         public void setProperty(String name, String value) throws WebOfTrustException {
113                 webOfTrustConnector.setProperty(this, name, value);
114                 setPropertyPrivate(name, value);
115         }
116
117         /**
118          * {@inheritDoc}
119          */
120         @Override
121         public void removeProperty(String name) throws WebOfTrustException {
122                 webOfTrustConnector.removeProperty(this, name);
123                 removePropertyPrivate(name);
124         }
125
126         /**
127          * {@inheritDoc}
128          */
129         @Override
130         public void setProperties(Map<String, String> properties) throws WebOfTrustException {
131                 for (Entry<String, String> oldProperty : getProperties().entrySet()) {
132                         if (!properties.containsKey(oldProperty.getKey())) {
133                                 webOfTrustConnector.removeProperty(this, oldProperty.getKey());
134                         } else {
135                                 webOfTrustConnector.setProperty(this, oldProperty.getKey(), properties.get(oldProperty.getKey()));
136                         }
137                 }
138                 for (Entry<String, String> newProperty : properties.entrySet()) {
139                         if (!getProperties().containsKey(newProperty.getKey())) {
140                                 webOfTrustConnector.setProperty(this, newProperty.getKey(), newProperty.getValue());
141                         }
142                 }
143                 setPropertiesPrivate(properties);
144         }
145
146         /**
147          * {@inheritDoc}
148          */
149         @Override
150         public void setTrust(Identity target, int trustValue, String comment) throws WebOfTrustException {
151                 Validation.begin().isNotNull("Trust Target", target).isNotNull("Trust Comment", comment).isLessOrEqual("Trust Value", trustValue, 100).isGreaterOrEqual("Trust Value", trustValue, -100).check();
152                 webOfTrustConnector.setTrust(this, target, trustValue, comment);
153                 if (target instanceof DefaultIdentity) {
154                         ((DefaultIdentity) target).setTrustPrivate(this, new Trust(trustValue, trustValue, 0));
155                 }
156         }
157
158         /**
159          * {@inheritDoc}
160          */
161         @Override
162         public void removeTrust(Identity target) throws WebOfTrustException {
163                 Validation.begin().isNotNull("Trust Target", target).check();
164                 webOfTrustConnector.removeTrust(this, target);
165                 if (target instanceof DefaultIdentity) {
166                         ((DefaultIdentity) target).setTrustPrivate(this, new Trust(null, null, null));
167                 }
168         }
169
170 }