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.impl;
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.core.Options;
32 import net.pterodactylus.sone.data.Album;
33 import net.pterodactylus.sone.data.Client;
34 import net.pterodactylus.sone.data.Post;
35 import net.pterodactylus.sone.data.PostReply;
36 import net.pterodactylus.sone.data.Profile;
37 import net.pterodactylus.sone.data.Reply;
38 import net.pterodactylus.sone.data.Sone;
39 import net.pterodactylus.sone.database.AlbumBuilder;
40 import net.pterodactylus.sone.freenet.wot.Identity;
41 import net.pterodactylus.util.logging.Logging;
43 import freenet.keys.FreenetURI;
45 import com.google.common.hash.Hasher;
46 import com.google.common.hash.Hashing;
49 * Dumb, store-everything-in-memory {@link Sone} implementation.
51 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
53 public class DefaultSone implements Sone {
56 private static final Logger logger = Logging.getLogger(DefaultSone.class);
58 /** The ID of this Sone. */
59 private final String id;
61 /** Whether the Sone is local. */
62 private final boolean local;
64 /** The identity of this Sone. */
65 private Identity identity;
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 latest edition of the Sone. */
75 private volatile long latestEdition;
77 /** The time of the last inserted update. */
78 private volatile long time;
80 /** The status of this Sone. */
81 private volatile SoneStatus status = SoneStatus.unknown;
83 /** The profile of this Sone. */
84 private volatile Profile profile = new Profile(this);
86 /** The client used by the Sone. */
87 private volatile Client client;
89 /** Whether this Sone is known. */
90 private volatile boolean known;
92 /** All friend Sones. */
93 private final Set<String> friendSones = new CopyOnWriteArraySet<String>();
96 private final Set<Post> posts = new CopyOnWriteArraySet<Post>();
99 private final Set<PostReply> replies = new CopyOnWriteArraySet<PostReply>();
101 /** The IDs of all liked posts. */
102 private final Set<String> likedPostIds = new CopyOnWriteArraySet<String>();
104 /** The IDs of all liked replies. */
105 private final Set<String> likedReplyIds = new CopyOnWriteArraySet<String>();
107 /** The root album containing all albums. */
108 private final DefaultAlbum rootAlbum = new DefaultAlbum(this, null);
110 /** Sone-specific options. */
111 private Options options = new Options();
114 * Creates a new Sone.
119 * {@code true} if the Sone is a local Sone, {@code false} otherwise
121 public DefaultSone(String id, boolean local) {
131 * Returns the identity of this Sone.
133 * @return The identity of this Sone
135 public String getId() {
140 * Returns the identity of this Sone.
142 * @return The identity of this Sone
144 public Identity getIdentity() {
149 * Sets the identity of this Sone. The {@link Identity#getId() ID} of the
150 * identity has to match this Sone’s {@link #getId()}.
153 * The identity of this Sone
154 * @return This Sone (for method chaining)
155 * @throws IllegalArgumentException
156 * if the ID of the identity does not match this Sone’s ID
158 public DefaultSone setIdentity(Identity identity) throws IllegalArgumentException {
159 if (!identity.getId().equals(id)) {
160 throw new IllegalArgumentException("Identity’s ID does not match Sone’s ID!");
162 this.identity = identity;
167 * Returns the name of this Sone.
169 * @return The name of this Sone
171 public String getName() {
172 return (identity != null) ? identity.getNickname() : null;
176 * Returns whether this Sone is a local Sone.
178 * @return {@code true} if this Sone is a local Sone, {@code false} otherwise
180 public boolean isLocal() {
185 * Returns the request URI of this Sone.
187 * @return The request URI of this Sone
189 public FreenetURI getRequestUri() {
190 return (requestUri != null) ? requestUri.setSuggestedEdition(latestEdition) : null;
194 * Sets the request URI of this Sone.
197 * The request URI of this Sone
198 * @return This Sone (for method chaining)
200 public Sone setRequestUri(FreenetURI requestUri) {
201 if (this.requestUri == null) {
202 this.requestUri = requestUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
205 if (!this.requestUri.equalsKeypair(requestUri)) {
206 logger.log(Level.WARNING, String.format("Request URI %s tried to overwrite %s!", requestUri, this.requestUri));
213 * Returns the insert URI of this Sone.
215 * @return The insert URI of this Sone
217 public FreenetURI getInsertUri() {
218 return (insertUri != null) ? insertUri.setSuggestedEdition(latestEdition) : null;
222 * Sets the insert URI of this Sone.
225 * The insert URI of this Sone
226 * @return This Sone (for method chaining)
228 public Sone setInsertUri(FreenetURI insertUri) {
229 if (this.insertUri == null) {
230 this.insertUri = insertUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
233 if (!this.insertUri.equalsKeypair(insertUri)) {
234 logger.log(Level.WARNING, String.format("Request URI %s tried to overwrite %s!", insertUri, this.insertUri));
241 * Returns the latest edition of this Sone.
243 * @return The latest edition of this Sone
245 public long getLatestEdition() {
246 return latestEdition;
250 * Sets the latest edition of this Sone. If the given latest edition is not
251 * greater than the current latest edition, the latest edition of this Sone is
254 * @param latestEdition
255 * The latest edition of this Sone
257 public void setLatestEdition(long latestEdition) {
258 if (!(latestEdition > this.latestEdition)) {
259 logger.log(Level.FINE, String.format("New latest edition %d is not greater than current latest edition %d!", latestEdition, this.latestEdition));
262 this.latestEdition = latestEdition;
266 * Return the time of the last inserted update of this Sone.
268 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
270 public long getTime() {
275 * Sets the time of the last inserted update of this Sone.
278 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
279 * @return This Sone (for method chaining)
281 public Sone setTime(long time) {
287 * Returns the status of this Sone.
289 * @return The status of this Sone
291 public SoneStatus getStatus() {
296 * Sets the new status of this Sone.
299 * The new status of this Sone
301 * @throws IllegalArgumentException
302 * if {@code status} is {@code null}
304 public Sone setStatus(SoneStatus status) {
305 this.status = checkNotNull(status, "status must not be null");
310 * Returns a copy of the profile. If you want to update values in the profile
311 * of this Sone, update the values in the returned {@link Profile} and use
312 * {@link #setProfile(Profile)} to change the profile in this Sone.
314 * @return A copy of the profile
316 public Profile getProfile() {
317 return new Profile(profile);
321 * Sets the profile of this Sone. A copy of the given profile is stored so that
322 * subsequent modifications of the given profile are not reflected in this
328 public void setProfile(Profile profile) {
329 this.profile = new Profile(profile);
333 * Returns the client used by this Sone.
335 * @return The client used by this Sone, or {@code null}
337 public Client getClient() {
342 * Sets the client used by this Sone.
345 * The client used by this Sone, or {@code null}
346 * @return This Sone (for method chaining)
348 public Sone setClient(Client client) {
349 this.client = client;
354 * Returns whether this Sone is known.
356 * @return {@code true} if this Sone is known, {@code false} otherwise
358 public boolean isKnown() {
363 * Sets whether this Sone is known.
366 * {@code true} if this Sone is known, {@code false} otherwise
369 public Sone setKnown(boolean known) {
375 * Returns all friend Sones of this Sone.
377 * @return The friend Sones of this Sone
379 public List<String> getFriends() {
380 List<String> friends = new ArrayList<String>(friendSones);
385 * Returns whether this Sone has the given Sone as a friend Sone.
387 * @param friendSoneId
388 * The ID of the Sone to check for
389 * @return {@code true} if this Sone has the given Sone as a friend, {@code
392 public boolean hasFriend(String friendSoneId) {
393 return friendSones.contains(friendSoneId);
397 * Adds the given Sone as a friend Sone.
400 * The friend Sone to add
401 * @return This Sone (for method chaining)
403 public Sone addFriend(String friendSone) {
404 if (!friendSone.equals(id)) {
405 friendSones.add(friendSone);
411 * Removes the given Sone as a friend Sone.
413 * @param friendSoneId
414 * The ID of the friend Sone to remove
415 * @return This Sone (for method chaining)
417 public Sone removeFriend(String friendSoneId) {
418 friendSones.remove(friendSoneId);
423 * Returns the list of posts of this Sone, sorted by time, newest first.
425 * @return All posts of this Sone
427 public List<Post> getPosts() {
428 List<Post> sortedPosts;
429 synchronized (this) {
430 sortedPosts = new ArrayList<Post>(posts);
432 Collections.sort(sortedPosts, Post.TIME_COMPARATOR);
437 * Sets all posts of this Sone at once.
440 * The new (and only) posts of this Sone
441 * @return This Sone (for method chaining)
443 public Sone setPosts(Collection<Post> posts) {
444 synchronized (this) {
446 this.posts.addAll(posts);
452 * Adds the given post to this Sone. The post will not be added if its {@link
453 * Post#getSone() Sone} is not this Sone.
458 public void addPost(Post post) {
459 if (post.getSone().equals(this) && posts.add(post)) {
460 logger.log(Level.FINEST, String.format("Adding %s to “%s”.", post, getName()));
465 * Removes the given post from this Sone.
470 public void removePost(Post post) {
471 if (post.getSone().equals(this)) {
477 * Returns all replies this Sone made.
479 * @return All replies this Sone made
481 public Set<PostReply> getReplies() {
482 return Collections.unmodifiableSet(replies);
486 * Sets all replies of this Sone at once.
489 * The new (and only) replies of this Sone
490 * @return This Sone (for method chaining)
492 public Sone setReplies(Collection<PostReply> replies) {
493 this.replies.clear();
494 this.replies.addAll(replies);
499 * Adds a reply to this Sone. If the given reply was not made by this Sone,
500 * nothing is added to this Sone.
505 public void addReply(PostReply reply) {
506 if (reply.getSone().equals(this)) {
512 * Removes a reply from this Sone.
515 * The reply to remove
517 public void removeReply(PostReply reply) {
518 if (reply.getSone().equals(this)) {
519 replies.remove(reply);
524 * Returns the IDs of all liked posts.
526 * @return All liked posts’ IDs
528 public Set<String> getLikedPostIds() {
529 return Collections.unmodifiableSet(likedPostIds);
533 * Sets the IDs of all liked posts.
535 * @param likedPostIds
536 * All liked posts’ IDs
537 * @return This Sone (for method chaining)
539 public Sone setLikePostIds(Set<String> likedPostIds) {
540 this.likedPostIds.clear();
541 this.likedPostIds.addAll(likedPostIds);
546 * Checks whether the given post ID is liked by this Sone.
550 * @return {@code true} if this Sone likes the given post, {@code false}
553 public boolean isLikedPostId(String postId) {
554 return likedPostIds.contains(postId);
558 * Adds the given post ID to the list of posts this Sone likes.
562 * @return This Sone (for method chaining)
564 public Sone addLikedPostId(String postId) {
565 likedPostIds.add(postId);
570 * Removes the given post ID from the list of posts this Sone likes.
574 * @return This Sone (for method chaining)
576 public Sone removeLikedPostId(String postId) {
577 likedPostIds.remove(postId);
582 * Returns the IDs of all liked replies.
584 * @return All liked replies’ IDs
586 public Set<String> getLikedReplyIds() {
587 return Collections.unmodifiableSet(likedReplyIds);
591 * Sets the IDs of all liked replies.
593 * @param likedReplyIds
594 * All liked replies’ IDs
595 * @return This Sone (for method chaining)
597 public Sone setLikeReplyIds(Set<String> likedReplyIds) {
598 this.likedReplyIds.clear();
599 this.likedReplyIds.addAll(likedReplyIds);
604 * Checks whether the given reply ID is liked by this Sone.
607 * The ID of the reply
608 * @return {@code true} if this Sone likes the given reply, {@code false}
611 public boolean isLikedReplyId(String replyId) {
612 return likedReplyIds.contains(replyId);
616 * Adds the given reply ID to the list of replies this Sone likes.
619 * The ID of the reply
620 * @return This Sone (for method chaining)
622 public Sone addLikedReplyId(String replyId) {
623 likedReplyIds.add(replyId);
628 * Removes the given post ID from the list of replies this Sone likes.
631 * The ID of the reply
632 * @return This Sone (for method chaining)
634 public Sone removeLikedReplyId(String replyId) {
635 likedReplyIds.remove(replyId);
640 * Returns the root album that contains all visible albums of this Sone.
642 * @return The root album of this Sone
644 public Album getRootAlbum() {
649 * Returns Sone-specific options.
651 * @return The options of this Sone
653 public Options getOptions() {
658 * Sets the options of this Sone.
661 * The options of this Sone
663 /* TODO - remove this method again, maybe add an option provider */
664 public void setOptions(Options options) {
665 this.options = options;
669 public AlbumBuilder newAlbumBuilder() {
670 return new DefaultAlbumBuilder(this, rootAlbum);
674 // FINGERPRINTABLE METHODS
679 public synchronized String getFingerprint() {
680 Hasher hash = Hashing.sha256().newHasher();
681 hash.putString(profile.getFingerprint());
683 hash.putString("Posts(");
684 for (Post post : getPosts()) {
685 hash.putString("Post(").putString(post.getId()).putString(")");
689 List<PostReply> replies = new ArrayList<PostReply>(getReplies());
690 Collections.sort(replies, Reply.TIME_COMPARATOR);
691 hash.putString("Replies(");
692 for (PostReply reply : replies) {
693 hash.putString("Reply(").putString(reply.getId()).putString(")");
697 List<String> likedPostIds = new ArrayList<String>(getLikedPostIds());
698 Collections.sort(likedPostIds);
699 hash.putString("LikedPosts(");
700 for (String likedPostId : likedPostIds) {
701 hash.putString("Post(").putString(likedPostId).putString(")");
705 List<String> likedReplyIds = new ArrayList<String>(getLikedReplyIds());
706 Collections.sort(likedReplyIds);
707 hash.putString("LikedReplies(");
708 for (String likedReplyId : likedReplyIds) {
709 hash.putString("Reply(").putString(likedReplyId).putString(")");
713 hash.putString("Albums(");
714 for (Album album : rootAlbum.getAlbums()) {
715 if (!Album.NOT_EMPTY.apply(album)) {
718 hash.putString(album.getFingerprint());
722 return hash.hash().toString();
726 // INTERFACE Comparable<Sone>
731 public int compareTo(Sone sone) {
732 return NICE_NAME_COMPARATOR.compare(this, sone);
741 public int hashCode() {
742 return id.hashCode();
747 public boolean equals(Object object) {
748 if (!(object instanceof Sone)) {
751 return ((Sone) object).getId().equals(id);
756 public String toString() {
757 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() + ")]";