Extract Identity interfaces, move functionality to identity classes.
[Sone.git] / src / main / java / net / pterodactylus / sone / 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 /**
25  * An own identity is an identity that the owner of the node has full control
26  * over.
27  *
28  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
29  */
30 public class DefaultOwnIdentity extends DefaultIdentity implements OwnIdentity {
31
32         /** The identity manager. */
33         private final WebOfTrustConnector webOfTrustConnector;
34
35         /** The insert URI of the identity. */
36         private final String insertUri;
37
38         /**
39          * Creates a new own identity.
40          *
41          * @param webOfTrustConnector
42          *            The identity manager
43          * @param id
44          *            The ID of the identity
45          * @param nickname
46          *            The nickname of the identity
47          * @param requestUri
48          *            The request URI of the identity
49          * @param insertUri
50          *            The insert URI of the identity
51          */
52         public DefaultOwnIdentity(WebOfTrustConnector webOfTrustConnector, String id, String nickname, String requestUri, String insertUri) {
53                 super(id, nickname, requestUri);
54                 this.webOfTrustConnector = webOfTrustConnector;
55                 this.insertUri = insertUri;
56         }
57
58         //
59         // ACCESSORS
60         //
61
62         /**
63          * {@inheritDoc}
64          */
65         @Override
66         public String getInsertUri() {
67                 return insertUri;
68         }
69
70         /**
71          * {@inheritDoc}
72          */
73         @Override
74         public void addContext(String context) throws WebOfTrustException {
75                 webOfTrustConnector.addContext(this, context);
76         }
77
78         /**
79          * {@inheritDoc}
80          */
81         @Override
82         public void removeContext(String context) throws WebOfTrustException {
83                 webOfTrustConnector.removeContext(this, context);
84         }
85
86         /**
87          * {@inheritDoc}
88          */
89         @Override
90         public void setContexts(Set<String> contexts) throws WebOfTrustException {
91                 for (String context : getContexts()) {
92                         if (!contexts.contains(context)) {
93                                 webOfTrustConnector.removeContext(this, context);
94                         }
95                 }
96                 for (String context : contexts) {
97                         if (!getContexts().contains(context)) {
98                                 webOfTrustConnector.addContext(this, context);
99                         }
100                 }
101         }
102
103         /**
104          * {@inheritDoc}
105          */
106         @Override
107         public void setProperty(String name, String value) throws WebOfTrustException {
108                 webOfTrustConnector.setProperty(this, name, value);
109         }
110
111         /**
112          * {@inheritDoc}
113          */
114         @Override
115         public void removeProperty(String name) throws WebOfTrustException {
116                 webOfTrustConnector.removeProperty(this, name);
117         }
118
119         /**
120          * {@inheritDoc}
121          */
122         @Override
123         public void setProperties(Map<String, String> properties) throws WebOfTrustException {
124                 for (Entry<String, String> oldProperty : getProperties().entrySet()) {
125                         if (!properties.containsKey(oldProperty.getKey())) {
126                                 webOfTrustConnector.removeProperty(this, oldProperty.getKey());
127                         } else {
128                                 webOfTrustConnector.setProperty(this, oldProperty.getKey(), properties.get(oldProperty.getKey()));
129                         }
130                 }
131                 for (Entry<String, String> newProperty : properties.entrySet()) {
132                         if (!getProperties().containsKey(newProperty.getKey())) {
133                                 webOfTrustConnector.setProperty(this, newProperty.getKey(), newProperty.getValue());
134                         }
135                 }
136         }
137
138 }