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.template.SoneAccessor;
31 import net.pterodactylus.util.logging.Logging;
32 import freenet.keys.FreenetURI;
35 * A Sone defines everything about a user: her profile, her status updates, her
36 * replies, her likes and dislikes, etc.
38 * Operations that modify the Sone need to synchronize on the Sone in question.
40 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
45 private static final Logger logger = Logging.getLogger(Sone.class);
47 /** The ID of this Sone. */
48 private final String id;
50 /** The name of this Sone. */
51 private volatile String name;
53 /** The URI under which the Sone is stored in Freenet. */
54 private volatile FreenetURI requestUri;
56 /** The URI used to insert a new version of this Sone. */
57 /* This will be null for remote Sones! */
58 private volatile FreenetURI insertUri;
60 /** The time of the last inserted update. */
61 private volatile long time;
63 /** The profile of this Sone. */
64 private volatile Profile profile;
66 /** All friend Sones. */
67 private final Set<Sone> friendSones = Collections.synchronizedSet(new HashSet<Sone>());
70 private final Set<Post> posts = Collections.synchronizedSet(new HashSet<Post>());
73 private final Set<Reply> replies = Collections.synchronizedSet(new HashSet<Reply>());
75 /** The IDs of all blocked Sones. */
76 private final Set<String> blockedSoneIds = Collections.synchronizedSet(new HashSet<String>());
78 /** The IDs of all liked posts. */
79 private final Set<String> likedPostIds = Collections.synchronizedSet(new HashSet<String>());
81 /** The IDs of all liked replies. */
82 private final Set<String> likedReplyIds = Collections.synchronizedSet(new HashSet<String>());
84 /** Modification count. */
85 private volatile long modificationCounter = 0;
93 public Sone(String id) {
102 * Returns the ID of this Sone.
104 * @return The ID of this Sone
106 public String getId() {
111 * Returns the name of this Sone.
113 * @return The name of this Sone
115 public String getName() {
120 * Sets the name of this Sone.
123 * The name of this Sone
124 * @return This sone (for method chaining)
126 public Sone setName(String name) {
132 * Returns the request URI of this Sone.
134 * @return The request URI of this Sone
136 public FreenetURI getRequestUri() {
141 * Sets the request URI of this Sone.
144 * The request URI of this Sone
145 * @return This Sone (for method chaining)
147 public Sone setRequestUri(FreenetURI requestUri) {
148 if (this.requestUri == null) {
149 this.requestUri = requestUri;
153 if (!this.requestUri.equalsKeypair(requestUri)) {
154 logger.log(Level.WARNING, "Request URI %s tried to overwrite %s!", new Object[] { requestUri, this.requestUri });
157 long latestEdition = requestUri.getEdition();
158 if ((latestEdition > this.requestUri.getEdition()) || (latestEdition > this.requestUri.getSuggestedEdition())) {
159 this.requestUri.setSuggestedEdition(latestEdition);
165 * Returns the insert URI of this Sone.
167 * @return The insert URI of this Sone
169 public FreenetURI getInsertUri() {
174 * Sets the insert URI of this Sone.
177 * The insert URI of this Sone
178 * @return This Sone (for method chaining)
180 public Sone setInsertUri(FreenetURI insertUri) {
181 if (this.insertUri == null) {
182 this.insertUri = insertUri;
186 if (!this.insertUri.equalsKeypair(insertUri)) {
187 logger.log(Level.WARNING, "Request URI %s tried to overwrite %s!", new Object[] { insertUri, this.insertUri });
190 long latestEdition = insertUri.getEdition();
191 if ((latestEdition > this.insertUri.getEdition()) || (latestEdition > this.insertUri.getSuggestedEdition())) {
192 this.insertUri.setSuggestedEdition(latestEdition);
198 * Return the time of the last inserted update of this Sone.
200 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
202 public long getTime() {
207 * Sets the time of the last inserted update of this Sone.
210 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
211 * @return This Sone (for method chaining)
213 public Sone setTime(long time) {
219 * Returns a copy of the profile. If you want to update values in the
220 * profile of this Sone, update the values in the returned {@link Profile}
221 * and use {@link #setProfile(Profile)} to change the profile in this Sone.
223 * @return A copy of the profile
225 public Profile getProfile() {
226 return new Profile(profile);
230 * Sets the profile of this Sone. A copy of the given profile is stored so
231 * that subsequent modifications of the given profile are not reflected in
237 public synchronized void setProfile(Profile profile) {
238 this.profile = new Profile(profile);
239 modificationCounter++;
243 * Returns all friend Sones of this Sone.
245 * @return The friend Sones of this Sone
247 public List<Sone> getFriends() {
248 List<Sone> friends = new ArrayList<Sone>(friendSones);
249 Collections.sort(friends, new Comparator<Sone>() {
252 public int compare(Sone leftSone, Sone rightSone) {
253 int diff = SoneAccessor.getNiceName(leftSone).compareToIgnoreCase(SoneAccessor.getNiceName(rightSone));
257 return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightSone.getTime() - leftSone.getTime()));
264 * Sets all friends of this Sone at once.
267 * The new (and only) friends of this Sone
268 * @return This Sone (for method chaining)
270 public Sone setFriends(Collection<Sone> friends) {
272 friendSones.addAll(friends);
277 * Returns whether this Sone has the given Sone as a friend Sone.
280 * The friend Sone to check for
281 * @return {@code true} if this Sone has the given Sone as a friend,
282 * {@code false} otherwise
284 public boolean hasFriend(Sone friendSone) {
285 return friendSones.contains(friendSone);
289 * Adds the given Sone as a friend Sone.
292 * The friend Sone to add
293 * @return This Sone (for method chaining)
295 public Sone addFriend(Sone friendSone) {
296 if (!friendSone.equals(this)) {
297 friendSones.add(friendSone);
303 * Removes the given Sone as a friend Sone.
306 * The friend Sone to remove
307 * @return This Sone (for method chaining)
309 public Sone removeFriend(Sone friendSone) {
310 friendSones.remove(friendSone);
315 * Returns the list of posts of this Sone, sorted by time, newest first.
317 * @return All posts of this Sone
319 public List<Post> getPosts() {
320 List<Post> sortedPosts = new ArrayList<Post>(posts);
321 Collections.sort(sortedPosts, new Comparator<Post>() {
324 public int compare(Post leftPost, Post rightPost) {
325 return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightPost.getTime() - leftPost.getTime()));
333 * Sets all posts of this Sone at once.
336 * The new (and only) posts of this Sone
337 * @return This Sone (for method chaining)
339 public synchronized Sone setPosts(Collection<Post> posts) {
341 this.posts.addAll(posts);
342 modificationCounter++;
347 * Adds the given post to this Sone. The post will not be added if its
348 * {@link Post#getSone() Sone} is not this Sone.
353 public synchronized void addPost(Post post) {
354 if (post.getSone().equals(this) && posts.add(post)) {
355 logger.log(Level.FINEST, "Adding %s to “%s”.", new Object[] { post, getName() });
356 modificationCounter++;
361 * Removes the given post from this Sone.
366 public synchronized void removePost(Post post) {
367 if (post.getSone().equals(this) && posts.remove(post)) {
368 modificationCounter++;
373 * Returns all replies this Sone made.
375 * @return All replies this Sone made
377 public Set<Reply> getReplies() {
378 logger.log(Level.FINEST, "Friends of %s: %s", new Object[] { this, friendSones });
379 return Collections.unmodifiableSet(replies);
383 * Sets all replies of this Sone at once.
386 * The new (and only) replies of this Sone
387 * @return This Sone (for method chaining)
389 public synchronized Sone setReplies(Collection<Reply> replies) {
390 this.replies.clear();
391 this.replies.addAll(replies);
392 modificationCounter++;
397 * Adds a reply to this Sone. If the given reply was not made by this Sone,
398 * nothing is added to this Sone.
403 public synchronized void addReply(Reply reply) {
404 if (reply.getSone().equals(this) && replies.add(reply)) {
405 modificationCounter++;
410 * Removes a reply from this Sone.
413 * The reply to remove
415 public synchronized void removeReply(Reply reply) {
416 if (reply.getSone().equals(this) && replies.remove(reply)) {
417 modificationCounter++;
422 * Returns the IDs of all blocked Sones. These Sones will not propagated
423 * using the “known Sones” mechanism.
425 * @return The IDs of all blocked Sones
427 public Set<String> getBlockedSoneIds() {
428 return Collections.unmodifiableSet(blockedSoneIds);
432 * Returns whether the given Sone ID is blocked.
435 * The Sone ID to check
436 * @return {@code true} if the given Sone ID is blocked, {@code false}
439 public boolean isSoneBlocked(String soneId) {
440 return blockedSoneIds.contains(soneId);
444 * Adds the given ID to the list of blocked IDs.
447 * The Sone ID to block
449 public synchronized void addBlockedSoneId(String soneId) {
450 if (blockedSoneIds.add(soneId)) {
451 modificationCounter++;
456 * Removes the given ID from the list of blocked IDs.
459 * The Sone ID to unblock
461 public synchronized void removeBlockedSoneId(String soneId) {
462 if (blockedSoneIds.remove(soneId)) {
463 modificationCounter++;
468 * Returns the IDs of all liked posts.
470 * @return All liked posts’ IDs
472 public Set<String> getLikedPostIds() {
473 return Collections.unmodifiableSet(likedPostIds);
477 * Sets the IDs of all liked posts.
479 * @param likedPostIds
480 * All liked posts’ IDs
481 * @return This Sone (for method chaining)
483 public synchronized Sone setLikePostIds(Set<String> likedPostIds) {
484 this.likedPostIds.clear();
485 this.likedPostIds.addAll(likedPostIds);
486 modificationCounter++;
491 * Checks whether the given post ID is liked by this Sone.
495 * @return {@code true} if this Sone likes the given post, {@code false}
498 public boolean isLikedPostId(String postId) {
499 return likedPostIds.contains(postId);
503 * Adds the given post ID to the list of posts this Sone likes.
507 * @return This Sone (for method chaining)
509 public synchronized Sone addLikedPostId(String postId) {
510 if (likedPostIds.add(postId)) {
511 modificationCounter++;
517 * Removes the given post ID from the list of posts this Sone likes.
521 * @return This Sone (for method chaining)
523 public synchronized Sone removeLikedPostId(String postId) {
524 if (likedPostIds.remove(postId)) {
525 modificationCounter++;
531 * Returns the IDs of all liked replies.
533 * @return All liked replies’ IDs
535 public Set<String> getLikedReplyIds() {
536 return Collections.unmodifiableSet(likedReplyIds);
540 * Sets the IDs of all liked replies.
542 * @param likedReplyIds
543 * All liked replies’ IDs
544 * @return This Sone (for method chaining)
546 public synchronized Sone setLikeReplyIds(Set<String> likedReplyIds) {
547 this.likedReplyIds.clear();
548 this.likedReplyIds.addAll(likedReplyIds);
549 modificationCounter++;
554 * Checks whether the given reply ID is liked by this Sone.
557 * The ID of the reply
558 * @return {@code true} if this Sone likes the given reply, {@code false}
561 public boolean isLikedReplyId(String replyId) {
562 return likedReplyIds.contains(replyId);
566 * Adds the given reply ID to the list of replies this Sone likes.
569 * The ID of the reply
570 * @return This Sone (for method chaining)
572 public synchronized Sone addLikedReplyId(String replyId) {
573 if (likedReplyIds.add(replyId)) {
574 modificationCounter++;
580 * Removes the given post ID from the list of replies this Sone likes.
583 * The ID of the reply
584 * @return This Sone (for method chaining)
586 public synchronized Sone removeLikedReplyId(String replyId) {
587 if (likedReplyIds.remove(replyId)) {
588 modificationCounter++;
594 * Returns the modification counter.
596 * @return The modification counter
598 public synchronized long getModificationCounter() {
599 return modificationCounter;
603 * Sets the modification counter.
605 * @param modificationCounter
606 * The new modification counter
608 public synchronized void setModificationCounter(long modificationCounter) {
609 this.modificationCounter = modificationCounter;
613 * Updates the suggested edition in both the request URI and the insert URI.
615 * @param latestEdition
616 * The latest edition to update the URIs to
618 public void updateUris(long latestEdition) {
619 if ((requestUri != null) && (requestUri.getEdition() < latestEdition)) {
620 requestUri = requestUri.setSuggestedEdition(latestEdition);
622 if ((insertUri != null) && (insertUri.getEdition() < latestEdition)) {
623 insertUri = insertUri.setSuggestedEdition(latestEdition);
632 * Updates the editions of the request URI and the insert URI (if latter is
633 * not {@code null}) with the greater edition of either one.
635 private void updateEditions() {
636 long requestEdition = 0;
637 if (requestUri != null) {
638 requestEdition = requestUri.getEdition();
640 long insertEdition = 0;
641 if (insertUri != null) {
642 insertEdition = insertUri.getEdition();
644 updateUris(Math.max(requestEdition, insertEdition));
655 public int hashCode() {
656 return id.hashCode();
663 public boolean equals(Object object) {
664 if (!(object instanceof Sone)) {
667 return ((Sone) object).id.equals(id);
674 public String toString() {
675 return getClass().getName() + "[id=" + id + ",name=" + name + ",requestUri=" + requestUri + ",insertUri=" + insertUri + ",friends(" + friendSones.size() + "),posts(" + posts.size() + "),replies(" + replies.size() + ")]";