Implement “like” button.
[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.sone.template.SoneAccessor;
32 import net.pterodactylus.util.logging.Logging;
33 import freenet.keys.FreenetURI;
34
35 /**
36  * A Sone defines everything about a user: her profile, her status updates, her
37  * replies, her likes and dislikes, etc.
38  * <p>
39  * Operations that modify the Sone need to synchronize on the Sone in question.
40  *
41  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42  */
43 public class Sone {
44
45         /** The logger. */
46         private static final Logger logger = Logging.getLogger(Sone.class);
47
48         /** A GUID for this Sone. */
49         private final UUID id;
50
51         /** The name of this Sone. */
52         private String name;
53
54         /** The URI under which the Sone is stored in Freenet. */
55         private FreenetURI requestUri;
56
57         /** The URI used to insert a new version of this Sone. */
58         /* This will be null for remote Sones! */
59         private FreenetURI insertUri;
60
61         /** The time of the last inserted update. */
62         private long time;
63
64         /** The profile of this Sone. */
65         private Profile profile;
66
67         /** All friend Sones. */
68         private final Set<Sone> friendSones = new HashSet<Sone>();
69
70         /** All posts. */
71         private final Set<Post> posts = new HashSet<Post>();
72
73         /** All replies. */
74         private final Set<Reply> replies = new HashSet<Reply>();
75
76         /** The IDs of all blocked Sones. */
77         private final Set<String> blockedSoneIds = new HashSet<String>();
78
79         /** The IDs of all liked posts. */
80         private final Set<String> likedPostIds = new HashSet<String>();
81
82         /** Modification count. */
83         private volatile long modificationCounter = 0;
84
85         /**
86          * Creates a new Sone.
87          *
88          * @param id
89          *            The ID of this Sone
90          */
91         public Sone(String id) {
92                 this.id = UUID.fromString(id);
93         }
94
95         //
96         // ACCESSORS
97         //
98
99         /**
100          * Returns the ID of this Sone.
101          *
102          * @return The ID of this Sone
103          */
104         public String getId() {
105                 return id.toString();
106         }
107
108         /**
109          * Returns the name of this Sone.
110          *
111          * @return The name of this Sone
112          */
113         public String getName() {
114                 return name;
115         }
116
117         /**
118          * Sets the name of this Sone.
119          *
120          * @param name
121          *            The name of this Sone
122          * @return This sone (for method chaining)
123          */
124         public Sone setName(String name) {
125                 this.name = name;
126                 return this;
127         }
128
129         /**
130          * Returns the request URI of this Sone.
131          *
132          * @return The request URI of this Sone
133          */
134         public FreenetURI getRequestUri() {
135                 return requestUri;
136         }
137
138         /**
139          * Sets the request URI of this Sone.
140          *
141          * @param requestUri
142          *            The request URI of this Sone
143          * @return This Sone (for method chaining)
144          */
145         public Sone setRequestUri(FreenetURI requestUri) {
146                 this.requestUri = requestUri;
147                 return this;
148         }
149
150         /**
151          * Returns the insert URI of this Sone.
152          *
153          * @return The insert URI of this Sone
154          */
155         public FreenetURI getInsertUri() {
156                 return insertUri;
157         }
158
159         /**
160          * Sets the insert URI of this Sone.
161          *
162          * @param insertUri
163          *            The insert URI of this Sone
164          * @return This Sone (for method chaining)
165          */
166         public Sone setInsertUri(FreenetURI insertUri) {
167                 this.insertUri = insertUri;
168                 return this;
169         }
170
171         /**
172          * Return the time of the last inserted update of this Sone.
173          *
174          * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
175          */
176         public long getTime() {
177                 return time;
178         }
179
180         /**
181          * Sets the time of the last inserted update of this Sone.
182          *
183          * @param time
184          *            The time of the update (in milliseconds since Jan 1, 1970 UTC)
185          * @return This Sone (for method chaining)
186          */
187         public Sone setTime(long time) {
188                 this.time = time;
189                 return this;
190         }
191
192         /**
193          * Returns a copy of the profile. If you want to update values in the
194          * profile of this Sone, update the values in the returned {@link Profile}
195          * and use {@link #setProfile(Profile)} to change the profile in this Sone.
196          *
197          * @return A copy of the profile
198          */
199         public Profile getProfile() {
200                 return new Profile(profile);
201         }
202
203         /**
204          * Sets the profile of this Sone. A copy of the given profile is stored so
205          * that subsequent modifications of the given profile are not reflected in
206          * this Sone!
207          *
208          * @param profile
209          *            The profile to set
210          */
211         public synchronized void setProfile(Profile profile) {
212                 this.profile = new Profile(profile);
213                 modificationCounter++;
214         }
215
216         /**
217          * Returns all friend Sones of this Sone.
218          *
219          * @return The friend Sones of this Sone
220          */
221         public List<Sone> getFriends() {
222                 List<Sone> friends = new ArrayList<Sone>(friendSones);
223                 Collections.sort(friends, new Comparator<Sone>() {
224
225                         @Override
226                         public int compare(Sone leftSone, Sone rightSone) {
227                                 int diff = SoneAccessor.getNiceName(leftSone).compareTo(SoneAccessor.getNiceName(rightSone));
228                                 if (diff != 0) {
229                                         return diff;
230                                 }
231                                 return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightSone.getTime() - leftSone.getTime()));
232                         }
233                 });
234                 return friends;
235         }
236
237         /**
238          * Sets all friends of this Sone at once.
239          *
240          * @param friends
241          *            The new (and only) friends of this Sone
242          * @return This Sone (for method chaining)
243          */
244         public Sone setFriends(Collection<Sone> friends) {
245                 friendSones.clear();
246                 friendSones.addAll(friends);
247                 return this;
248         }
249
250         /**
251          * Returns whether this Sone has the given Sone as a friend Sone.
252          *
253          * @param friendSone
254          *            The friend Sone to check for
255          * @return {@code true} if this Sone has the given Sone as a friend,
256          *         {@code false} otherwise
257          */
258         public boolean hasFriend(Sone friendSone) {
259                 return friendSones.contains(friendSone);
260         }
261
262         /**
263          * Adds the given Sone as a friend Sone.
264          *
265          * @param friendSone
266          *            The friend Sone to add
267          * @return This Sone (for method chaining)
268          */
269         public Sone addFriend(Sone friendSone) {
270                 if (!friendSone.equals(this)) {
271                         friendSones.add(friendSone);
272                 }
273                 return this;
274         }
275
276         /**
277          * Removes the given Sone as a friend Sone.
278          *
279          * @param friendSone
280          *            The friend Sone to remove
281          * @return This Sone (for method chaining)
282          */
283         public Sone removeFriend(Sone friendSone) {
284                 friendSones.remove(friendSone);
285                 return this;
286         }
287
288         /**
289          * Returns the list of posts of this Sone, sorted by time, newest first.
290          *
291          * @return All posts of this Sone
292          */
293         public List<Post> getPosts() {
294                 List<Post> sortedPosts = new ArrayList<Post>(posts);
295                 Collections.sort(sortedPosts, new Comparator<Post>() {
296
297                         @Override
298                         public int compare(Post leftPost, Post rightPost) {
299                                 return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightPost.getTime() - leftPost.getTime()));
300                         }
301
302                 });
303                 return sortedPosts;
304         }
305
306         /**
307          * Sets all posts of this Sone at once.
308          *
309          * @param posts
310          *            The new (and only) posts of this Sone
311          * @return This Sone (for method chaining)
312          */
313         public synchronized Sone setPosts(Collection<Post> posts) {
314                 this.posts.clear();
315                 this.posts.addAll(posts);
316                 modificationCounter++;
317                 return this;
318         }
319
320         /**
321          * Adds the given post to this Sone. The post will not be added if its
322          * {@link Post#getSone() Sone} is not this Sone.
323          *
324          * @param post
325          *            The post to add
326          */
327         public synchronized void addPost(Post post) {
328                 if (post.getSone().equals(this) && posts.add(post)) {
329                         logger.log(Level.FINEST, "Adding %s to “%s”.", new Object[] { post, getName() });
330                         modificationCounter++;
331                 }
332         }
333
334         /**
335          * Removes the given post from this Sone.
336          *
337          * @param post
338          *            The post to remove
339          */
340         public synchronized void removePost(Post post) {
341                 if (post.getSone().equals(this) && posts.remove(post)) {
342                         modificationCounter++;
343                 }
344         }
345
346         /**
347          * Returns all replies this Sone made.
348          *
349          * @return All replies this Sone made
350          */
351         public Set<Reply> getReplies() {
352                 logger.log(Level.FINEST, "Friends of %s: %s", new Object[] { this, friendSones });
353                 return Collections.unmodifiableSet(replies);
354         }
355
356         /**
357          * Sets all replies of this Sone at once.
358          *
359          * @param replies
360          *            The new (and only) replies of this Sone
361          * @return This Sone (for method chaining)
362          */
363         public synchronized Sone setReplies(Collection<Reply> replies) {
364                 this.replies.clear();
365                 this.replies.addAll(replies);
366                 modificationCounter++;
367                 return this;
368         }
369
370         /**
371          * Adds a reply to this Sone. If the given reply was not made by this Sone,
372          * nothing is added to this Sone.
373          *
374          * @param reply
375          *            The reply to add
376          */
377         public synchronized void addReply(Reply reply) {
378                 if (reply.getSone().equals(this) && replies.add(reply)) {
379                         modificationCounter++;
380                 }
381         }
382
383         /**
384          * Removes a reply from this Sone.
385          *
386          * @param reply
387          *            The reply to remove
388          */
389         public synchronized void removeReply(Reply reply) {
390                 if (reply.getSone().equals(this) && replies.remove(reply)) {
391                         modificationCounter++;
392                 }
393         }
394
395         /**
396          * Returns the IDs of all blocked Sones. These Sones will not propagated
397          * using the “known Sones” mechanism.
398          *
399          * @return The IDs of all blocked Sones
400          */
401         public Set<String> getBlockedSoneIds() {
402                 return Collections.unmodifiableSet(blockedSoneIds);
403         }
404
405         /**
406          * Returns whether the given Sone ID is blocked.
407          *
408          * @param soneId
409          *            The Sone ID to check
410          * @return {@code true} if the given Sone ID is blocked, {@code false}
411          *         otherwise
412          */
413         public boolean isSoneBlocked(String soneId) {
414                 return blockedSoneIds.contains(soneId);
415         }
416
417         /**
418          * Adds the given ID to the list of blocked IDs.
419          *
420          * @param soneId
421          *            The Sone ID to block
422          */
423         public synchronized void addBlockedSoneId(String soneId) {
424                 if (blockedSoneIds.add(soneId)) {
425                         modificationCounter++;
426                 }
427         }
428
429         /**
430          * Removes the given ID from the list of blocked IDs.
431          *
432          * @param soneId
433          *            The Sone ID to unblock
434          */
435         public synchronized void removeBlockedSoneId(String soneId) {
436                 if (blockedSoneIds.remove(soneId)) {
437                         modificationCounter++;
438                 }
439         }
440
441         /**
442          * Returns the IDs of all liked posts.
443          *
444          * @return All liked posts’ IDs
445          */
446         public Set<String> getLikedPostIds() {
447                 return Collections.unmodifiableSet(likedPostIds);
448         }
449
450         /**
451          * Sets the IDs of all liked posts.
452          *
453          * @param likedPostIds
454          *            All liked posts’ IDs
455          * @return This Sone (for method chaining)
456          */
457         public synchronized Sone setLikePostIds(Set<String> likedPostIds) {
458                 this.likedPostIds.clear();
459                 this.likedPostIds.addAll(likedPostIds);
460                 modificationCounter++;
461                 return this;
462         }
463
464         /**
465          * Checks whether the given post ID is liked by this Sone.
466          *
467          * @param postId
468          *            The ID of the post
469          * @return {@code true} if this Sone likes the given post, {@code false}
470          *         otherwise
471          */
472         public boolean isLikedPostId(String postId) {
473                 return likedPostIds.contains(postId);
474         }
475
476         /**
477          * Adds the given post ID to the list of posts this Sone likes.
478          *
479          * @param postId
480          *            The ID of the post
481          * @return This Sone (for method chaining)
482          */
483         public synchronized Sone addLikedPostId(String postId) {
484                 if (likedPostIds.add(postId)) {
485                         modificationCounter++;
486                 }
487                 return this;
488         }
489
490         /**
491          * Removes the given post ID from the list of posts this Sone likes.
492          *
493          * @param postId
494          *            The ID of the post
495          * @return This Sone (for method chaining)
496          */
497         public synchronized Sone removeLikedPostId(String postId) {
498                 if (likedPostIds.remove(postId)) {
499                         modificationCounter++;
500                 }
501                 return this;
502         }
503
504         /**
505          * Returns the modification counter.
506          *
507          * @return The modification counter
508          */
509         public synchronized long getModificationCounter() {
510                 return modificationCounter;
511         }
512
513         /**
514          * Sets the modification counter.
515          *
516          * @param modificationCounter
517          *            The new modification counter
518          */
519         public synchronized void setModificationCounter(long modificationCounter) {
520                 this.modificationCounter = modificationCounter;
521         }
522
523         /**
524          * Updates the suggested edition in both the request URI and the insert URI.
525          *
526          * @param requestUri
527          *            The request URI that resulted from an insert
528          */
529         public void updateUris(FreenetURI requestUri) {
530                 /* TODO - check for the correct URI. */
531                 long latestEdition = requestUri.getSuggestedEdition();
532                 this.requestUri = this.requestUri.setSuggestedEdition(latestEdition);
533                 if (this.insertUri != null) {
534                         this.insertUri = this.insertUri.setSuggestedEdition(latestEdition);
535                 }
536         }
537
538         //
539         // OBJECT METHODS
540         //
541
542         /**
543          * {@inheritDoc}
544          */
545         @Override
546         public int hashCode() {
547                 return id.hashCode();
548         }
549
550         /**
551          * {@inheritDoc}
552          */
553         @Override
554         public boolean equals(Object object) {
555                 if (!(object instanceof Sone)) {
556                         return false;
557                 }
558                 return ((Sone) object).id.equals(id);
559         }
560
561         /**
562          * {@inheritDoc}
563          */
564         @Override
565         public String toString() {
566                 return getClass().getName() + "[id=" + id + ",name=" + name + ",requestUri=" + requestUri + ",insertUri=" + insertUri + ",friends(" + friendSones.size() + "),posts(" + posts.size() + "),replies(" + replies.size() + ")]";
567         }
568
569 }