Formatting.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / Sone.java
1 /*
2  * Sone - Sone.java - Copyright © 2010–2013 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 static com.google.common.base.Preconditions.*;
21
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.Comparator;
26 import java.util.List;
27 import java.util.Set;
28 import java.util.concurrent.CopyOnWriteArrayList;
29 import java.util.concurrent.CopyOnWriteArraySet;
30 import java.util.logging.Level;
31 import java.util.logging.Logger;
32
33 import net.pterodactylus.sone.core.Options;
34 import net.pterodactylus.sone.freenet.wot.Identity;
35 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
36 import net.pterodactylus.sone.template.SoneAccessor;
37 import net.pterodactylus.util.logging.Logging;
38
39 import freenet.keys.FreenetURI;
40
41 import com.google.common.base.Predicate;
42 import com.google.common.collect.FluentIterable;
43 import com.google.common.hash.Hasher;
44 import com.google.common.hash.Hashing;
45
46 /**
47  * A Sone defines everything about a user: her profile, her status updates, her
48  * replies, her likes and dislikes, etc.
49  * <p/>
50  * Operations that modify the Sone need to synchronize on the Sone in question.
51  *
52  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
53  */
54 public class Sone implements Fingerprintable, Comparable<Sone> {
55
56         /**
57          * Enumeration for the possible states of a {@link Sone}.
58          *
59          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
60          */
61         public enum SoneStatus {
62
63                 /** The Sone is unknown, i.e. not yet downloaded. */
64                 unknown,
65
66                 /** The Sone is idle, i.e. not being downloaded or inserted. */
67                 idle,
68
69                 /** The Sone is currently being inserted. */
70                 inserting,
71
72                 /** The Sone is currently being downloaded. */
73                 downloading,
74         }
75
76         /**
77          * The possible values for the “show custom avatars” option.
78          *
79          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
80          */
81         public static enum ShowCustomAvatars {
82
83                 /** Never show custom avatars. */
84                 NEVER,
85
86                 /** Only show custom avatars of followed Sones. */
87                 FOLLOWED,
88
89                 /** Only show custom avatars of Sones you manually trust. */
90                 MANUALLY_TRUSTED,
91
92                 /** Only show custom avatars of automatically trusted Sones. */
93                 TRUSTED,
94
95                 /** Always show custom avatars. */
96                 ALWAYS,
97
98         }
99
100         /** comparator that sorts Sones by their nice name. */
101         public static final Comparator<Sone> NICE_NAME_COMPARATOR = new Comparator<Sone>() {
102
103                 @Override
104                 public int compare(Sone leftSone, Sone rightSone) {
105                         int diff = SoneAccessor.getNiceName(leftSone).compareToIgnoreCase(SoneAccessor.getNiceName(rightSone));
106                         if (diff != 0) {
107                                 return diff;
108                         }
109                         return leftSone.getId().compareToIgnoreCase(rightSone.getId());
110                 }
111
112         };
113
114         /** Comparator that sorts Sones by last activity (least recent active first). */
115         public static final Comparator<Sone> LAST_ACTIVITY_COMPARATOR = new Comparator<Sone>() {
116
117                 @Override
118                 public int compare(Sone firstSone, Sone secondSone) {
119                         return (int) Math.min(Integer.MAX_VALUE, Math.max(Integer.MIN_VALUE, secondSone.getTime() - firstSone.getTime()));
120                 }
121         };
122
123         /** Comparator that sorts Sones by numbers of posts (descending). */
124         public static final Comparator<Sone> POST_COUNT_COMPARATOR = new Comparator<Sone>() {
125
126                 /**
127                  * {@inheritDoc}
128                  */
129                 @Override
130                 public int compare(Sone leftSone, Sone rightSone) {
131                         return (leftSone.getPosts().size() != rightSone.getPosts().size()) ? (rightSone.getPosts().size() - leftSone.getPosts().size()) : (rightSone.getReplies().size() - leftSone.getReplies().size());
132                 }
133         };
134
135         /** Comparator that sorts Sones by number of images (descending). */
136         public static final Comparator<Sone> IMAGE_COUNT_COMPARATOR = new Comparator<Sone>() {
137
138                 /**
139                  * {@inheritDoc}
140                  */
141                 @Override
142                 public int compare(Sone leftSone, Sone rightSone) {
143                         return rightSone.getAllImages().size() - leftSone.getAllImages().size();
144                 }
145         };
146
147         /** Filter to remove Sones that have not been downloaded. */
148         public static final Predicate<Sone> EMPTY_SONE_FILTER = new Predicate<Sone>() {
149
150                 @Override
151                 public boolean apply(Sone sone) {
152                         return sone.getTime() != 0;
153                 }
154         };
155
156         /** Filter that matches all {@link Sone#isLocal() local Sones}. */
157         public static final Predicate<Sone> LOCAL_SONE_FILTER = new Predicate<Sone>() {
158
159                 @Override
160                 public boolean apply(Sone sone) {
161                         return sone.getIdentity() instanceof OwnIdentity;
162                 }
163
164         };
165
166         /** Filter that matches Sones that have at least one album. */
167         public static final Predicate<Sone> HAS_ALBUM_FILTER = new Predicate<Sone>() {
168
169                 @Override
170                 public boolean apply(Sone sone) {
171                         return !sone.getAlbums().isEmpty();
172                 }
173         };
174
175         /** The logger. */
176         private static final Logger logger = Logging.getLogger(Sone.class);
177
178         /** The ID of this Sone. */
179         private final String id;
180
181         /** Whether the Sone is local. */
182         private final boolean local;
183
184         /** The identity of this Sone. */
185         private Identity identity;
186
187         /** The URI under which the Sone is stored in Freenet. */
188         private volatile FreenetURI requestUri;
189
190         /** The URI used to insert a new version of this Sone. */
191         /* This will be null for remote Sones! */
192         private volatile FreenetURI insertUri;
193
194         /** The latest edition of the Sone. */
195         private volatile long latestEdition;
196
197         /** The time of the last inserted update. */
198         private volatile long time;
199
200         /** The status of this Sone. */
201         private volatile SoneStatus status = SoneStatus.unknown;
202
203         /** The profile of this Sone. */
204         private volatile Profile profile = new Profile(this);
205
206         /** The client used by the Sone. */
207         private volatile Client client;
208
209         /** Whether this Sone is known. */
210         private volatile boolean known;
211
212         /** All friend Sones. */
213         private final Set<String> friendSones = new CopyOnWriteArraySet<String>();
214
215         /** All posts. */
216         private final Set<Post> posts = new CopyOnWriteArraySet<Post>();
217
218         /** All replies. */
219         private final Set<PostReply> replies = new CopyOnWriteArraySet<PostReply>();
220
221         /** The IDs of all liked posts. */
222         private final Set<String> likedPostIds = new CopyOnWriteArraySet<String>();
223
224         /** The IDs of all liked replies. */
225         private final Set<String> likedReplyIds = new CopyOnWriteArraySet<String>();
226
227         /** The albums of this Sone. */
228         private final List<Album> albums = new CopyOnWriteArrayList<Album>();
229
230         /** Sone-specific options. */
231         private Options options = new Options();
232
233         /**
234          * Creates a new Sone.
235          *
236          * @param id
237          *              The ID of the Sone
238          * @param local
239          *              {@code true} if the Sone is a local Sone, {@code false} otherwise
240          */
241         public Sone(String id, boolean local) {
242                 this.id = id;
243                 this.local = local;
244         }
245
246         //
247         // ACCESSORS
248         //
249
250         /**
251          * Returns the identity of this Sone.
252          *
253          * @return The identity of this Sone
254          */
255         public String getId() {
256                 return id;
257         }
258
259         /**
260          * Returns the identity of this Sone.
261          *
262          * @return The identity of this Sone
263          */
264         public Identity getIdentity() {
265                 return identity;
266         }
267
268         /**
269          * Sets the identity of this Sone. The {@link Identity#getId() ID} of the
270          * identity has to match this Sone’s {@link #getId()}.
271          *
272          * @param identity
273          *              The identity of this Sone
274          * @return This Sone (for method chaining)
275          * @throws IllegalArgumentException
276          *              if the ID of the identity does not match this Sone’s ID
277          */
278         public Sone setIdentity(Identity identity) throws IllegalArgumentException {
279                 if (!identity.getId().equals(id)) {
280                         throw new IllegalArgumentException("Identity’s ID does not match Sone’s ID!");
281                 }
282                 this.identity = identity;
283                 return this;
284         }
285
286         /**
287          * Returns the name of this Sone.
288          *
289          * @return The name of this Sone
290          */
291         public String getName() {
292                 return (identity != null) ? identity.getNickname() : null;
293         }
294
295         /**
296          * Returns whether this Sone is a local Sone.
297          *
298          * @return {@code true} if this Sone is a local Sone, {@code false} otherwise
299          */
300         public boolean isLocal() {
301                 return local;
302         }
303
304         /**
305          * Returns the request URI of this Sone.
306          *
307          * @return The request URI of this Sone
308          */
309         public FreenetURI getRequestUri() {
310                 return (requestUri != null) ? requestUri.setSuggestedEdition(latestEdition) : null;
311         }
312
313         /**
314          * Sets the request URI of this Sone.
315          *
316          * @param requestUri
317          *              The request URI of this Sone
318          * @return This Sone (for method chaining)
319          */
320         public Sone setRequestUri(FreenetURI requestUri) {
321                 if (this.requestUri == null) {
322                         this.requestUri = requestUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
323                         return this;
324                 }
325                 if (!this.requestUri.equalsKeypair(requestUri)) {
326                         logger.log(Level.WARNING, String.format("Request URI %s tried to overwrite %s!", requestUri, this.requestUri));
327                         return this;
328                 }
329                 return this;
330         }
331
332         /**
333          * Returns the insert URI of this Sone.
334          *
335          * @return The insert URI of this Sone
336          */
337         public FreenetURI getInsertUri() {
338                 return (insertUri != null) ? insertUri.setSuggestedEdition(latestEdition) : null;
339         }
340
341         /**
342          * Sets the insert URI of this Sone.
343          *
344          * @param insertUri
345          *              The insert URI of this Sone
346          * @return This Sone (for method chaining)
347          */
348         public Sone setInsertUri(FreenetURI insertUri) {
349                 if (this.insertUri == null) {
350                         this.insertUri = insertUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
351                         return this;
352                 }
353                 if (!this.insertUri.equalsKeypair(insertUri)) {
354                         logger.log(Level.WARNING, String.format("Request URI %s tried to overwrite %s!", insertUri, this.insertUri));
355                         return this;
356                 }
357                 return this;
358         }
359
360         /**
361          * Returns the latest edition of this Sone.
362          *
363          * @return The latest edition of this Sone
364          */
365         public long getLatestEdition() {
366                 return latestEdition;
367         }
368
369         /**
370          * Sets the latest edition of this Sone. If the given latest edition is not
371          * greater than the current latest edition, the latest edition of this Sone is
372          * not changed.
373          *
374          * @param latestEdition
375          *              The latest edition of this Sone
376          */
377         public void setLatestEdition(long latestEdition) {
378                 if (!(latestEdition > this.latestEdition)) {
379                         logger.log(Level.FINE, String.format("New latest edition %d is not greater than current latest edition %d!", latestEdition, this.latestEdition));
380                         return;
381                 }
382                 this.latestEdition = latestEdition;
383         }
384
385         /**
386          * Return the time of the last inserted update of this Sone.
387          *
388          * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
389          */
390         public long getTime() {
391                 return time;
392         }
393
394         /**
395          * Sets the time of the last inserted update of this Sone.
396          *
397          * @param time
398          *              The time of the update (in milliseconds since Jan 1, 1970 UTC)
399          * @return This Sone (for method chaining)
400          */
401         public Sone setTime(long time) {
402                 this.time = time;
403                 return this;
404         }
405
406         /**
407          * Returns the status of this Sone.
408          *
409          * @return The status of this Sone
410          */
411         public SoneStatus getStatus() {
412                 return status;
413         }
414
415         /**
416          * Sets the new status of this Sone.
417          *
418          * @param status
419          *              The new status of this Sone
420          * @return This Sone
421          * @throws IllegalArgumentException
422          *              if {@code status} is {@code null}
423          */
424         public Sone setStatus(SoneStatus status) {
425                 this.status = checkNotNull(status, "status must not be null");
426                 return this;
427         }
428
429         /**
430          * Returns a copy of the profile. If you want to update values in the profile
431          * of this Sone, update the values in the returned {@link Profile} and use
432          * {@link #setProfile(Profile)} to change the profile in this Sone.
433          *
434          * @return A copy of the profile
435          */
436         public Profile getProfile() {
437                 return new Profile(profile);
438         }
439
440         /**
441          * Sets the profile of this Sone. A copy of the given profile is stored so that
442          * subsequent modifications of the given profile are not reflected in this
443          * Sone!
444          *
445          * @param profile
446          *              The profile to set
447          */
448         public void setProfile(Profile profile) {
449                 this.profile = new Profile(profile);
450         }
451
452         /**
453          * Returns the client used by this Sone.
454          *
455          * @return The client used by this Sone, or {@code null}
456          */
457         public Client getClient() {
458                 return client;
459         }
460
461         /**
462          * Sets the client used by this Sone.
463          *
464          * @param client
465          *              The client used by this Sone, or {@code null}
466          * @return This Sone (for method chaining)
467          */
468         public Sone setClient(Client client) {
469                 this.client = client;
470                 return this;
471         }
472
473         /**
474          * Returns whether this Sone is known.
475          *
476          * @return {@code true} if this Sone is known, {@code false} otherwise
477          */
478         public boolean isKnown() {
479                 return known;
480         }
481
482         /**
483          * Sets whether this Sone is known.
484          *
485          * @param known
486          *              {@code true} if this Sone is known, {@code false} otherwise
487          * @return This Sone
488          */
489         public Sone setKnown(boolean known) {
490                 this.known = known;
491                 return this;
492         }
493
494         /**
495          * Returns all friend Sones of this Sone.
496          *
497          * @return The friend Sones of this Sone
498          */
499         public List<String> getFriends() {
500                 List<String> friends = new ArrayList<String>(friendSones);
501                 return friends;
502         }
503
504         /**
505          * Returns whether this Sone has the given Sone as a friend Sone.
506          *
507          * @param friendSoneId
508          *              The ID of the Sone to check for
509          * @return {@code true} if this Sone has the given Sone as a friend, {@code
510          *         false} otherwise
511          */
512         public boolean hasFriend(String friendSoneId) {
513                 return friendSones.contains(friendSoneId);
514         }
515
516         /**
517          * Adds the given Sone as a friend Sone.
518          *
519          * @param friendSone
520          *              The friend Sone to add
521          * @return This Sone (for method chaining)
522          */
523         public Sone addFriend(String friendSone) {
524                 if (!friendSone.equals(id)) {
525                         friendSones.add(friendSone);
526                 }
527                 return this;
528         }
529
530         /**
531          * Removes the given Sone as a friend Sone.
532          *
533          * @param friendSoneId
534          *              The ID of the friend Sone to remove
535          * @return This Sone (for method chaining)
536          */
537         public Sone removeFriend(String friendSoneId) {
538                 friendSones.remove(friendSoneId);
539                 return this;
540         }
541
542         /**
543          * Returns the list of posts of this Sone, sorted by time, newest first.
544          *
545          * @return All posts of this Sone
546          */
547         public List<Post> getPosts() {
548                 List<Post> sortedPosts;
549                 synchronized (this) {
550                         sortedPosts = new ArrayList<Post>(posts);
551                 }
552                 Collections.sort(sortedPosts, Post.TIME_COMPARATOR);
553                 return sortedPosts;
554         }
555
556         /**
557          * Sets all posts of this Sone at once.
558          *
559          * @param posts
560          *              The new (and only) posts of this Sone
561          * @return This Sone (for method chaining)
562          */
563         public Sone setPosts(Collection<Post> posts) {
564                 synchronized (this) {
565                         this.posts.clear();
566                         this.posts.addAll(posts);
567                 }
568                 return this;
569         }
570
571         /**
572          * Adds the given post to this Sone. The post will not be added if its {@link
573          * Post#getSone() Sone} is not this Sone.
574          *
575          * @param post
576          *              The post to add
577          */
578         public void addPost(Post post) {
579                 if (post.getSone().equals(this) && posts.add(post)) {
580                         logger.log(Level.FINEST, String.format("Adding %s to “%s”.", post, getName()));
581                 }
582         }
583
584         /**
585          * Removes the given post from this Sone.
586          *
587          * @param post
588          *              The post to remove
589          */
590         public void removePost(Post post) {
591                 if (post.getSone().equals(this)) {
592                         posts.remove(post);
593                 }
594         }
595
596         /**
597          * Returns all replies this Sone made.
598          *
599          * @return All replies this Sone made
600          */
601         public Set<PostReply> getReplies() {
602                 return Collections.unmodifiableSet(replies);
603         }
604
605         /**
606          * Sets all replies of this Sone at once.
607          *
608          * @param replies
609          *              The new (and only) replies of this Sone
610          * @return This Sone (for method chaining)
611          */
612         public Sone setReplies(Collection<PostReply> replies) {
613                 this.replies.clear();
614                 this.replies.addAll(replies);
615                 return this;
616         }
617
618         /**
619          * Adds a reply to this Sone. If the given reply was not made by this Sone,
620          * nothing is added to this Sone.
621          *
622          * @param reply
623          *              The reply to add
624          */
625         public void addReply(PostReply reply) {
626                 if (reply.getSone().equals(this)) {
627                         replies.add(reply);
628                 }
629         }
630
631         /**
632          * Removes a reply from this Sone.
633          *
634          * @param reply
635          *              The reply to remove
636          */
637         public void removeReply(PostReply reply) {
638                 if (reply.getSone().equals(this)) {
639                         replies.remove(reply);
640                 }
641         }
642
643         /**
644          * Returns the IDs of all liked posts.
645          *
646          * @return All liked posts’ IDs
647          */
648         public Set<String> getLikedPostIds() {
649                 return Collections.unmodifiableSet(likedPostIds);
650         }
651
652         /**
653          * Sets the IDs of all liked posts.
654          *
655          * @param likedPostIds
656          *              All liked posts’ IDs
657          * @return This Sone (for method chaining)
658          */
659         public Sone setLikePostIds(Set<String> likedPostIds) {
660                 this.likedPostIds.clear();
661                 this.likedPostIds.addAll(likedPostIds);
662                 return this;
663         }
664
665         /**
666          * Checks whether the given post ID is liked by this Sone.
667          *
668          * @param postId
669          *              The ID of the post
670          * @return {@code true} if this Sone likes the given post, {@code false}
671          *         otherwise
672          */
673         public boolean isLikedPostId(String postId) {
674                 return likedPostIds.contains(postId);
675         }
676
677         /**
678          * Adds the given post ID to the list of posts this Sone likes.
679          *
680          * @param postId
681          *              The ID of the post
682          * @return This Sone (for method chaining)
683          */
684         public Sone addLikedPostId(String postId) {
685                 likedPostIds.add(postId);
686                 return this;
687         }
688
689         /**
690          * Removes the given post ID from the list of posts this Sone likes.
691          *
692          * @param postId
693          *              The ID of the post
694          * @return This Sone (for method chaining)
695          */
696         public Sone removeLikedPostId(String postId) {
697                 likedPostIds.remove(postId);
698                 return this;
699         }
700
701         /**
702          * Returns the IDs of all liked replies.
703          *
704          * @return All liked replies’ IDs
705          */
706         public Set<String> getLikedReplyIds() {
707                 return Collections.unmodifiableSet(likedReplyIds);
708         }
709
710         /**
711          * Sets the IDs of all liked replies.
712          *
713          * @param likedReplyIds
714          *              All liked replies’ IDs
715          * @return This Sone (for method chaining)
716          */
717         public Sone setLikeReplyIds(Set<String> likedReplyIds) {
718                 this.likedReplyIds.clear();
719                 this.likedReplyIds.addAll(likedReplyIds);
720                 return this;
721         }
722
723         /**
724          * Checks whether the given reply ID is liked by this Sone.
725          *
726          * @param replyId
727          *              The ID of the reply
728          * @return {@code true} if this Sone likes the given reply, {@code false}
729          *         otherwise
730          */
731         public boolean isLikedReplyId(String replyId) {
732                 return likedReplyIds.contains(replyId);
733         }
734
735         /**
736          * Adds the given reply ID to the list of replies this Sone likes.
737          *
738          * @param replyId
739          *              The ID of the reply
740          * @return This Sone (for method chaining)
741          */
742         public Sone addLikedReplyId(String replyId) {
743                 likedReplyIds.add(replyId);
744                 return this;
745         }
746
747         /**
748          * Removes the given post ID from the list of replies this Sone likes.
749          *
750          * @param replyId
751          *              The ID of the reply
752          * @return This Sone (for method chaining)
753          */
754         public Sone removeLikedReplyId(String replyId) {
755                 likedReplyIds.remove(replyId);
756                 return this;
757         }
758
759         /**
760          * Returns the albums of this Sone.
761          *
762          * @return The albums of this Sone
763          */
764         public List<Album> getAlbums() {
765                 return Collections.unmodifiableList(albums);
766         }
767
768         /**
769          * Returns all images of a Sone. Images of a album are inserted into this list
770          * before images of all child albums.
771          *
772          * @return The list of all images
773          */
774         public List<Image> getAllImages() {
775                 List<Image> allImages = new ArrayList<Image>();
776                 for (Album album : FluentIterable.from(getAlbums()).transformAndConcat(Album.FLATTENER).toList()) {
777                         allImages.addAll(album.getImages());
778                 }
779                 return allImages;
780         }
781
782         /**
783          * Adds an album to this Sone.
784          *
785          * @param album
786          *              The album to add
787          */
788         public void addAlbum(Album album) {
789                 checkNotNull(album, "album must not be null");
790                 checkArgument(album.getSone().equals(this), "album must belong to this Sone");
791                 if (!albums.contains(album)) {
792                         albums.add(album);
793                 }
794         }
795
796         /**
797          * Sets the albums of this Sone.
798          *
799          * @param albums
800          *              The albums of this Sone
801          */
802         public void setAlbums(Collection<? extends Album> albums) {
803                 checkNotNull(albums, "albums must not be null");
804                 this.albums.clear();
805                 for (Album album : albums) {
806                         addAlbum(album);
807                 }
808         }
809
810         /**
811          * Removes an album from this Sone.
812          *
813          * @param album
814          *              The album to remove
815          */
816         public void removeAlbum(Album album) {
817                 checkNotNull(album, "album must not be null");
818                 checkArgument(album.getSone().equals(this), "album must belong to this Sone");
819                 albums.remove(album);
820         }
821
822         /**
823          * Moves the given album up in this album’s albums. If the album is already the
824          * first album, nothing happens.
825          *
826          * @param album
827          *              The album to move up
828          * @return The album that the given album swapped the place with, or
829          *         <code>null</code> if the album did not change its place
830          */
831         public Album moveAlbumUp(Album album) {
832                 checkNotNull(album, "album must not be null");
833                 checkArgument(album.getSone().equals(this), "album must belong to this Sone");
834                 checkArgument(album.getParent() == null, "album must not have a parent");
835                 int oldIndex = albums.indexOf(album);
836                 if (oldIndex <= 0) {
837                         return null;
838                 }
839                 albums.remove(oldIndex);
840                 albums.add(oldIndex - 1, album);
841                 return albums.get(oldIndex);
842         }
843
844         /**
845          * Moves the given album down in this album’s albums. If the album is already
846          * the last album, nothing happens.
847          *
848          * @param album
849          *              The album to move down
850          * @return The album that the given album swapped the place with, or
851          *         <code>null</code> if the album did not change its place
852          */
853         public Album moveAlbumDown(Album album) {
854                 checkNotNull(album, "album must not be null");
855                 checkArgument(album.getSone().equals(this), "album must belong to this Sone");
856                 checkArgument(album.getParent() == null, "album must not have a parent");
857                 int oldIndex = albums.indexOf(album);
858                 if ((oldIndex < 0) || (oldIndex >= (albums.size() - 1))) {
859                         return null;
860                 }
861                 albums.remove(oldIndex);
862                 albums.add(oldIndex + 1, album);
863                 return albums.get(oldIndex);
864         }
865
866         /**
867          * Returns Sone-specific options.
868          *
869          * @return The options of this Sone
870          */
871         public Options getOptions() {
872                 return options;
873         }
874
875         /**
876          * Sets the options of this Sone.
877          *
878          * @param options
879          *              The options of this Sone
880          */
881         /* TODO - remove this method again, maybe add an option provider */
882         public void setOptions(Options options) {
883                 this.options = options;
884         }
885
886         //
887         // FINGERPRINTABLE METHODS
888         //
889
890         /** {@inheritDoc} */
891         @Override
892         public synchronized String getFingerprint() {
893                 Hasher hash = Hashing.sha256().newHasher();
894                 hash.putString(profile.getFingerprint());
895
896                 hash.putString("Posts(");
897                 for (Post post : getPosts()) {
898                         hash.putString("Post(").putString(post.getId()).putString(")");
899                 }
900                 hash.putString(")");
901
902                 List<PostReply> replies = new ArrayList<PostReply>(getReplies());
903                 Collections.sort(replies, Reply.TIME_COMPARATOR);
904                 hash.putString("Replies(");
905                 for (PostReply reply : replies) {
906                         hash.putString("Reply(").putString(reply.getId()).putString(")");
907                 }
908                 hash.putString(")");
909
910                 List<String> likedPostIds = new ArrayList<String>(getLikedPostIds());
911                 Collections.sort(likedPostIds);
912                 hash.putString("LikedPosts(");
913                 for (String likedPostId : likedPostIds) {
914                         hash.putString("Post(").putString(likedPostId).putString(")");
915                 }
916                 hash.putString(")");
917
918                 List<String> likedReplyIds = new ArrayList<String>(getLikedReplyIds());
919                 Collections.sort(likedReplyIds);
920                 hash.putString("LikedReplies(");
921                 for (String likedReplyId : likedReplyIds) {
922                         hash.putString("Reply(").putString(likedReplyId).putString(")");
923                 }
924                 hash.putString(")");
925
926                 hash.putString("Albums(");
927                 for (Album album : albums) {
928                         hash.putString(album.getFingerprint());
929                 }
930                 hash.putString(")");
931
932                 return hash.hash().toString();
933         }
934
935         //
936         // INTERFACE Comparable<Sone>
937         //
938
939         /** {@inheritDoc} */
940         @Override
941         public int compareTo(Sone sone) {
942                 return NICE_NAME_COMPARATOR.compare(this, sone);
943         }
944
945         //
946         // OBJECT METHODS
947         //
948
949         /** {@inheritDoc} */
950         @Override
951         public int hashCode() {
952                 return id.hashCode();
953         }
954
955         /** {@inheritDoc} */
956         @Override
957         public boolean equals(Object object) {
958                 if (!(object instanceof Sone)) {
959                         return false;
960                 }
961                 return ((Sone) object).id.equals(id);
962         }
963
964         /** {@inheritDoc} */
965         @Override
966         public String toString() {
967                 return getClass().getName() + "[identity=" + identity + ",requestUri=" + requestUri + ",insertUri(" + String.valueOf(insertUri).length() + "),friends(" + friendSones.size() + "),posts(" + posts.size() + "),replies(" + replies.size() + ")]";
968         }
969
970 }