Store locality of a Sone in the Sone itself.
[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.Core;
32 import net.pterodactylus.sone.core.Options;
33 import net.pterodactylus.sone.freenet.wot.Identity;
34 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
35 import net.pterodactylus.sone.template.SoneAccessor;
36 import net.pterodactylus.util.collection.filter.Filter;
37 import net.pterodactylus.util.logging.Logging;
38 import net.pterodactylus.util.validation.Validation;
39 import freenet.keys.FreenetURI;
40
41 /**
42  * A Sone defines everything about a user: her profile, her status updates, her
43  * replies, her likes and dislikes, etc.
44  * <p>
45  * Operations that modify the Sone need to synchronize on the Sone in question.
46  *
47  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
48  */
49 public class Sone implements Fingerprintable, Comparable<Sone> {
50
51         /**
52          * Enumeration for the possible states of a {@link Sone}.
53          *
54          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
55          */
56         public enum SoneStatus {
57
58                 /** The Sone is unknown, i.e. not yet downloaded. */
59                 unknown,
60
61                 /** The Sone is idle, i.e. not being downloaded or inserted. */
62                 idle,
63
64                 /** The Sone is currently being inserted. */
65                 inserting,
66
67                 /** The Sone is currently being downloaded. */
68                 downloading,
69         }
70
71         /**
72          * The possible values for the “show custom avatars” option.
73          *
74          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
75          */
76         public static enum ShowCustomAvatars {
77
78                 /** Never show custom avatars. */
79                 NEVER,
80
81                 /** Only show custom avatars of followed Sones. */
82                 FOLLOWED,
83
84                 /** Only show custom avatars of Sones you manually trust. */
85                 MANUALLY_TRUSTED,
86
87                 /** Only show custom avatars of automatically trusted Sones. */
88                 TRUSTED,
89
90                 /** Always show custom avatars. */
91                 ALWAYS,
92
93         }
94
95         /** comparator that sorts Sones by their nice name. */
96         public static final Comparator<Sone> NICE_NAME_COMPARATOR = new Comparator<Sone>() {
97
98                 @Override
99                 public int compare(Sone leftSone, Sone rightSone) {
100                         int diff = SoneAccessor.getNiceName(leftSone).compareToIgnoreCase(SoneAccessor.getNiceName(rightSone));
101                         if (diff != 0) {
102                                 return diff;
103                         }
104                         return leftSone.getId().compareToIgnoreCase(rightSone.getId());
105                 }
106
107         };
108
109         /**
110          * Comparator that sorts Sones by last activity (least recent active first).
111          */
112         public static final Comparator<Sone> LAST_ACTIVITY_COMPARATOR = new Comparator<Sone>() {
113
114                 @Override
115                 public int compare(Sone firstSone, Sone secondSone) {
116                         return (int) Math.min(Integer.MAX_VALUE, Math.max(Integer.MIN_VALUE, secondSone.getTime() - firstSone.getTime()));
117                 }
118         };
119
120         /** Comparator that sorts Sones by numbers of posts (descending). */
121         public static final Comparator<Sone> POST_COUNT_COMPARATOR = new Comparator<Sone>() {
122
123                 /**
124                  * {@inheritDoc}
125                  */
126                 @Override
127                 public int compare(Sone leftSone, Sone rightSone) {
128                         return (leftSone.getPosts().size() != rightSone.getPosts().size()) ? (rightSone.getPosts().size() - leftSone.getPosts().size()) : (rightSone.getReplies().size() - leftSone.getReplies().size());
129                 }
130         };
131
132         /** Comparator that sorts Sones by number of images (descending). */
133         public static final Comparator<Sone> IMAGE_COUNT_COMPARATOR = new Comparator<Sone>() {
134
135                 /**
136                  * {@inheritDoc}
137                  */
138                 @Override
139                 public int compare(Sone leftSone, Sone rightSone) {
140                         return rightSone.getAllImages().size() - leftSone.getAllImages().size();
141                 }
142         };
143
144         /** Filter to remove Sones that have not been downloaded. */
145         public static final Filter<Sone> EMPTY_SONE_FILTER = new Filter<Sone>() {
146
147                 @Override
148                 public boolean filterObject(Sone sone) {
149                         return sone.getTime() != 0;
150                 }
151         };
152
153         /** Filter that matches all {@link Core#isLocalSone(Sone) local Sones}. */
154         public static final Filter<Sone> LOCAL_SONE_FILTER = new Filter<Sone>() {
155
156                 @Override
157                 public boolean filterObject(Sone sone) {
158                         return sone.getIdentity() instanceof OwnIdentity;
159                 }
160
161         };
162
163         /** Filter that matches Sones that have at least one album. */
164         public static final Filter<Sone> HAS_ALBUM_FILTER = new Filter<Sone>() {
165
166                 @Override
167                 public boolean filterObject(Sone sone) {
168                         return !sone.getAlbums().isEmpty();
169                 }
170         };
171
172         /** The logger. */
173         private static final Logger logger = Logging.getLogger(Sone.class);
174
175         /** The ID of this Sone. */
176         private final String id;
177
178         /** Whether the Sone is local. */
179         private final boolean local;
180
181         /** The identity of this Sone. */
182         private Identity identity;
183
184         /** The URI under which the Sone is stored in Freenet. */
185         private volatile FreenetURI requestUri;
186
187         /** The URI used to insert a new version of this Sone. */
188         /* This will be null for remote Sones! */
189         private volatile FreenetURI insertUri;
190
191         /** The latest edition of the Sone. */
192         private volatile long latestEdition;
193
194         /** The time of the last inserted update. */
195         private volatile long time;
196
197         /** The status of this Sone. */
198         private volatile SoneStatus status = SoneStatus.unknown;
199
200         /** The profile of this Sone. */
201         private volatile Profile profile = new Profile(this);
202
203         /** The client used by the Sone. */
204         private volatile Client client;
205
206         /** Whether this Sone is known. */
207         private volatile boolean known;
208
209         /** All friend Sones. */
210         private final Set<String> friendSones = new CopyOnWriteArraySet<String>();
211
212         /** All posts. */
213         private final Set<Post> posts = new CopyOnWriteArraySet<Post>();
214
215         /** All replies. */
216         private final Set<PostReply> replies = new CopyOnWriteArraySet<PostReply>();
217
218         /** The IDs of all liked posts. */
219         private final Set<String> likedPostIds = new CopyOnWriteArraySet<String>();
220
221         /** The IDs of all liked replies. */
222         private final Set<String> likedReplyIds = new CopyOnWriteArraySet<String>();
223
224         /** The albums of this Sone. */
225         private final List<Album> albums = new CopyOnWriteArrayList<Album>();
226
227         /** Sone-specific options. */
228         private final Options options = new Options();
229
230         /**
231          * Creates a new Sone.
232          *
233          * @param id
234          *            The ID of the Sone
235          * @param local
236          *            {@code true} if the Sone is a local Sone, {@code false}
237          *            otherwise
238          */
239         public Sone(String id, boolean local) {
240                 this.id = id;
241                 this.local = local;
242         }
243
244         //
245         // ACCESSORS
246         //
247
248         /**
249          * Returns the identity of this Sone.
250          *
251          * @return The identity of this Sone
252          */
253         public String getId() {
254                 return id;
255         }
256
257         /**
258          * Returns the identity of this Sone.
259          *
260          * @return The identity of this Sone
261          */
262         public Identity getIdentity() {
263                 return identity;
264         }
265
266         /**
267          * Sets the identity of this Sone. The {@link Identity#getId() ID} of the
268          * identity has to match this Sone’s {@link #getId()}.
269          *
270          * @param identity
271          *            The identity of this Sone
272          * @return This Sone (for method chaining)
273          * @throws IllegalArgumentException
274          *             if the ID of the identity does not match this Sone’s ID
275          */
276         public Sone setIdentity(Identity identity) throws IllegalArgumentException {
277                 if (!identity.getId().equals(id)) {
278                         throw new IllegalArgumentException("Identity’s ID does not match Sone’s ID!");
279                 }
280                 this.identity = identity;
281                 return this;
282         }
283
284         /**
285          * Returns the name of this Sone.
286          *
287          * @return The name of this Sone
288          */
289         public String getName() {
290                 return (identity != null) ? identity.getNickname() : null;
291         }
292
293         /**
294          * Returns whether this Sone is a local Sone.
295          *
296          * @return {@code true} if this Sone is a local Sone, {@code false}
297          *         otherwise
298          */
299         public boolean isLocal() {
300                 return local;
301         }
302
303         /**
304          * Returns the request URI of this Sone.
305          *
306          * @return The request URI of this Sone
307          */
308         public FreenetURI getRequestUri() {
309                 return (requestUri != null) ? requestUri.setSuggestedEdition(latestEdition) : null;
310         }
311
312         /**
313          * Sets the request URI of this Sone.
314          *
315          * @param requestUri
316          *            The request URI of this Sone
317          * @return This Sone (for method chaining)
318          */
319         public Sone setRequestUri(FreenetURI requestUri) {
320                 if (this.requestUri == null) {
321                         this.requestUri = requestUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
322                         return this;
323                 }
324                 if (!this.requestUri.equalsKeypair(requestUri)) {
325                         logger.log(Level.WARNING, String.format("Request URI %s tried to overwrite %s!", requestUri, this.requestUri));
326                         return this;
327                 }
328                 return this;
329         }
330
331         /**
332          * Returns the insert URI of this Sone.
333          *
334          * @return The insert URI of this Sone
335          */
336         public FreenetURI getInsertUri() {
337                 return (insertUri != null) ? insertUri.setSuggestedEdition(latestEdition) : null;
338         }
339
340         /**
341          * Sets the insert URI of this Sone.
342          *
343          * @param insertUri
344          *            The insert URI of this Sone
345          * @return This Sone (for method chaining)
346          */
347         public Sone setInsertUri(FreenetURI insertUri) {
348                 if (this.insertUri == null) {
349                         this.insertUri = insertUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
350                         return this;
351                 }
352                 if (!this.insertUri.equalsKeypair(insertUri)) {
353                         logger.log(Level.WARNING, String.format("Request URI %s tried to overwrite %s!", insertUri, this.insertUri));
354                         return this;
355                 }
356                 return this;
357         }
358
359         /**
360          * Returns the latest edition of this Sone.
361          *
362          * @return The latest edition of this Sone
363          */
364         public long getLatestEdition() {
365                 return latestEdition;
366         }
367
368         /**
369          * Sets the latest edition of this Sone. If the given latest edition is not
370          * greater than the current latest edition, the latest edition of this Sone
371          * is not changed.
372          *
373          * @param latestEdition
374          *            The latest edition of this Sone
375          */
376         public void setLatestEdition(long latestEdition) {
377                 if (!(latestEdition > this.latestEdition)) {
378                         logger.log(Level.FINE, String.format("New latest edition %d is not greater than current latest edition %d!", latestEdition, this.latestEdition));
379                         return;
380                 }
381                 this.latestEdition = latestEdition;
382         }
383
384         /**
385          * Return the time of the last inserted update of this Sone.
386          *
387          * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
388          */
389         public long getTime() {
390                 return time;
391         }
392
393         /**
394          * Sets the time of the last inserted update of this Sone.
395          *
396          * @param time
397          *            The time of the update (in milliseconds since Jan 1, 1970 UTC)
398          * @return This Sone (for method chaining)
399          */
400         public Sone setTime(long time) {
401                 this.time = time;
402                 return this;
403         }
404
405         /**
406          * Returns the status of this Sone.
407          *
408          * @return The status of this Sone
409          */
410         public SoneStatus getStatus() {
411                 return status;
412         }
413
414         /**
415          * Sets the new status of this Sone.
416          *
417          * @param status
418          *            The new status of this Sone
419          * @return This Sone
420          * @throws IllegalArgumentException
421          *             if {@code status} is {@code null}
422          */
423         public Sone setStatus(SoneStatus status) {
424                 Validation.begin().isNotNull("Sone Status", status).check();
425                 this.status = status;
426                 return this;
427         }
428
429         /**
430          * Returns a copy of the profile. If you want to update values in the
431          * profile of this Sone, update the values in the returned {@link Profile}
432          * and use {@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
442          * that subsequent modifications of the given profile are not reflected in
443          * this 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,
510          *         {@code 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
573          * {@link 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 a flattened list of all albums of this Sone. The resulting list
770          * contains parent albums before child albums so that the resulting list can
771          * be parsed in a single pass.
772          *
773          * @return The flattened albums
774          */
775         public List<Album> getAllAlbums() {
776                 List<Album> flatAlbums = new ArrayList<Album>();
777                 flatAlbums.addAll(albums);
778                 int lastAlbumIndex = 0;
779                 while (lastAlbumIndex < flatAlbums.size()) {
780                         int previousAlbumCount = flatAlbums.size();
781                         for (Album album : new ArrayList<Album>(flatAlbums.subList(lastAlbumIndex, flatAlbums.size()))) {
782                                 flatAlbums.addAll(album.getAlbums());
783                         }
784                         lastAlbumIndex = previousAlbumCount;
785                 }
786                 return flatAlbums;
787         }
788
789         /**
790          * Returns all images of a Sone. Images of a album are inserted into this
791          * list before images of all child albums.
792          *
793          * @return The list of all images
794          */
795         public List<Image> getAllImages() {
796                 List<Image> allImages = new ArrayList<Image>();
797                 for (Album album : getAllAlbums()) {
798                         allImages.addAll(album.getImages());
799                 }
800                 return allImages;
801         }
802
803         /**
804          * Adds an album to this Sone.
805          *
806          * @param album
807          *            The album to add
808          */
809         public void addAlbum(Album album) {
810                 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.getSone(), this).check();
811                 if (!albums.contains(album)) {
812                         albums.add(album);
813                 }
814         }
815
816         /**
817          * Sets the albums of this Sone.
818          *
819          * @param albums
820          *            The albums of this Sone
821          */
822         public void setAlbums(Collection<? extends Album> albums) {
823                 Validation.begin().isNotNull("Albums", albums).check();
824                 this.albums.clear();
825                 for (Album album : albums) {
826                         addAlbum(album);
827                 }
828         }
829
830         /**
831          * Removes an album from this Sone.
832          *
833          * @param album
834          *            The album to remove
835          */
836         public void removeAlbum(Album album) {
837                 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.getSone(), this).check();
838                 albums.remove(album);
839         }
840
841         /**
842          * Moves the given album up in this album’s albums. If the album is already
843          * the first album, nothing happens.
844          *
845          * @param album
846          *            The album to move up
847          * @return The album that the given album swapped the place with, or
848          *         <code>null</code> if the album did not change its place
849          */
850         public Album moveAlbumUp(Album album) {
851                 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.getSone(), this).isNull("Album Parent", album.getParent()).check();
852                 int oldIndex = albums.indexOf(album);
853                 if (oldIndex <= 0) {
854                         return null;
855                 }
856                 albums.remove(oldIndex);
857                 albums.add(oldIndex - 1, album);
858                 return albums.get(oldIndex);
859         }
860
861         /**
862          * Moves the given album down in this album’s albums. If the album is
863          * already the last album, nothing happens.
864          *
865          * @param album
866          *            The album to move down
867          * @return The album that the given album swapped the place with, or
868          *         <code>null</code> if the album did not change its place
869          */
870         public Album moveAlbumDown(Album album) {
871                 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.getSone(), this).isNull("Album Parent", album.getParent()).check();
872                 int oldIndex = albums.indexOf(album);
873                 if ((oldIndex < 0) || (oldIndex >= (albums.size() - 1))) {
874                         return null;
875                 }
876                 albums.remove(oldIndex);
877                 albums.add(oldIndex + 1, album);
878                 return albums.get(oldIndex);
879         }
880
881         /**
882          * Returns Sone-specific options.
883          *
884          * @return The options of this Sone
885          */
886         public Options getOptions() {
887                 return options;
888         }
889
890         //
891         // FINGERPRINTABLE METHODS
892         //
893
894         /**
895          * {@inheritDoc}
896          */
897         @Override
898         public synchronized String getFingerprint() {
899                 StringBuilder fingerprint = new StringBuilder();
900                 fingerprint.append(profile.getFingerprint());
901
902                 fingerprint.append("Posts(");
903                 for (Post post : getPosts()) {
904                         fingerprint.append("Post(").append(post.getId()).append(')');
905                 }
906                 fingerprint.append(")");
907
908                 List<PostReply> replies = new ArrayList<PostReply>(getReplies());
909                 Collections.sort(replies, Reply.TIME_COMPARATOR);
910                 fingerprint.append("Replies(");
911                 for (PostReply reply : replies) {
912                         fingerprint.append("Reply(").append(reply.getId()).append(')');
913                 }
914                 fingerprint.append(')');
915
916                 List<String> likedPostIds = new ArrayList<String>(getLikedPostIds());
917                 Collections.sort(likedPostIds);
918                 fingerprint.append("LikedPosts(");
919                 for (String likedPostId : likedPostIds) {
920                         fingerprint.append("Post(").append(likedPostId).append(')');
921                 }
922                 fingerprint.append(')');
923
924                 List<String> likedReplyIds = new ArrayList<String>(getLikedReplyIds());
925                 Collections.sort(likedReplyIds);
926                 fingerprint.append("LikedReplies(");
927                 for (String likedReplyId : likedReplyIds) {
928                         fingerprint.append("Reply(").append(likedReplyId).append(')');
929                 }
930                 fingerprint.append(')');
931
932                 fingerprint.append("Albums(");
933                 for (Album album : albums) {
934                         fingerprint.append(album.getFingerprint());
935                 }
936                 fingerprint.append(')');
937
938                 return fingerprint.toString();
939         }
940
941         //
942         // INTERFACE Comparable<Sone>
943         //
944
945         /**
946          * {@inheritDoc}
947          */
948         @Override
949         public int compareTo(Sone sone) {
950                 return NICE_NAME_COMPARATOR.compare(this, sone);
951         }
952
953         //
954         // OBJECT METHODS
955         //
956
957         /**
958          * {@inheritDoc}
959          */
960         @Override
961         public int hashCode() {
962                 return id.hashCode();
963         }
964
965         /**
966          * {@inheritDoc}
967          */
968         @Override
969         public boolean equals(Object object) {
970                 if (!(object instanceof Sone)) {
971                         return false;
972                 }
973                 return ((Sone) object).id.equals(id);
974         }
975
976         /**
977          * {@inheritDoc}
978          */
979         @Override
980         public String toString() {
981                 return getClass().getName() + "[identity=" + identity + ",requestUri=" + requestUri + ",insertUri(" + String.valueOf(insertUri).length() + "),friends(" + friendSones.size() + "),posts(" + posts.size() + "),replies(" + replies.size() + ")]";
982         }
983
984 }