9219e990ac76390a088ca299b35d9de3c234f05c
[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.Collection;
22 import java.util.Collections;
23 import java.util.Comparator;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Set;
27 import java.util.UUID;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
30
31 import net.pterodactylus.util.logging.Logging;
32 import freenet.keys.FreenetURI;
33
34 /**
35  * A Sone defines everything about a user: her profile, her status updates, her
36  * replies, her likes and dislikes, etc.
37  * <p>
38  * Operations that modify the Sone need to synchronize on the Sone in question.
39  *
40  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41  */
42 public class Sone {
43
44         /** The logger. */
45         private static final Logger logger = Logging.getLogger(Sone.class);
46
47         /** A GUID for this Sone. */
48         private final UUID id;
49
50         /** The name of this Sone. */
51         private String name;
52
53         /** The URI under which the Sone is stored in Freenet. */
54         private FreenetURI requestUri;
55
56         /** The URI used to insert a new version of this Sone. */
57         /* This will be null for remote Sones! */
58         private FreenetURI insertUri;
59
60         /** The profile of this Sone. */
61         private Profile profile;
62
63         /** All friend Sones. */
64         private final Set<Sone> friendSones = new HashSet<Sone>();
65
66         /** All posts. */
67         private final List<Post> posts = new ArrayList<Post>();
68
69         /** All replies. */
70         private final Set<Reply> replies = new HashSet<Reply>();
71
72         /** Modification count. */
73         private volatile long modificationCounter = 0;
74
75         /**
76          * Creates a new Sone.
77          *
78          * @param id
79          *            The ID of this Sone
80          */
81         public Sone(String id) {
82                 this.id = UUID.fromString(id);
83         }
84
85         //
86         // ACCESSORS
87         //
88
89         /**
90          * Returns the ID of this Sone.
91          *
92          * @return The ID of this Sone
93          */
94         public String getId() {
95                 return id.toString();
96         }
97
98         /**
99          * Returns the name of this Sone.
100          *
101          * @return The name of this Sone
102          */
103         public String getName() {
104                 return name;
105         }
106
107         /**
108          * Sets the name of this Sone.
109          *
110          * @param name
111          *            The name of this Sone
112          * @return This sone (for method chaining)
113          */
114         public Sone setName(String name) {
115                 this.name = name;
116                 return this;
117         }
118
119         /**
120          * Returns the request URI of this Sone.
121          *
122          * @return The request URI of this Sone
123          */
124         public FreenetURI getRequestUri() {
125                 return requestUri;
126         }
127
128         /**
129          * Sets the request URI of this Sone.
130          *
131          * @param requestUri
132          *            The request URI of this Sone
133          * @return This Sone (for method chaining)
134          */
135         public Sone setRequestUri(FreenetURI requestUri) {
136                 this.requestUri = requestUri;
137                 return this;
138         }
139
140         /**
141          * Returns the insert URI of this Sone.
142          *
143          * @return The insert URI of this Sone
144          */
145         public FreenetURI getInsertUri() {
146                 return insertUri;
147         }
148
149         /**
150          * Sets the insert URI of this Sone.
151          *
152          * @param insertUri
153          *            The insert URI of this Sone
154          * @return This Sone (for method chaining)
155          */
156         public Sone setInsertUri(FreenetURI insertUri) {
157                 this.insertUri = insertUri;
158                 return this;
159         }
160
161         /**
162          * Returns a copy of the profile. If you want to update values in the
163          * profile of this Sone, update the values in the returned {@link Profile}
164          * and use {@link #setProfile(Profile)} to change the profile in this Sone.
165          *
166          * @return A copy of the profile
167          */
168         public Profile getProfile() {
169                 return new Profile(profile);
170         }
171
172         /**
173          * Sets the profile of this Sone. A copy of the given profile is stored so
174          * that subsequent modifications of the given profile are not reflected in
175          * this Sone!
176          *
177          * @param profile
178          *            The profile to set
179          */
180         public synchronized void setProfile(Profile profile) {
181                 this.profile = new Profile(profile);
182                 modificationCounter++;
183         }
184
185         /**
186          * Returns all friend Sones of this Sone.
187          *
188          * @return The friend Sones of this Sone
189          */
190         public Set<Sone> getFriends() {
191                 return Collections.unmodifiableSet(friendSones);
192         }
193
194         /**
195          * Sets all friends of this Sone at once.
196          *
197          * @param friends
198          *            The new (and only) friends of this Sone
199          * @return This Sone (for method chaining)
200          */
201         public synchronized Sone setFriends(Collection<Sone> friends) {
202                 friendSones.clear();
203                 friendSones.addAll(friends);
204                 modificationCounter++;
205                 return this;
206         }
207
208         /**
209          * Returns whether this Sone has the given Sone as a friend Sone.
210          *
211          * @param friendSone
212          *            The friend Sone to check for
213          * @return {@code true} if this Sone has the given Sone as a friend,
214          *         {@code false} otherwise
215          */
216         public boolean hasFriend(Sone friendSone) {
217                 return friendSones.contains(friendSone);
218         }
219
220         /**
221          * Adds the given Sone as a friend Sone.
222          *
223          * @param friendSone
224          *            The friend Sone to add
225          * @return This Sone (for method chaining)
226          */
227         public synchronized Sone addFriend(Sone friendSone) {
228                 if (!friendSone.equals(this) && friendSones.add(friendSone)) {
229                         modificationCounter++;
230                 }
231                 return this;
232         }
233
234         /**
235          * Removes the given Sone as a friend Sone.
236          *
237          * @param friendSone
238          *            The friend Sone to remove
239          * @return This Sone (for method chaining)
240          */
241         public synchronized Sone removeFriend(Sone friendSone) {
242                 if (friendSones.remove(friendSone)) {
243                         modificationCounter++;
244                 }
245                 return this;
246         }
247
248         /**
249          * Returns the list of posts of this Sone, sorted by time, newest first.
250          *
251          * @return All posts of this Sone
252          */
253         public List<Post> getPosts() {
254                 List<Post> sortedPosts = new ArrayList<Post>(posts);
255                 Collections.sort(sortedPosts, new Comparator<Post>() {
256
257                         @Override
258                         public int compare(Post leftPost, Post rightPost) {
259                                 return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightPost.getTime() - leftPost.getTime()));
260                         }
261
262                 });
263                 return sortedPosts;
264         }
265
266         /**
267          * Sets all posts of this Sone at once.
268          *
269          * @param posts
270          *            The new (and only) posts of this Sone
271          * @return This Sone (for method chaining)
272          */
273         public synchronized Sone setPosts(Collection<Post> posts) {
274                 this.posts.clear();
275                 this.posts.addAll(posts);
276                 modificationCounter++;
277                 return this;
278         }
279
280         /**
281          * Adds the given post to this Sone. The post will not be added if its
282          * {@link Post#getSone() Sone} is not this Sone.
283          *
284          * @param post
285          *            The post to add
286          */
287         public synchronized void addPost(Post post) {
288                 if (post.getSone().equals(this) && posts.add(post)) {
289                         logger.log(Level.FINEST, "Adding %s to “%s”.", new Object[] { post, getName() });
290                         modificationCounter++;
291                 }
292         }
293
294         /**
295          * Removes the given post from this Sone.
296          *
297          * @param post
298          *            The post to remove
299          */
300         public synchronized void removePost(Post post) {
301                 if (post.getSone().equals(this) && posts.remove(post)) {
302                         modificationCounter++;
303                 }
304         }
305
306         /**
307          * Returns all replies this Sone made.
308          *
309          * @return All replies this Sone made
310          */
311         public Set<Reply> getReplies() {
312                 logger.log(Level.FINEST, "Friends of %s: %s", new Object[] { this, friendSones });
313                 return Collections.unmodifiableSet(replies);
314         }
315
316         /**
317          * Sets all replies of this Sone at once.
318          *
319          * @param replies
320          *            The new (and only) replies of this Sone
321          * @return This Sone (for method chaining)
322          */
323         public synchronized Sone setReplies(Collection<Reply> replies) {
324                 this.replies.clear();
325                 this.replies.addAll(replies);
326                 modificationCounter++;
327                 return this;
328         }
329
330         /**
331          * Adds a reply to this Sone. If the given reply was not made by this Sone,
332          * nothing is added to this Sone.
333          *
334          * @param reply
335          *            The reply to add
336          */
337         public synchronized void addReply(Reply reply) {
338                 if (reply.getSone().equals(this) && replies.add(reply)) {
339                         modificationCounter++;
340                 }
341         }
342
343         /**
344          * Removes a reply from this Sone.
345          *
346          * @param reply
347          *            The reply to remove
348          */
349         public synchronized void removeReply(Reply reply) {
350                 if (reply.getSone().equals(this) && replies.remove(reply)) {
351                         modificationCounter++;
352                 }
353         }
354
355         /**
356          * Returns the modification counter.
357          *
358          * @return The modification counter
359          */
360         public synchronized long getModificationCounter() {
361                 return modificationCounter;
362         }
363
364         /**
365          * Sets the modification counter.
366          *
367          * @param modificationCounter
368          *            The new modification counter
369          */
370         public synchronized void setModificationCounter(long modificationCounter) {
371                 this.modificationCounter = modificationCounter;
372         }
373
374         /**
375          * Updates the suggested edition in both the request URI and the insert URI.
376          *
377          * @param requestUri
378          *            The request URI that resulted from an insert
379          */
380         public void updateUris(FreenetURI requestUri) {
381                 /* TODO - check for the correct URI. */
382                 long latestEdition = requestUri.getSuggestedEdition();
383                 this.requestUri = this.requestUri.setSuggestedEdition(latestEdition);
384                 if (this.insertUri != null) {
385                         this.insertUri = this.insertUri.setSuggestedEdition(latestEdition);
386                 }
387         }
388
389         //
390         // OBJECT METHODS
391         //
392
393         /**
394          * {@inheritDoc}
395          */
396         @Override
397         public int hashCode() {
398                 return id.hashCode();
399         }
400
401         /**
402          * {@inheritDoc}
403          */
404         @Override
405         public boolean equals(Object object) {
406                 if (!(object instanceof Sone)) {
407                         return false;
408                 }
409                 return ((Sone) object).id.equals(id);
410         }
411
412         /**
413          * {@inheritDoc}
414          */
415         @Override
416         public String toString() {
417                 return getClass().getName() + "[id=" + id + ",name=" + name + ",requestUri=" + requestUri + ",insertUri=" + insertUri + ",friends(" + friendSones.size() + "),posts(" + posts.size() + "),replies(" + replies.size() + ")]";
418         }
419
420 }