Merge branch 'next' into wot-integration
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / wot / Identity.java
1 /*
2  * Sone - Identity.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.Set;
21
22 /**
23  * A Web of Trust identity.
24  *
25  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
26  */
27 public class Identity {
28
29         /** The Web of Trust connector. */
30         protected final WebOfTrustConnector webOfTrustConnector;
31
32         /** The ID of the identity. */
33         private final String id;
34
35         /** The nickname of the identity. */
36         private final String nickname;
37
38         /** The request URI of the identity. */
39         private final String requestUri;
40
41         /**
42          * Creates a new identity.
43          *
44          * @param webOfTrustConnector
45          *            The Web of Trust connector
46          * @param id
47          *            The ID of the identity
48          * @param nickname
49          *            The nickname of the identity
50          * @param requestUri
51          *            The request URI of the identity
52          */
53         public Identity(WebOfTrustConnector webOfTrustConnector, String id, String nickname, String requestUri) {
54                 this.webOfTrustConnector = webOfTrustConnector;
55                 this.id = id;
56                 this.nickname = nickname;
57                 this.requestUri = requestUri;
58         }
59
60         //
61         // ACCESSORS
62         //
63
64         /**
65          * Returns the ID of the identity.
66          *
67          * @return The ID of the identity
68          */
69         public String getId() {
70                 return id;
71         }
72
73         /**
74          * Returns the nickname of the identity.
75          *
76          * @return The nickname of the identity
77          */
78         public String getNickname() {
79                 return nickname;
80         }
81
82         /**
83          * Returns the request URI of the identity.
84          *
85          * @return The request URI of the identity
86          */
87         public String getRequestUri() {
88                 return requestUri;
89         }
90
91         /**
92          * Returns the contexts of the identity. If the contexts have not been
93          * loaded yet, they will be loaded. If loading the contexts fails, an empty
94          * set is returned.
95          *
96          * @return The contexts of the identity
97          * @throws PluginException
98          *             if an error occured communicating with the Web of Trust
99          *             plugin
100          */
101         public Set<String> getContexts() throws PluginException {
102                 return webOfTrustConnector.loadIdentityContexts(this);
103         }
104
105         /**
106          * Returns whether the identity contains the given context.
107          *
108          * @param context
109          *            The context to check for
110          * @return {@code true} if this identity has the given context,
111          *         {@code false} otherwise
112          * @throws PluginException
113          *             if an error occured communicating with the Web of Trust
114          *             plugin
115          */
116         public boolean hasContext(String context) throws PluginException {
117                 return getContexts().contains(context);
118         }
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, or {@code null} if there is no such
126          *         property
127          * @throws PluginException
128          *             if an error occured communicating with the Web of Trust
129          *             plugin
130          */
131         public String getProperty(String name) throws PluginException {
132                 return webOfTrustConnector.getProperty(this, name);
133         }
134
135         //
136         // OBJECT METHODS
137         //
138
139         /**
140          * {@inheritDoc}
141          */
142         @Override
143         public int hashCode() {
144                 return id.hashCode();
145         }
146
147         /**
148          * {@inheritDoc}
149          */
150         @Override
151         public boolean equals(Object object) {
152                 if (!(object instanceof Identity)) {
153                         return false;
154                 }
155                 Identity identity = (Identity) object;
156                 return identity.id.equals(id);
157         }
158
159 }