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.core.Options;
32 import net.pterodactylus.sone.data.impl.DefaultAlbum;
33 import net.pterodactylus.sone.freenet.wot.Identity;
34 import net.pterodactylus.util.logging.Logging;
36 import freenet.keys.FreenetURI;
38 import com.google.common.hash.Hasher;
39 import com.google.common.hash.Hashing;
42 * {@link Sone} implementation.
44 * Operations that modify the Sone need to synchronize on the Sone in question.
46 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
48 public class SoneImpl implements Sone {
51 private static final Logger logger = Logging.getLogger(SoneImpl.class);
53 /** The ID of this Sone. */
54 private final String id;
56 /** Whether the Sone is local. */
57 private final boolean local;
59 /** The identity of this Sone. */
60 private Identity identity;
62 /** The URI under which the Sone is stored in Freenet. */
63 private volatile FreenetURI requestUri;
65 /** The URI used to insert a new version of this Sone. */
66 /* This will be null for remote Sones! */
67 private volatile FreenetURI insertUri;
69 /** The latest edition of the Sone. */
70 private volatile long latestEdition;
72 /** The time of the last inserted update. */
73 private volatile long time;
75 /** The status of this Sone. */
76 private volatile SoneStatus status = SoneStatus.unknown;
78 /** The profile of this Sone. */
79 private volatile Profile profile = new Profile(this);
81 /** The client used by the Sone. */
82 private volatile Client client;
84 /** Whether this Sone is known. */
85 private volatile boolean known;
87 /** All friend Sones. */
88 private final Set<String> friendSones = new CopyOnWriteArraySet<String>();
91 private final Set<Post> posts = new CopyOnWriteArraySet<Post>();
94 private final Set<PostReply> replies = new CopyOnWriteArraySet<PostReply>();
96 /** The IDs of all liked posts. */
97 private final Set<String> likedPostIds = new CopyOnWriteArraySet<String>();
99 /** The IDs of all liked replies. */
100 private final Set<String> likedReplyIds = new CopyOnWriteArraySet<String>();
102 /** The root album containing all albums. */
103 private final Album rootAlbum = new DefaultAlbum(this, null);
105 /** Sone-specific options. */
106 private Options options = new Options();
109 * Creates a new Sone.
114 * {@code true} if the Sone is a local Sone, {@code false} otherwise
116 public SoneImpl(String id, boolean local) {
126 * Returns the identity of this Sone.
128 * @return The identity of this Sone
130 public String getId() {
135 * Returns the identity of this Sone.
137 * @return The identity of this Sone
139 public Identity getIdentity() {
144 * Sets the identity of this Sone. The {@link Identity#getId() ID} of the
145 * identity has to match this Sone’s {@link #getId()}.
148 * The identity of this Sone
149 * @return This Sone (for method chaining)
150 * @throws IllegalArgumentException
151 * if the ID of the identity does not match this Sone’s ID
153 public SoneImpl setIdentity(Identity identity) throws IllegalArgumentException {
154 if (!identity.getId().equals(id)) {
155 throw new IllegalArgumentException("Identity’s ID does not match Sone’s ID!");
157 this.identity = identity;
162 * Returns the name of this Sone.
164 * @return The name of this Sone
166 public String getName() {
167 return (identity != null) ? identity.getNickname() : null;
171 * Returns whether this Sone is a local Sone.
173 * @return {@code true} if this Sone is a local Sone, {@code false} otherwise
175 public boolean isLocal() {
180 * Returns the request URI of this Sone.
182 * @return The request URI of this Sone
184 public FreenetURI getRequestUri() {
185 return (requestUri != null) ? requestUri.setSuggestedEdition(latestEdition) : null;
189 * Sets the request URI of this Sone.
192 * The request URI of this Sone
193 * @return This Sone (for method chaining)
195 public Sone setRequestUri(FreenetURI requestUri) {
196 if (this.requestUri == null) {
197 this.requestUri = requestUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
200 if (!this.requestUri.equalsKeypair(requestUri)) {
201 logger.log(Level.WARNING, String.format("Request URI %s tried to overwrite %s!", requestUri, this.requestUri));
208 * Returns the insert URI of this Sone.
210 * @return The insert URI of this Sone
212 public FreenetURI getInsertUri() {
213 return (insertUri != null) ? insertUri.setSuggestedEdition(latestEdition) : null;
217 * Sets the insert URI of this Sone.
220 * The insert URI of this Sone
221 * @return This Sone (for method chaining)
223 public Sone setInsertUri(FreenetURI insertUri) {
224 if (this.insertUri == null) {
225 this.insertUri = insertUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
228 if (!this.insertUri.equalsKeypair(insertUri)) {
229 logger.log(Level.WARNING, String.format("Request URI %s tried to overwrite %s!", insertUri, this.insertUri));
236 * Returns the latest edition of this Sone.
238 * @return The latest edition of this Sone
240 public long getLatestEdition() {
241 return latestEdition;
245 * Sets the latest edition of this Sone. If the given latest edition is not
246 * greater than the current latest edition, the latest edition of this Sone is
249 * @param latestEdition
250 * The latest edition of this Sone
252 public void setLatestEdition(long latestEdition) {
253 if (!(latestEdition > this.latestEdition)) {
254 logger.log(Level.FINE, String.format("New latest edition %d is not greater than current latest edition %d!", latestEdition, this.latestEdition));
257 this.latestEdition = latestEdition;
261 * Return the time of the last inserted update of this Sone.
263 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
265 public long getTime() {
270 * Sets the time of the last inserted update of this Sone.
273 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
274 * @return This Sone (for method chaining)
276 public Sone setTime(long time) {
282 * Returns the status of this Sone.
284 * @return The status of this Sone
286 public SoneStatus getStatus() {
291 * Sets the new status of this Sone.
294 * The new status of this Sone
296 * @throws IllegalArgumentException
297 * if {@code status} is {@code null}
299 public Sone setStatus(SoneStatus status) {
300 this.status = checkNotNull(status, "status must not be null");
305 * Returns a copy of the profile. If you want to update values in the profile
306 * of this Sone, update the values in the returned {@link Profile} and use
307 * {@link #setProfile(Profile)} to change the profile in this Sone.
309 * @return A copy of the profile
311 public Profile getProfile() {
312 return new Profile(profile);
316 * Sets the profile of this Sone. A copy of the given profile is stored so that
317 * subsequent modifications of the given profile are not reflected in this
323 public void setProfile(Profile profile) {
324 this.profile = new Profile(profile);
328 * Returns the client used by this Sone.
330 * @return The client used by this Sone, or {@code null}
332 public Client getClient() {
337 * Sets the client used by this Sone.
340 * The client used by this Sone, or {@code null}
341 * @return This Sone (for method chaining)
343 public Sone setClient(Client client) {
344 this.client = client;
349 * Returns whether this Sone is known.
351 * @return {@code true} if this Sone is known, {@code false} otherwise
353 public boolean isKnown() {
358 * Sets whether this Sone is known.
361 * {@code true} if this Sone is known, {@code false} otherwise
364 public Sone setKnown(boolean known) {
370 * Returns all friend Sones of this Sone.
372 * @return The friend Sones of this Sone
374 public List<String> getFriends() {
375 List<String> friends = new ArrayList<String>(friendSones);
380 * Returns whether this Sone has the given Sone as a friend Sone.
382 * @param friendSoneId
383 * The ID of the Sone to check for
384 * @return {@code true} if this Sone has the given Sone as a friend, {@code
387 public boolean hasFriend(String friendSoneId) {
388 return friendSones.contains(friendSoneId);
392 * Adds the given Sone as a friend Sone.
395 * The friend Sone to add
396 * @return This Sone (for method chaining)
398 public Sone addFriend(String friendSone) {
399 if (!friendSone.equals(id)) {
400 friendSones.add(friendSone);
406 * Removes the given Sone as a friend Sone.
408 * @param friendSoneId
409 * The ID of the friend Sone to remove
410 * @return This Sone (for method chaining)
412 public Sone removeFriend(String friendSoneId) {
413 friendSones.remove(friendSoneId);
418 * Returns the list of posts of this Sone, sorted by time, newest first.
420 * @return All posts of this Sone
422 public List<Post> getPosts() {
423 List<Post> sortedPosts;
424 synchronized (this) {
425 sortedPosts = new ArrayList<Post>(posts);
427 Collections.sort(sortedPosts, Post.TIME_COMPARATOR);
432 * Sets all posts of this Sone at once.
435 * The new (and only) posts of this Sone
436 * @return This Sone (for method chaining)
438 public Sone setPosts(Collection<Post> posts) {
439 synchronized (this) {
441 this.posts.addAll(posts);
447 * Adds the given post to this Sone. The post will not be added if its {@link
448 * Post#getSone() Sone} is not this Sone.
453 public void addPost(Post post) {
454 if (post.getSone().equals(this) && posts.add(post)) {
455 logger.log(Level.FINEST, String.format("Adding %s to “%s”.", post, getName()));
460 * Removes the given post from this Sone.
465 public void removePost(Post post) {
466 if (post.getSone().equals(this)) {
472 * Returns all replies this Sone made.
474 * @return All replies this Sone made
476 public Set<PostReply> getReplies() {
477 return Collections.unmodifiableSet(replies);
481 * Sets all replies of this Sone at once.
484 * The new (and only) replies of this Sone
485 * @return This Sone (for method chaining)
487 public Sone setReplies(Collection<PostReply> replies) {
488 this.replies.clear();
489 this.replies.addAll(replies);
494 * Adds a reply to this Sone. If the given reply was not made by this Sone,
495 * nothing is added to this Sone.
500 public void addReply(PostReply reply) {
501 if (reply.getSone().equals(this)) {
507 * Removes a reply from this Sone.
510 * The reply to remove
512 public void removeReply(PostReply reply) {
513 if (reply.getSone().equals(this)) {
514 replies.remove(reply);
519 * Returns the IDs of all liked posts.
521 * @return All liked posts’ IDs
523 public Set<String> getLikedPostIds() {
524 return Collections.unmodifiableSet(likedPostIds);
528 * Sets the IDs of all liked posts.
530 * @param likedPostIds
531 * All liked posts’ IDs
532 * @return This Sone (for method chaining)
534 public Sone setLikePostIds(Set<String> likedPostIds) {
535 this.likedPostIds.clear();
536 this.likedPostIds.addAll(likedPostIds);
541 * Checks whether the given post ID is liked by this Sone.
545 * @return {@code true} if this Sone likes the given post, {@code false}
548 public boolean isLikedPostId(String postId) {
549 return likedPostIds.contains(postId);
553 * Adds the given post ID to the list of posts this Sone likes.
557 * @return This Sone (for method chaining)
559 public Sone addLikedPostId(String postId) {
560 likedPostIds.add(postId);
565 * Removes the given post ID from the list of posts this Sone likes.
569 * @return This Sone (for method chaining)
571 public Sone removeLikedPostId(String postId) {
572 likedPostIds.remove(postId);
577 * Returns the IDs of all liked replies.
579 * @return All liked replies’ IDs
581 public Set<String> getLikedReplyIds() {
582 return Collections.unmodifiableSet(likedReplyIds);
586 * Sets the IDs of all liked replies.
588 * @param likedReplyIds
589 * All liked replies’ IDs
590 * @return This Sone (for method chaining)
592 public Sone setLikeReplyIds(Set<String> likedReplyIds) {
593 this.likedReplyIds.clear();
594 this.likedReplyIds.addAll(likedReplyIds);
599 * Checks whether the given reply ID is liked by this Sone.
602 * The ID of the reply
603 * @return {@code true} if this Sone likes the given reply, {@code false}
606 public boolean isLikedReplyId(String replyId) {
607 return likedReplyIds.contains(replyId);
611 * Adds the given reply ID to the list of replies this Sone likes.
614 * The ID of the reply
615 * @return This Sone (for method chaining)
617 public Sone addLikedReplyId(String replyId) {
618 likedReplyIds.add(replyId);
623 * Removes the given post ID from the list of replies this Sone likes.
626 * The ID of the reply
627 * @return This Sone (for method chaining)
629 public Sone removeLikedReplyId(String replyId) {
630 likedReplyIds.remove(replyId);
635 * Returns the root album that contains all visible albums of this Sone.
637 * @return The root album of this Sone
639 public Album getRootAlbum() {
644 * Returns Sone-specific options.
646 * @return The options of this Sone
648 public Options getOptions() {
653 * Sets the options of this Sone.
656 * The options of this Sone
658 /* TODO - remove this method again, maybe add an option provider */
659 public void setOptions(Options options) {
660 this.options = options;
664 // FINGERPRINTABLE METHODS
669 public synchronized String getFingerprint() {
670 Hasher hash = Hashing.sha256().newHasher();
671 hash.putString(profile.getFingerprint());
673 hash.putString("Posts(");
674 for (Post post : getPosts()) {
675 hash.putString("Post(").putString(post.getId()).putString(")");
679 List<PostReply> replies = new ArrayList<PostReply>(getReplies());
680 Collections.sort(replies, Reply.TIME_COMPARATOR);
681 hash.putString("Replies(");
682 for (PostReply reply : replies) {
683 hash.putString("Reply(").putString(reply.getId()).putString(")");
687 List<String> likedPostIds = new ArrayList<String>(getLikedPostIds());
688 Collections.sort(likedPostIds);
689 hash.putString("LikedPosts(");
690 for (String likedPostId : likedPostIds) {
691 hash.putString("Post(").putString(likedPostId).putString(")");
695 List<String> likedReplyIds = new ArrayList<String>(getLikedReplyIds());
696 Collections.sort(likedReplyIds);
697 hash.putString("LikedReplies(");
698 for (String likedReplyId : likedReplyIds) {
699 hash.putString("Reply(").putString(likedReplyId).putString(")");
703 hash.putString("Albums(");
704 for (Album album : rootAlbum.getAlbums()) {
705 if (!Album.NOT_EMPTY.apply(album)) {
708 hash.putString(album.getFingerprint());
712 return hash.hash().toString();
716 // INTERFACE Comparable<Sone>
721 public int compareTo(Sone sone) {
722 return NICE_NAME_COMPARATOR.compare(this, sone);
731 public int hashCode() {
732 return id.hashCode();
737 public boolean equals(Object object) {
738 if (!(object instanceof Sone)) {
741 return ((Sone) object).getId().equals(id);
746 public String toString() {
747 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() + ")]";