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 ID of this Sone. */
63 private final String id;
65 /** The identity of this Sone. */
66 private Identity identity;
68 /** The URI under which the Sone is stored in Freenet. */
69 private volatile FreenetURI requestUri;
71 /** The URI used to insert a new version of this Sone. */
72 /* This will be null for remote Sones! */
73 private volatile FreenetURI insertUri;
75 /** The latest edition of the Sone. */
76 private volatile long latestEdition;
78 /** The time of the last inserted update. */
79 private volatile long time;
81 /** The profile of this Sone. */
82 private volatile Profile profile;
84 /** All friend Sones. */
85 private final Set<Sone> friendSones = Collections.synchronizedSet(new HashSet<Sone>());
88 private final Set<Post> posts = Collections.synchronizedSet(new HashSet<Post>());
91 private final Set<Reply> replies = Collections.synchronizedSet(new HashSet<Reply>());
93 /** The IDs of all liked posts. */
94 private final Set<String> likedPostIds = Collections.synchronizedSet(new HashSet<String>());
96 /** The IDs of all liked replies. */
97 private final Set<String> likedReplyIds = Collections.synchronizedSet(new HashSet<String>());
99 /** Modification count. */
100 private volatile long modificationCounter = 0;
103 * Creates a new Sone.
108 public Sone(String id) {
117 * Returns the identity of this Sone.
119 * @return The identity of this Sone
121 public String getId() {
126 * Returns the identity of this Sone.
128 * @return The identity of this Sone
130 public Identity getIdentity() {
135 * Sets the identity of this Sone. The {@link Identity#getId() ID} of the
136 * identity has to match this Sone’s {@link #getId()}.
139 * The identity of this Sone
140 * @return This Sone (for method chaining)
141 * @throws IllegalArgumentException
142 * if the ID of the identity does not match this Sone’s ID
144 public Sone setIdentity(Identity identity) throws IllegalArgumentException {
145 if (!identity.getId().equals(id)) {
146 throw new IllegalArgumentException("Identity’s ID does not match Sone’s ID!");
148 this.identity = identity;
153 * Returns the name of this Sone.
155 * @return The name of this Sone
157 public String getName() {
158 return (identity != null) ? identity.getNickname() : null;
162 * Returns the request URI of this Sone.
164 * @return The request URI of this Sone
166 public FreenetURI getRequestUri() {
167 return requestUri.setSuggestedEdition(latestEdition);
171 * Sets the request URI of this Sone.
174 * The request URI of this Sone
175 * @return This Sone (for method chaining)
177 public Sone setRequestUri(FreenetURI requestUri) {
178 if (this.requestUri == null) {
179 this.requestUri = requestUri.setDocName("Sone").setMetaString(new String[0]);
182 if (!this.requestUri.equalsKeypair(requestUri)) {
183 logger.log(Level.WARNING, "Request URI %s tried to overwrite %s!", new Object[] { requestUri, this.requestUri });
190 * Returns the insert URI of this Sone.
192 * @return The insert URI of this Sone
194 public FreenetURI getInsertUri() {
195 return (insertUri != null) ? insertUri.setSuggestedEdition(latestEdition) : null;
199 * Sets the insert URI of this Sone.
202 * The insert URI of this Sone
203 * @return This Sone (for method chaining)
205 public Sone setInsertUri(FreenetURI insertUri) {
206 if (this.insertUri == null) {
207 this.insertUri = insertUri.setDocName("Sone").setMetaString(new String[0]);
210 if (!this.insertUri.equalsKeypair(insertUri)) {
211 logger.log(Level.WARNING, "Request URI %s tried to overwrite %s!", new Object[] { insertUri, this.insertUri });
218 * Returns the latest edition of this Sone.
220 * @return The latest edition of this Sone
222 public long getLatestEdition() {
223 return latestEdition;
227 * Sets the latest edition of this Sone. If the given latest edition is not
228 * greater than the current latest edition, the latest edition of this Sone
231 * @param latestEdition
232 * The latest edition of this Sone
234 public void setLatestEdition(long latestEdition) {
235 if (!(latestEdition > this.latestEdition)) {
236 logger.log(Level.INFO, "New latest edition %d is not greater than current latest edition %d!", new Object[] { latestEdition, this.latestEdition });
239 this.latestEdition = latestEdition;
243 * Return the time of the last inserted update of this Sone.
245 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
247 public long getTime() {
252 * Sets the time of the last inserted update of this Sone.
255 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
256 * @return This Sone (for method chaining)
258 public Sone setTime(long time) {
264 * Returns a copy of the profile. If you want to update values in the
265 * profile of this Sone, update the values in the returned {@link Profile}
266 * and use {@link #setProfile(Profile)} to change the profile in this Sone.
268 * @return A copy of the profile
270 public Profile getProfile() {
271 return new Profile(profile);
275 * Sets the profile of this Sone. A copy of the given profile is stored so
276 * that subsequent modifications of the given profile are not reflected in
282 public synchronized void setProfile(Profile profile) {
283 this.profile = new Profile(profile);
284 modificationCounter++;
288 * Returns all friend Sones of this Sone.
290 * @return The friend Sones of this Sone
292 public List<Sone> getFriends() {
293 List<Sone> friends = new ArrayList<Sone>(friendSones);
294 Collections.sort(friends, new Comparator<Sone>() {
297 public int compare(Sone leftSone, Sone rightSone) {
298 int diff = SoneAccessor.getNiceName(leftSone).compareToIgnoreCase(SoneAccessor.getNiceName(rightSone));
302 return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightSone.getTime() - leftSone.getTime()));
309 * Sets all friends of this Sone at once.
312 * The new (and only) friends of this Sone
313 * @return This Sone (for method chaining)
315 public Sone setFriends(Collection<Sone> friends) {
317 friendSones.addAll(friends);
322 * Returns whether this Sone has the given Sone as a friend Sone.
325 * The friend Sone to check for
326 * @return {@code true} if this Sone has the given Sone as a friend,
327 * {@code false} otherwise
329 public boolean hasFriend(Sone friendSone) {
330 return friendSones.contains(friendSone);
334 * Adds the given Sone as a friend Sone.
337 * The friend Sone to add
338 * @return This Sone (for method chaining)
340 public Sone addFriend(Sone friendSone) {
341 if (!friendSone.equals(this)) {
342 friendSones.add(friendSone);
348 * Removes the given Sone as a friend Sone.
351 * The friend Sone to remove
352 * @return This Sone (for method chaining)
354 public Sone removeFriend(Sone friendSone) {
355 friendSones.remove(friendSone);
360 * Returns the list of posts of this Sone, sorted by time, newest first.
362 * @return All posts of this Sone
364 public List<Post> getPosts() {
365 List<Post> sortedPosts = new ArrayList<Post>(posts);
366 Collections.sort(sortedPosts, new Comparator<Post>() {
369 public int compare(Post leftPost, Post rightPost) {
370 return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightPost.getTime() - leftPost.getTime()));
378 * Sets all posts of this Sone at once.
381 * The new (and only) posts of this Sone
382 * @return This Sone (for method chaining)
384 public synchronized Sone setPosts(Collection<Post> posts) {
386 this.posts.addAll(posts);
387 modificationCounter++;
392 * Adds the given post to this Sone. The post will not be added if its
393 * {@link Post#getSone() Sone} is not this Sone.
398 public synchronized void addPost(Post post) {
399 if (post.getSone().equals(this) && posts.add(post)) {
400 logger.log(Level.FINEST, "Adding %s to “%s”.", new Object[] { post, getName() });
401 modificationCounter++;
406 * Removes the given post from this Sone.
411 public synchronized void removePost(Post post) {
412 if (post.getSone().equals(this) && posts.remove(post)) {
413 modificationCounter++;
418 * Returns all replies this Sone made.
420 * @return All replies this Sone made
422 public Set<Reply> getReplies() {
423 return Collections.unmodifiableSet(replies);
427 * Sets all replies of this Sone at once.
430 * The new (and only) replies of this Sone
431 * @return This Sone (for method chaining)
433 public synchronized Sone setReplies(Collection<Reply> replies) {
434 this.replies.clear();
435 this.replies.addAll(replies);
436 modificationCounter++;
441 * Adds a reply to this Sone. If the given reply was not made by this Sone,
442 * nothing is added to this Sone.
447 public synchronized void addReply(Reply reply) {
448 if (reply.getSone().equals(this) && replies.add(reply)) {
449 modificationCounter++;
454 * Removes a reply from this Sone.
457 * The reply to remove
459 public synchronized void removeReply(Reply reply) {
460 if (reply.getSone().equals(this) && replies.remove(reply)) {
461 modificationCounter++;
466 * Returns the IDs of all liked posts.
468 * @return All liked posts’ IDs
470 public Set<String> getLikedPostIds() {
471 return Collections.unmodifiableSet(likedPostIds);
475 * Sets the IDs of all liked posts.
477 * @param likedPostIds
478 * All liked posts’ IDs
479 * @return This Sone (for method chaining)
481 public synchronized Sone setLikePostIds(Set<String> likedPostIds) {
482 this.likedPostIds.clear();
483 this.likedPostIds.addAll(likedPostIds);
484 modificationCounter++;
489 * Checks whether the given post ID is liked by this Sone.
493 * @return {@code true} if this Sone likes the given post, {@code false}
496 public boolean isLikedPostId(String postId) {
497 return likedPostIds.contains(postId);
501 * Adds the given post ID to the list of posts this Sone likes.
505 * @return This Sone (for method chaining)
507 public synchronized Sone addLikedPostId(String postId) {
508 if (likedPostIds.add(postId)) {
509 modificationCounter++;
515 * Removes the given post ID from the list of posts this Sone likes.
519 * @return This Sone (for method chaining)
521 public synchronized Sone removeLikedPostId(String postId) {
522 if (likedPostIds.remove(postId)) {
523 modificationCounter++;
529 * Returns the IDs of all liked replies.
531 * @return All liked replies’ IDs
533 public Set<String> getLikedReplyIds() {
534 return Collections.unmodifiableSet(likedReplyIds);
538 * Sets the IDs of all liked replies.
540 * @param likedReplyIds
541 * All liked replies’ IDs
542 * @return This Sone (for method chaining)
544 public synchronized Sone setLikeReplyIds(Set<String> likedReplyIds) {
545 this.likedReplyIds.clear();
546 this.likedReplyIds.addAll(likedReplyIds);
547 modificationCounter++;
552 * Checks whether the given reply ID is liked by this Sone.
555 * The ID of the reply
556 * @return {@code true} if this Sone likes the given reply, {@code false}
559 public boolean isLikedReplyId(String replyId) {
560 return likedReplyIds.contains(replyId);
564 * Adds the given reply ID to the list of replies this Sone likes.
567 * The ID of the reply
568 * @return This Sone (for method chaining)
570 public synchronized Sone addLikedReplyId(String replyId) {
571 if (likedReplyIds.add(replyId)) {
572 modificationCounter++;
578 * Removes the given post ID from the list of replies this Sone likes.
581 * The ID of the reply
582 * @return This Sone (for method chaining)
584 public synchronized Sone removeLikedReplyId(String replyId) {
585 if (likedReplyIds.remove(replyId)) {
586 modificationCounter++;
592 * Returns the modification counter.
594 * @return The modification counter
596 public synchronized long getModificationCounter() {
597 return modificationCounter;
601 * Sets the modification counter.
603 * @param modificationCounter
604 * The new modification counter
606 public synchronized void setModificationCounter(long modificationCounter) {
607 this.modificationCounter = modificationCounter;
618 public int hashCode() {
619 return id.hashCode();
626 public boolean equals(Object object) {
627 if (!(object instanceof Sone)) {
630 return ((Sone) object).id.equals(id);
637 public String toString() {
638 return getClass().getName() + "[identity=" + identity + ",requestUri=" + requestUri + ",insertUri=" + insertUri + ",friends(" + friendSones.size() + "),posts(" + posts.size() + "),replies(" + replies.size() + ")]";