Remove obsolete User class.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / Sone.java
1 /*
2  * FreenetSone - Sone.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.data;
19
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Set;
25 import java.util.UUID;
26
27 import freenet.keys.FreenetURI;
28
29 /**
30  * A Sone defines everything about a user: her profile, her status updates, her
31  * replies, her likes and dislikes, etc.
32  * <p>
33  * Operations that modify the Sone need to synchronize on the Sone in question.
34  *
35  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36  */
37 public class Sone {
38
39         /** A GUID for this Sone. */
40         private final UUID id;
41
42         /** The name of this Sone. */
43         private final String name;
44
45         /** The URI under which the Sone is stored in Freenet. */
46         private final FreenetURI requestUri;
47
48         /** The URI used to insert a new version of this Sone. */
49         /* This will be null for remote Sones! */
50         private final FreenetURI insertUri;
51
52         /** The profile of this Sone. */
53         private Profile profile;
54
55         /** All friend Sones. */
56         private final Set<Sone> friendSones = new HashSet<Sone>();
57
58         /** All posts. */
59         private final List<Post> posts = new ArrayList<Post>();
60
61         /** All replies. */
62         private final Set<Reply> replies = new HashSet<Reply>();
63
64         /** Modification count. */
65         private volatile long modificationCounter = 0;
66
67         /**
68          * Creates a new Sone.
69          *
70          * @param id
71          *            The ID of this Sone
72          * @param name
73          *            The name of the Sone
74          * @param requestUri
75          *            The request URI of the Sone
76          */
77         public Sone(UUID id, String name, FreenetURI requestUri) {
78                 this(id, name, requestUri, null);
79         }
80
81         /**
82          * Creates a new Sone.
83          *
84          * @param id
85          *            The ID of this Sone
86          * @param name
87          *            The name of the Sone
88          * @param requestUri
89          *            The request URI of the Sone
90          * @param insertUri
91          *            The insert URI of the Sone
92          */
93         public Sone(UUID id, String name, FreenetURI requestUri, FreenetURI insertUri) {
94                 this.id = id;
95                 this.name = name;
96                 this.requestUri = requestUri;
97                 this.insertUri = insertUri;
98         }
99
100         //
101         // ACCESSORS
102         //
103
104         /**
105          * Returns the ID of this Sone.
106          *
107          * @return The ID of this Sone
108          */
109         public String getId() {
110                 return id.toString();
111         }
112
113         /**
114          * Returns the name of this Sone.
115          *
116          * @return The name of this Sone
117          */
118         public String getName() {
119                 return name;
120         }
121
122         /**
123          * Returns the request URI of this Sone.
124          *
125          * @return The request URI of this Sone
126          */
127         public FreenetURI getRequestUri() {
128                 return requestUri;
129         }
130
131         /**
132          * Returns the insert URI of this Sone.
133          *
134          * @return The insert URI of this Sone
135          */
136         public FreenetURI getInsertUri() {
137                 return insertUri;
138         }
139
140         /**
141          * Returns a copy of the profile. If you want to update values in the
142          * profile of this Sone, update the values in the returned {@link Profile}
143          * and use {@link #setProfile(Profile)} to change the profile in this Sone.
144          *
145          * @return A copy of the profile
146          */
147         public Profile getProfile() {
148                 return new Profile(profile);
149         }
150
151         /**
152          * Sets the profile of this Sone. A copy of the given profile is stored so
153          * that subsequent modifications of the given profile are not reflected in
154          * this Sone!
155          *
156          * @param profile
157          *            The profile to set
158          */
159         public synchronized void setProfile(Profile profile) {
160                 this.profile = new Profile(profile);
161                 modificationCounter++;
162         }
163
164         /**
165          * Returns all friend Sones of this Sone.
166          *
167          * @return The friend Sones of this Sone
168          */
169         public Set<Sone> getFriendSones() {
170                 return Collections.unmodifiableSet(friendSones);
171         }
172
173         /**
174          * Returns whether this Sone has the given Sone as a friend Sone.
175          *
176          * @param friendSone
177          *            The friend Sone to check for
178          * @return {@code true} if this Sone has the given Sone as a friend,
179          *         {@code false} otherwise
180          */
181         public boolean hasFriendSone(Sone friendSone) {
182                 return friendSones.contains(friendSone);
183         }
184
185         /**
186          * Adds the given Sone as a friend Sone.
187          *
188          * @param friendSone
189          *            The friend Sone to add
190          * @return This Sone (for method chaining)
191          */
192         public synchronized Sone addFriendSone(Sone friendSone) {
193                 if (friendSones.add(friendSone)) {
194                         modificationCounter++;
195                 }
196                 return this;
197         }
198
199         /**
200          * Removes the given Sone as a friend Sone.
201          *
202          * @param friendSone
203          *            The friend Sone to remove
204          * @return This Sone (for method chaining)
205          */
206         public synchronized Sone removeFriendSone(Sone friendSone) {
207                 if (friendSones.remove(friendSone)) {
208                         modificationCounter++;
209                 }
210                 return this;
211         }
212
213         /**
214          * Returns the list of posts of this Sone.
215          *
216          * @return All posts of this Sone
217          */
218         public List<Post> getPosts() {
219                 return Collections.unmodifiableList(posts);
220         }
221
222         /**
223          * Adds the given post to this Sone. The post will not be added if its
224          * {@link Post#getSone() Sone} is not this Sone.
225          *
226          * @param post
227          *            The post to add
228          */
229         public synchronized void addPost(Post post) {
230                 if (post.getSone().equals(this) && posts.add(post)) {
231                         modificationCounter++;
232                 }
233         }
234
235         /**
236          * Removes the given post from this Sone.
237          *
238          * @param post
239          *            The post to remove
240          */
241         public synchronized void removePost(Post post) {
242                 if (post.getSone().equals(this) && posts.remove(post)) {
243                         modificationCounter++;
244                 }
245         }
246
247         /**
248          * Returns all replies this Sone made.
249          *
250          * @return All replies this Sone made
251          */
252         public Set<Reply> getReplies() {
253                 return Collections.unmodifiableSet(replies);
254         }
255
256         /**
257          * Adds a reply to this Sone. If the given reply was not made by this Sone,
258          * nothing is added to this Sone.
259          *
260          * @param reply
261          *            The reply to add
262          */
263         public synchronized void addReply(Reply reply) {
264                 if (reply.getSone().equals(this) && replies.add(reply)) {
265                         modificationCounter++;
266                 }
267         }
268
269         /**
270          * Removes a reply from this Sone.
271          *
272          * @param reply
273          *            The reply to remove
274          */
275         public synchronized void removeReply(Reply reply) {
276                 if (reply.getSone().equals(this) && replies.remove(reply)) {
277                         modificationCounter++;
278                 }
279         }
280
281         /**
282          * Returns the modification counter.
283          *
284          * @return The modification counter
285          */
286         public synchronized long getModificationCounter() {
287                 return modificationCounter;
288         }
289
290         /**
291          * Sets the modification counter.
292          *
293          * @param modificationCounter
294          *            The new modification counter
295          */
296         public synchronized void setModificationCounter(long modificationCounter) {
297                 this.modificationCounter = modificationCounter;
298         }
299
300         //
301         // OBJECT METHODS
302         //
303
304         /**
305          * {@inheritDoc}
306          */
307         @Override
308         public int hashCode() {
309                 return id.hashCode();
310         }
311
312         /**
313          * {@inheritDoc}
314          */
315         @Override
316         public boolean equals(Object object) {
317                 if (!(object instanceof Sone)) {
318                         return false;
319                 }
320                 return ((Sone) object).id.equals(id);
321         }
322
323 }