523a42bd10180e9ff369d23d376745fbfbf1ae57
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / wot / Identity.java
1 /*
2  * Sone - Identity.java - Copyright © 2010–2013 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.Map;
23 import java.util.Map.Entry;
24 import java.util.Set;
25
26 import com.google.common.base.Function;
27
28 /**
29  * Interface for web of trust identities, defining all functions that can be
30  * performed on an identity. An identity is only a container for identity data
31  * and will not perform any updating in the WebOfTrust plugin itself.
32  *
33  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34  */
35 public interface Identity {
36
37         public static final Function<Identity, Set<String>> TO_CONTEXTS = new Function<Identity, Set<String>>() {
38                 @Override
39                 public Set<String> apply(Identity identity) {
40                         return (identity == null) ? Collections.<String>emptySet() : identity.getContexts();
41                 }
42         };
43
44         public static final Function<Identity, Collection<Map.Entry<String, String>>> TO_PROPERTIES = new Function<Identity, Collection<Entry<String, String>>>() {
45                 @Override
46                 public Collection<Entry<String, String>> apply(Identity input) {
47                         return (input == null) ? Collections.<Map.Entry<String, String>>emptySet() : input.getProperties().entrySet();
48                 }
49         };
50
51         /**
52          * Returns the ID of the identity.
53          *
54          * @return The ID of the identity
55          */
56         public String getId();
57
58         /**
59          * Returns the nickname of the identity.
60          *
61          * @return The nickname of the identity
62          */
63         public String getNickname();
64
65         /**
66          * Returns the request URI of the identity.
67          *
68          * @return The request URI of the identity
69          */
70         public String getRequestUri();
71
72         /**
73          * Returns all contexts of this identity.
74          *
75          * @return All contexts of this identity
76          */
77         public Set<String> getContexts();
78
79         /**
80          * Returns whether this identity has the given context.
81          *
82          * @param context
83          *            The context to check for
84          * @return {@code true} if this identity has the given context,
85          *         {@code false} otherwise
86          */
87         public boolean hasContext(String context);
88
89         /**
90          * Adds the given context to this identity.
91          *
92          * @param context
93          *            The context to add
94          */
95         public Identity addContext(String context);
96
97         /**
98          * Sets all contexts of this identity.
99          *
100          * @param contexts
101          *            All contexts of the identity
102          */
103         public void setContexts(Collection<String> contexts);
104
105         /**
106          * Removes the given context from this identity.
107          *
108          * @param context
109          *            The context to remove
110          */
111         public Identity removeContext(String context);
112
113         /**
114          * Returns all properties of this identity.
115          *
116          * @return All properties of this identity
117          */
118         public Map<String, String> getProperties();
119
120         /**
121          * Returns the value of the property with the given name.
122          *
123          * @param name
124          *            The name of the property
125          * @return The value of the property
126          */
127         public String getProperty(String name);
128
129         /**
130          * Sets the property with the given name to the given value.
131          *
132          * @param name
133          *            The name of the property
134          * @param value
135          *            The value of the property
136          */
137         public Identity setProperty(String name, String value);
138
139         /**
140          * Sets all properties of this identity.
141          *
142          * @param properties
143          *            The new properties of this identity
144          */
145         public void setProperties(Map<String, String> properties);
146
147         /**
148          * Removes the property with the given name.
149          *
150          * @param name
151          *            The name of the property to remove
152          */
153         public Identity removeProperty(String name);
154
155         /**
156          * Retrieves the trust that this identity receives from the given own
157          * identity. If this identity is not in the own identity’s trust tree, a
158          * {@link Trust} is returned that has all its elements set to {@code null}.
159          * If the trust can not be retrieved, {@code null} is returned.
160          *
161          * @param ownIdentity
162          *            The own identity to get the trust for
163          * @return The trust assigned to this identity, or {@code null} if the trust
164          *         could not be retrieved
165          */
166         public Trust getTrust(OwnIdentity ownIdentity);
167
168         /**
169          * Sets the trust given by an own identity to this identity.
170          *
171          * @param ownIdentity
172          *            The own identity that gave trust to this identity
173          * @param trust
174          *            The trust given by the given own identity
175          */
176         public Identity setTrust(OwnIdentity ownIdentity, Trust trust);
177
178         /**
179          * Removes trust assignment from the given own identity for this identity.
180          *
181          * @param ownIdentity
182          *            The own identity that removed the trust assignment for this
183          *            identity
184          */
185         public Identity removeTrust(OwnIdentity ownIdentity);
186
187 }