Store locality of a Sone in the Sone itself, remove related methods from Database.
[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#getLocalSones() 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 this is a local Sone. */
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 this Sone is local, {@code false} 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 whether this Sone is local.
285          *
286          * @return {@code true} if this Sone is local, {@code false} otherwise
287          */
288         public boolean isLocal() {
289                 return local;
290         }
291
292         /**
293          * Returns the name of this Sone.
294          *
295          * @return The name of this Sone
296          */
297         public String getName() {
298                 return (identity != null) ? identity.getNickname() : null;
299         }
300
301         /**
302          * Returns the request URI of this Sone.
303          *
304          * @return The request URI of this Sone
305          */
306         public FreenetURI getRequestUri() {
307                 return (requestUri != null) ? requestUri.setSuggestedEdition(latestEdition) : null;
308         }
309
310         /**
311          * Sets the request URI of this Sone.
312          *
313          * @param requestUri
314          *            The request URI of this Sone
315          * @return This Sone (for method chaining)
316          */
317         public Sone setRequestUri(FreenetURI requestUri) {
318                 if (this.requestUri == null) {
319                         this.requestUri = requestUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
320                         return this;
321                 }
322                 if (!this.requestUri.equalsKeypair(requestUri)) {
323                         logger.log(Level.WARNING, String.format("Request URI %s tried to overwrite %s!", requestUri, this.requestUri));
324                         return this;
325                 }
326                 return this;
327         }
328
329         /**
330          * Returns the insert URI of this Sone.
331          *
332          * @return The insert URI of this Sone
333          */
334         public FreenetURI getInsertUri() {
335                 return (insertUri != null) ? insertUri.setSuggestedEdition(latestEdition) : null;
336         }
337
338         /**
339          * Sets the insert URI of this Sone.
340          *
341          * @param insertUri
342          *            The insert URI of this Sone
343          * @return This Sone (for method chaining)
344          */
345         public Sone setInsertUri(FreenetURI insertUri) {
346                 if (this.insertUri == null) {
347                         this.insertUri = insertUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
348                         return this;
349                 }
350                 if (!this.insertUri.equalsKeypair(insertUri)) {
351                         logger.log(Level.WARNING, String.format("Request URI %s tried to overwrite %s!", insertUri, this.insertUri));
352                         return this;
353                 }
354                 return this;
355         }
356
357         /**
358          * Returns the latest edition of this Sone.
359          *
360          * @return The latest edition of this Sone
361          */
362         public long getLatestEdition() {
363                 return latestEdition;
364         }
365
366         /**
367          * Sets the latest edition of this Sone. If the given latest edition is not
368          * greater than the current latest edition, the latest edition of this Sone
369          * is not changed.
370          *
371          * @param latestEdition
372          *            The latest edition of this Sone
373          */
374         public void setLatestEdition(long latestEdition) {
375                 if (!(latestEdition > this.latestEdition)) {
376                         logger.log(Level.FINE, String.format("New latest edition %d is not greater than current latest edition %d!", latestEdition, this.latestEdition));
377                         return;
378                 }
379                 this.latestEdition = latestEdition;
380         }
381
382         /**
383          * Return the time of the last inserted update of this Sone.
384          *
385          * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
386          */
387         public long getTime() {
388                 return time;
389         }
390
391         /**
392          * Sets the time of the last inserted update of this Sone.
393          *
394          * @param time
395          *            The time of the update (in milliseconds since Jan 1, 1970 UTC)
396          * @return This Sone (for method chaining)
397          */
398         public Sone setTime(long time) {
399                 this.time = time;
400                 return this;
401         }
402
403         /**
404          * Returns the status of this Sone.
405          *
406          * @return The status of this Sone
407          */
408         public SoneStatus getStatus() {
409                 return status;
410         }
411
412         /**
413          * Sets the new status of this Sone.
414          *
415          * @param status
416          *            The new status of this Sone
417          * @return This Sone
418          * @throws IllegalArgumentException
419          *             if {@code status} is {@code null}
420          */
421         public Sone setStatus(SoneStatus status) {
422                 Validation.begin().isNotNull("Sone Status", status).check();
423                 this.status = status;
424                 return this;
425         }
426
427         /**
428          * Returns a copy of the profile. If you want to update values in the
429          * profile of this Sone, update the values in the returned {@link Profile}
430          * and use {@link #setProfile(Profile)} to change the profile in this Sone.
431          *
432          * @return A copy of the profile
433          */
434         public Profile getProfile() {
435                 return new Profile(profile);
436         }
437
438         /**
439          * Sets the profile of this Sone. A copy of the given profile is stored so
440          * that subsequent modifications of the given profile are not reflected in
441          * this Sone!
442          *
443          * @param profile
444          *            The profile to set
445          */
446         public void setProfile(Profile profile) {
447                 this.profile = new Profile(profile);
448         }
449
450         /**
451          * Returns the client used by this Sone.
452          *
453          * @return The client used by this Sone, or {@code null}
454          */
455         public Client getClient() {
456                 return client;
457         }
458
459         /**
460          * Sets the client used by this Sone.
461          *
462          * @param client
463          *            The client used by this Sone, or {@code null}
464          * @return This Sone (for method chaining)
465          */
466         public Sone setClient(Client client) {
467                 this.client = client;
468                 return this;
469         }
470
471         /**
472          * Returns whether this Sone is known.
473          *
474          * @return {@code true} if this Sone is known, {@code false} otherwise
475          */
476         public boolean isKnown() {
477                 return known;
478         }
479
480         /**
481          * Sets whether this Sone is known.
482          *
483          * @param known
484          *            {@code true} if this Sone is known, {@code false} otherwise
485          * @return This Sone
486          */
487         public Sone setKnown(boolean known) {
488                 this.known = known;
489                 return this;
490         }
491
492         /**
493          * Returns all friend Sones of this Sone.
494          *
495          * @return The friend Sones of this Sone
496          */
497         public List<String> getFriends() {
498                 List<String> friends = new ArrayList<String>(friendSones);
499                 return friends;
500         }
501
502         /**
503          * Returns whether this Sone has the given Sone as a friend Sone.
504          *
505          * @param friendSoneId
506          *            The ID of the Sone to check for
507          * @return {@code true} if this Sone has the given Sone as a friend,
508          *         {@code false} otherwise
509          */
510         public boolean hasFriend(String friendSoneId) {
511                 return friendSones.contains(friendSoneId);
512         }
513
514         /**
515          * Adds the given Sone as a friend Sone.
516          *
517          * @param friendSone
518          *            The friend Sone to add
519          * @return This Sone (for method chaining)
520          */
521         public Sone addFriend(String friendSone) {
522                 if (!friendSone.equals(id)) {
523                         friendSones.add(friendSone);
524                 }
525                 return this;
526         }
527
528         /**
529          * Removes the given Sone as a friend Sone.
530          *
531          * @param friendSoneId
532          *            The ID of the friend Sone to remove
533          * @return This Sone (for method chaining)
534          */
535         public Sone removeFriend(String friendSoneId) {
536                 friendSones.remove(friendSoneId);
537                 return this;
538         }
539
540         /**
541          * Returns the list of posts of this Sone, sorted by time, newest first.
542          *
543          * @return All posts of this Sone
544          */
545         public List<Post> getPosts() {
546                 List<Post> sortedPosts;
547                 synchronized (this) {
548                         sortedPosts = new ArrayList<Post>(posts);
549                 }
550                 Collections.sort(sortedPosts, Post.TIME_COMPARATOR);
551                 return sortedPosts;
552         }
553
554         /**
555          * Sets all posts of this Sone at once.
556          *
557          * @param posts
558          *            The new (and only) posts of this Sone
559          * @return This Sone (for method chaining)
560          */
561         public Sone setPosts(Collection<Post> posts) {
562                 synchronized (this) {
563                         this.posts.clear();
564                         this.posts.addAll(posts);
565                 }
566                 return this;
567         }
568
569         /**
570          * Adds the given post to this Sone. The post will not be added if its
571          * {@link Post#getSone() Sone} is not this Sone.
572          *
573          * @param post
574          *            The post to add
575          */
576         public void addPost(Post post) {
577                 if (post.getSone().equals(this) && posts.add(post)) {
578                         logger.log(Level.FINEST, String.format("Adding %s to “%s”.", post, getName()));
579                 }
580         }
581
582         /**
583          * Removes the given post from this Sone.
584          *
585          * @param post
586          *            The post to remove
587          */
588         public void removePost(Post post) {
589                 if (post.getSone().equals(this)) {
590                         posts.remove(post);
591                 }
592         }
593
594         /**
595          * Returns all replies this Sone made.
596          *
597          * @return All replies this Sone made
598          */
599         public Set<PostReply> getReplies() {
600                 return Collections.unmodifiableSet(replies);
601         }
602
603         /**
604          * Sets all replies of this Sone at once.
605          *
606          * @param replies
607          *            The new (and only) replies of this Sone
608          * @return This Sone (for method chaining)
609          */
610         public Sone setReplies(Collection<PostReply> replies) {
611                 this.replies.clear();
612                 this.replies.addAll(replies);
613                 return this;
614         }
615
616         /**
617          * Adds a reply to this Sone. If the given reply was not made by this Sone,
618          * nothing is added to this Sone.
619          *
620          * @param reply
621          *            The reply to add
622          */
623         public void addReply(PostReply reply) {
624                 if (reply.getSone().equals(this)) {
625                         replies.add(reply);
626                 }
627         }
628
629         /**
630          * Removes a reply from this Sone.
631          *
632          * @param reply
633          *            The reply to remove
634          */
635         public void removeReply(PostReply reply) {
636                 if (reply.getSone().equals(this)) {
637                         replies.remove(reply);
638                 }
639         }
640
641         /**
642          * Returns the IDs of all liked posts.
643          *
644          * @return All liked posts’ IDs
645          */
646         public Set<String> getLikedPostIds() {
647                 return Collections.unmodifiableSet(likedPostIds);
648         }
649
650         /**
651          * Sets the IDs of all liked posts.
652          *
653          * @param likedPostIds
654          *            All liked posts’ IDs
655          * @return This Sone (for method chaining)
656          */
657         public Sone setLikePostIds(Set<String> likedPostIds) {
658                 this.likedPostIds.clear();
659                 this.likedPostIds.addAll(likedPostIds);
660                 return this;
661         }
662
663         /**
664          * Checks whether the given post ID is liked by this Sone.
665          *
666          * @param postId
667          *            The ID of the post
668          * @return {@code true} if this Sone likes the given post, {@code false}
669          *         otherwise
670          */
671         public boolean isLikedPostId(String postId) {
672                 return likedPostIds.contains(postId);
673         }
674
675         /**
676          * Adds the given post ID to the list of posts this Sone likes.
677          *
678          * @param postId
679          *            The ID of the post
680          * @return This Sone (for method chaining)
681          */
682         public Sone addLikedPostId(String postId) {
683                 likedPostIds.add(postId);
684                 return this;
685         }
686
687         /**
688          * Removes the given post ID from the list of posts this Sone likes.
689          *
690          * @param postId
691          *            The ID of the post
692          * @return This Sone (for method chaining)
693          */
694         public Sone removeLikedPostId(String postId) {
695                 likedPostIds.remove(postId);
696                 return this;
697         }
698
699         /**
700          * Returns the IDs of all liked replies.
701          *
702          * @return All liked replies’ IDs
703          */
704         public Set<String> getLikedReplyIds() {
705                 return Collections.unmodifiableSet(likedReplyIds);
706         }
707
708         /**
709          * Sets the IDs of all liked replies.
710          *
711          * @param likedReplyIds
712          *            All liked replies’ IDs
713          * @return This Sone (for method chaining)
714          */
715         public Sone setLikeReplyIds(Set<String> likedReplyIds) {
716                 this.likedReplyIds.clear();
717                 this.likedReplyIds.addAll(likedReplyIds);
718                 return this;
719         }
720
721         /**
722          * Checks whether the given reply ID is liked by this Sone.
723          *
724          * @param replyId
725          *            The ID of the reply
726          * @return {@code true} if this Sone likes the given reply, {@code false}
727          *         otherwise
728          */
729         public boolean isLikedReplyId(String replyId) {
730                 return likedReplyIds.contains(replyId);
731         }
732
733         /**
734          * Adds the given reply ID to the list of replies this Sone likes.
735          *
736          * @param replyId
737          *            The ID of the reply
738          * @return This Sone (for method chaining)
739          */
740         public Sone addLikedReplyId(String replyId) {
741                 likedReplyIds.add(replyId);
742                 return this;
743         }
744
745         /**
746          * Removes the given post ID from the list of replies this Sone likes.
747          *
748          * @param replyId
749          *            The ID of the reply
750          * @return This Sone (for method chaining)
751          */
752         public Sone removeLikedReplyId(String replyId) {
753                 likedReplyIds.remove(replyId);
754                 return this;
755         }
756
757         /**
758          * Returns the albums of this Sone.
759          *
760          * @return The albums of this Sone
761          */
762         public List<Album> getAlbums() {
763                 return Collections.unmodifiableList(albums);
764         }
765
766         /**
767          * Returns a flattened list of all albums of this Sone. The resulting list
768          * contains parent albums before child albums so that the resulting list can
769          * be parsed in a single pass.
770          *
771          * @return The flattened albums
772          */
773         public List<Album> getAllAlbums() {
774                 List<Album> flatAlbums = new ArrayList<Album>();
775                 flatAlbums.addAll(albums);
776                 int lastAlbumIndex = 0;
777                 while (lastAlbumIndex < flatAlbums.size()) {
778                         int previousAlbumCount = flatAlbums.size();
779                         for (Album album : new ArrayList<Album>(flatAlbums.subList(lastAlbumIndex, flatAlbums.size()))) {
780                                 flatAlbums.addAll(album.getAlbums());
781                         }
782                         lastAlbumIndex = previousAlbumCount;
783                 }
784                 return flatAlbums;
785         }
786
787         /**
788          * Returns all images of a Sone. Images of a album are inserted into this
789          * list before images of all child albums.
790          *
791          * @return The list of all images
792          */
793         public List<Image> getAllImages() {
794                 List<Image> allImages = new ArrayList<Image>();
795                 for (Album album : getAllAlbums()) {
796                         allImages.addAll(album.getImages());
797                 }
798                 return allImages;
799         }
800
801         /**
802          * Adds an album to this Sone.
803          *
804          * @param album
805          *            The album to add
806          */
807         public void addAlbum(Album album) {
808                 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.getSone(), this).check();
809                 albums.add(album);
810         }
811
812         /**
813          * Sets the albums of this Sone.
814          *
815          * @param albums
816          *            The albums of this Sone
817          */
818         public void setAlbums(Collection<? extends Album> albums) {
819                 Validation.begin().isNotNull("Albums", albums).check();
820                 this.albums.clear();
821                 for (Album album : albums) {
822                         addAlbum(album);
823                 }
824         }
825
826         /**
827          * Removes an album from this Sone.
828          *
829          * @param album
830          *            The album to remove
831          */
832         public void removeAlbum(Album album) {
833                 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.getSone(), this).check();
834                 albums.remove(album);
835         }
836
837         /**
838          * Moves the given album up in this album’s albums. If the album is already
839          * the first album, nothing happens.
840          *
841          * @param album
842          *            The album to move up
843          * @return The album that the given album swapped the place with, or
844          *         <code>null</code> if the album did not change its place
845          */
846         public Album moveAlbumUp(Album album) {
847                 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.getSone(), this).isNull("Album Parent", album.getParent()).check();
848                 int oldIndex = albums.indexOf(album);
849                 if (oldIndex <= 0) {
850                         return null;
851                 }
852                 albums.remove(oldIndex);
853                 albums.add(oldIndex - 1, album);
854                 return albums.get(oldIndex);
855         }
856
857         /**
858          * Moves the given album down in this album’s albums. If the album is
859          * already the last album, nothing happens.
860          *
861          * @param album
862          *            The album to move down
863          * @return The album that the given album swapped the place with, or
864          *         <code>null</code> if the album did not change its place
865          */
866         public Album moveAlbumDown(Album album) {
867                 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.getSone(), this).isNull("Album Parent", album.getParent()).check();
868                 int oldIndex = albums.indexOf(album);
869                 if ((oldIndex < 0) || (oldIndex >= (albums.size() - 1))) {
870                         return null;
871                 }
872                 albums.remove(oldIndex);
873                 albums.add(oldIndex + 1, album);
874                 return albums.get(oldIndex);
875         }
876
877         /**
878          * Returns Sone-specific options.
879          *
880          * @return The options of this Sone
881          */
882         public Options getOptions() {
883                 return options;
884         }
885
886         //
887         // FINGERPRINTABLE METHODS
888         //
889
890         /**
891          * {@inheritDoc}
892          */
893         @Override
894         public synchronized String getFingerprint() {
895                 StringBuilder fingerprint = new StringBuilder();
896                 fingerprint.append(profile.getFingerprint());
897
898                 fingerprint.append("Posts(");
899                 for (Post post : getPosts()) {
900                         fingerprint.append("Post(").append(post.getId()).append(')');
901                 }
902                 fingerprint.append(")");
903
904                 List<PostReply> replies = new ArrayList<PostReply>(getReplies());
905                 Collections.sort(replies, Reply.TIME_COMPARATOR);
906                 fingerprint.append("Replies(");
907                 for (PostReply reply : replies) {
908                         fingerprint.append("Reply(").append(reply.getId()).append(')');
909                 }
910                 fingerprint.append(')');
911
912                 List<String> likedPostIds = new ArrayList<String>(getLikedPostIds());
913                 Collections.sort(likedPostIds);
914                 fingerprint.append("LikedPosts(");
915                 for (String likedPostId : likedPostIds) {
916                         fingerprint.append("Post(").append(likedPostId).append(')');
917                 }
918                 fingerprint.append(')');
919
920                 List<String> likedReplyIds = new ArrayList<String>(getLikedReplyIds());
921                 Collections.sort(likedReplyIds);
922                 fingerprint.append("LikedReplies(");
923                 for (String likedReplyId : likedReplyIds) {
924                         fingerprint.append("Reply(").append(likedReplyId).append(')');
925                 }
926                 fingerprint.append(')');
927
928                 fingerprint.append("Albums(");
929                 for (Album album : albums) {
930                         fingerprint.append(album.getFingerprint());
931                 }
932                 fingerprint.append(')');
933
934                 return fingerprint.toString();
935         }
936
937         //
938         // INTERFACE Comparable<Sone>
939         //
940
941         /**
942          * {@inheritDoc}
943          */
944         @Override
945         public int compareTo(Sone sone) {
946                 return NICE_NAME_COMPARATOR.compare(this, sone);
947         }
948
949         //
950         // OBJECT METHODS
951         //
952
953         /**
954          * {@inheritDoc}
955          */
956         @Override
957         public int hashCode() {
958                 return id.hashCode();
959         }
960
961         /**
962          * {@inheritDoc}
963          */
964         @Override
965         public boolean equals(Object object) {
966                 if (!(object instanceof Sone)) {
967                         return false;
968                 }
969                 return ((Sone) object).id.equals(id);
970         }
971
972         /**
973          * {@inheritDoc}
974          */
975         @Override
976         public String toString() {
977                 return getClass().getName() + "[identity=" + identity + ",requestUri=" + requestUri + ",insertUri(" + String.valueOf(insertUri).length() + "),friends(" + friendSones.size() + "),posts(" + posts.size() + "),replies(" + replies.size() + ")]";
978         }
979
980 }