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.filter.Filter;
33 import net.pterodactylus.util.logging.Logging;
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, Comparable<Sone> {
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());
60 /** Filter to remove Sones that have not been downloaded. */
61 public static final Filter<Sone> EMPTY_SONE_FILTER = new Filter<Sone>() {
64 public boolean filterObject(Sone sone) {
65 return sone.getTime() != 0;
70 private static final Logger logger = Logging.getLogger(Sone.class);
72 /** The ID of this Sone. */
73 private final String id;
75 /** The identity of this Sone. */
76 private Identity identity;
78 /** The URI under which the Sone is stored in Freenet. */
79 private volatile FreenetURI requestUri;
81 /** The URI used to insert a new version of this Sone. */
82 /* This will be null for remote Sones! */
83 private volatile FreenetURI insertUri;
85 /** The latest edition of the Sone. */
86 private volatile long latestEdition;
88 /** The time of the last inserted update. */
89 private volatile long time;
91 /** The profile of this Sone. */
92 private volatile Profile profile = new Profile();
94 /** The client used by the Sone. */
95 private volatile Client client;
97 /** All friend Sones. */
98 private final Set<String> friendSones = Collections.synchronizedSet(new HashSet<String>());
101 private final Set<Post> posts = Collections.synchronizedSet(new HashSet<Post>());
104 private final Set<Reply> replies = Collections.synchronizedSet(new HashSet<Reply>());
106 /** The IDs of all liked posts. */
107 private final Set<String> likedPostIds = Collections.synchronizedSet(new HashSet<String>());
109 /** The IDs of all liked replies. */
110 private final Set<String> likedReplyIds = Collections.synchronizedSet(new HashSet<String>());
113 * Creates a new Sone.
118 public Sone(String id) {
127 * Returns the identity of this Sone.
129 * @return The identity of this Sone
131 public String getId() {
136 * Returns the identity of this Sone.
138 * @return The identity of this Sone
140 public Identity getIdentity() {
145 * Sets the identity of this Sone. The {@link Identity#getId() ID} of the
146 * identity has to match this Sone’s {@link #getId()}.
149 * The identity of this Sone
150 * @return This Sone (for method chaining)
151 * @throws IllegalArgumentException
152 * if the ID of the identity does not match this Sone’s ID
154 public Sone setIdentity(Identity identity) throws IllegalArgumentException {
155 if (!identity.getId().equals(id)) {
156 throw new IllegalArgumentException("Identity’s ID does not match Sone’s ID!");
158 this.identity = identity;
163 * Returns the name of this Sone.
165 * @return The name of this Sone
167 public String getName() {
168 return (identity != null) ? identity.getNickname() : null;
172 * Returns the request URI of this Sone.
174 * @return The request URI of this Sone
176 public FreenetURI getRequestUri() {
177 return (requestUri != null) ? requestUri.setSuggestedEdition(latestEdition) : null;
181 * Sets the request URI of this Sone.
184 * The request URI of this Sone
185 * @return This Sone (for method chaining)
187 public Sone setRequestUri(FreenetURI requestUri) {
188 if (this.requestUri == null) {
189 this.requestUri = requestUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
192 if (!this.requestUri.equalsKeypair(requestUri)) {
193 logger.log(Level.WARNING, "Request URI %s tried to overwrite %s!", new Object[] { requestUri, this.requestUri });
200 * Returns the insert URI of this Sone.
202 * @return The insert URI of this Sone
204 public FreenetURI getInsertUri() {
205 return (insertUri != null) ? insertUri.setSuggestedEdition(latestEdition) : null;
209 * Sets the insert URI of this Sone.
212 * The insert URI of this Sone
213 * @return This Sone (for method chaining)
215 public Sone setInsertUri(FreenetURI insertUri) {
216 if (this.insertUri == null) {
217 this.insertUri = insertUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
220 if (!this.insertUri.equalsKeypair(insertUri)) {
221 logger.log(Level.WARNING, "Request URI %s tried to overwrite %s!", new Object[] { insertUri, this.insertUri });
228 * Returns the latest edition of this Sone.
230 * @return The latest edition of this Sone
232 public long getLatestEdition() {
233 return latestEdition;
237 * Sets the latest edition of this Sone. If the given latest edition is not
238 * greater than the current latest edition, the latest edition of this Sone
241 * @param latestEdition
242 * The latest edition of this Sone
244 public void setLatestEdition(long latestEdition) {
245 if (!(latestEdition > this.latestEdition)) {
246 logger.log(Level.INFO, "New latest edition %d is not greater than current latest edition %d!", new Object[] { latestEdition, this.latestEdition });
249 this.latestEdition = latestEdition;
253 * Return the time of the last inserted update of this Sone.
255 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
257 public long getTime() {
262 * Sets the time of the last inserted update of this Sone.
265 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
266 * @return This Sone (for method chaining)
268 public Sone setTime(long time) {
274 * Returns a copy of the profile. If you want to update values in the
275 * profile of this Sone, update the values in the returned {@link Profile}
276 * and use {@link #setProfile(Profile)} to change the profile in this Sone.
278 * @return A copy of the profile
280 public synchronized Profile getProfile() {
281 return new Profile(profile);
285 * Sets the profile of this Sone. A copy of the given profile is stored so
286 * that subsequent modifications of the given profile are not reflected in
292 public synchronized void setProfile(Profile profile) {
293 this.profile = new Profile(profile);
297 * Returns the client used by this Sone.
299 * @return The client used by this Sone, or {@code null}
301 public Client getClient() {
306 * Sets the client used by this Sone.
309 * The client used by this Sone, or {@code null}
310 * @return This Sone (for method chaining)
312 public Sone setClient(Client client) {
313 this.client = client;
318 * Returns all friend Sones of this Sone.
320 * @return The friend Sones of this Sone
322 public List<String> getFriends() {
323 List<String> friends = new ArrayList<String>(friendSones);
328 * Sets all friends of this Sone at once.
331 * The new (and only) friends of this Sone
332 * @return This Sone (for method chaining)
334 public Sone setFriends(Collection<String> friends) {
336 friendSones.addAll(friends);
341 * Returns whether this Sone has the given Sone as a friend Sone.
343 * @param friendSoneId
344 * The ID of the Sone to check for
345 * @return {@code true} if this Sone has the given Sone as a friend,
346 * {@code false} otherwise
348 public boolean hasFriend(String friendSoneId) {
349 return friendSones.contains(friendSoneId);
353 * Adds the given Sone as a friend Sone.
356 * The friend Sone to add
357 * @return This Sone (for method chaining)
359 public Sone addFriend(String friendSone) {
360 if (!friendSone.equals(id)) {
361 friendSones.add(friendSone);
367 * Removes the given Sone as a friend Sone.
369 * @param friendSoneId
370 * The ID of the friend Sone to remove
371 * @return This Sone (for method chaining)
373 public Sone removeFriend(String friendSoneId) {
374 friendSones.remove(friendSoneId);
379 * Returns the list of posts of this Sone, sorted by time, newest first.
381 * @return All posts of this Sone
383 public List<Post> getPosts() {
384 List<Post> sortedPosts;
385 synchronized (this) {
386 sortedPosts = new ArrayList<Post>(posts);
388 Collections.sort(sortedPosts, Post.TIME_COMPARATOR);
393 * Sets all posts of this Sone at once.
396 * The new (and only) posts of this Sone
397 * @return This Sone (for method chaining)
399 public synchronized Sone setPosts(Collection<Post> posts) {
400 synchronized (this) {
402 this.posts.addAll(posts);
408 * Adds the given post to this Sone. The post will not be added if its
409 * {@link Post#getSone() Sone} is not this Sone.
414 public synchronized void addPost(Post post) {
415 if (post.getSone().equals(this) && posts.add(post)) {
416 logger.log(Level.FINEST, "Adding %s to “%s”.", new Object[] { post, getName() });
421 * Removes the given post from this Sone.
426 public synchronized void removePost(Post post) {
427 if (post.getSone().equals(this)) {
433 * Returns all replies this Sone made.
435 * @return All replies this Sone made
437 public synchronized Set<Reply> getReplies() {
438 return Collections.unmodifiableSet(replies);
442 * Sets all replies of this Sone at once.
445 * The new (and only) replies of this Sone
446 * @return This Sone (for method chaining)
448 public synchronized Sone setReplies(Collection<Reply> replies) {
449 this.replies.clear();
450 this.replies.addAll(replies);
455 * Adds a reply to this Sone. If the given reply was not made by this Sone,
456 * nothing is added to this Sone.
461 public synchronized void addReply(Reply reply) {
462 if (reply.getSone().equals(this)) {
468 * Removes a reply from this Sone.
471 * The reply to remove
473 public synchronized void removeReply(Reply reply) {
474 if (reply.getSone().equals(this)) {
475 replies.remove(reply);
480 * Returns the IDs of all liked posts.
482 * @return All liked posts’ IDs
484 public Set<String> getLikedPostIds() {
485 return Collections.unmodifiableSet(likedPostIds);
489 * Sets the IDs of all liked posts.
491 * @param likedPostIds
492 * All liked posts’ IDs
493 * @return This Sone (for method chaining)
495 public synchronized Sone setLikePostIds(Set<String> likedPostIds) {
496 this.likedPostIds.clear();
497 this.likedPostIds.addAll(likedPostIds);
502 * Checks whether the given post ID is liked by this Sone.
506 * @return {@code true} if this Sone likes the given post, {@code false}
509 public boolean isLikedPostId(String postId) {
510 return likedPostIds.contains(postId);
514 * Adds the given post ID to the list of posts this Sone likes.
518 * @return This Sone (for method chaining)
520 public synchronized Sone addLikedPostId(String postId) {
521 likedPostIds.add(postId);
526 * Removes the given post ID from the list of posts this Sone likes.
530 * @return This Sone (for method chaining)
532 public synchronized Sone removeLikedPostId(String postId) {
533 likedPostIds.remove(postId);
538 * Returns the IDs of all liked replies.
540 * @return All liked replies’ IDs
542 public Set<String> getLikedReplyIds() {
543 return Collections.unmodifiableSet(likedReplyIds);
547 * Sets the IDs of all liked replies.
549 * @param likedReplyIds
550 * All liked replies’ IDs
551 * @return This Sone (for method chaining)
553 public synchronized Sone setLikeReplyIds(Set<String> likedReplyIds) {
554 this.likedReplyIds.clear();
555 this.likedReplyIds.addAll(likedReplyIds);
560 * Checks whether the given reply ID is liked by this Sone.
563 * The ID of the reply
564 * @return {@code true} if this Sone likes the given reply, {@code false}
567 public boolean isLikedReplyId(String replyId) {
568 return likedReplyIds.contains(replyId);
572 * Adds the given reply ID to the list of replies this Sone likes.
575 * The ID of the reply
576 * @return This Sone (for method chaining)
578 public synchronized Sone addLikedReplyId(String replyId) {
579 likedReplyIds.add(replyId);
584 * Removes the given post ID from the list of replies this Sone likes.
587 * The ID of the reply
588 * @return This Sone (for method chaining)
590 public synchronized Sone removeLikedReplyId(String replyId) {
591 likedReplyIds.remove(replyId);
596 // FINGERPRINTABLE METHODS
603 public synchronized String getFingerprint() {
604 StringBuilder fingerprint = new StringBuilder();
605 fingerprint.append(profile.getFingerprint());
607 fingerprint.append("Posts(");
608 for (Post post : getPosts()) {
609 fingerprint.append("Post(").append(post.getId()).append(')');
611 fingerprint.append(")");
613 List<Reply> replies = new ArrayList<Reply>(getReplies());
614 Collections.sort(replies, Reply.TIME_COMPARATOR);
615 fingerprint.append("Replies(");
616 for (Reply reply : replies) {
617 fingerprint.append("Reply(").append(reply.getId()).append(')');
619 fingerprint.append(')');
621 List<String> likedPostIds = new ArrayList<String>(getLikedPostIds());
622 Collections.sort(likedPostIds);
623 fingerprint.append("LikedPosts(");
624 for (String likedPostId : likedPostIds) {
625 fingerprint.append("Post(").append(likedPostId).append(')');
627 fingerprint.append(')');
629 List<String> likedReplyIds = new ArrayList<String>(getLikedReplyIds());
630 Collections.sort(likedReplyIds);
631 fingerprint.append("LikedReplies(");
632 for (String likedReplyId : likedReplyIds) {
633 fingerprint.append("Reply(").append(likedReplyId).append(')');
635 fingerprint.append(')');
637 return fingerprint.toString();
641 // INTERFACE Comparable<Sone>
648 public int compareTo(Sone sone) {
649 return NICE_NAME_COMPARATOR.compare(this, sone);
660 public int hashCode() {
661 return id.hashCode();
668 public boolean equals(Object object) {
669 if (!(object instanceof Sone)) {
672 return ((Sone) object).id.equals(id);
679 public String toString() {
680 return getClass().getName() + "[identity=" + identity + ",requestUri=" + requestUri + ",insertUri(" + String.valueOf(insertUri).length() + "),friends(" + friendSones.size() + "),posts(" + posts.size() + "),replies(" + replies.size() + ")]";