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>
45 private static final Logger logger = Logging.getLogger(Sone.class);
47 /** The ID of this Sone. */
48 private final String id;
50 /** The name of this Sone. */
51 private volatile String name;
53 /** The URI under which the Sone is stored in Freenet. */
54 private volatile FreenetURI requestUri;
56 /** The URI used to insert a new version of this Sone. */
57 /* This will be null for remote Sones! */
58 private volatile FreenetURI insertUri;
60 /** The time of the last inserted update. */
61 private volatile long time;
63 /** The profile of this Sone. */
64 private volatile Profile profile;
66 /** All friend Sones. */
67 private final Set<Sone> friendSones = Collections.synchronizedSet(new HashSet<Sone>());
70 private final Set<Post> posts = Collections.synchronizedSet(new HashSet<Post>());
73 private final Set<Reply> replies = Collections.synchronizedSet(new HashSet<Reply>());
75 /** The IDs of all liked posts. */
76 private final Set<String> likedPostIds = Collections.synchronizedSet(new HashSet<String>());
78 /** The IDs of all liked replies. */
79 private final Set<String> likedReplyIds = Collections.synchronizedSet(new HashSet<String>());
81 /** Modification count. */
82 private volatile long modificationCounter = 0;
90 public Sone(String id) {
99 * Returns the ID of this Sone.
101 * @return The ID of this Sone
103 public String getId() {
108 * Returns the name of this Sone.
110 * @return The name of this Sone
112 public String getName() {
117 * Sets the name of this Sone.
120 * The name of this Sone
121 * @return This sone (for method chaining)
123 public Sone setName(String name) {
129 * Returns the request URI of this Sone.
131 * @return The request URI of this Sone
133 public FreenetURI getRequestUri() {
138 * Sets the request URI of this Sone.
141 * The request URI of this Sone
142 * @return This Sone (for method chaining)
144 public Sone setRequestUri(FreenetURI requestUri) {
145 if (this.requestUri == null) {
146 this.requestUri = requestUri;
150 if (!this.requestUri.equalsKeypair(requestUri)) {
151 logger.log(Level.WARNING, "Request URI %s tried to overwrite %s!", new Object[] { requestUri, this.requestUri });
154 long latestEdition = requestUri.getEdition();
155 if ((latestEdition > this.requestUri.getEdition()) || (latestEdition > this.requestUri.getSuggestedEdition())) {
156 this.requestUri.setSuggestedEdition(latestEdition);
162 * Returns the insert URI of this Sone.
164 * @return The insert URI of this Sone
166 public FreenetURI getInsertUri() {
171 * Sets the insert URI of this Sone.
174 * The insert URI of this Sone
175 * @return This Sone (for method chaining)
177 public Sone setInsertUri(FreenetURI insertUri) {
178 if (this.insertUri == null) {
179 this.insertUri = insertUri;
183 if (!this.insertUri.equalsKeypair(insertUri)) {
184 logger.log(Level.WARNING, "Request URI %s tried to overwrite %s!", new Object[] { insertUri, this.insertUri });
187 long latestEdition = insertUri.getEdition();
188 if ((latestEdition > this.insertUri.getEdition()) || (latestEdition > this.insertUri.getSuggestedEdition())) {
189 this.insertUri.setSuggestedEdition(latestEdition);
195 * Return the time of the last inserted update of this Sone.
197 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
199 public long getTime() {
204 * Sets the time of the last inserted update of this Sone.
207 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
208 * @return This Sone (for method chaining)
210 public Sone setTime(long time) {
216 * Returns a copy of the profile. If you want to update values in the
217 * profile of this Sone, update the values in the returned {@link Profile}
218 * and use {@link #setProfile(Profile)} to change the profile in this Sone.
220 * @return A copy of the profile
222 public Profile getProfile() {
223 return new Profile(profile);
227 * Sets the profile of this Sone. A copy of the given profile is stored so
228 * that subsequent modifications of the given profile are not reflected in
234 public synchronized void setProfile(Profile profile) {
235 this.profile = new Profile(profile);
236 modificationCounter++;
240 * Returns all friend Sones of this Sone.
242 * @return The friend Sones of this Sone
244 public List<Sone> getFriends() {
245 List<Sone> friends = new ArrayList<Sone>(friendSones);
246 Collections.sort(friends, new Comparator<Sone>() {
249 public int compare(Sone leftSone, Sone rightSone) {
250 int diff = SoneAccessor.getNiceName(leftSone).compareToIgnoreCase(SoneAccessor.getNiceName(rightSone));
254 return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightSone.getTime() - leftSone.getTime()));
261 * Sets all friends of this Sone at once.
264 * The new (and only) friends of this Sone
265 * @return This Sone (for method chaining)
267 public Sone setFriends(Collection<Sone> friends) {
269 friendSones.addAll(friends);
274 * Returns whether this Sone has the given Sone as a friend Sone.
277 * The friend Sone to check for
278 * @return {@code true} if this Sone has the given Sone as a friend,
279 * {@code false} otherwise
281 public boolean hasFriend(Sone friendSone) {
282 return friendSones.contains(friendSone);
286 * Adds the given Sone as a friend Sone.
289 * The friend Sone to add
290 * @return This Sone (for method chaining)
292 public Sone addFriend(Sone friendSone) {
293 if (!friendSone.equals(this)) {
294 friendSones.add(friendSone);
300 * Removes the given Sone as a friend Sone.
303 * The friend Sone to remove
304 * @return This Sone (for method chaining)
306 public Sone removeFriend(Sone friendSone) {
307 friendSones.remove(friendSone);
312 * Returns the list of posts of this Sone, sorted by time, newest first.
314 * @return All posts of this Sone
316 public List<Post> getPosts() {
317 List<Post> sortedPosts = new ArrayList<Post>(posts);
318 Collections.sort(sortedPosts, new Comparator<Post>() {
321 public int compare(Post leftPost, Post rightPost) {
322 return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightPost.getTime() - leftPost.getTime()));
330 * Sets all posts of this Sone at once.
333 * The new (and only) posts of this Sone
334 * @return This Sone (for method chaining)
336 public synchronized Sone setPosts(Collection<Post> posts) {
338 this.posts.addAll(posts);
339 modificationCounter++;
344 * Adds the given post to this Sone. The post will not be added if its
345 * {@link Post#getSone() Sone} is not this Sone.
350 public synchronized void addPost(Post post) {
351 if (post.getSone().equals(this) && posts.add(post)) {
352 logger.log(Level.FINEST, "Adding %s to “%s”.", new Object[] { post, getName() });
353 modificationCounter++;
358 * Removes the given post from this Sone.
363 public synchronized void removePost(Post post) {
364 if (post.getSone().equals(this) && posts.remove(post)) {
365 modificationCounter++;
370 * Returns all replies this Sone made.
372 * @return All replies this Sone made
374 public Set<Reply> getReplies() {
375 logger.log(Level.FINEST, "Friends of %s: %s", new Object[] { this, friendSones });
376 return Collections.unmodifiableSet(replies);
380 * Sets all replies of this Sone at once.
383 * The new (and only) replies of this Sone
384 * @return This Sone (for method chaining)
386 public synchronized Sone setReplies(Collection<Reply> replies) {
387 this.replies.clear();
388 this.replies.addAll(replies);
389 modificationCounter++;
394 * Adds a reply to this Sone. If the given reply was not made by this Sone,
395 * nothing is added to this Sone.
400 public synchronized void addReply(Reply reply) {
401 if (reply.getSone().equals(this) && replies.add(reply)) {
402 modificationCounter++;
407 * Removes a reply from this Sone.
410 * The reply to remove
412 public synchronized void removeReply(Reply reply) {
413 if (reply.getSone().equals(this) && replies.remove(reply)) {
414 modificationCounter++;
419 * Returns the IDs of all liked posts.
421 * @return All liked posts’ IDs
423 public Set<String> getLikedPostIds() {
424 return Collections.unmodifiableSet(likedPostIds);
428 * Sets the IDs of all liked posts.
430 * @param likedPostIds
431 * All liked posts’ IDs
432 * @return This Sone (for method chaining)
434 public synchronized Sone setLikePostIds(Set<String> likedPostIds) {
435 this.likedPostIds.clear();
436 this.likedPostIds.addAll(likedPostIds);
437 modificationCounter++;
442 * Checks whether the given post ID is liked by this Sone.
446 * @return {@code true} if this Sone likes the given post, {@code false}
449 public boolean isLikedPostId(String postId) {
450 return likedPostIds.contains(postId);
454 * Adds the given post ID to the list of posts this Sone likes.
458 * @return This Sone (for method chaining)
460 public synchronized Sone addLikedPostId(String postId) {
461 if (likedPostIds.add(postId)) {
462 modificationCounter++;
468 * Removes the given post ID from the list of posts this Sone likes.
472 * @return This Sone (for method chaining)
474 public synchronized Sone removeLikedPostId(String postId) {
475 if (likedPostIds.remove(postId)) {
476 modificationCounter++;
482 * Returns the IDs of all liked replies.
484 * @return All liked replies’ IDs
486 public Set<String> getLikedReplyIds() {
487 return Collections.unmodifiableSet(likedReplyIds);
491 * Sets the IDs of all liked replies.
493 * @param likedReplyIds
494 * All liked replies’ IDs
495 * @return This Sone (for method chaining)
497 public synchronized Sone setLikeReplyIds(Set<String> likedReplyIds) {
498 this.likedReplyIds.clear();
499 this.likedReplyIds.addAll(likedReplyIds);
500 modificationCounter++;
505 * Checks whether the given reply ID is liked by this Sone.
508 * The ID of the reply
509 * @return {@code true} if this Sone likes the given reply, {@code false}
512 public boolean isLikedReplyId(String replyId) {
513 return likedReplyIds.contains(replyId);
517 * Adds the given reply ID to the list of replies this Sone likes.
520 * The ID of the reply
521 * @return This Sone (for method chaining)
523 public synchronized Sone addLikedReplyId(String replyId) {
524 if (likedReplyIds.add(replyId)) {
525 modificationCounter++;
531 * Removes the given post ID from the list of replies this Sone likes.
534 * The ID of the reply
535 * @return This Sone (for method chaining)
537 public synchronized Sone removeLikedReplyId(String replyId) {
538 if (likedReplyIds.remove(replyId)) {
539 modificationCounter++;
545 * Returns the modification counter.
547 * @return The modification counter
549 public synchronized long getModificationCounter() {
550 return modificationCounter;
554 * Sets the modification counter.
556 * @param modificationCounter
557 * The new modification counter
559 public synchronized void setModificationCounter(long modificationCounter) {
560 this.modificationCounter = modificationCounter;
564 * Updates the suggested edition in both the request URI and the insert URI.
566 * @param latestEdition
567 * The latest edition to update the URIs to
569 public void updateUris(long latestEdition) {
570 if ((requestUri != null) && (requestUri.getEdition() < latestEdition)) {
571 requestUri = requestUri.setSuggestedEdition(latestEdition);
573 if ((insertUri != null) && (insertUri.getEdition() < latestEdition)) {
574 insertUri = insertUri.setSuggestedEdition(latestEdition);
583 * Updates the editions of the request URI and the insert URI (if latter is
584 * not {@code null}) with the greater edition of either one.
586 private void updateEditions() {
587 long requestEdition = 0;
588 if (requestUri != null) {
589 requestEdition = requestUri.getEdition();
591 long insertEdition = 0;
592 if (insertUri != null) {
593 insertEdition = insertUri.getEdition();
595 updateUris(Math.max(requestEdition, insertEdition));
606 public int hashCode() {
607 return id.hashCode();
614 public boolean equals(Object object) {
615 if (!(object instanceof Sone)) {
618 return ((Sone) object).id.equals(id);
625 public String toString() {
626 return getClass().getName() + "[id=" + id + ",name=" + name + ",requestUri=" + requestUri + ",insertUri=" + insertUri + ",friends(" + friendSones.size() + "),posts(" + posts.size() + "),replies(" + replies.size() + ")]";