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. */
52 private volatile String name;
54 /** The URI under which the Sone is stored in Freenet. */
55 private volatile FreenetURI requestUri;
57 /** The URI used to insert a new version of this Sone. */
58 /* This will be null for remote Sones! */
59 private volatile FreenetURI insertUri;
61 /** The time of the last inserted update. */
62 private volatile long time;
64 /** The profile of this Sone. */
65 private volatile Profile profile;
67 /** All friend Sones. */
68 private final Set<Sone> friendSones = Collections.synchronizedSet(new HashSet<Sone>());
71 private final Set<Post> posts = Collections.synchronizedSet(new HashSet<Post>());
74 private final Set<Reply> replies = Collections.synchronizedSet(new HashSet<Reply>());
76 /** The IDs of all liked posts. */
77 private final Set<String> likedPostIds = Collections.synchronizedSet(new HashSet<String>());
79 /** The IDs of all liked replies. */
80 private final Set<String> likedReplyIds = Collections.synchronizedSet(new HashSet<String>());
82 /** Modification count. */
83 private volatile long modificationCounter = 0;
91 public Sone(String id) {
92 this.id = UUID.fromString(id);
100 * Returns the ID of this Sone.
102 * @return The ID of this Sone
104 public String getId() {
105 return id.toString();
109 * Returns the name of this Sone.
111 * @return The name of this Sone
113 public String getName() {
118 * Sets the name of this Sone.
121 * The name of this Sone
122 * @return This sone (for method chaining)
124 public Sone setName(String name) {
130 * Returns the request URI of this Sone.
132 * @return The request URI of this Sone
134 public FreenetURI getRequestUri() {
139 * Sets the request URI of this Sone.
142 * The request URI of this Sone
143 * @return This Sone (for method chaining)
145 public Sone setRequestUri(FreenetURI requestUri) {
146 if (this.requestUri == null) {
147 this.requestUri = requestUri;
151 if (!this.requestUri.equalsKeypair(requestUri)) {
152 logger.log(Level.WARNING, "Request URI %s tried to overwrite %s!", new Object[] { requestUri, this.requestUri });
155 long latestEdition = requestUri.getEdition();
156 if ((latestEdition > this.requestUri.getEdition()) || (latestEdition > this.requestUri.getSuggestedEdition())) {
157 this.requestUri.setSuggestedEdition(latestEdition);
163 * Returns the insert URI of this Sone.
165 * @return The insert URI of this Sone
167 public FreenetURI getInsertUri() {
172 * Sets the insert URI of this Sone.
175 * The insert URI of this Sone
176 * @return This Sone (for method chaining)
178 public Sone setInsertUri(FreenetURI insertUri) {
179 if (this.insertUri == null) {
180 this.insertUri = insertUri;
184 if (!this.insertUri.equalsKeypair(insertUri)) {
185 logger.log(Level.WARNING, "Request URI %s tried to overwrite %s!", new Object[] { insertUri, this.insertUri });
188 long latestEdition = insertUri.getEdition();
189 if ((latestEdition > this.insertUri.getEdition()) || (latestEdition > this.insertUri.getSuggestedEdition())) {
190 this.insertUri.setSuggestedEdition(latestEdition);
196 * Return the time of the last inserted update of this Sone.
198 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
200 public long getTime() {
205 * Sets the time of the last inserted update of this Sone.
208 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
209 * @return This Sone (for method chaining)
211 public Sone setTime(long time) {
217 * Returns a copy of the profile. If you want to update values in the
218 * profile of this Sone, update the values in the returned {@link Profile}
219 * and use {@link #setProfile(Profile)} to change the profile in this Sone.
221 * @return A copy of the profile
223 public Profile getProfile() {
224 return new Profile(profile);
228 * Sets the profile of this Sone. A copy of the given profile is stored so
229 * that subsequent modifications of the given profile are not reflected in
235 public synchronized void setProfile(Profile profile) {
236 this.profile = new Profile(profile);
237 modificationCounter++;
241 * Returns all friend Sones of this Sone.
243 * @return The friend Sones of this Sone
245 public List<Sone> getFriends() {
246 List<Sone> friends = new ArrayList<Sone>(friendSones);
247 Collections.sort(friends, new Comparator<Sone>() {
250 public int compare(Sone leftSone, Sone rightSone) {
251 int diff = SoneAccessor.getNiceName(leftSone).compareToIgnoreCase(SoneAccessor.getNiceName(rightSone));
255 return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightSone.getTime() - leftSone.getTime()));
262 * Sets all friends of this Sone at once.
265 * The new (and only) friends of this Sone
266 * @return This Sone (for method chaining)
268 public Sone setFriends(Collection<Sone> friends) {
270 friendSones.addAll(friends);
275 * Returns whether this Sone has the given Sone as a friend Sone.
278 * The friend Sone to check for
279 * @return {@code true} if this Sone has the given Sone as a friend,
280 * {@code false} otherwise
282 public boolean hasFriend(Sone friendSone) {
283 return friendSones.contains(friendSone);
287 * Adds the given Sone as a friend Sone.
290 * The friend Sone to add
291 * @return This Sone (for method chaining)
293 public Sone addFriend(Sone friendSone) {
294 if (!friendSone.equals(this)) {
295 friendSones.add(friendSone);
301 * Removes the given Sone as a friend Sone.
304 * The friend Sone to remove
305 * @return This Sone (for method chaining)
307 public Sone removeFriend(Sone friendSone) {
308 friendSones.remove(friendSone);
313 * Returns the list of posts of this Sone, sorted by time, newest first.
315 * @return All posts of this Sone
317 public List<Post> getPosts() {
318 List<Post> sortedPosts = new ArrayList<Post>(posts);
319 Collections.sort(sortedPosts, new Comparator<Post>() {
322 public int compare(Post leftPost, Post rightPost) {
323 return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightPost.getTime() - leftPost.getTime()));
331 * Sets all posts of this Sone at once.
334 * The new (and only) posts of this Sone
335 * @return This Sone (for method chaining)
337 public synchronized Sone setPosts(Collection<Post> posts) {
339 this.posts.addAll(posts);
340 modificationCounter++;
345 * Adds the given post to this Sone. The post will not be added if its
346 * {@link Post#getSone() Sone} is not this Sone.
351 public synchronized void addPost(Post post) {
352 if (post.getSone().equals(this) && posts.add(post)) {
353 logger.log(Level.FINEST, "Adding %s to “%s”.", new Object[] { post, getName() });
354 modificationCounter++;
359 * Removes the given post from this Sone.
364 public synchronized void removePost(Post post) {
365 if (post.getSone().equals(this) && posts.remove(post)) {
366 modificationCounter++;
371 * Returns all replies this Sone made.
373 * @return All replies this Sone made
375 public Set<Reply> getReplies() {
376 logger.log(Level.FINEST, "Friends of %s: %s", new Object[] { this, friendSones });
377 return Collections.unmodifiableSet(replies);
381 * Sets all replies of this Sone at once.
384 * The new (and only) replies of this Sone
385 * @return This Sone (for method chaining)
387 public synchronized Sone setReplies(Collection<Reply> replies) {
388 this.replies.clear();
389 this.replies.addAll(replies);
390 modificationCounter++;
395 * Adds a reply to this Sone. If the given reply was not made by this Sone,
396 * nothing is added to this Sone.
401 public synchronized void addReply(Reply reply) {
402 if (reply.getSone().equals(this) && replies.add(reply)) {
403 modificationCounter++;
408 * Removes a reply from this Sone.
411 * The reply to remove
413 public synchronized void removeReply(Reply reply) {
414 if (reply.getSone().equals(this) && replies.remove(reply)) {
415 modificationCounter++;
420 * Returns the IDs of all liked posts.
422 * @return All liked posts’ IDs
424 public Set<String> getLikedPostIds() {
425 return Collections.unmodifiableSet(likedPostIds);
429 * Sets the IDs of all liked posts.
431 * @param likedPostIds
432 * All liked posts’ IDs
433 * @return This Sone (for method chaining)
435 public synchronized Sone setLikePostIds(Set<String> likedPostIds) {
436 this.likedPostIds.clear();
437 this.likedPostIds.addAll(likedPostIds);
438 modificationCounter++;
443 * Checks whether the given post ID is liked by this Sone.
447 * @return {@code true} if this Sone likes the given post, {@code false}
450 public boolean isLikedPostId(String postId) {
451 return likedPostIds.contains(postId);
455 * Adds the given post ID to the list of posts this Sone likes.
459 * @return This Sone (for method chaining)
461 public synchronized Sone addLikedPostId(String postId) {
462 if (likedPostIds.add(postId)) {
463 modificationCounter++;
469 * Removes the given post ID from the list of posts this Sone likes.
473 * @return This Sone (for method chaining)
475 public synchronized Sone removeLikedPostId(String postId) {
476 if (likedPostIds.remove(postId)) {
477 modificationCounter++;
483 * Returns the IDs of all liked replies.
485 * @return All liked replies’ IDs
487 public Set<String> getLikedReplyIds() {
488 return Collections.unmodifiableSet(likedReplyIds);
492 * Sets the IDs of all liked replies.
494 * @param likedReplyIds
495 * All liked replies’ IDs
496 * @return This Sone (for method chaining)
498 public synchronized Sone setLikeReplyIds(Set<String> likedReplyIds) {
499 this.likedReplyIds.clear();
500 this.likedReplyIds.addAll(likedReplyIds);
501 modificationCounter++;
506 * Checks whether the given reply ID is liked by this Sone.
509 * The ID of the reply
510 * @return {@code true} if this Sone likes the given reply, {@code false}
513 public boolean isLikedReplyId(String replyId) {
514 return likedReplyIds.contains(replyId);
518 * Adds the given reply ID to the list of replies this Sone likes.
521 * The ID of the reply
522 * @return This Sone (for method chaining)
524 public synchronized Sone addLikedReplyId(String replyId) {
525 if (likedReplyIds.add(replyId)) {
526 modificationCounter++;
532 * Removes the given post ID from the list of replies this Sone likes.
535 * The ID of the reply
536 * @return This Sone (for method chaining)
538 public synchronized Sone removeLikedReplyId(String replyId) {
539 if (likedReplyIds.remove(replyId)) {
540 modificationCounter++;
546 * Returns the modification counter.
548 * @return The modification counter
550 public synchronized long getModificationCounter() {
551 return modificationCounter;
555 * Sets the modification counter.
557 * @param modificationCounter
558 * The new modification counter
560 public synchronized void setModificationCounter(long modificationCounter) {
561 this.modificationCounter = modificationCounter;
565 * Updates the suggested edition in both the request URI and the insert URI.
567 * @param latestEdition
568 * The latest edition to update the URIs to
570 public void updateUris(long latestEdition) {
571 if ((requestUri != null) && (requestUri.getEdition() < latestEdition)) {
572 requestUri = requestUri.setSuggestedEdition(latestEdition);
574 if ((insertUri != null) && (insertUri.getEdition() < latestEdition)) {
575 insertUri = insertUri.setSuggestedEdition(latestEdition);
584 * Updates the editions of the request URI and the insert URI (if latter is
585 * not {@code null}) with the greater edition of either one.
587 private void updateEditions() {
588 long requestEdition = 0;
589 if (requestUri != null) {
590 requestEdition = requestUri.getEdition();
592 long insertEdition = 0;
593 if (insertUri != null) {
594 insertEdition = insertUri.getEdition();
596 updateUris(Math.max(requestEdition, insertEdition));
607 public int hashCode() {
608 return id.hashCode();
615 public boolean equals(Object object) {
616 if (!(object instanceof Sone)) {
619 return ((Sone) object).id.equals(id);
626 public String toString() {
627 return getClass().getName() + "[id=" + id + ",name=" + name + ",requestUri=" + requestUri + ",insertUri=" + insertUri + ",friends(" + friendSones.size() + "),posts(" + posts.size() + "),replies(" + replies.size() + ")]";