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.UUID;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
31 import net.pterodactylus.sone.template.SoneAccessor;
32 import net.pterodactylus.util.logging.Logging;
33 import freenet.keys.FreenetURI;
36 * A Sone defines everything about a user: her profile, her status updates, her
37 * replies, her likes and dislikes, etc.
39 * Operations that modify the Sone need to synchronize on the Sone in question.
41 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
46 private static final Logger logger = Logging.getLogger(Sone.class);
48 /** A GUID for this Sone. */
49 private final UUID id;
51 /** The name of this Sone. */
54 /** The URI under which the Sone is stored in Freenet. */
55 private FreenetURI requestUri;
57 /** The URI used to insert a new version of this Sone. */
58 /* This will be null for remote Sones! */
59 private FreenetURI insertUri;
61 /** The time of the last inserted update. */
64 /** The profile of this Sone. */
65 private Profile profile;
67 /** All friend Sones. */
68 private final Set<Sone> friendSones = new HashSet<Sone>();
71 private final Set<Post> posts = new HashSet<Post>();
74 private final Set<Reply> replies = new HashSet<Reply>();
76 /** The IDs of all blocked Sones. */
77 private final Set<String> blockedSoneIds = new HashSet<String>();
79 /** The IDs of all liked posts. */
80 private final Set<String> likedPostIds = new HashSet<String>();
82 /** The IDs of all liked replies. */
83 private final Set<String> likedReplyIds = new HashSet<String>();
85 /** Modification count. */
86 private volatile long modificationCounter = 0;
94 public Sone(String id) {
95 this.id = UUID.fromString(id);
103 * Returns the ID of this Sone.
105 * @return The ID of this Sone
107 public String getId() {
108 return id.toString();
112 * Returns the name of this Sone.
114 * @return The name of this Sone
116 public String getName() {
121 * Sets the name of this Sone.
124 * The name of this Sone
125 * @return This sone (for method chaining)
127 public Sone setName(String name) {
133 * Returns the request URI of this Sone.
135 * @return The request URI of this Sone
137 public FreenetURI getRequestUri() {
142 * Sets the request URI of this Sone.
145 * The request URI of this Sone
146 * @return This Sone (for method chaining)
148 public Sone setRequestUri(FreenetURI requestUri) {
149 this.requestUri = requestUri;
154 * Returns the insert URI of this Sone.
156 * @return The insert URI of this Sone
158 public FreenetURI getInsertUri() {
163 * Sets the insert URI of this Sone.
166 * The insert URI of this Sone
167 * @return This Sone (for method chaining)
169 public Sone setInsertUri(FreenetURI insertUri) {
170 this.insertUri = insertUri;
175 * Return the time of the last inserted update of this Sone.
177 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
179 public long getTime() {
184 * Sets the time of the last inserted update of this Sone.
187 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
188 * @return This Sone (for method chaining)
190 public Sone setTime(long time) {
196 * Returns a copy of the profile. If you want to update values in the
197 * profile of this Sone, update the values in the returned {@link Profile}
198 * and use {@link #setProfile(Profile)} to change the profile in this Sone.
200 * @return A copy of the profile
202 public Profile getProfile() {
203 return new Profile(profile);
207 * Sets the profile of this Sone. A copy of the given profile is stored so
208 * that subsequent modifications of the given profile are not reflected in
214 public synchronized void setProfile(Profile profile) {
215 this.profile = new Profile(profile);
216 modificationCounter++;
220 * Returns all friend Sones of this Sone.
222 * @return The friend Sones of this Sone
224 public List<Sone> getFriends() {
225 List<Sone> friends = new ArrayList<Sone>(friendSones);
226 Collections.sort(friends, new Comparator<Sone>() {
229 public int compare(Sone leftSone, Sone rightSone) {
230 int diff = SoneAccessor.getNiceName(leftSone).compareTo(SoneAccessor.getNiceName(rightSone));
234 return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightSone.getTime() - leftSone.getTime()));
241 * Sets all friends of this Sone at once.
244 * The new (and only) friends of this Sone
245 * @return This Sone (for method chaining)
247 public Sone setFriends(Collection<Sone> friends) {
249 friendSones.addAll(friends);
254 * Returns whether this Sone has the given Sone as a friend Sone.
257 * The friend Sone to check for
258 * @return {@code true} if this Sone has the given Sone as a friend,
259 * {@code false} otherwise
261 public boolean hasFriend(Sone friendSone) {
262 return friendSones.contains(friendSone);
266 * Adds the given Sone as a friend Sone.
269 * The friend Sone to add
270 * @return This Sone (for method chaining)
272 public Sone addFriend(Sone friendSone) {
273 if (!friendSone.equals(this)) {
274 friendSones.add(friendSone);
280 * Removes the given Sone as a friend Sone.
283 * The friend Sone to remove
284 * @return This Sone (for method chaining)
286 public Sone removeFriend(Sone friendSone) {
287 friendSones.remove(friendSone);
292 * Returns the list of posts of this Sone, sorted by time, newest first.
294 * @return All posts of this Sone
296 public List<Post> getPosts() {
297 List<Post> sortedPosts = new ArrayList<Post>(posts);
298 Collections.sort(sortedPosts, new Comparator<Post>() {
301 public int compare(Post leftPost, Post rightPost) {
302 return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightPost.getTime() - leftPost.getTime()));
310 * Sets all posts of this Sone at once.
313 * The new (and only) posts of this Sone
314 * @return This Sone (for method chaining)
316 public synchronized Sone setPosts(Collection<Post> posts) {
318 this.posts.addAll(posts);
319 modificationCounter++;
324 * Adds the given post to this Sone. The post will not be added if its
325 * {@link Post#getSone() Sone} is not this Sone.
330 public synchronized void addPost(Post post) {
331 if (post.getSone().equals(this) && posts.add(post)) {
332 logger.log(Level.FINEST, "Adding %s to “%s”.", new Object[] { post, getName() });
333 modificationCounter++;
338 * Removes the given post from this Sone.
343 public synchronized void removePost(Post post) {
344 if (post.getSone().equals(this) && posts.remove(post)) {
345 modificationCounter++;
350 * Returns all replies this Sone made.
352 * @return All replies this Sone made
354 public Set<Reply> getReplies() {
355 logger.log(Level.FINEST, "Friends of %s: %s", new Object[] { this, friendSones });
356 return Collections.unmodifiableSet(replies);
360 * Sets all replies of this Sone at once.
363 * The new (and only) replies of this Sone
364 * @return This Sone (for method chaining)
366 public synchronized Sone setReplies(Collection<Reply> replies) {
367 this.replies.clear();
368 this.replies.addAll(replies);
369 modificationCounter++;
374 * Adds a reply to this Sone. If the given reply was not made by this Sone,
375 * nothing is added to this Sone.
380 public synchronized void addReply(Reply reply) {
381 if (reply.getSone().equals(this) && replies.add(reply)) {
382 modificationCounter++;
387 * Removes a reply from this Sone.
390 * The reply to remove
392 public synchronized void removeReply(Reply reply) {
393 if (reply.getSone().equals(this) && replies.remove(reply)) {
394 modificationCounter++;
399 * Returns the IDs of all blocked Sones. These Sones will not propagated
400 * using the “known Sones” mechanism.
402 * @return The IDs of all blocked Sones
404 public Set<String> getBlockedSoneIds() {
405 return Collections.unmodifiableSet(blockedSoneIds);
409 * Returns whether the given Sone ID is blocked.
412 * The Sone ID to check
413 * @return {@code true} if the given Sone ID is blocked, {@code false}
416 public boolean isSoneBlocked(String soneId) {
417 return blockedSoneIds.contains(soneId);
421 * Adds the given ID to the list of blocked IDs.
424 * The Sone ID to block
426 public synchronized void addBlockedSoneId(String soneId) {
427 if (blockedSoneIds.add(soneId)) {
428 modificationCounter++;
433 * Removes the given ID from the list of blocked IDs.
436 * The Sone ID to unblock
438 public synchronized void removeBlockedSoneId(String soneId) {
439 if (blockedSoneIds.remove(soneId)) {
440 modificationCounter++;
445 * Returns the IDs of all liked posts.
447 * @return All liked posts’ IDs
449 public Set<String> getLikedPostIds() {
450 return Collections.unmodifiableSet(likedPostIds);
454 * Sets the IDs of all liked posts.
456 * @param likedPostIds
457 * All liked posts’ IDs
458 * @return This Sone (for method chaining)
460 public synchronized Sone setLikePostIds(Set<String> likedPostIds) {
461 this.likedPostIds.clear();
462 this.likedPostIds.addAll(likedPostIds);
463 modificationCounter++;
468 * Checks whether the given post ID is liked by this Sone.
472 * @return {@code true} if this Sone likes the given post, {@code false}
475 public boolean isLikedPostId(String postId) {
476 return likedPostIds.contains(postId);
480 * Adds the given post ID to the list of posts this Sone likes.
484 * @return This Sone (for method chaining)
486 public synchronized Sone addLikedPostId(String postId) {
487 if (likedPostIds.add(postId)) {
488 modificationCounter++;
494 * Removes the given post ID from the list of posts this Sone likes.
498 * @return This Sone (for method chaining)
500 public synchronized Sone removeLikedPostId(String postId) {
501 if (likedPostIds.remove(postId)) {
502 modificationCounter++;
508 * Returns the IDs of all liked replies.
510 * @return All liked replies’ IDs
512 public Set<String> getLikedReplyIds() {
513 return Collections.unmodifiableSet(likedReplyIds);
517 * Sets the IDs of all liked replies.
519 * @param likedReplyIds
520 * All liked replies’ IDs
521 * @return This Sone (for method chaining)
523 public synchronized Sone setLikeReplyIds(Set<String> likedReplyIds) {
524 this.likedReplyIds.clear();
525 this.likedReplyIds.addAll(likedReplyIds);
526 modificationCounter++;
531 * Checks whether the given reply ID is liked by this Sone.
534 * The ID of the reply
535 * @return {@code true} if this Sone likes the given reply, {@code false}
538 public boolean isLikedReplyId(String replyId) {
539 return likedReplyIds.contains(replyId);
543 * Adds the given reply ID to the list of replies this Sone likes.
546 * The ID of the reply
547 * @return This Sone (for method chaining)
549 public synchronized Sone addLikedReplyId(String replyId) {
550 if (likedReplyIds.add(replyId)) {
551 modificationCounter++;
557 * Removes the given post ID from the list of replies this Sone likes.
560 * The ID of the reply
561 * @return This Sone (for method chaining)
563 public synchronized Sone removeLikedReplyId(String replyId) {
564 if (likedReplyIds.remove(replyId)) {
565 modificationCounter++;
571 * Returns the modification counter.
573 * @return The modification counter
575 public synchronized long getModificationCounter() {
576 return modificationCounter;
580 * Sets the modification counter.
582 * @param modificationCounter
583 * The new modification counter
585 public synchronized void setModificationCounter(long modificationCounter) {
586 this.modificationCounter = modificationCounter;
590 * Updates the suggested edition in both the request URI and the insert URI.
593 * The request URI that resulted from an insert
595 public void updateUris(FreenetURI requestUri) {
596 /* TODO - check for the correct URI. */
597 long latestEdition = requestUri.getSuggestedEdition();
598 this.requestUri = this.requestUri.setSuggestedEdition(latestEdition);
599 if (this.insertUri != null) {
600 this.insertUri = this.insertUri.setSuggestedEdition(latestEdition);
612 public int hashCode() {
613 return id.hashCode();
620 public boolean equals(Object object) {
621 if (!(object instanceof Sone)) {
624 return ((Sone) object).id.equals(id);
631 public String toString() {
632 return getClass().getName() + "[id=" + id + ",name=" + name + ",requestUri=" + requestUri + ",insertUri=" + insertUri + ",friends(" + friendSones.size() + "),posts(" + posts.size() + "),replies(" + replies.size() + ")]";