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