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());
63 /** Filter to remove Sones that have not been downloaded. */
64 public static final Filter<Sone> EMPTY_SONE_FILTER = new Filter<Sone>() {
67 public boolean filterObject(Sone sone) {
68 return sone.getTime() != 0;
72 /** Filter that matches all {@link Core#isLocalSone(Sone) local Sones}. */
73 public static final Filter<Sone> LOCAL_SONE_FILTER = new Filter<Sone>() {
76 public boolean filterObject(Sone sone) {
77 return sone.getIdentity() instanceof OwnIdentity;
83 private static final Logger logger = Logging.getLogger(Sone.class);
85 /** The ID of this Sone. */
86 private final String id;
88 /** The identity of this Sone. */
89 private Identity identity;
91 /** The URI under which the Sone is stored in Freenet. */
92 private volatile FreenetURI requestUri;
94 /** The URI used to insert a new version of this Sone. */
95 /* This will be null for remote Sones! */
96 private volatile FreenetURI insertUri;
98 /** The latest edition of the Sone. */
99 private volatile long latestEdition;
101 /** The time of the last inserted update. */
102 private volatile long time;
104 /** The profile of this Sone. */
105 private volatile Profile profile = new Profile();
107 /** The client used by the Sone. */
108 private volatile Client client;
110 /** All friend Sones. */
111 private final Set<String> friendSones = Collections.synchronizedSet(new HashSet<String>());
114 private final Set<Post> posts = Collections.synchronizedSet(new HashSet<Post>());
117 private final Set<Reply> replies = Collections.synchronizedSet(new HashSet<Reply>());
119 /** The IDs of all liked posts. */
120 private final Set<String> likedPostIds = Collections.synchronizedSet(new HashSet<String>());
122 /** The IDs of all liked replies. */
123 private final Set<String> likedReplyIds = Collections.synchronizedSet(new HashSet<String>());
125 /** Sone-specific options. */
126 private final Options options = new Options();
129 * Creates a new Sone.
134 public Sone(String id) {
143 * Returns the identity of this Sone.
145 * @return The identity of this Sone
147 public String getId() {
152 * Returns the identity of this Sone.
154 * @return The identity of this Sone
156 public Identity getIdentity() {
161 * Sets the identity of this Sone. The {@link Identity#getId() ID} of the
162 * identity has to match this Sone’s {@link #getId()}.
165 * The identity of this Sone
166 * @return This Sone (for method chaining)
167 * @throws IllegalArgumentException
168 * if the ID of the identity does not match this Sone’s ID
170 public Sone setIdentity(Identity identity) throws IllegalArgumentException {
171 if (!identity.getId().equals(id)) {
172 throw new IllegalArgumentException("Identity’s ID does not match Sone’s ID!");
174 this.identity = identity;
179 * Returns the name of this Sone.
181 * @return The name of this Sone
183 public String getName() {
184 return (identity != null) ? identity.getNickname() : null;
188 * Returns the request URI of this Sone.
190 * @return The request URI of this Sone
192 public FreenetURI getRequestUri() {
193 return (requestUri != null) ? requestUri.setSuggestedEdition(latestEdition) : null;
197 * Sets the request URI of this Sone.
200 * The request URI of this Sone
201 * @return This Sone (for method chaining)
203 public Sone setRequestUri(FreenetURI requestUri) {
204 if (this.requestUri == null) {
205 this.requestUri = requestUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
208 if (!this.requestUri.equalsKeypair(requestUri)) {
209 logger.log(Level.WARNING, "Request URI %s tried to overwrite %s!", new Object[] { requestUri, this.requestUri });
216 * Returns the insert URI of this Sone.
218 * @return The insert URI of this Sone
220 public FreenetURI getInsertUri() {
221 return (insertUri != null) ? insertUri.setSuggestedEdition(latestEdition) : null;
225 * Sets the insert URI of this Sone.
228 * The insert URI of this Sone
229 * @return This Sone (for method chaining)
231 public Sone setInsertUri(FreenetURI insertUri) {
232 if (this.insertUri == null) {
233 this.insertUri = insertUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
236 if (!this.insertUri.equalsKeypair(insertUri)) {
237 logger.log(Level.WARNING, "Request URI %s tried to overwrite %s!", new Object[] { insertUri, this.insertUri });
244 * Returns the latest edition of this Sone.
246 * @return The latest edition of this Sone
248 public long getLatestEdition() {
249 return latestEdition;
253 * Sets the latest edition of this Sone. If the given latest edition is not
254 * greater than the current latest edition, the latest edition of this Sone
257 * @param latestEdition
258 * The latest edition of this Sone
260 public void setLatestEdition(long latestEdition) {
261 if (!(latestEdition > this.latestEdition)) {
262 logger.log(Level.INFO, "New latest edition %d is not greater than current latest edition %d!", new Object[] { latestEdition, this.latestEdition });
265 this.latestEdition = latestEdition;
269 * Return the time of the last inserted update of this Sone.
271 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
273 public long getTime() {
278 * Sets the time of the last inserted update of this Sone.
281 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
282 * @return This Sone (for method chaining)
284 public Sone setTime(long time) {
290 * Returns a copy of the profile. If you want to update values in the
291 * profile of this Sone, update the values in the returned {@link Profile}
292 * and use {@link #setProfile(Profile)} to change the profile in this Sone.
294 * @return A copy of the profile
296 public synchronized Profile getProfile() {
297 return new Profile(profile);
301 * Sets the profile of this Sone. A copy of the given profile is stored so
302 * that subsequent modifications of the given profile are not reflected in
308 public synchronized void setProfile(Profile profile) {
309 this.profile = new Profile(profile);
313 * Returns the client used by this Sone.
315 * @return The client used by this Sone, or {@code null}
317 public Client getClient() {
322 * Sets the client used by this Sone.
325 * The client used by this Sone, or {@code null}
326 * @return This Sone (for method chaining)
328 public Sone setClient(Client client) {
329 this.client = client;
334 * Returns all friend Sones of this Sone.
336 * @return The friend Sones of this Sone
338 public List<String> getFriends() {
339 List<String> friends = new ArrayList<String>(friendSones);
344 * Sets all friends of this Sone at once.
347 * The new (and only) friends of this Sone
348 * @return This Sone (for method chaining)
350 public Sone setFriends(Collection<String> friends) {
352 friendSones.addAll(friends);
357 * Returns whether this Sone has the given Sone as a friend Sone.
359 * @param friendSoneId
360 * The ID of the Sone to check for
361 * @return {@code true} if this Sone has the given Sone as a friend,
362 * {@code false} otherwise
364 public boolean hasFriend(String friendSoneId) {
365 return friendSones.contains(friendSoneId);
369 * Adds the given Sone as a friend Sone.
372 * The friend Sone to add
373 * @return This Sone (for method chaining)
375 public Sone addFriend(String friendSone) {
376 if (!friendSone.equals(id)) {
377 friendSones.add(friendSone);
383 * Removes the given Sone as a friend Sone.
385 * @param friendSoneId
386 * The ID of the friend Sone to remove
387 * @return This Sone (for method chaining)
389 public Sone removeFriend(String friendSoneId) {
390 friendSones.remove(friendSoneId);
395 * Returns the list of posts of this Sone, sorted by time, newest first.
397 * @return All posts of this Sone
399 public List<Post> getPosts() {
400 List<Post> sortedPosts;
401 synchronized (this) {
402 sortedPosts = new ArrayList<Post>(posts);
404 Collections.sort(sortedPosts, Post.TIME_COMPARATOR);
409 * Sets all posts of this Sone at once.
412 * The new (and only) posts of this Sone
413 * @return This Sone (for method chaining)
415 public synchronized Sone setPosts(Collection<Post> posts) {
416 synchronized (this) {
418 this.posts.addAll(posts);
424 * Adds the given post to this Sone. The post will not be added if its
425 * {@link Post#getSone() Sone} is not this Sone.
430 public synchronized void addPost(Post post) {
431 if (post.getSone().equals(this) && posts.add(post)) {
432 logger.log(Level.FINEST, "Adding %s to “%s”.", new Object[] { post, getName() });
437 * Removes the given post from this Sone.
442 public synchronized void removePost(Post post) {
443 if (post.getSone().equals(this)) {
449 * Returns all replies this Sone made.
451 * @return All replies this Sone made
453 public synchronized Set<Reply> getReplies() {
454 return Collections.unmodifiableSet(replies);
458 * Sets all replies of this Sone at once.
461 * The new (and only) replies of this Sone
462 * @return This Sone (for method chaining)
464 public synchronized Sone setReplies(Collection<Reply> replies) {
465 this.replies.clear();
466 this.replies.addAll(replies);
471 * Adds a reply to this Sone. If the given reply was not made by this Sone,
472 * nothing is added to this Sone.
477 public synchronized void addReply(Reply reply) {
478 if (reply.getSone().equals(this)) {
484 * Removes a reply from this Sone.
487 * The reply to remove
489 public synchronized void removeReply(Reply reply) {
490 if (reply.getSone().equals(this)) {
491 replies.remove(reply);
496 * Returns the IDs of all liked posts.
498 * @return All liked posts’ IDs
500 public Set<String> getLikedPostIds() {
501 return Collections.unmodifiableSet(likedPostIds);
505 * Sets the IDs of all liked posts.
507 * @param likedPostIds
508 * All liked posts’ IDs
509 * @return This Sone (for method chaining)
511 public synchronized Sone setLikePostIds(Set<String> likedPostIds) {
512 this.likedPostIds.clear();
513 this.likedPostIds.addAll(likedPostIds);
518 * Checks whether the given post ID is liked by this Sone.
522 * @return {@code true} if this Sone likes the given post, {@code false}
525 public boolean isLikedPostId(String postId) {
526 return likedPostIds.contains(postId);
530 * Adds the given post ID to the list of posts this Sone likes.
534 * @return This Sone (for method chaining)
536 public synchronized Sone addLikedPostId(String postId) {
537 likedPostIds.add(postId);
542 * Removes the given post ID from the list of posts this Sone likes.
546 * @return This Sone (for method chaining)
548 public synchronized Sone removeLikedPostId(String postId) {
549 likedPostIds.remove(postId);
554 * Returns the IDs of all liked replies.
556 * @return All liked replies’ IDs
558 public Set<String> getLikedReplyIds() {
559 return Collections.unmodifiableSet(likedReplyIds);
563 * Sets the IDs of all liked replies.
565 * @param likedReplyIds
566 * All liked replies’ IDs
567 * @return This Sone (for method chaining)
569 public synchronized Sone setLikeReplyIds(Set<String> likedReplyIds) {
570 this.likedReplyIds.clear();
571 this.likedReplyIds.addAll(likedReplyIds);
576 * Checks whether the given reply ID is liked by this Sone.
579 * The ID of the reply
580 * @return {@code true} if this Sone likes the given reply, {@code false}
583 public boolean isLikedReplyId(String replyId) {
584 return likedReplyIds.contains(replyId);
588 * Adds the given reply ID to the list of replies this Sone likes.
591 * The ID of the reply
592 * @return This Sone (for method chaining)
594 public synchronized Sone addLikedReplyId(String replyId) {
595 likedReplyIds.add(replyId);
600 * Removes the given post ID from the list of replies this Sone likes.
603 * The ID of the reply
604 * @return This Sone (for method chaining)
606 public synchronized Sone removeLikedReplyId(String replyId) {
607 likedReplyIds.remove(replyId);
612 * Returns Sone-specific options.
614 * @return The options of this Sone
616 public Options getOptions() {
621 // FINGERPRINTABLE METHODS
628 public synchronized String getFingerprint() {
629 StringBuilder fingerprint = new StringBuilder();
630 fingerprint.append(profile.getFingerprint());
632 fingerprint.append("Posts(");
633 for (Post post : getPosts()) {
634 fingerprint.append("Post(").append(post.getId()).append(')');
636 fingerprint.append(")");
638 List<Reply> replies = new ArrayList<Reply>(getReplies());
639 Collections.sort(replies, Reply.TIME_COMPARATOR);
640 fingerprint.append("Replies(");
641 for (Reply reply : replies) {
642 fingerprint.append("Reply(").append(reply.getId()).append(')');
644 fingerprint.append(')');
646 List<String> likedPostIds = new ArrayList<String>(getLikedPostIds());
647 Collections.sort(likedPostIds);
648 fingerprint.append("LikedPosts(");
649 for (String likedPostId : likedPostIds) {
650 fingerprint.append("Post(").append(likedPostId).append(')');
652 fingerprint.append(')');
654 List<String> likedReplyIds = new ArrayList<String>(getLikedReplyIds());
655 Collections.sort(likedReplyIds);
656 fingerprint.append("LikedReplies(");
657 for (String likedReplyId : likedReplyIds) {
658 fingerprint.append("Reply(").append(likedReplyId).append(')');
660 fingerprint.append(')');
662 return fingerprint.toString();
666 // INTERFACE Comparable<Sone>
673 public int compareTo(Sone sone) {
674 return NICE_NAME_COMPARATOR.compare(this, sone);
685 public int hashCode() {
686 return id.hashCode();
693 public boolean equals(Object object) {
694 if (!(object instanceof Sone)) {
697 return ((Sone) object).id.equals(id);
704 public String toString() {
705 return getClass().getName() + "[identity=" + identity + ",requestUri=" + requestUri + ",insertUri(" + String.valueOf(insertUri).length() + "),friends(" + friendSones.size() + "),posts(" + posts.size() + "),replies(" + replies.size() + ")]";