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.core.Options;
31 import net.pterodactylus.sone.freenet.wot.Identity;
32 import net.pterodactylus.sone.template.SoneAccessor;
33 import net.pterodactylus.util.filter.Filter;
34 import net.pterodactylus.util.logging.Logging;
35 import freenet.keys.FreenetURI;
38 * A Sone defines everything about a user: her profile, her status updates, her
39 * replies, her likes and dislikes, etc.
41 * Operations that modify the Sone need to synchronize on the Sone in question.
43 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
45 public class Sone implements Fingerprintable, Comparable<Sone> {
47 /** comparator that sorts Sones by their nice name. */
48 public static final Comparator<Sone> NICE_NAME_COMPARATOR = new Comparator<Sone>() {
51 public int compare(Sone leftSone, Sone rightSone) {
52 int diff = SoneAccessor.getNiceName(leftSone).compareToIgnoreCase(SoneAccessor.getNiceName(rightSone));
56 return leftSone.getId().compareToIgnoreCase(rightSone.getId());
61 /** Filter to remove Sones that have not been downloaded. */
62 public static final Filter<Sone> EMPTY_SONE_FILTER = new Filter<Sone>() {
65 public boolean filterObject(Sone sone) {
66 return sone.getTime() != 0;
71 private static final Logger logger = Logging.getLogger(Sone.class);
73 /** The ID of this Sone. */
74 private final String id;
76 /** The identity of this Sone. */
77 private Identity identity;
79 /** The URI under which the Sone is stored in Freenet. */
80 private volatile FreenetURI requestUri;
82 /** The URI used to insert a new version of this Sone. */
83 /* This will be null for remote Sones! */
84 private volatile FreenetURI insertUri;
86 /** The latest edition of the Sone. */
87 private volatile long latestEdition;
89 /** The time of the last inserted update. */
90 private volatile long time;
92 /** The profile of this Sone. */
93 private volatile Profile profile = new Profile();
95 /** The client used by the Sone. */
96 private volatile Client client;
98 /** All friend Sones. */
99 private final Set<String> friendSones = Collections.synchronizedSet(new HashSet<String>());
102 private final Set<Post> posts = Collections.synchronizedSet(new HashSet<Post>());
105 private final Set<Reply> replies = Collections.synchronizedSet(new HashSet<Reply>());
107 /** The IDs of all liked posts. */
108 private final Set<String> likedPostIds = Collections.synchronizedSet(new HashSet<String>());
110 /** The IDs of all liked replies. */
111 private final Set<String> likedReplyIds = Collections.synchronizedSet(new HashSet<String>());
113 /** Sone-specific options. */
114 private final Options options = new Options();
117 * Creates a new Sone.
122 public Sone(String id) {
131 * Returns the identity of this Sone.
133 * @return The identity of this Sone
135 public String getId() {
140 * Returns the identity of this Sone.
142 * @return The identity of this Sone
144 public Identity getIdentity() {
149 * Sets the identity of this Sone. The {@link Identity#getId() ID} of the
150 * identity has to match this Sone’s {@link #getId()}.
153 * The identity of this Sone
154 * @return This Sone (for method chaining)
155 * @throws IllegalArgumentException
156 * if the ID of the identity does not match this Sone’s ID
158 public Sone setIdentity(Identity identity) throws IllegalArgumentException {
159 if (!identity.getId().equals(id)) {
160 throw new IllegalArgumentException("Identity’s ID does not match Sone’s ID!");
162 this.identity = identity;
167 * Returns the name of this Sone.
169 * @return The name of this Sone
171 public String getName() {
172 return (identity != null) ? identity.getNickname() : null;
176 * Returns the request URI of this Sone.
178 * @return The request URI of this Sone
180 public FreenetURI getRequestUri() {
181 return (requestUri != null) ? requestUri.setSuggestedEdition(latestEdition) : null;
185 * Sets the request URI of this Sone.
188 * The request URI of this Sone
189 * @return This Sone (for method chaining)
191 public Sone setRequestUri(FreenetURI requestUri) {
192 if (this.requestUri == null) {
193 this.requestUri = requestUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
196 if (!this.requestUri.equalsKeypair(requestUri)) {
197 logger.log(Level.WARNING, "Request URI %s tried to overwrite %s!", new Object[] { requestUri, this.requestUri });
204 * Returns the insert URI of this Sone.
206 * @return The insert URI of this Sone
208 public FreenetURI getInsertUri() {
209 return (insertUri != null) ? insertUri.setSuggestedEdition(latestEdition) : null;
213 * Sets the insert URI of this Sone.
216 * The insert URI of this Sone
217 * @return This Sone (for method chaining)
219 public Sone setInsertUri(FreenetURI insertUri) {
220 if (this.insertUri == null) {
221 this.insertUri = insertUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
224 if (!this.insertUri.equalsKeypair(insertUri)) {
225 logger.log(Level.WARNING, "Request URI %s tried to overwrite %s!", new Object[] { insertUri, this.insertUri });
232 * Returns the latest edition of this Sone.
234 * @return The latest edition of this Sone
236 public long getLatestEdition() {
237 return latestEdition;
241 * Sets the latest edition of this Sone. If the given latest edition is not
242 * greater than the current latest edition, the latest edition of this Sone
245 * @param latestEdition
246 * The latest edition of this Sone
248 public void setLatestEdition(long latestEdition) {
249 if (!(latestEdition > this.latestEdition)) {
250 logger.log(Level.INFO, "New latest edition %d is not greater than current latest edition %d!", new Object[] { latestEdition, this.latestEdition });
253 this.latestEdition = latestEdition;
257 * Return the time of the last inserted update of this Sone.
259 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
261 public long getTime() {
266 * Sets the time of the last inserted update of this Sone.
269 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
270 * @return This Sone (for method chaining)
272 public Sone setTime(long time) {
278 * Returns a copy of the profile. If you want to update values in the
279 * profile of this Sone, update the values in the returned {@link Profile}
280 * and use {@link #setProfile(Profile)} to change the profile in this Sone.
282 * @return A copy of the profile
284 public synchronized Profile getProfile() {
285 return new Profile(profile);
289 * Sets the profile of this Sone. A copy of the given profile is stored so
290 * that subsequent modifications of the given profile are not reflected in
296 public synchronized void setProfile(Profile profile) {
297 this.profile = new Profile(profile);
301 * Returns the client used by this Sone.
303 * @return The client used by this Sone, or {@code null}
305 public Client getClient() {
310 * Sets the client used by this Sone.
313 * The client used by this Sone, or {@code null}
314 * @return This Sone (for method chaining)
316 public Sone setClient(Client client) {
317 this.client = client;
322 * Returns all friend Sones of this Sone.
324 * @return The friend Sones of this Sone
326 public List<String> getFriends() {
327 List<String> friends = new ArrayList<String>(friendSones);
332 * Sets all friends of this Sone at once.
335 * The new (and only) friends of this Sone
336 * @return This Sone (for method chaining)
338 public Sone setFriends(Collection<String> friends) {
340 friendSones.addAll(friends);
345 * Returns whether this Sone has the given Sone as a friend Sone.
347 * @param friendSoneId
348 * The ID of the Sone to check for
349 * @return {@code true} if this Sone has the given Sone as a friend,
350 * {@code false} otherwise
352 public boolean hasFriend(String friendSoneId) {
353 return friendSones.contains(friendSoneId);
357 * Adds the given Sone as a friend Sone.
360 * The friend Sone to add
361 * @return This Sone (for method chaining)
363 public Sone addFriend(String friendSone) {
364 if (!friendSone.equals(id)) {
365 friendSones.add(friendSone);
371 * Removes the given Sone as a friend Sone.
373 * @param friendSoneId
374 * The ID of the friend Sone to remove
375 * @return This Sone (for method chaining)
377 public Sone removeFriend(String friendSoneId) {
378 friendSones.remove(friendSoneId);
383 * Returns the list of posts of this Sone, sorted by time, newest first.
385 * @return All posts of this Sone
387 public List<Post> getPosts() {
388 List<Post> sortedPosts;
389 synchronized (this) {
390 sortedPosts = new ArrayList<Post>(posts);
392 Collections.sort(sortedPosts, Post.TIME_COMPARATOR);
397 * Sets all posts of this Sone at once.
400 * The new (and only) posts of this Sone
401 * @return This Sone (for method chaining)
403 public synchronized Sone setPosts(Collection<Post> posts) {
404 synchronized (this) {
406 this.posts.addAll(posts);
412 * Adds the given post to this Sone. The post will not be added if its
413 * {@link Post#getSone() Sone} is not this Sone.
418 public synchronized void addPost(Post post) {
419 if (post.getSone().equals(this) && posts.add(post)) {
420 logger.log(Level.FINEST, "Adding %s to “%s”.", new Object[] { post, getName() });
425 * Removes the given post from this Sone.
430 public synchronized void removePost(Post post) {
431 if (post.getSone().equals(this)) {
437 * Returns all replies this Sone made.
439 * @return All replies this Sone made
441 public synchronized Set<Reply> getReplies() {
442 return Collections.unmodifiableSet(replies);
446 * Sets all replies of this Sone at once.
449 * The new (and only) replies of this Sone
450 * @return This Sone (for method chaining)
452 public synchronized Sone setReplies(Collection<Reply> replies) {
453 this.replies.clear();
454 this.replies.addAll(replies);
459 * Adds a reply to this Sone. If the given reply was not made by this Sone,
460 * nothing is added to this Sone.
465 public synchronized void addReply(Reply reply) {
466 if (reply.getSone().equals(this)) {
472 * Removes a reply from this Sone.
475 * The reply to remove
477 public synchronized void removeReply(Reply reply) {
478 if (reply.getSone().equals(this)) {
479 replies.remove(reply);
484 * Returns the IDs of all liked posts.
486 * @return All liked posts’ IDs
488 public Set<String> getLikedPostIds() {
489 return Collections.unmodifiableSet(likedPostIds);
493 * Sets the IDs of all liked posts.
495 * @param likedPostIds
496 * All liked posts’ IDs
497 * @return This Sone (for method chaining)
499 public synchronized Sone setLikePostIds(Set<String> likedPostIds) {
500 this.likedPostIds.clear();
501 this.likedPostIds.addAll(likedPostIds);
506 * Checks whether the given post ID is liked by this Sone.
510 * @return {@code true} if this Sone likes the given post, {@code false}
513 public boolean isLikedPostId(String postId) {
514 return likedPostIds.contains(postId);
518 * Adds the given post ID to the list of posts this Sone likes.
522 * @return This Sone (for method chaining)
524 public synchronized Sone addLikedPostId(String postId) {
525 likedPostIds.add(postId);
530 * Removes the given post ID from the list of posts this Sone likes.
534 * @return This Sone (for method chaining)
536 public synchronized Sone removeLikedPostId(String postId) {
537 likedPostIds.remove(postId);
542 * Returns the IDs of all liked replies.
544 * @return All liked replies’ IDs
546 public Set<String> getLikedReplyIds() {
547 return Collections.unmodifiableSet(likedReplyIds);
551 * Sets the IDs of all liked replies.
553 * @param likedReplyIds
554 * All liked replies’ IDs
555 * @return This Sone (for method chaining)
557 public synchronized Sone setLikeReplyIds(Set<String> likedReplyIds) {
558 this.likedReplyIds.clear();
559 this.likedReplyIds.addAll(likedReplyIds);
564 * Checks whether the given reply ID is liked by this Sone.
567 * The ID of the reply
568 * @return {@code true} if this Sone likes the given reply, {@code false}
571 public boolean isLikedReplyId(String replyId) {
572 return likedReplyIds.contains(replyId);
576 * Adds the given reply ID to the list of replies this Sone likes.
579 * The ID of the reply
580 * @return This Sone (for method chaining)
582 public synchronized Sone addLikedReplyId(String replyId) {
583 likedReplyIds.add(replyId);
588 * Removes the given post ID from the list of replies this Sone likes.
591 * The ID of the reply
592 * @return This Sone (for method chaining)
594 public synchronized Sone removeLikedReplyId(String replyId) {
595 likedReplyIds.remove(replyId);
600 * Returns Sone-specific options.
602 * @return The options of this Sone
604 public Options getOptions() {
609 // FINGERPRINTABLE METHODS
616 public synchronized String getFingerprint() {
617 StringBuilder fingerprint = new StringBuilder();
618 fingerprint.append(profile.getFingerprint());
620 fingerprint.append("Posts(");
621 for (Post post : getPosts()) {
622 fingerprint.append("Post(").append(post.getId()).append(')');
624 fingerprint.append(")");
626 List<Reply> replies = new ArrayList<Reply>(getReplies());
627 Collections.sort(replies, Reply.TIME_COMPARATOR);
628 fingerprint.append("Replies(");
629 for (Reply reply : replies) {
630 fingerprint.append("Reply(").append(reply.getId()).append(')');
632 fingerprint.append(')');
634 List<String> likedPostIds = new ArrayList<String>(getLikedPostIds());
635 Collections.sort(likedPostIds);
636 fingerprint.append("LikedPosts(");
637 for (String likedPostId : likedPostIds) {
638 fingerprint.append("Post(").append(likedPostId).append(')');
640 fingerprint.append(')');
642 List<String> likedReplyIds = new ArrayList<String>(getLikedReplyIds());
643 Collections.sort(likedReplyIds);
644 fingerprint.append("LikedReplies(");
645 for (String likedReplyId : likedReplyIds) {
646 fingerprint.append("Reply(").append(likedReplyId).append(')');
648 fingerprint.append(')');
650 return fingerprint.toString();
654 // INTERFACE Comparable<Sone>
661 public int compareTo(Sone sone) {
662 return NICE_NAME_COMPARATOR.compare(this, sone);
673 public int hashCode() {
674 return id.hashCode();
681 public boolean equals(Object object) {
682 if (!(object instanceof Sone)) {
685 return ((Sone) object).id.equals(id);
692 public String toString() {
693 return getClass().getName() + "[identity=" + identity + ",requestUri=" + requestUri + ",insertUri(" + String.valueOf(insertUri).length() + "),friends(" + friendSones.size() + "),posts(" + posts.size() + "),replies(" + replies.size() + ")]";