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