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>
44 /** comparator that sorts Sones by their nice name. */
45 public static final Comparator<Sone> NICE_NAME_COMPARATOR = new Comparator<Sone>() {
48 public int compare(Sone leftSone, Sone rightSone) {
49 int diff = SoneAccessor.getNiceName(leftSone).compareToIgnoreCase(SoneAccessor.getNiceName(rightSone));
53 return leftSone.getId().compareToIgnoreCase(rightSone.getId());
59 private static final Logger logger = Logging.getLogger(Sone.class);
61 /** The ID of this Sone. */
62 private final String id;
64 /** The name of this Sone. */
65 private volatile String name;
67 /** The URI under which the Sone is stored in Freenet. */
68 private volatile FreenetURI requestUri;
70 /** The URI used to insert a new version of this Sone. */
71 /* This will be null for remote Sones! */
72 private volatile FreenetURI insertUri;
74 /** The time of the last inserted update. */
75 private volatile long time;
77 /** The profile of this Sone. */
78 private volatile Profile profile;
80 /** All friend Sones. */
81 private final Set<Sone> friendSones = Collections.synchronizedSet(new HashSet<Sone>());
84 private final Set<Post> posts = Collections.synchronizedSet(new HashSet<Post>());
87 private final Set<Reply> replies = Collections.synchronizedSet(new HashSet<Reply>());
89 /** The IDs of all liked posts. */
90 private final Set<String> likedPostIds = Collections.synchronizedSet(new HashSet<String>());
92 /** The IDs of all liked replies. */
93 private final Set<String> likedReplyIds = Collections.synchronizedSet(new HashSet<String>());
95 /** Modification count. */
96 private volatile long modificationCounter = 0;
102 * The ID of this Sone
104 public Sone(String id) {
113 * Returns the ID of this Sone.
115 * @return The ID of this Sone
117 public String getId() {
122 * Returns the name of this Sone.
124 * @return The name of this Sone
126 public String getName() {
131 * Sets the name of this Sone.
134 * The name of this Sone
135 * @return This sone (for method chaining)
137 public Sone setName(String name) {
143 * Returns the request URI of this Sone.
145 * @return The request URI of this Sone
147 public FreenetURI getRequestUri() {
152 * Sets the request URI of this Sone.
155 * The request URI of this Sone
156 * @return This Sone (for method chaining)
158 public Sone setRequestUri(FreenetURI requestUri) {
159 if (this.requestUri == null) {
160 this.requestUri = requestUri;
164 if (!this.requestUri.equalsKeypair(requestUri)) {
165 logger.log(Level.WARNING, "Request URI %s tried to overwrite %s!", new Object[] { requestUri, this.requestUri });
168 long latestEdition = requestUri.getEdition();
169 if ((latestEdition > this.requestUri.getEdition()) || (latestEdition > this.requestUri.getSuggestedEdition())) {
170 this.requestUri.setSuggestedEdition(latestEdition);
176 * Returns the insert URI of this Sone.
178 * @return The insert URI of this Sone
180 public FreenetURI getInsertUri() {
185 * Sets the insert URI of this Sone.
188 * The insert URI of this Sone
189 * @return This Sone (for method chaining)
191 public Sone setInsertUri(FreenetURI insertUri) {
192 if (this.insertUri == null) {
193 this.insertUri = insertUri;
197 if (!this.insertUri.equalsKeypair(insertUri)) {
198 logger.log(Level.WARNING, "Request URI %s tried to overwrite %s!", new Object[] { insertUri, this.insertUri });
201 long latestEdition = insertUri.getEdition();
202 if ((latestEdition > this.insertUri.getEdition()) || (latestEdition > this.insertUri.getSuggestedEdition())) {
203 this.insertUri.setSuggestedEdition(latestEdition);
209 * Return the time of the last inserted update of this Sone.
211 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
213 public long getTime() {
218 * Sets the time of the last inserted update of this Sone.
221 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
222 * @return This Sone (for method chaining)
224 public Sone setTime(long time) {
230 * Returns a copy of the profile. If you want to update values in the
231 * profile of this Sone, update the values in the returned {@link Profile}
232 * and use {@link #setProfile(Profile)} to change the profile in this Sone.
234 * @return A copy of the profile
236 public Profile getProfile() {
237 return new Profile(profile);
241 * Sets the profile of this Sone. A copy of the given profile is stored so
242 * that subsequent modifications of the given profile are not reflected in
248 public synchronized void setProfile(Profile profile) {
249 this.profile = new Profile(profile);
250 modificationCounter++;
254 * Returns all friend Sones of this Sone.
256 * @return The friend Sones of this Sone
258 public List<Sone> getFriends() {
259 List<Sone> friends = new ArrayList<Sone>(friendSones);
260 Collections.sort(friends, new Comparator<Sone>() {
263 public int compare(Sone leftSone, Sone rightSone) {
264 int diff = SoneAccessor.getNiceName(leftSone).compareToIgnoreCase(SoneAccessor.getNiceName(rightSone));
268 return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightSone.getTime() - leftSone.getTime()));
275 * Sets all friends of this Sone at once.
278 * The new (and only) friends of this Sone
279 * @return This Sone (for method chaining)
281 public Sone setFriends(Collection<Sone> friends) {
283 friendSones.addAll(friends);
288 * Returns whether this Sone has the given Sone as a friend Sone.
291 * The friend Sone to check for
292 * @return {@code true} if this Sone has the given Sone as a friend,
293 * {@code false} otherwise
295 public boolean hasFriend(Sone friendSone) {
296 return friendSones.contains(friendSone);
300 * Adds the given Sone as a friend Sone.
303 * The friend Sone to add
304 * @return This Sone (for method chaining)
306 public Sone addFriend(Sone friendSone) {
307 if (!friendSone.equals(this)) {
308 friendSones.add(friendSone);
314 * Removes the given Sone as a friend Sone.
317 * The friend Sone to remove
318 * @return This Sone (for method chaining)
320 public Sone removeFriend(Sone friendSone) {
321 friendSones.remove(friendSone);
326 * Returns the list of posts of this Sone, sorted by time, newest first.
328 * @return All posts of this Sone
330 public List<Post> getPosts() {
331 List<Post> sortedPosts = new ArrayList<Post>(posts);
332 Collections.sort(sortedPosts, new Comparator<Post>() {
335 public int compare(Post leftPost, Post rightPost) {
336 return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightPost.getTime() - leftPost.getTime()));
344 * Sets all posts of this Sone at once.
347 * The new (and only) posts of this Sone
348 * @return This Sone (for method chaining)
350 public synchronized Sone setPosts(Collection<Post> posts) {
352 this.posts.addAll(posts);
353 modificationCounter++;
358 * Adds the given post to this Sone. The post will not be added if its
359 * {@link Post#getSone() Sone} is not this Sone.
364 public synchronized void addPost(Post post) {
365 if (post.getSone().equals(this) && posts.add(post)) {
366 logger.log(Level.FINEST, "Adding %s to “%s”.", new Object[] { post, getName() });
367 modificationCounter++;
372 * Removes the given post from this Sone.
377 public synchronized void removePost(Post post) {
378 if (post.getSone().equals(this) && posts.remove(post)) {
379 modificationCounter++;
384 * Returns all replies this Sone made.
386 * @return All replies this Sone made
388 public Set<Reply> getReplies() {
389 logger.log(Level.FINEST, "Friends of %s: %s", new Object[] { this, friendSones });
390 return Collections.unmodifiableSet(replies);
394 * Sets all replies of this Sone at once.
397 * The new (and only) replies of this Sone
398 * @return This Sone (for method chaining)
400 public synchronized Sone setReplies(Collection<Reply> replies) {
401 this.replies.clear();
402 this.replies.addAll(replies);
403 modificationCounter++;
408 * Adds a reply to this Sone. If the given reply was not made by this Sone,
409 * nothing is added to this Sone.
414 public synchronized void addReply(Reply reply) {
415 if (reply.getSone().equals(this) && replies.add(reply)) {
416 modificationCounter++;
421 * Removes a reply from this Sone.
424 * The reply to remove
426 public synchronized void removeReply(Reply reply) {
427 if (reply.getSone().equals(this) && replies.remove(reply)) {
428 modificationCounter++;
433 * Returns the IDs of all liked posts.
435 * @return All liked posts’ IDs
437 public Set<String> getLikedPostIds() {
438 return Collections.unmodifiableSet(likedPostIds);
442 * Sets the IDs of all liked posts.
444 * @param likedPostIds
445 * All liked posts’ IDs
446 * @return This Sone (for method chaining)
448 public synchronized Sone setLikePostIds(Set<String> likedPostIds) {
449 this.likedPostIds.clear();
450 this.likedPostIds.addAll(likedPostIds);
451 modificationCounter++;
456 * Checks whether the given post ID is liked by this Sone.
460 * @return {@code true} if this Sone likes the given post, {@code false}
463 public boolean isLikedPostId(String postId) {
464 return likedPostIds.contains(postId);
468 * Adds the given post ID to the list of posts this Sone likes.
472 * @return This Sone (for method chaining)
474 public synchronized Sone addLikedPostId(String postId) {
475 if (likedPostIds.add(postId)) {
476 modificationCounter++;
482 * Removes the given post ID from the list of posts this Sone likes.
486 * @return This Sone (for method chaining)
488 public synchronized Sone removeLikedPostId(String postId) {
489 if (likedPostIds.remove(postId)) {
490 modificationCounter++;
496 * Returns the IDs of all liked replies.
498 * @return All liked replies’ IDs
500 public Set<String> getLikedReplyIds() {
501 return Collections.unmodifiableSet(likedReplyIds);
505 * Sets the IDs of all liked replies.
507 * @param likedReplyIds
508 * All liked replies’ IDs
509 * @return This Sone (for method chaining)
511 public synchronized Sone setLikeReplyIds(Set<String> likedReplyIds) {
512 this.likedReplyIds.clear();
513 this.likedReplyIds.addAll(likedReplyIds);
514 modificationCounter++;
519 * Checks whether the given reply ID is liked by this Sone.
522 * The ID of the reply
523 * @return {@code true} if this Sone likes the given reply, {@code false}
526 public boolean isLikedReplyId(String replyId) {
527 return likedReplyIds.contains(replyId);
531 * Adds the given reply ID to the list of replies this Sone likes.
534 * The ID of the reply
535 * @return This Sone (for method chaining)
537 public synchronized Sone addLikedReplyId(String replyId) {
538 if (likedReplyIds.add(replyId)) {
539 modificationCounter++;
545 * Removes the given post ID from the list of replies this Sone likes.
548 * The ID of the reply
549 * @return This Sone (for method chaining)
551 public synchronized Sone removeLikedReplyId(String replyId) {
552 if (likedReplyIds.remove(replyId)) {
553 modificationCounter++;
559 * Returns the modification counter.
561 * @return The modification counter
563 public synchronized long getModificationCounter() {
564 return modificationCounter;
568 * Sets the modification counter.
570 * @param modificationCounter
571 * The new modification counter
573 public synchronized void setModificationCounter(long modificationCounter) {
574 this.modificationCounter = modificationCounter;
578 * Updates the suggested edition in both the request URI and the insert URI.
580 * @param latestEdition
581 * The latest edition to update the URIs to
583 public void updateUris(long latestEdition) {
584 if ((requestUri != null) && (requestUri.getEdition() < latestEdition)) {
585 requestUri = requestUri.setSuggestedEdition(latestEdition);
587 if ((insertUri != null) && (insertUri.getEdition() < latestEdition)) {
588 insertUri = insertUri.setSuggestedEdition(latestEdition);
597 * Updates the editions of the request URI and the insert URI (if latter is
598 * not {@code null}) with the greater edition of either one.
600 private void updateEditions() {
601 long requestEdition = 0;
602 if (requestUri != null) {
603 requestEdition = requestUri.getEdition();
605 long insertEdition = 0;
606 if (insertUri != null) {
607 insertEdition = insertUri.getEdition();
609 updateUris(Math.max(requestEdition, insertEdition));
620 public int hashCode() {
621 return id.hashCode();
628 public boolean equals(Object object) {
629 if (!(object instanceof Sone)) {
632 return ((Sone) object).id.equals(id);
639 public String toString() {
640 return getClass().getName() + "[id=" + id + ",name=" + name + ",requestUri=" + requestUri + ",insertUri=" + insertUri + ",friends(" + friendSones.size() + "),posts(" + posts.size() + "),replies(" + replies.size() + ")]";