2 * FreenetSone - Sone.java - Copyright © 2010 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.data;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.Comparator;
24 import java.util.HashSet;
25 import java.util.List;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
30 import net.pterodactylus.sone.freenet.wot.Identity;
31 import net.pterodactylus.sone.template.SoneAccessor;
32 import net.pterodactylus.util.logging.Logging;
33 import net.pterodactylus.util.validation.Validation;
34 import freenet.keys.FreenetURI;
37 * A Sone defines everything about a user: her profile, her status updates, her
38 * replies, her likes and dislikes, etc.
40 * Operations that modify the Sone need to synchronize on the Sone in question.
42 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44 public class Sone implements Fingerprintable {
46 /** comparator that sorts Sones by their nice name. */
47 public static final Comparator<Sone> NICE_NAME_COMPARATOR = new Comparator<Sone>() {
50 public int compare(Sone leftSone, Sone rightSone) {
51 int diff = SoneAccessor.getNiceName(leftSone).compareToIgnoreCase(SoneAccessor.getNiceName(rightSone));
55 return leftSone.getId().compareToIgnoreCase(rightSone.getId());
61 private static final Logger logger = Logging.getLogger(Sone.class);
63 /** The ID of this Sone. */
64 private final String id;
66 /** The identity of this Sone. */
67 private Identity identity;
69 /** The URI under which the Sone is stored in Freenet. */
70 private volatile FreenetURI requestUri;
72 /** The URI used to insert a new version of this Sone. */
73 /* This will be null for remote Sones! */
74 private volatile FreenetURI insertUri;
76 /** The latest edition of the Sone. */
77 private volatile long latestEdition;
79 /** The time of the last inserted update. */
80 private volatile long time;
82 /** The profile of this Sone. */
83 private volatile Profile profile = new Profile();
85 /** The client used by the Sone. */
86 private volatile Client client;
88 /** All friend Sones. */
89 private final Set<String> friendSones = Collections.synchronizedSet(new HashSet<String>());
92 private final Set<Post> posts = Collections.synchronizedSet(new HashSet<Post>());
95 private final Set<Reply> replies = Collections.synchronizedSet(new HashSet<Reply>());
97 /** The IDs of all liked posts. */
98 private final Set<String> likedPostIds = Collections.synchronizedSet(new HashSet<String>());
100 /** The IDs of all liked replies. */
101 private final Set<String> likedReplyIds = Collections.synchronizedSet(new HashSet<String>());
103 /** The albums of this Sone. */
104 private final List<Album> albums = Collections.synchronizedList(new ArrayList<Album>());
107 * Creates a new Sone.
112 public Sone(String id) {
121 * Returns the identity of this Sone.
123 * @return The identity of this Sone
125 public String getId() {
130 * Returns the identity of this Sone.
132 * @return The identity of this Sone
134 public Identity getIdentity() {
139 * Sets the identity of this Sone. The {@link Identity#getId() ID} of the
140 * identity has to match this Sone’s {@link #getId()}.
143 * The identity of this Sone
144 * @return This Sone (for method chaining)
145 * @throws IllegalArgumentException
146 * if the ID of the identity does not match this Sone’s ID
148 public Sone setIdentity(Identity identity) throws IllegalArgumentException {
149 if (!identity.getId().equals(id)) {
150 throw new IllegalArgumentException("Identity’s ID does not match Sone’s ID!");
152 this.identity = identity;
157 * Returns the name of this Sone.
159 * @return The name of this Sone
161 public String getName() {
162 return (identity != null) ? identity.getNickname() : null;
166 * Returns the request URI of this Sone.
168 * @return The request URI of this Sone
170 public FreenetURI getRequestUri() {
171 return (requestUri != null) ? requestUri.setSuggestedEdition(latestEdition) : null;
175 * Sets the request URI of this Sone.
178 * The request URI of this Sone
179 * @return This Sone (for method chaining)
181 public Sone setRequestUri(FreenetURI requestUri) {
182 if (this.requestUri == null) {
183 this.requestUri = requestUri.setDocName("Sone").setMetaString(new String[0]);
186 if (!this.requestUri.equalsKeypair(requestUri)) {
187 logger.log(Level.WARNING, "Request URI %s tried to overwrite %s!", new Object[] { requestUri, this.requestUri });
194 * Returns the insert URI of this Sone.
196 * @return The insert URI of this Sone
198 public FreenetURI getInsertUri() {
199 return (insertUri != null) ? insertUri.setSuggestedEdition(latestEdition) : null;
203 * Sets the insert URI of this Sone.
206 * The insert URI of this Sone
207 * @return This Sone (for method chaining)
209 public Sone setInsertUri(FreenetURI insertUri) {
210 if (this.insertUri == null) {
211 this.insertUri = insertUri.setDocName("Sone").setMetaString(new String[0]);
214 if (!this.insertUri.equalsKeypair(insertUri)) {
215 logger.log(Level.WARNING, "Request URI %s tried to overwrite %s!", new Object[] { insertUri, this.insertUri });
222 * Returns the latest edition of this Sone.
224 * @return The latest edition of this Sone
226 public long getLatestEdition() {
227 return latestEdition;
231 * Sets the latest edition of this Sone. If the given latest edition is not
232 * greater than the current latest edition, the latest edition of this Sone
235 * @param latestEdition
236 * The latest edition of this Sone
238 public void setLatestEdition(long latestEdition) {
239 if (!(latestEdition > this.latestEdition)) {
240 logger.log(Level.INFO, "New latest edition %d is not greater than current latest edition %d!", new Object[] { latestEdition, this.latestEdition });
243 this.latestEdition = latestEdition;
247 * Return the time of the last inserted update of this Sone.
249 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
251 public long getTime() {
256 * Sets the time of the last inserted update of this Sone.
259 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
260 * @return This Sone (for method chaining)
262 public Sone setTime(long time) {
268 * Returns a copy of the profile. If you want to update values in the
269 * profile of this Sone, update the values in the returned {@link Profile}
270 * and use {@link #setProfile(Profile)} to change the profile in this Sone.
272 * @return A copy of the profile
274 public synchronized Profile getProfile() {
275 return new Profile(profile);
279 * Sets the profile of this Sone. A copy of the given profile is stored so
280 * that subsequent modifications of the given profile are not reflected in
286 public synchronized void setProfile(Profile profile) {
287 this.profile = new Profile(profile);
291 * Returns the client used by this Sone.
293 * @return The client used by this Sone, or {@code null}
295 public Client getClient() {
300 * Sets the client used by this Sone.
303 * The client used by this Sone, or {@code null}
304 * @return This Sone (for method chaining)
306 public Sone setClient(Client client) {
307 this.client = client;
312 * Returns all friend Sones of this Sone.
314 * @return The friend Sones of this Sone
316 public List<String> getFriends() {
317 List<String> friends = new ArrayList<String>(friendSones);
322 * Sets all friends of this Sone at once.
325 * The new (and only) friends of this Sone
326 * @return This Sone (for method chaining)
328 public Sone setFriends(Collection<String> friends) {
330 friendSones.addAll(friends);
335 * Returns whether this Sone has the given Sone as a friend Sone.
337 * @param friendSoneId
338 * The ID of the Sone to check for
339 * @return {@code true} if this Sone has the given Sone as a friend,
340 * {@code false} otherwise
342 public boolean hasFriend(String friendSoneId) {
343 return friendSones.contains(friendSoneId);
347 * Adds the given Sone as a friend Sone.
350 * The friend Sone to add
351 * @return This Sone (for method chaining)
353 public Sone addFriend(String friendSone) {
354 if (!friendSone.equals(id)) {
355 friendSones.add(friendSone);
361 * Removes the given Sone as a friend Sone.
363 * @param friendSoneId
364 * The ID of the friend Sone to remove
365 * @return This Sone (for method chaining)
367 public Sone removeFriend(String friendSoneId) {
368 friendSones.remove(friendSoneId);
373 * Returns the list of posts of this Sone, sorted by time, newest first.
375 * @return All posts of this Sone
377 public List<Post> getPosts() {
378 List<Post> sortedPosts;
379 synchronized (this) {
380 sortedPosts = new ArrayList<Post>(posts);
382 Collections.sort(sortedPosts, Post.TIME_COMPARATOR);
387 * Sets all posts of this Sone at once.
390 * The new (and only) posts of this Sone
391 * @return This Sone (for method chaining)
393 public synchronized Sone setPosts(Collection<Post> posts) {
395 this.posts.addAll(posts);
400 * Adds the given post to this Sone. The post will not be added if its
401 * {@link Post#getSone() Sone} is not this Sone.
406 public synchronized void addPost(Post post) {
407 if (post.getSone().equals(this) && posts.add(post)) {
408 logger.log(Level.FINEST, "Adding %s to “%s”.", new Object[] { post, getName() });
413 * Removes the given post from this Sone.
418 public synchronized void removePost(Post post) {
419 if (post.getSone().equals(this)) {
425 * Returns all replies this Sone made.
427 * @return All replies this Sone made
429 public synchronized Set<Reply> getReplies() {
430 return Collections.unmodifiableSet(replies);
434 * Sets all replies of this Sone at once.
437 * The new (and only) replies of this Sone
438 * @return This Sone (for method chaining)
440 public synchronized Sone setReplies(Collection<Reply> replies) {
441 this.replies.clear();
442 this.replies.addAll(replies);
447 * Adds a reply to this Sone. If the given reply was not made by this Sone,
448 * nothing is added to this Sone.
453 public synchronized void addReply(Reply reply) {
454 if (reply.getSone().equals(this)) {
460 * Removes a reply from this Sone.
463 * The reply to remove
465 public synchronized void removeReply(Reply reply) {
466 if (reply.getSone().equals(this)) {
467 replies.remove(reply);
472 * Returns the IDs of all liked posts.
474 * @return All liked posts’ IDs
476 public Set<String> getLikedPostIds() {
477 return Collections.unmodifiableSet(likedPostIds);
481 * Sets the IDs of all liked posts.
483 * @param likedPostIds
484 * All liked posts’ IDs
485 * @return This Sone (for method chaining)
487 public synchronized Sone setLikePostIds(Set<String> likedPostIds) {
488 this.likedPostIds.clear();
489 this.likedPostIds.addAll(likedPostIds);
494 * Checks whether the given post ID is liked by this Sone.
498 * @return {@code true} if this Sone likes the given post, {@code false}
501 public boolean isLikedPostId(String postId) {
502 return likedPostIds.contains(postId);
506 * Adds the given post ID to the list of posts this Sone likes.
510 * @return This Sone (for method chaining)
512 public synchronized Sone addLikedPostId(String postId) {
513 likedPostIds.add(postId);
518 * Removes the given post ID from the list of posts this Sone likes.
522 * @return This Sone (for method chaining)
524 public synchronized Sone removeLikedPostId(String postId) {
525 likedPostIds.remove(postId);
530 * Returns the IDs of all liked replies.
532 * @return All liked replies’ IDs
534 public Set<String> getLikedReplyIds() {
535 return Collections.unmodifiableSet(likedReplyIds);
539 * Sets the IDs of all liked replies.
541 * @param likedReplyIds
542 * All liked replies’ IDs
543 * @return This Sone (for method chaining)
545 public synchronized Sone setLikeReplyIds(Set<String> likedReplyIds) {
546 this.likedReplyIds.clear();
547 this.likedReplyIds.addAll(likedReplyIds);
552 * Checks whether the given reply ID is liked by this Sone.
555 * The ID of the reply
556 * @return {@code true} if this Sone likes the given reply, {@code false}
559 public boolean isLikedReplyId(String replyId) {
560 return likedReplyIds.contains(replyId);
564 * Adds the given reply ID to the list of replies this Sone likes.
567 * The ID of the reply
568 * @return This Sone (for method chaining)
570 public synchronized Sone addLikedReplyId(String replyId) {
571 likedReplyIds.add(replyId);
576 * Removes the given post ID from the list of replies this Sone likes.
579 * The ID of the reply
580 * @return This Sone (for method chaining)
582 public synchronized Sone removeLikedReplyId(String replyId) {
583 likedReplyIds.remove(replyId);
588 * Returns the albums of this Sone.
590 * @return The albums of this Sone
592 public List<Album> getAlbums() {
593 return Collections.unmodifiableList(albums);
597 * Adds an album to this Sone.
602 public synchronized void addAlbum(Album album) {
603 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.getSone(), this).check();
608 * Removes an album from this Sone.
611 * The album to remove
613 public synchronized void removeAlbum(Album album) {
614 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.getSone(), this).check();
615 albums.remove(album);
619 // FINGERPRINTABLE METHODS
626 public synchronized String getFingerprint() {
627 StringBuilder fingerprint = new StringBuilder();
628 fingerprint.append("Profile(");
629 if (profile.getFirstName() != null) {
630 fingerprint.append("FirstName(").append(profile.getFirstName()).append(')');
632 if (profile.getMiddleName() != null) {
633 fingerprint.append("MiddleName(").append(profile.getMiddleName()).append(')');
635 if (profile.getLastName() != null) {
636 fingerprint.append("LastName(").append(profile.getLastName()).append(')');
638 if (profile.getBirthDay() != null) {
639 fingerprint.append("BirthDay(").append(profile.getBirthDay()).append(')');
641 if (profile.getBirthMonth() != null) {
642 fingerprint.append("BirthMonth(").append(profile.getBirthMonth()).append(')');
644 if (profile.getBirthYear() != null) {
645 fingerprint.append("BirthYear(").append(profile.getBirthYear()).append(')');
647 fingerprint.append(")");
649 fingerprint.append("Posts(");
650 for (Post post : getPosts()) {
651 fingerprint.append("Post(").append(post.getId()).append(')');
653 fingerprint.append(")");
655 List<Reply> replies = new ArrayList<Reply>(getReplies());
656 Collections.sort(replies, Reply.TIME_COMPARATOR);
657 fingerprint.append("Replies(");
658 for (Reply reply : replies) {
659 fingerprint.append("Reply(").append(reply.getId()).append(')');
661 fingerprint.append(')');
663 List<String> likedPostIds = new ArrayList<String>(getLikedPostIds());
664 Collections.sort(likedPostIds);
665 fingerprint.append("LikedPosts(");
666 for (String likedPostId : likedPostIds) {
667 fingerprint.append("Post(").append(likedPostId).append(')');
669 fingerprint.append(')');
671 List<String> likedReplyIds = new ArrayList<String>(getLikedReplyIds());
672 Collections.sort(likedReplyIds);
673 fingerprint.append("LikedReplies(");
674 for (String likedReplyId : likedReplyIds) {
675 fingerprint.append("Reply(").append(likedReplyId).append(')');
677 fingerprint.append(')');
679 fingerprint.append("Albums(");
680 for (Album album : albums) {
681 fingerprint.append(album.getFingerprint());
683 fingerprint.append(')');
685 return fingerprint.toString();
696 public int hashCode() {
697 return id.hashCode();
704 public boolean equals(Object object) {
705 if (!(object instanceof Sone)) {
708 return ((Sone) object).id.equals(id);
715 public String toString() {
716 return getClass().getName() + "[identity=" + identity + ",requestUri=" + requestUri + ",insertUri(" + String.valueOf(insertUri).length() + "),friends(" + friendSones.size() + "),posts(" + posts.size() + "),replies(" + replies.size() + ")]";