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;
21 import static java.lang.String.format;
22 import static java.util.logging.Logger.getLogger;
24 import java.net.MalformedURLException;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.List;
30 import java.util.concurrent.CopyOnWriteArraySet;
31 import java.util.logging.Level;
32 import java.util.logging.Logger;
34 import net.pterodactylus.sone.data.Album;
35 import net.pterodactylus.sone.data.AlbumImpl;
36 import net.pterodactylus.sone.data.Client;
37 import net.pterodactylus.sone.data.Post;
38 import net.pterodactylus.sone.data.PostReply;
39 import net.pterodactylus.sone.data.Profile;
40 import net.pterodactylus.sone.data.Reply;
41 import net.pterodactylus.sone.data.Sone;
42 import net.pterodactylus.sone.data.SoneOptions;
43 import net.pterodactylus.sone.data.SoneOptions.DefaultSoneOptions;
44 import net.pterodactylus.sone.freenet.wot.Identity;
45 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
47 import freenet.keys.FreenetURI;
49 import com.google.common.hash.Hasher;
50 import com.google.common.hash.Hashing;
53 * {@link Sone} implementation.
55 * Operations that modify the Sone need to synchronize on the Sone in question.
57 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
59 public class SoneImpl implements Sone {
62 private static final Logger logger = getLogger("Sone.Data");
64 /** The ID of this Sone. */
65 private final String id;
67 /** Whether the Sone is local. */
68 private final boolean local;
70 /** The identity of this Sone. */
71 private final Identity identity;
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 AlbumImpl(this);
109 /** Sone-specific options. */
110 private SoneOptions options = new DefaultSoneOptions();
113 * Creates a new Sone.
116 * The identity of the Sone
118 * {@code true} if the Sone is a local Sone, {@code false} otherwise
120 public SoneImpl(Identity identity, boolean local) {
121 this.id = identity.getId();
122 this.identity = identity;
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 * Returns the name of this Sone.
151 * @return The name of this Sone
153 public String getName() {
154 return (identity != null) ? identity.getNickname() : null;
158 * Returns whether this Sone is a local Sone.
160 * @return {@code true} if this Sone is a local Sone, {@code false} otherwise
162 public boolean isLocal() {
167 * Returns the request URI of this Sone.
169 * @return The request URI of this Sone
171 public FreenetURI getRequestUri() {
173 return new FreenetURI(getIdentity().getRequestUri())
176 .setMetaString(new String[0])
177 .setSuggestedEdition(latestEdition);
178 } catch (MalformedURLException e) {
179 throw new IllegalStateException(
180 format("Identity %s's request URI is incorrect.",
186 * Returns the insert URI of this Sone.
188 * @return The insert URI of this Sone
190 public FreenetURI getInsertUri() {
195 return new FreenetURI(((OwnIdentity) getIdentity()).getInsertUri())
197 .setMetaString(new String[0])
198 .setSuggestedEdition(latestEdition);
199 } catch (MalformedURLException e) {
200 throw new IllegalStateException(format("Own identity %s's insert URI is incorrect.", getIdentity()), e);
205 * Returns the latest edition of this Sone.
207 * @return The latest edition of this Sone
209 public long getLatestEdition() {
210 return latestEdition;
214 * Sets the latest edition of this Sone. If the given latest edition is not
215 * greater than the current latest edition, the latest edition of this Sone is
218 * @param latestEdition
219 * The latest edition of this Sone
221 public void setLatestEdition(long latestEdition) {
222 if (!(latestEdition > this.latestEdition)) {
223 logger.log(Level.FINE, String.format("New latest edition %d is not greater than current latest edition %d!", latestEdition, this.latestEdition));
226 this.latestEdition = latestEdition;
230 * Return the time of the last inserted update of this Sone.
232 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
234 public long getTime() {
239 * Sets the time of the last inserted update of this Sone.
242 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
243 * @return This Sone (for method chaining)
245 public Sone setTime(long time) {
251 * Returns the status of this Sone.
253 * @return The status of this Sone
255 public SoneStatus getStatus() {
260 * Sets the new status of this Sone.
263 * The new status of this Sone
265 * @throws IllegalArgumentException
266 * if {@code status} is {@code null}
268 public Sone setStatus(SoneStatus status) {
269 this.status = checkNotNull(status, "status must not be null");
274 * Returns a copy of the profile. If you want to update values in the profile
275 * of this Sone, update the values in the returned {@link Profile} and use
276 * {@link #setProfile(Profile)} to change the profile in this Sone.
278 * @return A copy of the profile
280 public Profile getProfile() {
281 return new Profile(profile);
285 * Sets the profile of this Sone. A copy of the given profile is stored so that
286 * subsequent modifications of the given profile are not reflected in this
292 public void setProfile(Profile profile) {
293 this.profile = new Profile(profile);
297 * Returns the client used by this Sone.
299 * @return The client used by this Sone, or {@code null}
301 public Client getClient() {
306 * Sets the client used by this Sone.
309 * The client used by this Sone, or {@code null}
310 * @return This Sone (for method chaining)
312 public Sone setClient(Client client) {
313 this.client = client;
318 * Returns whether this Sone is known.
320 * @return {@code true} if this Sone is known, {@code false} otherwise
322 public boolean isKnown() {
327 * Sets whether this Sone is known.
330 * {@code true} if this Sone is known, {@code false} otherwise
333 public Sone setKnown(boolean known) {
339 * Returns all friend Sones of this Sone.
341 * @return The friend Sones of this Sone
343 public List<String> getFriends() {
344 return new ArrayList<String>(friendSones);
348 * Returns whether this Sone has the given Sone as a friend Sone.
350 * @param friendSoneId
351 * The ID of the Sone to check for
352 * @return {@code true} if this Sone has the given Sone as a friend, {@code
355 public boolean hasFriend(String friendSoneId) {
356 return friendSones.contains(friendSoneId);
360 * Adds the given Sone as a friend Sone.
363 * The friend Sone to add
364 * @return This Sone (for method chaining)
366 public Sone addFriend(String friendSone) {
367 if (!friendSone.equals(id)) {
368 friendSones.add(friendSone);
374 * Removes the given Sone as a friend Sone.
376 * @param friendSoneId
377 * The ID of the friend Sone to remove
378 * @return This Sone (for method chaining)
380 public Sone removeFriend(String friendSoneId) {
381 friendSones.remove(friendSoneId);
386 * Returns the list of posts of this Sone, sorted by time, newest first.
388 * @return All posts of this Sone
390 public List<Post> getPosts() {
391 List<Post> sortedPosts;
392 synchronized (this) {
393 sortedPosts = new ArrayList<Post>(posts);
395 Collections.sort(sortedPosts, Post.TIME_COMPARATOR);
400 * Sets all posts of this Sone at once.
403 * The new (and only) posts of this Sone
404 * @return This Sone (for method chaining)
406 public Sone setPosts(Collection<Post> posts) {
407 synchronized (this) {
409 this.posts.addAll(posts);
415 * Adds the given post to this Sone. The post will not be added if its {@link
416 * Post#getSone() Sone} is not this Sone.
421 public void addPost(Post post) {
422 if (post.getSone().equals(this) && posts.add(post)) {
423 logger.log(Level.FINEST, String.format("Adding %s to “%s”.", post, getName()));
428 * Removes the given post from this Sone.
433 public void removePost(Post post) {
434 if (post.getSone().equals(this)) {
440 * Returns all replies this Sone made.
442 * @return All replies this Sone made
444 public Set<PostReply> getReplies() {
445 return Collections.unmodifiableSet(replies);
449 * Sets all replies of this Sone at once.
452 * The new (and only) replies of this Sone
453 * @return This Sone (for method chaining)
455 public Sone setReplies(Collection<PostReply> replies) {
456 this.replies.clear();
457 this.replies.addAll(replies);
462 * Adds a reply to this Sone. If the given reply was not made by this Sone,
463 * nothing is added to this Sone.
468 public void addReply(PostReply reply) {
469 if (reply.getSone().equals(this)) {
475 * Removes a reply from this Sone.
478 * The reply to remove
480 public void removeReply(PostReply reply) {
481 if (reply.getSone().equals(this)) {
482 replies.remove(reply);
487 * Returns the IDs of all liked posts.
489 * @return All liked posts’ IDs
491 public Set<String> getLikedPostIds() {
492 return Collections.unmodifiableSet(likedPostIds);
496 * Sets the IDs of all liked posts.
498 * @param likedPostIds
499 * All liked posts’ IDs
500 * @return This Sone (for method chaining)
502 public Sone setLikePostIds(Set<String> likedPostIds) {
503 this.likedPostIds.clear();
504 this.likedPostIds.addAll(likedPostIds);
509 * Checks whether the given post ID is liked by this Sone.
513 * @return {@code true} if this Sone likes the given post, {@code false}
516 public boolean isLikedPostId(String postId) {
517 return likedPostIds.contains(postId);
521 * Adds the given post ID to the list of posts this Sone likes.
525 * @return This Sone (for method chaining)
527 public Sone addLikedPostId(String postId) {
528 likedPostIds.add(postId);
533 * Removes the given post ID from the list of posts this Sone likes.
537 * @return This Sone (for method chaining)
539 public Sone removeLikedPostId(String postId) {
540 likedPostIds.remove(postId);
545 * Returns the IDs of all liked replies.
547 * @return All liked replies’ IDs
549 public Set<String> getLikedReplyIds() {
550 return Collections.unmodifiableSet(likedReplyIds);
554 * Sets the IDs of all liked replies.
556 * @param likedReplyIds
557 * All liked replies’ IDs
558 * @return This Sone (for method chaining)
560 public Sone setLikeReplyIds(Set<String> likedReplyIds) {
561 this.likedReplyIds.clear();
562 this.likedReplyIds.addAll(likedReplyIds);
567 * Checks whether the given reply ID is liked by this Sone.
570 * The ID of the reply
571 * @return {@code true} if this Sone likes the given reply, {@code false}
574 public boolean isLikedReplyId(String replyId) {
575 return likedReplyIds.contains(replyId);
579 * Adds the given reply ID to the list of replies this Sone likes.
582 * The ID of the reply
583 * @return This Sone (for method chaining)
585 public Sone addLikedReplyId(String replyId) {
586 likedReplyIds.add(replyId);
591 * Removes the given post ID from the list of replies this Sone likes.
594 * The ID of the reply
595 * @return This Sone (for method chaining)
597 public Sone removeLikedReplyId(String replyId) {
598 likedReplyIds.remove(replyId);
603 * Returns the root album that contains all visible albums of this Sone.
605 * @return The root album of this Sone
607 public Album getRootAlbum() {
612 * Returns Sone-specific options.
614 * @return The options of this Sone
616 public SoneOptions getOptions() {
621 * Sets the options of this Sone.
624 * The options of this Sone
626 /* TODO - remove this method again, maybe add an option provider */
627 public void setOptions(SoneOptions options) {
628 this.options = options;
632 // FINGERPRINTABLE METHODS
637 public synchronized String getFingerprint() {
638 Hasher hash = Hashing.sha256().newHasher();
639 hash.putString(profile.getFingerprint());
641 hash.putString("Posts(");
642 for (Post post : getPosts()) {
643 hash.putString("Post(").putString(post.getId()).putString(")");
647 List<PostReply> replies = new ArrayList<PostReply>(getReplies());
648 Collections.sort(replies, Reply.TIME_COMPARATOR);
649 hash.putString("Replies(");
650 for (PostReply reply : replies) {
651 hash.putString("Reply(").putString(reply.getId()).putString(")");
655 List<String> likedPostIds = new ArrayList<String>(getLikedPostIds());
656 Collections.sort(likedPostIds);
657 hash.putString("LikedPosts(");
658 for (String likedPostId : likedPostIds) {
659 hash.putString("Post(").putString(likedPostId).putString(")");
663 List<String> likedReplyIds = new ArrayList<String>(getLikedReplyIds());
664 Collections.sort(likedReplyIds);
665 hash.putString("LikedReplies(");
666 for (String likedReplyId : likedReplyIds) {
667 hash.putString("Reply(").putString(likedReplyId).putString(")");
671 hash.putString("Albums(");
672 for (Album album : rootAlbum.getAlbums()) {
673 if (!Album.NOT_EMPTY.apply(album)) {
676 hash.putString(album.getFingerprint());
680 return hash.hash().toString();
684 // INTERFACE Comparable<Sone>
689 public int compareTo(Sone sone) {
690 return NICE_NAME_COMPARATOR.compare(this, sone);
699 public int hashCode() {
700 return id.hashCode();
705 public boolean equals(Object object) {
706 if (!(object instanceof Sone)) {
709 return ((Sone) object).getId().equals(id);
714 public String toString() {
715 return getClass().getName() + "[identity=" + identity + ",friends(" + friendSones.size() + "),posts(" + posts.size() + "),replies(" + replies.size() + "),albums(" + getRootAlbum().getAlbums().size() + ")]";