2 * Sone - SoneImpl.java - Copyright © 2010–2013 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 static com.google.common.base.Preconditions.checkNotNull;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.List;
27 import java.util.concurrent.CopyOnWriteArraySet;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
31 import net.pterodactylus.sone.data.SoneOptions.DefaultSoneOptions;
32 import net.pterodactylus.sone.freenet.wot.Identity;
33 import net.pterodactylus.util.logging.Logging;
35 import freenet.keys.FreenetURI;
37 import com.google.common.hash.Hasher;
38 import com.google.common.hash.Hashing;
41 * {@link Sone} implementation.
43 * Operations that modify the Sone need to synchronize on the Sone in question.
45 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
47 public class SoneImpl implements Sone {
50 private static final Logger logger = Logging.getLogger(SoneImpl.class);
52 /** The ID of this Sone. */
53 private final String id;
55 /** Whether the Sone is local. */
56 private final boolean local;
58 /** The identity of this Sone. */
59 private Identity identity;
61 /** The URI under which the Sone is stored in Freenet. */
62 private volatile FreenetURI requestUri;
64 /** The URI used to insert a new version of this Sone. */
65 /* This will be null for remote Sones! */
66 private volatile FreenetURI insertUri;
68 /** The latest edition of the Sone. */
69 private volatile long latestEdition;
71 /** The time of the last inserted update. */
72 private volatile long time;
74 /** The status of this Sone. */
75 private volatile SoneStatus status = SoneStatus.unknown;
77 /** The profile of this Sone. */
78 private volatile Profile profile = new Profile(this);
80 /** The client used by the Sone. */
81 private volatile Client client;
83 /** Whether this Sone is known. */
84 private volatile boolean known;
86 /** All friend Sones. */
87 private final Set<String> friendSones = new CopyOnWriteArraySet<String>();
90 private final Set<Post> posts = new CopyOnWriteArraySet<Post>();
93 private final Set<PostReply> replies = new CopyOnWriteArraySet<PostReply>();
95 /** The IDs of all liked posts. */
96 private final Set<String> likedPostIds = new CopyOnWriteArraySet<String>();
98 /** The IDs of all liked replies. */
99 private final Set<String> likedReplyIds = new CopyOnWriteArraySet<String>();
101 /** The root album containing all albums. */
102 private final Album rootAlbum = new AlbumImpl().setSone(this);
104 /** Sone-specific options. */
105 private SoneOptions options = new DefaultSoneOptions();
108 * Creates a new Sone.
113 * {@code true} if the Sone is a local Sone, {@code false} otherwise
115 public SoneImpl(String id, boolean local) {
125 * Returns the identity of this Sone.
127 * @return The identity of this Sone
129 public String getId() {
134 * Returns the identity of this Sone.
136 * @return The identity of this Sone
138 public Identity getIdentity() {
143 * Sets the identity of this Sone. The {@link Identity#getId() ID} of the
144 * identity has to match this Sone’s {@link #getId()}.
147 * The identity of this Sone
148 * @return This Sone (for method chaining)
149 * @throws IllegalArgumentException
150 * if the ID of the identity does not match this Sone’s ID
152 public SoneImpl setIdentity(Identity identity) throws IllegalArgumentException {
153 if (!identity.getId().equals(id)) {
154 throw new IllegalArgumentException("Identity’s ID does not match Sone’s ID!");
156 this.identity = identity;
161 * Returns the name of this Sone.
163 * @return The name of this Sone
165 public String getName() {
166 return (identity != null) ? identity.getNickname() : null;
170 * Returns whether this Sone is a local Sone.
172 * @return {@code true} if this Sone is a local Sone, {@code false} otherwise
174 public boolean isLocal() {
179 * Returns the request URI of this Sone.
181 * @return The request URI of this Sone
183 public FreenetURI getRequestUri() {
184 return (requestUri != null) ? requestUri.setSuggestedEdition(latestEdition) : null;
188 * Sets the request URI of this Sone.
191 * The request URI of this Sone
192 * @return This Sone (for method chaining)
194 public Sone setRequestUri(FreenetURI requestUri) {
195 if (this.requestUri == null) {
196 this.requestUri = requestUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
199 if (!this.requestUri.equalsKeypair(requestUri)) {
200 logger.log(Level.WARNING, String.format("Request URI %s tried to overwrite %s!", requestUri, this.requestUri));
207 * Returns the insert URI of this Sone.
209 * @return The insert URI of this Sone
211 public FreenetURI getInsertUri() {
212 return (insertUri != null) ? insertUri.setSuggestedEdition(latestEdition) : null;
216 * Sets the insert URI of this Sone.
219 * The insert URI of this Sone
220 * @return This Sone (for method chaining)
222 public Sone setInsertUri(FreenetURI insertUri) {
223 if (this.insertUri == null) {
224 this.insertUri = insertUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
227 if (!this.insertUri.equalsKeypair(insertUri)) {
228 logger.log(Level.WARNING, String.format("Request URI %s tried to overwrite %s!", insertUri, this.insertUri));
235 * Returns the latest edition of this Sone.
237 * @return The latest edition of this Sone
239 public long getLatestEdition() {
240 return latestEdition;
244 * Sets the latest edition of this Sone. If the given latest edition is not
245 * greater than the current latest edition, the latest edition of this Sone is
248 * @param latestEdition
249 * The latest edition of this Sone
251 public void setLatestEdition(long latestEdition) {
252 if (!(latestEdition > this.latestEdition)) {
253 logger.log(Level.FINE, String.format("New latest edition %d is not greater than current latest edition %d!", latestEdition, this.latestEdition));
256 this.latestEdition = latestEdition;
260 * Return the time of the last inserted update of this Sone.
262 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
264 public long getTime() {
269 * Sets the time of the last inserted update of this Sone.
272 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
273 * @return This Sone (for method chaining)
275 public Sone setTime(long time) {
281 * Returns the status of this Sone.
283 * @return The status of this Sone
285 public SoneStatus getStatus() {
290 * Sets the new status of this Sone.
293 * The new status of this Sone
295 * @throws IllegalArgumentException
296 * if {@code status} is {@code null}
298 public Sone setStatus(SoneStatus status) {
299 this.status = checkNotNull(status, "status must not be null");
304 * Returns a copy of the profile. If you want to update values in the profile
305 * of this Sone, update the values in the returned {@link Profile} and use
306 * {@link #setProfile(Profile)} to change the profile in this Sone.
308 * @return A copy of the profile
310 public Profile getProfile() {
311 return new Profile(profile);
315 * Sets the profile of this Sone. A copy of the given profile is stored so that
316 * subsequent modifications of the given profile are not reflected in this
322 public void setProfile(Profile profile) {
323 this.profile = new Profile(profile);
327 * Returns the client used by this Sone.
329 * @return The client used by this Sone, or {@code null}
331 public Client getClient() {
336 * Sets the client used by this Sone.
339 * The client used by this Sone, or {@code null}
340 * @return This Sone (for method chaining)
342 public Sone setClient(Client client) {
343 this.client = client;
348 * Returns whether this Sone is known.
350 * @return {@code true} if this Sone is known, {@code false} otherwise
352 public boolean isKnown() {
357 * Sets whether this Sone is known.
360 * {@code true} if this Sone is known, {@code false} otherwise
363 public Sone setKnown(boolean known) {
369 * Returns all friend Sones of this Sone.
371 * @return The friend Sones of this Sone
373 public List<String> getFriends() {
374 return new ArrayList<String>(friendSones);
378 * Returns whether this Sone has the given Sone as a friend Sone.
380 * @param friendSoneId
381 * The ID of the Sone to check for
382 * @return {@code true} if this Sone has the given Sone as a friend, {@code
385 public boolean hasFriend(String friendSoneId) {
386 return friendSones.contains(friendSoneId);
390 * Adds the given Sone as a friend Sone.
393 * The friend Sone to add
394 * @return This Sone (for method chaining)
396 public Sone addFriend(String friendSone) {
397 if (!friendSone.equals(id)) {
398 friendSones.add(friendSone);
404 * Removes the given Sone as a friend Sone.
406 * @param friendSoneId
407 * The ID of the friend Sone to remove
408 * @return This Sone (for method chaining)
410 public Sone removeFriend(String friendSoneId) {
411 friendSones.remove(friendSoneId);
416 * Returns the list of posts of this Sone, sorted by time, newest first.
418 * @return All posts of this Sone
420 public List<Post> getPosts() {
421 List<Post> sortedPosts;
422 synchronized (this) {
423 sortedPosts = new ArrayList<Post>(posts);
425 Collections.sort(sortedPosts, Post.TIME_COMPARATOR);
430 * Sets all posts of this Sone at once.
433 * The new (and only) posts of this Sone
434 * @return This Sone (for method chaining)
436 public Sone setPosts(Collection<Post> posts) {
437 synchronized (this) {
439 this.posts.addAll(posts);
445 * Adds the given post to this Sone. The post will not be added if its {@link
446 * Post#getSone() Sone} is not this Sone.
451 public void addPost(Post post) {
452 if (post.getSone().equals(this) && posts.add(post)) {
453 logger.log(Level.FINEST, String.format("Adding %s to “%s”.", post, getName()));
458 * Removes the given post from this Sone.
463 public void removePost(Post post) {
464 if (post.getSone().equals(this)) {
470 * Returns all replies this Sone made.
472 * @return All replies this Sone made
474 public Set<PostReply> getReplies() {
475 return Collections.unmodifiableSet(replies);
479 * Sets all replies of this Sone at once.
482 * The new (and only) replies of this Sone
483 * @return This Sone (for method chaining)
485 public Sone setReplies(Collection<PostReply> replies) {
486 this.replies.clear();
487 this.replies.addAll(replies);
492 * Adds a reply to this Sone. If the given reply was not made by this Sone,
493 * nothing is added to this Sone.
498 public void addReply(PostReply reply) {
499 if (reply.getSone().equals(this)) {
505 * Removes a reply from this Sone.
508 * The reply to remove
510 public void removeReply(PostReply reply) {
511 if (reply.getSone().equals(this)) {
512 replies.remove(reply);
517 * Returns the IDs of all liked posts.
519 * @return All liked posts’ IDs
521 public Set<String> getLikedPostIds() {
522 return Collections.unmodifiableSet(likedPostIds);
526 * Sets the IDs of all liked posts.
528 * @param likedPostIds
529 * All liked posts’ IDs
530 * @return This Sone (for method chaining)
532 public Sone setLikePostIds(Set<String> likedPostIds) {
533 this.likedPostIds.clear();
534 this.likedPostIds.addAll(likedPostIds);
539 * Checks whether the given post ID is liked by this Sone.
543 * @return {@code true} if this Sone likes the given post, {@code false}
546 public boolean isLikedPostId(String postId) {
547 return likedPostIds.contains(postId);
551 * Adds the given post ID to the list of posts this Sone likes.
555 * @return This Sone (for method chaining)
557 public Sone addLikedPostId(String postId) {
558 likedPostIds.add(postId);
563 * Removes the given post ID from the list of posts this Sone likes.
567 * @return This Sone (for method chaining)
569 public Sone removeLikedPostId(String postId) {
570 likedPostIds.remove(postId);
575 * Returns the IDs of all liked replies.
577 * @return All liked replies’ IDs
579 public Set<String> getLikedReplyIds() {
580 return Collections.unmodifiableSet(likedReplyIds);
584 * Sets the IDs of all liked replies.
586 * @param likedReplyIds
587 * All liked replies’ IDs
588 * @return This Sone (for method chaining)
590 public Sone setLikeReplyIds(Set<String> likedReplyIds) {
591 this.likedReplyIds.clear();
592 this.likedReplyIds.addAll(likedReplyIds);
597 * Checks whether the given reply ID is liked by this Sone.
600 * The ID of the reply
601 * @return {@code true} if this Sone likes the given reply, {@code false}
604 public boolean isLikedReplyId(String replyId) {
605 return likedReplyIds.contains(replyId);
609 * Adds the given reply ID to the list of replies this Sone likes.
612 * The ID of the reply
613 * @return This Sone (for method chaining)
615 public Sone addLikedReplyId(String replyId) {
616 likedReplyIds.add(replyId);
621 * Removes the given post ID from the list of replies this Sone likes.
624 * The ID of the reply
625 * @return This Sone (for method chaining)
627 public Sone removeLikedReplyId(String replyId) {
628 likedReplyIds.remove(replyId);
633 * Returns the root album that contains all visible albums of this Sone.
635 * @return The root album of this Sone
637 public Album getRootAlbum() {
642 * Returns Sone-specific options.
644 * @return The options of this Sone
646 public SoneOptions getOptions() {
651 * Sets the options of this Sone.
654 * The options of this Sone
656 /* TODO - remove this method again, maybe add an option provider */
657 public void setOptions(SoneOptions options) {
658 this.options = options;
662 // FINGERPRINTABLE METHODS
667 public synchronized String getFingerprint() {
668 Hasher hash = Hashing.sha256().newHasher();
669 hash.putString(profile.getFingerprint());
671 hash.putString("Posts(");
672 for (Post post : getPosts()) {
673 hash.putString("Post(").putString(post.getId()).putString(")");
677 List<PostReply> replies = new ArrayList<PostReply>(getReplies());
678 Collections.sort(replies, Reply.TIME_COMPARATOR);
679 hash.putString("Replies(");
680 for (PostReply reply : replies) {
681 hash.putString("Reply(").putString(reply.getId()).putString(")");
685 List<String> likedPostIds = new ArrayList<String>(getLikedPostIds());
686 Collections.sort(likedPostIds);
687 hash.putString("LikedPosts(");
688 for (String likedPostId : likedPostIds) {
689 hash.putString("Post(").putString(likedPostId).putString(")");
693 List<String> likedReplyIds = new ArrayList<String>(getLikedReplyIds());
694 Collections.sort(likedReplyIds);
695 hash.putString("LikedReplies(");
696 for (String likedReplyId : likedReplyIds) {
697 hash.putString("Reply(").putString(likedReplyId).putString(")");
701 hash.putString("Albums(");
702 for (Album album : rootAlbum.getAlbums()) {
703 if (!Album.NOT_EMPTY.apply(album)) {
706 hash.putString(album.getFingerprint());
710 return hash.hash().toString();
714 // INTERFACE Comparable<Sone>
719 public int compareTo(Sone sone) {
720 return NICE_NAME_COMPARATOR.compare(this, sone);
729 public int hashCode() {
730 return id.hashCode();
735 public boolean equals(Object object) {
736 if (!(object instanceof Sone)) {
739 return ((Sone) object).getId().equals(id);
744 public String toString() {
745 return getClass().getName() + "[identity=" + identity + ",requestUri=" + requestUri + ",insertUri(" + String.valueOf(insertUri).length() + "),friends(" + friendSones.size() + "),posts(" + posts.size() + "),replies(" + replies.size() + "),albums(" + getRootAlbum().getAlbums().size() + ")]";