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;
21 import static java.lang.String.format;
23 import java.net.MalformedURLException;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.List;
29 import java.util.concurrent.CopyOnWriteArraySet;
30 import java.util.logging.Level;
31 import java.util.logging.Logger;
33 import net.pterodactylus.sone.data.SoneOptions.DefaultSoneOptions;
34 import net.pterodactylus.sone.freenet.wot.Identity;
35 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
36 import net.pterodactylus.util.logging.Logging;
38 import freenet.keys.FreenetURI;
40 import com.google.common.hash.Hasher;
41 import com.google.common.hash.Hashing;
44 * {@link Sone} implementation.
46 * Operations that modify the Sone need to synchronize on the Sone in question.
48 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
50 public class SoneImpl implements Sone {
53 private static final Logger logger = Logging.getLogger(SoneImpl.class);
55 /** The ID of this Sone. */
56 private final String id;
58 /** Whether the Sone is local. */
59 private final boolean local;
61 /** The identity of this Sone. */
62 private Identity identity;
64 /** The URI under which the Sone is stored in Freenet. */
65 private volatile FreenetURI requestUri;
67 /** The URI used to insert a new version of this Sone. */
68 /* This will be null for remote Sones! */
69 private volatile FreenetURI insertUri;
71 /** The latest edition of the Sone. */
72 private volatile long latestEdition;
74 /** The time of the last inserted update. */
75 private volatile long time;
77 /** The status of this Sone. */
78 private volatile SoneStatus status = SoneStatus.unknown;
80 /** The profile of this Sone. */
81 private volatile Profile profile = new Profile(this);
83 /** The client used by the Sone. */
84 private volatile Client client;
86 /** Whether this Sone is known. */
87 private volatile boolean known;
89 /** All friend Sones. */
90 private final Set<String> friendSones = new CopyOnWriteArraySet<String>();
93 private final Set<Post> posts = new CopyOnWriteArraySet<Post>();
96 private final Set<PostReply> replies = new CopyOnWriteArraySet<PostReply>();
98 /** The IDs of all liked posts. */
99 private final Set<String> likedPostIds = new CopyOnWriteArraySet<String>();
101 /** The IDs of all liked replies. */
102 private final Set<String> likedReplyIds = new CopyOnWriteArraySet<String>();
104 /** The root album containing all albums. */
105 private final Album rootAlbum = new AlbumImpl(this);
107 /** Sone-specific options. */
108 private SoneOptions options = new DefaultSoneOptions();
111 * Creates a new Sone.
116 * {@code true} if the Sone is a local Sone, {@code false} otherwise
118 public SoneImpl(String id, boolean local) {
128 * Returns the identity of this Sone.
130 * @return The identity of this Sone
132 public String getId() {
137 * Returns the identity of this Sone.
139 * @return The identity of this Sone
141 public Identity getIdentity() {
146 * Returns the name of this Sone.
148 * @return The name of this Sone
150 public String getName() {
151 return (identity != null) ? identity.getNickname() : null;
155 * Returns whether this Sone is a local Sone.
157 * @return {@code true} if this Sone is a local Sone, {@code false} otherwise
159 public boolean isLocal() {
164 * Returns the request URI of this Sone.
166 * @return The request URI of this Sone
168 public FreenetURI getRequestUri() {
169 return (requestUri != null) ? requestUri.setSuggestedEdition(latestEdition) : null;
173 * Sets the request URI of this Sone.
176 * The request URI of this Sone
177 * @return This Sone (for method chaining)
179 public Sone setRequestUri(FreenetURI requestUri) {
180 if (this.requestUri == null) {
181 this.requestUri = requestUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
184 if (!this.requestUri.equalsKeypair(requestUri)) {
185 logger.log(Level.WARNING, String.format("Request URI %s tried to overwrite %s!", requestUri, this.requestUri));
192 * Returns the insert URI of this Sone.
194 * @return The insert URI of this Sone
196 public FreenetURI getInsertUri() {
201 return new FreenetURI(((OwnIdentity) getIdentity()).getInsertUri()).setDocName("Sone").setMetaString(new String[0]);
202 } catch (MalformedURLException e) {
203 throw new IllegalStateException(format("Own identity %s's insert URI is incorrect.", getIdentity()), e);
208 * Returns the latest edition of this Sone.
210 * @return The latest edition of this Sone
212 public long getLatestEdition() {
213 return latestEdition;
217 * Sets the latest edition of this Sone. If the given latest edition is not
218 * greater than the current latest edition, the latest edition of this Sone is
221 * @param latestEdition
222 * The latest edition of this Sone
224 public void setLatestEdition(long latestEdition) {
225 if (!(latestEdition > this.latestEdition)) {
226 logger.log(Level.FINE, String.format("New latest edition %d is not greater than current latest edition %d!", latestEdition, this.latestEdition));
229 this.latestEdition = latestEdition;
233 * Return the time of the last inserted update of this Sone.
235 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
237 public long getTime() {
242 * Sets the time of the last inserted update of this Sone.
245 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
246 * @return This Sone (for method chaining)
248 public Sone setTime(long time) {
254 * Returns the status of this Sone.
256 * @return The status of this Sone
258 public SoneStatus getStatus() {
263 * Sets the new status of this Sone.
266 * The new status of this Sone
268 * @throws IllegalArgumentException
269 * if {@code status} is {@code null}
271 public Sone setStatus(SoneStatus status) {
272 this.status = checkNotNull(status, "status must not be null");
277 * Returns a copy of the profile. If you want to update values in the profile
278 * of this Sone, update the values in the returned {@link Profile} and use
279 * {@link #setProfile(Profile)} to change the profile in this Sone.
281 * @return A copy of the profile
283 public Profile getProfile() {
284 return new Profile(profile);
288 * Sets the profile of this Sone. A copy of the given profile is stored so that
289 * subsequent modifications of the given profile are not reflected in this
295 public void setProfile(Profile profile) {
296 this.profile = new Profile(profile);
300 * Returns the client used by this Sone.
302 * @return The client used by this Sone, or {@code null}
304 public Client getClient() {
309 * Sets the client used by this Sone.
312 * The client used by this Sone, or {@code null}
313 * @return This Sone (for method chaining)
315 public Sone setClient(Client client) {
316 this.client = client;
321 * Returns whether this Sone is known.
323 * @return {@code true} if this Sone is known, {@code false} otherwise
325 public boolean isKnown() {
330 * Sets whether this Sone is known.
333 * {@code true} if this Sone is known, {@code false} otherwise
336 public Sone setKnown(boolean known) {
342 * Returns all friend Sones of this Sone.
344 * @return The friend Sones of this Sone
346 public List<String> getFriends() {
347 return new ArrayList<String>(friendSones);
351 * Returns whether this Sone has the given Sone as a friend Sone.
353 * @param friendSoneId
354 * The ID of the Sone to check for
355 * @return {@code true} if this Sone has the given Sone as a friend, {@code
358 public boolean hasFriend(String friendSoneId) {
359 return friendSones.contains(friendSoneId);
363 * Adds the given Sone as a friend Sone.
366 * The friend Sone to add
367 * @return This Sone (for method chaining)
369 public Sone addFriend(String friendSone) {
370 if (!friendSone.equals(id)) {
371 friendSones.add(friendSone);
377 * Removes the given Sone as a friend Sone.
379 * @param friendSoneId
380 * The ID of the friend Sone to remove
381 * @return This Sone (for method chaining)
383 public Sone removeFriend(String friendSoneId) {
384 friendSones.remove(friendSoneId);
389 * Returns the list of posts of this Sone, sorted by time, newest first.
391 * @return All posts of this Sone
393 public List<Post> getPosts() {
394 List<Post> sortedPosts;
395 synchronized (this) {
396 sortedPosts = new ArrayList<Post>(posts);
398 Collections.sort(sortedPosts, Post.TIME_COMPARATOR);
403 * Sets all posts of this Sone at once.
406 * The new (and only) posts of this Sone
407 * @return This Sone (for method chaining)
409 public Sone setPosts(Collection<Post> posts) {
410 synchronized (this) {
412 this.posts.addAll(posts);
418 * Adds the given post to this Sone. The post will not be added if its {@link
419 * Post#getSone() Sone} is not this Sone.
424 public void addPost(Post post) {
425 if (post.getSone().equals(this) && posts.add(post)) {
426 logger.log(Level.FINEST, String.format("Adding %s to “%s”.", post, getName()));
431 * Removes the given post from this Sone.
436 public void removePost(Post post) {
437 if (post.getSone().equals(this)) {
443 * Returns all replies this Sone made.
445 * @return All replies this Sone made
447 public Set<PostReply> getReplies() {
448 return Collections.unmodifiableSet(replies);
452 * Sets all replies of this Sone at once.
455 * The new (and only) replies of this Sone
456 * @return This Sone (for method chaining)
458 public Sone setReplies(Collection<PostReply> replies) {
459 this.replies.clear();
460 this.replies.addAll(replies);
465 * Adds a reply to this Sone. If the given reply was not made by this Sone,
466 * nothing is added to this Sone.
471 public void addReply(PostReply reply) {
472 if (reply.getSone().equals(this)) {
478 * Removes a reply from this Sone.
481 * The reply to remove
483 public void removeReply(PostReply reply) {
484 if (reply.getSone().equals(this)) {
485 replies.remove(reply);
490 * Returns the IDs of all liked posts.
492 * @return All liked posts’ IDs
494 public Set<String> getLikedPostIds() {
495 return Collections.unmodifiableSet(likedPostIds);
499 * Sets the IDs of all liked posts.
501 * @param likedPostIds
502 * All liked posts’ IDs
503 * @return This Sone (for method chaining)
505 public Sone setLikePostIds(Set<String> likedPostIds) {
506 this.likedPostIds.clear();
507 this.likedPostIds.addAll(likedPostIds);
512 * Checks whether the given post ID is liked by this Sone.
516 * @return {@code true} if this Sone likes the given post, {@code false}
519 public boolean isLikedPostId(String postId) {
520 return likedPostIds.contains(postId);
524 * Adds the given post ID to the list of posts this Sone likes.
528 * @return This Sone (for method chaining)
530 public Sone addLikedPostId(String postId) {
531 likedPostIds.add(postId);
536 * Removes the given post ID from the list of posts this Sone likes.
540 * @return This Sone (for method chaining)
542 public Sone removeLikedPostId(String postId) {
543 likedPostIds.remove(postId);
548 * Returns the IDs of all liked replies.
550 * @return All liked replies’ IDs
552 public Set<String> getLikedReplyIds() {
553 return Collections.unmodifiableSet(likedReplyIds);
557 * Sets the IDs of all liked replies.
559 * @param likedReplyIds
560 * All liked replies’ IDs
561 * @return This Sone (for method chaining)
563 public Sone setLikeReplyIds(Set<String> likedReplyIds) {
564 this.likedReplyIds.clear();
565 this.likedReplyIds.addAll(likedReplyIds);
570 * Checks whether the given reply ID is liked by this Sone.
573 * The ID of the reply
574 * @return {@code true} if this Sone likes the given reply, {@code false}
577 public boolean isLikedReplyId(String replyId) {
578 return likedReplyIds.contains(replyId);
582 * Adds the given reply ID to the list of replies this Sone likes.
585 * The ID of the reply
586 * @return This Sone (for method chaining)
588 public Sone addLikedReplyId(String replyId) {
589 likedReplyIds.add(replyId);
594 * Removes the given post ID from the list of replies this Sone likes.
597 * The ID of the reply
598 * @return This Sone (for method chaining)
600 public Sone removeLikedReplyId(String replyId) {
601 likedReplyIds.remove(replyId);
606 * Returns the root album that contains all visible albums of this Sone.
608 * @return The root album of this Sone
610 public Album getRootAlbum() {
615 * Returns Sone-specific options.
617 * @return The options of this Sone
619 public SoneOptions getOptions() {
624 * Sets the options of this Sone.
627 * The options of this Sone
629 /* TODO - remove this method again, maybe add an option provider */
630 public void setOptions(SoneOptions options) {
631 this.options = options;
635 // FINGERPRINTABLE METHODS
640 public synchronized String getFingerprint() {
641 Hasher hash = Hashing.sha256().newHasher();
642 hash.putString(profile.getFingerprint());
644 hash.putString("Posts(");
645 for (Post post : getPosts()) {
646 hash.putString("Post(").putString(post.getId()).putString(")");
650 List<PostReply> replies = new ArrayList<PostReply>(getReplies());
651 Collections.sort(replies, Reply.TIME_COMPARATOR);
652 hash.putString("Replies(");
653 for (PostReply reply : replies) {
654 hash.putString("Reply(").putString(reply.getId()).putString(")");
658 List<String> likedPostIds = new ArrayList<String>(getLikedPostIds());
659 Collections.sort(likedPostIds);
660 hash.putString("LikedPosts(");
661 for (String likedPostId : likedPostIds) {
662 hash.putString("Post(").putString(likedPostId).putString(")");
666 List<String> likedReplyIds = new ArrayList<String>(getLikedReplyIds());
667 Collections.sort(likedReplyIds);
668 hash.putString("LikedReplies(");
669 for (String likedReplyId : likedReplyIds) {
670 hash.putString("Reply(").putString(likedReplyId).putString(")");
674 hash.putString("Albums(");
675 for (Album album : rootAlbum.getAlbums()) {
676 if (!Album.NOT_EMPTY.apply(album)) {
679 hash.putString(album.getFingerprint());
683 return hash.hash().toString();
687 // INTERFACE Comparable<Sone>
692 public int compareTo(Sone sone) {
693 return NICE_NAME_COMPARATOR.compare(this, sone);
702 public int hashCode() {
703 return id.hashCode();
708 public boolean equals(Object object) {
709 if (!(object instanceof Sone)) {
712 return ((Sone) object).getId().equals(id);
717 public String toString() {
718 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() + ")]";