2 * Sone - 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.Core;
31 import net.pterodactylus.sone.core.Options;
32 import net.pterodactylus.sone.freenet.wot.Identity;
33 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
34 import net.pterodactylus.sone.template.SoneAccessor;
35 import net.pterodactylus.util.filter.Filter;
36 import net.pterodactylus.util.logging.Logging;
37 import freenet.keys.FreenetURI;
40 * A Sone defines everything about a user: her profile, her status updates, her
41 * replies, her likes and dislikes, etc.
43 * Operations that modify the Sone need to synchronize on the Sone in question.
45 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
47 public class Sone implements Fingerprintable, Comparable<Sone> {
49 /** comparator that sorts Sones by their nice name. */
50 public static final Comparator<Sone> NICE_NAME_COMPARATOR = new Comparator<Sone>() {
53 public int compare(Sone leftSone, Sone rightSone) {
54 int diff = SoneAccessor.getNiceName(leftSone).compareToIgnoreCase(SoneAccessor.getNiceName(rightSone));
58 return leftSone.getId().compareToIgnoreCase(rightSone.getId());
64 * Comparator that sorts Sones by last activity (least recent active first).
66 public static final Comparator<Sone> LAST_ACTIVITY_COMPARATOR = new Comparator<Sone>() {
69 public int compare(Sone firstSone, Sone secondSone) {
70 return (int) Math.min(Integer.MAX_VALUE, Math.max(Integer.MIN_VALUE, secondSone.getTime() - firstSone.getTime()));
74 /** Comparator that sorts Sones by numbers of posts (descending). */
75 public static final Comparator<Sone> POST_COUNT_COMPARATOR = new Comparator<Sone>() {
81 public int compare(Sone leftSone, Sone rightSone) {
82 return (leftSone.getPosts().size() != rightSone.getPosts().size()) ? (rightSone.getPosts().size() - leftSone.getPosts().size()) : (rightSone.getReplies().size() - leftSone.getReplies().size());
86 /** Filter to remove Sones that have not been downloaded. */
87 public static final Filter<Sone> EMPTY_SONE_FILTER = new Filter<Sone>() {
90 public boolean filterObject(Sone sone) {
91 return sone.getTime() != 0;
95 /** Filter that matches all {@link Core#isLocalSone(Sone) local Sones}. */
96 public static final Filter<Sone> LOCAL_SONE_FILTER = new Filter<Sone>() {
99 public boolean filterObject(Sone sone) {
100 return sone.getIdentity() instanceof OwnIdentity;
106 private static final Logger logger = Logging.getLogger(Sone.class);
108 /** The ID of this Sone. */
109 private final String id;
111 /** The identity of this Sone. */
112 private Identity identity;
114 /** The URI under which the Sone is stored in Freenet. */
115 private volatile FreenetURI requestUri;
117 /** The URI used to insert a new version of this Sone. */
118 /* This will be null for remote Sones! */
119 private volatile FreenetURI insertUri;
121 /** The latest edition of the Sone. */
122 private volatile long latestEdition;
124 /** The time of the last inserted update. */
125 private volatile long time;
127 /** The profile of this Sone. */
128 private volatile Profile profile = new Profile();
130 /** The client used by the Sone. */
131 private volatile Client client;
133 /** All friend Sones. */
134 private final Set<String> friendSones = Collections.synchronizedSet(new HashSet<String>());
137 private final Set<Post> posts = Collections.synchronizedSet(new HashSet<Post>());
140 private final Set<Reply> replies = Collections.synchronizedSet(new HashSet<Reply>());
142 /** The IDs of all liked posts. */
143 private final Set<String> likedPostIds = Collections.synchronizedSet(new HashSet<String>());
145 /** The IDs of all liked replies. */
146 private final Set<String> likedReplyIds = Collections.synchronizedSet(new HashSet<String>());
148 /** Sone-specific options. */
149 private final Options options = new Options();
152 * Creates a new Sone.
157 public Sone(String id) {
166 * Returns the identity of this Sone.
168 * @return The identity of this Sone
170 public String getId() {
175 * Returns the identity of this Sone.
177 * @return The identity of this Sone
179 public Identity getIdentity() {
184 * Sets the identity of this Sone. The {@link Identity#getId() ID} of the
185 * identity has to match this Sone’s {@link #getId()}.
188 * The identity of this Sone
189 * @return This Sone (for method chaining)
190 * @throws IllegalArgumentException
191 * if the ID of the identity does not match this Sone’s ID
193 public Sone setIdentity(Identity identity) throws IllegalArgumentException {
194 if (!identity.getId().equals(id)) {
195 throw new IllegalArgumentException("Identity’s ID does not match Sone’s ID!");
197 this.identity = identity;
202 * Returns the name of this Sone.
204 * @return The name of this Sone
206 public String getName() {
207 return (identity != null) ? identity.getNickname() : null;
211 * Returns the request URI of this Sone.
213 * @return The request URI of this Sone
215 public FreenetURI getRequestUri() {
216 return (requestUri != null) ? requestUri.setSuggestedEdition(latestEdition) : null;
220 * Sets the request URI of this Sone.
223 * The request URI of this Sone
224 * @return This Sone (for method chaining)
226 public Sone setRequestUri(FreenetURI requestUri) {
227 if (this.requestUri == null) {
228 this.requestUri = requestUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
231 if (!this.requestUri.equalsKeypair(requestUri)) {
232 logger.log(Level.WARNING, "Request URI %s tried to overwrite %s!", new Object[] { requestUri, this.requestUri });
239 * Returns the insert URI of this Sone.
241 * @return The insert URI of this Sone
243 public FreenetURI getInsertUri() {
244 return (insertUri != null) ? insertUri.setSuggestedEdition(latestEdition) : null;
248 * Sets the insert URI of this Sone.
251 * The insert URI of this Sone
252 * @return This Sone (for method chaining)
254 public Sone setInsertUri(FreenetURI insertUri) {
255 if (this.insertUri == null) {
256 this.insertUri = insertUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
259 if (!this.insertUri.equalsKeypair(insertUri)) {
260 logger.log(Level.WARNING, "Request URI %s tried to overwrite %s!", new Object[] { insertUri, this.insertUri });
267 * Returns the latest edition of this Sone.
269 * @return The latest edition of this Sone
271 public long getLatestEdition() {
272 return latestEdition;
276 * Sets the latest edition of this Sone. If the given latest edition is not
277 * greater than the current latest edition, the latest edition of this Sone
280 * @param latestEdition
281 * The latest edition of this Sone
283 public void setLatestEdition(long latestEdition) {
284 if (!(latestEdition > this.latestEdition)) {
285 logger.log(Level.INFO, "New latest edition %d is not greater than current latest edition %d!", new Object[] { latestEdition, this.latestEdition });
288 this.latestEdition = latestEdition;
292 * Return the time of the last inserted update of this Sone.
294 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
296 public long getTime() {
301 * Sets the time of the last inserted update of this Sone.
304 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
305 * @return This Sone (for method chaining)
307 public Sone setTime(long time) {
313 * Returns a copy of the profile. If you want to update values in the
314 * profile of this Sone, update the values in the returned {@link Profile}
315 * and use {@link #setProfile(Profile)} to change the profile in this Sone.
317 * @return A copy of the profile
319 public synchronized Profile getProfile() {
320 return new Profile(profile);
324 * Sets the profile of this Sone. A copy of the given profile is stored so
325 * that subsequent modifications of the given profile are not reflected in
331 public synchronized void setProfile(Profile profile) {
332 this.profile = new Profile(profile);
336 * Returns the client used by this Sone.
338 * @return The client used by this Sone, or {@code null}
340 public Client getClient() {
345 * Sets the client used by this Sone.
348 * The client used by this Sone, or {@code null}
349 * @return This Sone (for method chaining)
351 public Sone setClient(Client client) {
352 this.client = client;
357 * Returns all friend Sones of this Sone.
359 * @return The friend Sones of this Sone
361 public List<String> getFriends() {
362 List<String> friends = new ArrayList<String>(friendSones);
367 * Sets all friends of this Sone at once.
370 * The new (and only) friends of this Sone
371 * @return This Sone (for method chaining)
373 public Sone setFriends(Collection<String> friends) {
375 friendSones.addAll(friends);
380 * Returns whether this Sone has the given Sone as a friend Sone.
382 * @param friendSoneId
383 * The ID of the Sone to check for
384 * @return {@code true} if this Sone has the given Sone as a friend,
385 * {@code false} otherwise
387 public boolean hasFriend(String friendSoneId) {
388 return friendSones.contains(friendSoneId);
392 * Adds the given Sone as a friend Sone.
395 * The friend Sone to add
396 * @return This Sone (for method chaining)
398 public Sone addFriend(String friendSone) {
399 if (!friendSone.equals(id)) {
400 friendSones.add(friendSone);
406 * Removes the given Sone as a friend Sone.
408 * @param friendSoneId
409 * The ID of the friend Sone to remove
410 * @return This Sone (for method chaining)
412 public Sone removeFriend(String friendSoneId) {
413 friendSones.remove(friendSoneId);
418 * Returns the list of posts of this Sone, sorted by time, newest first.
420 * @return All posts of this Sone
422 public List<Post> getPosts() {
423 List<Post> sortedPosts;
424 synchronized (this) {
425 sortedPosts = new ArrayList<Post>(posts);
427 Collections.sort(sortedPosts, Post.TIME_COMPARATOR);
432 * Sets all posts of this Sone at once.
435 * The new (and only) posts of this Sone
436 * @return This Sone (for method chaining)
438 public synchronized Sone setPosts(Collection<Post> posts) {
439 synchronized (this) {
441 this.posts.addAll(posts);
447 * Adds the given post to this Sone. The post will not be added if its
448 * {@link Post#getSone() Sone} is not this Sone.
453 public synchronized void addPost(Post post) {
454 if (post.getSone().equals(this) && posts.add(post)) {
455 logger.log(Level.FINEST, "Adding %s to “%s”.", new Object[] { post, getName() });
460 * Removes the given post from this Sone.
465 public synchronized void removePost(Post post) {
466 if (post.getSone().equals(this)) {
472 * Returns all replies this Sone made.
474 * @return All replies this Sone made
476 public synchronized Set<Reply> getReplies() {
477 return Collections.unmodifiableSet(replies);
481 * Sets all replies of this Sone at once.
484 * The new (and only) replies of this Sone
485 * @return This Sone (for method chaining)
487 public synchronized Sone setReplies(Collection<Reply> replies) {
488 this.replies.clear();
489 this.replies.addAll(replies);
494 * Adds a reply to this Sone. If the given reply was not made by this Sone,
495 * nothing is added to this Sone.
500 public synchronized void addReply(Reply reply) {
501 if (reply.getSone().equals(this)) {
507 * Removes a reply from this Sone.
510 * The reply to remove
512 public synchronized void removeReply(Reply reply) {
513 if (reply.getSone().equals(this)) {
514 replies.remove(reply);
519 * Returns the IDs of all liked posts.
521 * @return All liked posts’ IDs
523 public Set<String> getLikedPostIds() {
524 return Collections.unmodifiableSet(likedPostIds);
528 * Sets the IDs of all liked posts.
530 * @param likedPostIds
531 * All liked posts’ IDs
532 * @return This Sone (for method chaining)
534 public synchronized Sone setLikePostIds(Set<String> likedPostIds) {
535 this.likedPostIds.clear();
536 this.likedPostIds.addAll(likedPostIds);
541 * Checks whether the given post ID is liked by this Sone.
545 * @return {@code true} if this Sone likes the given post, {@code false}
548 public boolean isLikedPostId(String postId) {
549 return likedPostIds.contains(postId);
553 * Adds the given post ID to the list of posts this Sone likes.
557 * @return This Sone (for method chaining)
559 public synchronized Sone addLikedPostId(String postId) {
560 likedPostIds.add(postId);
565 * Removes the given post ID from the list of posts this Sone likes.
569 * @return This Sone (for method chaining)
571 public synchronized Sone removeLikedPostId(String postId) {
572 likedPostIds.remove(postId);
577 * Returns the IDs of all liked replies.
579 * @return All liked replies’ IDs
581 public Set<String> getLikedReplyIds() {
582 return Collections.unmodifiableSet(likedReplyIds);
586 * Sets the IDs of all liked replies.
588 * @param likedReplyIds
589 * All liked replies’ IDs
590 * @return This Sone (for method chaining)
592 public synchronized Sone setLikeReplyIds(Set<String> likedReplyIds) {
593 this.likedReplyIds.clear();
594 this.likedReplyIds.addAll(likedReplyIds);
599 * Checks whether the given reply ID is liked by this Sone.
602 * The ID of the reply
603 * @return {@code true} if this Sone likes the given reply, {@code false}
606 public boolean isLikedReplyId(String replyId) {
607 return likedReplyIds.contains(replyId);
611 * Adds the given reply ID to the list of replies this Sone likes.
614 * The ID of the reply
615 * @return This Sone (for method chaining)
617 public synchronized Sone addLikedReplyId(String replyId) {
618 likedReplyIds.add(replyId);
623 * Removes the given post ID from the list of replies this Sone likes.
626 * The ID of the reply
627 * @return This Sone (for method chaining)
629 public synchronized Sone removeLikedReplyId(String replyId) {
630 likedReplyIds.remove(replyId);
635 * Returns Sone-specific options.
637 * @return The options of this Sone
639 public Options getOptions() {
644 // FINGERPRINTABLE METHODS
651 public synchronized String getFingerprint() {
652 StringBuilder fingerprint = new StringBuilder();
653 fingerprint.append(profile.getFingerprint());
655 fingerprint.append("Posts(");
656 for (Post post : getPosts()) {
657 fingerprint.append("Post(").append(post.getId()).append(')');
659 fingerprint.append(")");
661 List<Reply> replies = new ArrayList<Reply>(getReplies());
662 Collections.sort(replies, Reply.TIME_COMPARATOR);
663 fingerprint.append("Replies(");
664 for (Reply reply : replies) {
665 fingerprint.append("Reply(").append(reply.getId()).append(')');
667 fingerprint.append(')');
669 List<String> likedPostIds = new ArrayList<String>(getLikedPostIds());
670 Collections.sort(likedPostIds);
671 fingerprint.append("LikedPosts(");
672 for (String likedPostId : likedPostIds) {
673 fingerprint.append("Post(").append(likedPostId).append(')');
675 fingerprint.append(')');
677 List<String> likedReplyIds = new ArrayList<String>(getLikedReplyIds());
678 Collections.sort(likedReplyIds);
679 fingerprint.append("LikedReplies(");
680 for (String likedReplyId : likedReplyIds) {
681 fingerprint.append("Reply(").append(likedReplyId).append(')');
683 fingerprint.append(')');
685 return fingerprint.toString();
689 // INTERFACE Comparable<Sone>
696 public int compareTo(Sone sone) {
697 return NICE_NAME_COMPARATOR.compare(this, sone);
708 public int hashCode() {
709 return id.hashCode();
716 public boolean equals(Object object) {
717 if (!(object instanceof Sone)) {
720 return ((Sone) object).id.equals(id);
727 public String toString() {
728 return getClass().getName() + "[identity=" + identity + ",requestUri=" + requestUri + ",insertUri(" + String.valueOf(insertUri).length() + "),friends(" + friendSones.size() + "),posts(" + posts.size() + "),replies(" + replies.size() + ")]";