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.freenet.wot.Identity;
40 import net.pterodactylus.util.logging.Logging;
42 import freenet.keys.FreenetURI;
44 import com.google.common.hash.Hasher;
45 import com.google.common.hash.Hashing;
48 * Dumb, store-everything-in-memory {@link Sone} implementation.
50 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
52 public class DefaultSone implements Sone {
55 private static final Logger logger = Logging.getLogger(DefaultSone.class);
57 /** The ID of this Sone. */
58 private final String id;
60 /** Whether the Sone is local. */
61 private final boolean local;
63 /** The identity of this Sone. */
64 private Identity identity;
66 /** The URI under which the Sone is stored in Freenet. */
67 private volatile FreenetURI requestUri;
69 /** The URI used to insert a new version of this Sone. */
70 /* This will be null for remote Sones! */
71 private volatile FreenetURI insertUri;
73 /** The latest edition of the Sone. */
74 private volatile long latestEdition;
76 /** The time of the last inserted update. */
77 private volatile long time;
79 /** The status of this Sone. */
80 private volatile SoneStatus status = SoneStatus.unknown;
82 /** The profile of this Sone. */
83 private volatile Profile profile = new Profile(this);
85 /** The client used by the Sone. */
86 private volatile Client client;
88 /** Whether this Sone is known. */
89 private volatile boolean known;
91 /** All friend Sones. */
92 private final Set<String> friendSones = new CopyOnWriteArraySet<String>();
95 private final Set<Post> posts = new CopyOnWriteArraySet<Post>();
98 private final Set<PostReply> replies = new CopyOnWriteArraySet<PostReply>();
100 /** The IDs of all liked posts. */
101 private final Set<String> likedPostIds = new CopyOnWriteArraySet<String>();
103 /** The IDs of all liked replies. */
104 private final Set<String> likedReplyIds = new CopyOnWriteArraySet<String>();
106 /** The root album containing all albums. */
107 private final Album rootAlbum = new DefaultAlbum(this, null);
109 /** Sone-specific options. */
110 private Options options = new Options();
113 * Creates a new Sone.
118 * {@code true} if the Sone is a local Sone, {@code false} otherwise
120 public DefaultSone(String id, boolean local) {
130 * Returns the identity of this Sone.
132 * @return The identity of this Sone
134 public String getId() {
139 * Returns the identity of this Sone.
141 * @return The identity of this Sone
143 public Identity getIdentity() {
148 * Sets the identity of this Sone. The {@link Identity#getId() ID} of the
149 * identity has to match this Sone’s {@link #getId()}.
152 * The identity of this Sone
153 * @return This Sone (for method chaining)
154 * @throws IllegalArgumentException
155 * if the ID of the identity does not match this Sone’s ID
157 public DefaultSone setIdentity(Identity identity) throws IllegalArgumentException {
158 if (!identity.getId().equals(id)) {
159 throw new IllegalArgumentException("Identity’s ID does not match Sone’s ID!");
161 this.identity = identity;
166 * Returns the name of this Sone.
168 * @return The name of this Sone
170 public String getName() {
171 return (identity != null) ? identity.getNickname() : null;
175 * Returns whether this Sone is a local Sone.
177 * @return {@code true} if this Sone is a local Sone, {@code false} otherwise
179 public boolean isLocal() {
184 * Returns the request URI of this Sone.
186 * @return The request URI of this Sone
188 public FreenetURI getRequestUri() {
189 return (requestUri != null) ? requestUri.setSuggestedEdition(latestEdition) : null;
193 * Sets the request URI of this Sone.
196 * The request URI of this Sone
197 * @return This Sone (for method chaining)
199 public Sone setRequestUri(FreenetURI requestUri) {
200 if (this.requestUri == null) {
201 this.requestUri = requestUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
204 if (!this.requestUri.equalsKeypair(requestUri)) {
205 logger.log(Level.WARNING, String.format("Request URI %s tried to overwrite %s!", requestUri, this.requestUri));
212 * Returns the insert URI of this Sone.
214 * @return The insert URI of this Sone
216 public FreenetURI getInsertUri() {
217 return (insertUri != null) ? insertUri.setSuggestedEdition(latestEdition) : null;
221 * Sets the insert URI of this Sone.
224 * The insert URI of this Sone
225 * @return This Sone (for method chaining)
227 public Sone setInsertUri(FreenetURI insertUri) {
228 if (this.insertUri == null) {
229 this.insertUri = insertUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
232 if (!this.insertUri.equalsKeypair(insertUri)) {
233 logger.log(Level.WARNING, String.format("Request URI %s tried to overwrite %s!", insertUri, this.insertUri));
240 * Returns the latest edition of this Sone.
242 * @return The latest edition of this Sone
244 public long getLatestEdition() {
245 return latestEdition;
249 * Sets the latest edition of this Sone. If the given latest edition is not
250 * greater than the current latest edition, the latest edition of this Sone is
253 * @param latestEdition
254 * The latest edition of this Sone
256 public void setLatestEdition(long latestEdition) {
257 if (!(latestEdition > this.latestEdition)) {
258 logger.log(Level.FINE, String.format("New latest edition %d is not greater than current latest edition %d!", latestEdition, this.latestEdition));
261 this.latestEdition = latestEdition;
265 * Return the time of the last inserted update of this Sone.
267 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
269 public long getTime() {
274 * Sets the time of the last inserted update of this Sone.
277 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
278 * @return This Sone (for method chaining)
280 public Sone setTime(long time) {
286 * Returns the status of this Sone.
288 * @return The status of this Sone
290 public SoneStatus getStatus() {
295 * Sets the new status of this Sone.
298 * The new status of this Sone
300 * @throws IllegalArgumentException
301 * if {@code status} is {@code null}
303 public Sone setStatus(SoneStatus status) {
304 this.status = checkNotNull(status, "status must not be null");
309 * Returns a copy of the profile. If you want to update values in the profile
310 * of this Sone, update the values in the returned {@link Profile} and use
311 * {@link #setProfile(Profile)} to change the profile in this Sone.
313 * @return A copy of the profile
315 public Profile getProfile() {
316 return new Profile(profile);
320 * Sets the profile of this Sone. A copy of the given profile is stored so that
321 * subsequent modifications of the given profile are not reflected in this
327 public void setProfile(Profile profile) {
328 this.profile = new Profile(profile);
332 * Returns the client used by this Sone.
334 * @return The client used by this Sone, or {@code null}
336 public Client getClient() {
341 * Sets the client used by this Sone.
344 * The client used by this Sone, or {@code null}
345 * @return This Sone (for method chaining)
347 public Sone setClient(Client client) {
348 this.client = client;
353 * Returns whether this Sone is known.
355 * @return {@code true} if this Sone is known, {@code false} otherwise
357 public boolean isKnown() {
362 * Sets whether this Sone is known.
365 * {@code true} if this Sone is known, {@code false} otherwise
368 public Sone setKnown(boolean known) {
374 * Returns all friend Sones of this Sone.
376 * @return The friend Sones of this Sone
378 public List<String> getFriends() {
379 List<String> friends = new ArrayList<String>(friendSones);
384 * Returns whether this Sone has the given Sone as a friend Sone.
386 * @param friendSoneId
387 * The ID of the Sone to check for
388 * @return {@code true} if this Sone has the given Sone as a friend, {@code
391 public boolean hasFriend(String friendSoneId) {
392 return friendSones.contains(friendSoneId);
396 * Adds the given Sone as a friend Sone.
399 * The friend Sone to add
400 * @return This Sone (for method chaining)
402 public Sone addFriend(String friendSone) {
403 if (!friendSone.equals(id)) {
404 friendSones.add(friendSone);
410 * Removes the given Sone as a friend Sone.
412 * @param friendSoneId
413 * The ID of the friend Sone to remove
414 * @return This Sone (for method chaining)
416 public Sone removeFriend(String friendSoneId) {
417 friendSones.remove(friendSoneId);
422 * Returns the list of posts of this Sone, sorted by time, newest first.
424 * @return All posts of this Sone
426 public List<Post> getPosts() {
427 List<Post> sortedPosts;
428 synchronized (this) {
429 sortedPosts = new ArrayList<Post>(posts);
431 Collections.sort(sortedPosts, Post.TIME_COMPARATOR);
436 * Sets all posts of this Sone at once.
439 * The new (and only) posts of this Sone
440 * @return This Sone (for method chaining)
442 public Sone setPosts(Collection<Post> posts) {
443 synchronized (this) {
445 this.posts.addAll(posts);
451 * Adds the given post to this Sone. The post will not be added if its {@link
452 * Post#getSone() Sone} is not this Sone.
457 public void addPost(Post post) {
458 if (post.getSone().equals(this) && posts.add(post)) {
459 logger.log(Level.FINEST, String.format("Adding %s to “%s”.", post, getName()));
464 * Removes the given post from this Sone.
469 public void removePost(Post post) {
470 if (post.getSone().equals(this)) {
476 * Returns all replies this Sone made.
478 * @return All replies this Sone made
480 public Set<PostReply> getReplies() {
481 return Collections.unmodifiableSet(replies);
485 * Sets all replies of this Sone at once.
488 * The new (and only) replies of this Sone
489 * @return This Sone (for method chaining)
491 public Sone setReplies(Collection<PostReply> replies) {
492 this.replies.clear();
493 this.replies.addAll(replies);
498 * Adds a reply to this Sone. If the given reply was not made by this Sone,
499 * nothing is added to this Sone.
504 public void addReply(PostReply reply) {
505 if (reply.getSone().equals(this)) {
511 * Removes a reply from this Sone.
514 * The reply to remove
516 public void removeReply(PostReply reply) {
517 if (reply.getSone().equals(this)) {
518 replies.remove(reply);
523 * Returns the IDs of all liked posts.
525 * @return All liked posts’ IDs
527 public Set<String> getLikedPostIds() {
528 return Collections.unmodifiableSet(likedPostIds);
532 * Sets the IDs of all liked posts.
534 * @param likedPostIds
535 * All liked posts’ IDs
536 * @return This Sone (for method chaining)
538 public Sone setLikePostIds(Set<String> likedPostIds) {
539 this.likedPostIds.clear();
540 this.likedPostIds.addAll(likedPostIds);
545 * Checks whether the given post ID is liked by this Sone.
549 * @return {@code true} if this Sone likes the given post, {@code false}
552 public boolean isLikedPostId(String postId) {
553 return likedPostIds.contains(postId);
557 * Adds the given post ID to the list of posts this Sone likes.
561 * @return This Sone (for method chaining)
563 public Sone addLikedPostId(String postId) {
564 likedPostIds.add(postId);
569 * Removes the given post ID from the list of posts this Sone likes.
573 * @return This Sone (for method chaining)
575 public Sone removeLikedPostId(String postId) {
576 likedPostIds.remove(postId);
581 * Returns the IDs of all liked replies.
583 * @return All liked replies’ IDs
585 public Set<String> getLikedReplyIds() {
586 return Collections.unmodifiableSet(likedReplyIds);
590 * Sets the IDs of all liked replies.
592 * @param likedReplyIds
593 * All liked replies’ IDs
594 * @return This Sone (for method chaining)
596 public Sone setLikeReplyIds(Set<String> likedReplyIds) {
597 this.likedReplyIds.clear();
598 this.likedReplyIds.addAll(likedReplyIds);
603 * Checks whether the given reply ID is liked by this Sone.
606 * The ID of the reply
607 * @return {@code true} if this Sone likes the given reply, {@code false}
610 public boolean isLikedReplyId(String replyId) {
611 return likedReplyIds.contains(replyId);
615 * Adds the given reply ID to the list of replies this Sone likes.
618 * The ID of the reply
619 * @return This Sone (for method chaining)
621 public Sone addLikedReplyId(String replyId) {
622 likedReplyIds.add(replyId);
627 * Removes the given post ID from the list of replies this Sone likes.
630 * The ID of the reply
631 * @return This Sone (for method chaining)
633 public Sone removeLikedReplyId(String replyId) {
634 likedReplyIds.remove(replyId);
639 * Returns the root album that contains all visible albums of this Sone.
641 * @return The root album of this Sone
643 public Album getRootAlbum() {
648 * Returns Sone-specific options.
650 * @return The options of this Sone
652 public Options getOptions() {
657 * Sets the options of this Sone.
660 * The options of this Sone
662 /* TODO - remove this method again, maybe add an option provider */
663 public void setOptions(Options options) {
664 this.options = options;
668 // FINGERPRINTABLE METHODS
673 public synchronized String getFingerprint() {
674 Hasher hash = Hashing.sha256().newHasher();
675 hash.putString(profile.getFingerprint());
677 hash.putString("Posts(");
678 for (Post post : getPosts()) {
679 hash.putString("Post(").putString(post.getId()).putString(")");
683 List<PostReply> replies = new ArrayList<PostReply>(getReplies());
684 Collections.sort(replies, Reply.TIME_COMPARATOR);
685 hash.putString("Replies(");
686 for (PostReply reply : replies) {
687 hash.putString("Reply(").putString(reply.getId()).putString(")");
691 List<String> likedPostIds = new ArrayList<String>(getLikedPostIds());
692 Collections.sort(likedPostIds);
693 hash.putString("LikedPosts(");
694 for (String likedPostId : likedPostIds) {
695 hash.putString("Post(").putString(likedPostId).putString(")");
699 List<String> likedReplyIds = new ArrayList<String>(getLikedReplyIds());
700 Collections.sort(likedReplyIds);
701 hash.putString("LikedReplies(");
702 for (String likedReplyId : likedReplyIds) {
703 hash.putString("Reply(").putString(likedReplyId).putString(")");
707 hash.putString("Albums(");
708 for (Album album : rootAlbum.getAlbums()) {
709 if (!Album.NOT_EMPTY.apply(album)) {
712 hash.putString(album.getFingerprint());
716 return hash.hash().toString();
720 // INTERFACE Comparable<Sone>
725 public int compareTo(Sone sone) {
726 return NICE_NAME_COMPARATOR.compare(this, sone);
735 public int hashCode() {
736 return id.hashCode();
741 public boolean equals(Object object) {
742 if (!(object instanceof Sone)) {
745 return ((Sone) object).getId().equals(id);
750 public String toString() {
751 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() + ")]";