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.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>
45 /** comparator that sorts Sones by their nice name. */
46 public static final Comparator<Sone> NICE_NAME_COMPARATOR = new Comparator<Sone>() {
49 public int compare(Sone leftSone, Sone rightSone) {
50 int diff = SoneAccessor.getNiceName(leftSone).compareToIgnoreCase(SoneAccessor.getNiceName(rightSone));
54 return leftSone.getId().compareToIgnoreCase(rightSone.getId());
60 private static final Logger logger = Logging.getLogger(Sone.class);
62 /** The identity of this Sone. */
63 private final Identity identity;
65 /** The URI under which the Sone is stored in Freenet. */
66 private volatile FreenetURI requestUri;
68 /** The URI used to insert a new version of this Sone. */
69 /* This will be null for remote Sones! */
70 private volatile FreenetURI insertUri;
72 /** The latest edition of the Sone. */
73 private volatile long latestEdition;
75 /** The time of the last inserted update. */
76 private volatile long time;
78 /** The profile of this Sone. */
79 private volatile Profile profile;
81 /** All friend Sones. */
82 private final Set<Sone> friendSones = Collections.synchronizedSet(new HashSet<Sone>());
85 private final Set<Post> posts = Collections.synchronizedSet(new HashSet<Post>());
88 private final Set<Reply> replies = Collections.synchronizedSet(new HashSet<Reply>());
90 /** The IDs of all liked posts. */
91 private final Set<String> likedPostIds = Collections.synchronizedSet(new HashSet<String>());
93 /** The IDs of all liked replies. */
94 private final Set<String> likedReplyIds = Collections.synchronizedSet(new HashSet<String>());
96 /** Modification count. */
97 private volatile long modificationCounter = 0;
100 * Creates a new Sone.
103 * The identity of the Sone
105 public Sone(Identity identity) {
106 this.identity = identity;
114 * Returns the identity of this Sone.
116 * @return The identity of this Sone
118 public String getId() {
119 return identity.getId();
123 * Returns the identity of this Sone.
125 * @return The identity of this Sone
127 public Identity getIdentity() {
132 * Returns the name of this Sone.
134 * @return The name of this Sone
136 public String getName() {
137 return identity.getNickname();
141 * Returns the request URI of this Sone.
143 * @return The request URI of this Sone
145 public FreenetURI getRequestUri() {
146 return requestUri.setSuggestedEdition(latestEdition);
150 * Sets the request URI of this Sone.
153 * The request URI of this Sone
154 * @return This Sone (for method chaining)
156 public Sone setRequestUri(FreenetURI requestUri) {
157 if (this.requestUri == null) {
158 this.requestUri = requestUri.setDocName("Sone").setMetaString(new String[0]);
161 if (!this.requestUri.equalsKeypair(requestUri)) {
162 logger.log(Level.WARNING, "Request URI %s tried to overwrite %s!", new Object[] { requestUri, this.requestUri });
165 setLatestEdition(requestUri.getEdition());
170 * Returns the insert URI of this Sone.
172 * @return The insert URI of this Sone
174 public FreenetURI getInsertUri() {
175 return insertUri.setSuggestedEdition(latestEdition);
179 * Sets the insert URI of this Sone.
182 * The insert URI of this Sone
183 * @return This Sone (for method chaining)
185 public Sone setInsertUri(FreenetURI insertUri) {
186 if (this.insertUri == null) {
187 this.insertUri = insertUri.setDocName("Sone").setMetaString(new String[0]);
190 if (!this.insertUri.equalsKeypair(insertUri)) {
191 logger.log(Level.WARNING, "Request URI %s tried to overwrite %s!", new Object[] { insertUri, this.insertUri });
194 setLatestEdition(insertUri.getEdition());
199 * Returns the latest edition of this Sone.
201 * @return The latest edition of this Sone
203 public long getLatestEdition() {
204 return latestEdition;
208 * Sets the latest edition of this Sone. If the given latest edition is not
209 * greater than the current latest edition, the latest edition of this Sone
212 * @param latestEdition
213 * The latest edition of this Sone
215 public void setLatestEdition(long latestEdition) {
216 if (!(latestEdition > this.latestEdition)) {
217 logger.log(Level.INFO, "New latest edition %d is not greater than current latest edition %d!", new Object[] { latestEdition, this.latestEdition });
220 this.latestEdition = latestEdition;
224 * Return the time of the last inserted update of this Sone.
226 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
228 public long getTime() {
233 * Sets the time of the last inserted update of this Sone.
236 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
237 * @return This Sone (for method chaining)
239 public Sone setTime(long time) {
245 * Returns a copy of the profile. If you want to update values in the
246 * profile of this Sone, update the values in the returned {@link Profile}
247 * and use {@link #setProfile(Profile)} to change the profile in this Sone.
249 * @return A copy of the profile
251 public Profile getProfile() {
252 return new Profile(profile);
256 * Sets the profile of this Sone. A copy of the given profile is stored so
257 * that subsequent modifications of the given profile are not reflected in
263 public synchronized void setProfile(Profile profile) {
264 this.profile = new Profile(profile);
265 modificationCounter++;
269 * Returns all friend Sones of this Sone.
271 * @return The friend Sones of this Sone
273 public List<Sone> getFriends() {
274 List<Sone> friends = new ArrayList<Sone>(friendSones);
275 Collections.sort(friends, new Comparator<Sone>() {
278 public int compare(Sone leftSone, Sone rightSone) {
279 int diff = SoneAccessor.getNiceName(leftSone).compareToIgnoreCase(SoneAccessor.getNiceName(rightSone));
283 return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightSone.getTime() - leftSone.getTime()));
290 * Sets all friends of this Sone at once.
293 * The new (and only) friends of this Sone
294 * @return This Sone (for method chaining)
296 public Sone setFriends(Collection<Sone> friends) {
298 friendSones.addAll(friends);
303 * Returns whether this Sone has the given Sone as a friend Sone.
306 * The friend Sone to check for
307 * @return {@code true} if this Sone has the given Sone as a friend,
308 * {@code false} otherwise
310 public boolean hasFriend(Sone friendSone) {
311 return friendSones.contains(friendSone);
315 * Adds the given Sone as a friend Sone.
318 * The friend Sone to add
319 * @return This Sone (for method chaining)
321 public Sone addFriend(Sone friendSone) {
322 if (!friendSone.equals(this)) {
323 friendSones.add(friendSone);
329 * Removes the given Sone as a friend Sone.
332 * The friend Sone to remove
333 * @return This Sone (for method chaining)
335 public Sone removeFriend(Sone friendSone) {
336 friendSones.remove(friendSone);
341 * Returns the list of posts of this Sone, sorted by time, newest first.
343 * @return All posts of this Sone
345 public List<Post> getPosts() {
346 List<Post> sortedPosts = new ArrayList<Post>(posts);
347 Collections.sort(sortedPosts, new Comparator<Post>() {
350 public int compare(Post leftPost, Post rightPost) {
351 return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightPost.getTime() - leftPost.getTime()));
359 * Sets all posts of this Sone at once.
362 * The new (and only) posts of this Sone
363 * @return This Sone (for method chaining)
365 public synchronized Sone setPosts(Collection<Post> posts) {
367 this.posts.addAll(posts);
368 modificationCounter++;
373 * Adds the given post to this Sone. The post will not be added if its
374 * {@link Post#getSone() Sone} is not this Sone.
379 public synchronized void addPost(Post post) {
380 if (post.getSone().equals(this) && posts.add(post)) {
381 logger.log(Level.FINEST, "Adding %s to ā%sā.", new Object[] { post, getName() });
382 modificationCounter++;
387 * Removes the given post from this Sone.
392 public synchronized void removePost(Post post) {
393 if (post.getSone().equals(this) && posts.remove(post)) {
394 modificationCounter++;
399 * Returns all replies this Sone made.
401 * @return All replies this Sone made
403 public Set<Reply> getReplies() {
404 logger.log(Level.FINEST, "Friends of %s: %s", new Object[] { this, friendSones });
405 return Collections.unmodifiableSet(replies);
409 * Sets all replies of this Sone at once.
412 * The new (and only) replies of this Sone
413 * @return This Sone (for method chaining)
415 public synchronized Sone setReplies(Collection<Reply> replies) {
416 this.replies.clear();
417 this.replies.addAll(replies);
418 modificationCounter++;
423 * Adds a reply to this Sone. If the given reply was not made by this Sone,
424 * nothing is added to this Sone.
429 public synchronized void addReply(Reply reply) {
430 if (reply.getSone().equals(this) && replies.add(reply)) {
431 modificationCounter++;
436 * Removes a reply from this Sone.
439 * The reply to remove
441 public synchronized void removeReply(Reply reply) {
442 if (reply.getSone().equals(this) && replies.remove(reply)) {
443 modificationCounter++;
448 * Returns the IDs of all liked posts.
450 * @return All liked postsā IDs
452 public Set<String> getLikedPostIds() {
453 return Collections.unmodifiableSet(likedPostIds);
457 * Sets the IDs of all liked posts.
459 * @param likedPostIds
460 * All liked postsā IDs
461 * @return This Sone (for method chaining)
463 public synchronized Sone setLikePostIds(Set<String> likedPostIds) {
464 this.likedPostIds.clear();
465 this.likedPostIds.addAll(likedPostIds);
466 modificationCounter++;
471 * Checks whether the given post ID is liked by this Sone.
475 * @return {@code true} if this Sone likes the given post, {@code false}
478 public boolean isLikedPostId(String postId) {
479 return likedPostIds.contains(postId);
483 * Adds the given post ID to the list of posts this Sone likes.
487 * @return This Sone (for method chaining)
489 public synchronized Sone addLikedPostId(String postId) {
490 if (likedPostIds.add(postId)) {
491 modificationCounter++;
497 * Removes the given post ID from the list of posts this Sone likes.
501 * @return This Sone (for method chaining)
503 public synchronized Sone removeLikedPostId(String postId) {
504 if (likedPostIds.remove(postId)) {
505 modificationCounter++;
511 * Returns the IDs of all liked replies.
513 * @return All liked repliesā IDs
515 public Set<String> getLikedReplyIds() {
516 return Collections.unmodifiableSet(likedReplyIds);
520 * Sets the IDs of all liked replies.
522 * @param likedReplyIds
523 * All liked repliesā IDs
524 * @return This Sone (for method chaining)
526 public synchronized Sone setLikeReplyIds(Set<String> likedReplyIds) {
527 this.likedReplyIds.clear();
528 this.likedReplyIds.addAll(likedReplyIds);
529 modificationCounter++;
534 * Checks whether the given reply ID is liked by this Sone.
537 * The ID of the reply
538 * @return {@code true} if this Sone likes the given reply, {@code false}
541 public boolean isLikedReplyId(String replyId) {
542 return likedReplyIds.contains(replyId);
546 * Adds the given reply ID to the list of replies this Sone likes.
549 * The ID of the reply
550 * @return This Sone (for method chaining)
552 public synchronized Sone addLikedReplyId(String replyId) {
553 if (likedReplyIds.add(replyId)) {
554 modificationCounter++;
560 * Removes the given post ID from the list of replies this Sone likes.
563 * The ID of the reply
564 * @return This Sone (for method chaining)
566 public synchronized Sone removeLikedReplyId(String replyId) {
567 if (likedReplyIds.remove(replyId)) {
568 modificationCounter++;
574 * Returns the modification counter.
576 * @return The modification counter
578 public synchronized long getModificationCounter() {
579 return modificationCounter;
583 * Sets the modification counter.
585 * @param modificationCounter
586 * The new modification counter
588 public synchronized void setModificationCounter(long modificationCounter) {
589 this.modificationCounter = modificationCounter;
600 public int hashCode() {
601 return identity.getId().hashCode();
608 public boolean equals(Object object) {
609 if (!(object instanceof Sone)) {
612 return ((Sone) object).identity.getId().equals(identity.getId());
619 public String toString() {
620 return getClass().getName() + "[identity=" + identity + ",requestUri=" + requestUri + ",insertUri=" + insertUri + ",friends(" + friendSones.size() + "),posts(" + posts.size() + "),replies(" + replies.size() + ")]";