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.Client;
36 import net.pterodactylus.sone.data.Post;
37 import net.pterodactylus.sone.data.PostReply;
38 import net.pterodactylus.sone.data.Profile;
39 import net.pterodactylus.sone.data.Reply;
40 import net.pterodactylus.sone.data.Sone;
41 import net.pterodactylus.sone.data.SoneOptions;
42 import net.pterodactylus.sone.data.SoneOptions.DefaultSoneOptions;
43 import net.pterodactylus.sone.freenet.wot.Identity;
44 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
46 import freenet.keys.FreenetURI;
48 import com.google.common.hash.Hasher;
49 import com.google.common.hash.Hashing;
52 * {@link Sone} implementation.
54 * Operations that modify the Sone need to synchronize on the Sone in question.
56 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
58 public class SoneImpl implements Sone {
61 private static final Logger logger = getLogger("Sone.Data");
63 /** The ID of this Sone. */
64 private final String id;
66 /** Whether the Sone is local. */
67 private final boolean local;
69 /** The identity of this Sone. */
70 private final Identity identity;
72 /** The latest edition of the Sone. */
73 private volatile long latestEdition;
75 /** The time of the last inserted update. */
76 private volatile long time;
78 /** The status of this Sone. */
79 private volatile SoneStatus status = SoneStatus.unknown;
81 /** The profile of this Sone. */
82 private volatile Profile profile = new Profile(this);
84 /** The client used by the Sone. */
85 private volatile Client client;
87 /** Whether this Sone is known. */
88 private volatile boolean known;
90 /** All friend Sones. */
91 private final Set<String> friendSones = new CopyOnWriteArraySet<String>();
94 private final Set<Post> posts = new CopyOnWriteArraySet<Post>();
97 private final Set<PostReply> replies = new CopyOnWriteArraySet<PostReply>();
99 /** The IDs of all liked posts. */
100 private final Set<String> likedPostIds = new CopyOnWriteArraySet<String>();
102 /** The IDs of all liked replies. */
103 private final Set<String> likedReplyIds = new CopyOnWriteArraySet<String>();
105 /** The root album containing all albums. */
106 private final Album rootAlbum = new AlbumImpl(this);
108 /** Sone-specific options. */
109 private SoneOptions options = new DefaultSoneOptions();
112 * Creates a new Sone.
115 * The identity of the Sone
117 * {@code true} if the Sone is a local Sone, {@code false} otherwise
119 public SoneImpl(Identity identity, boolean local) {
120 this.id = identity.getId();
121 this.identity = identity;
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 * Returns the name of this Sone.
150 * @return The name of this Sone
152 public String getName() {
153 return (identity != null) ? identity.getNickname() : null;
157 * Returns whether this Sone is a local Sone.
159 * @return {@code true} if this Sone is a local Sone, {@code false} otherwise
161 public boolean isLocal() {
166 * Returns the request URI of this Sone.
168 * @return The request URI of this Sone
170 public FreenetURI getRequestUri() {
172 return new FreenetURI(getIdentity().getRequestUri())
175 .setMetaString(new String[0])
176 .setSuggestedEdition(latestEdition);
177 } catch (MalformedURLException e) {
178 throw new IllegalStateException(
179 format("Identity %s's request URI is incorrect.",
185 * Returns the insert URI of this Sone.
187 * @return The insert URI of this Sone
189 public FreenetURI getInsertUri() {
194 return new FreenetURI(((OwnIdentity) getIdentity()).getInsertUri())
196 .setMetaString(new String[0])
197 .setSuggestedEdition(latestEdition);
198 } catch (MalformedURLException e) {
199 throw new IllegalStateException(format("Own identity %s's insert URI is incorrect.", getIdentity()), e);
204 * Returns the latest edition of this Sone.
206 * @return The latest edition of this Sone
208 public long getLatestEdition() {
209 return latestEdition;
213 * Sets the latest edition of this Sone. If the given latest edition is not
214 * greater than the current latest edition, the latest edition of this Sone is
217 * @param latestEdition
218 * The latest edition of this Sone
220 public void setLatestEdition(long latestEdition) {
221 if (!(latestEdition > this.latestEdition)) {
222 logger.log(Level.FINE, String.format("New latest edition %d is not greater than current latest edition %d!", latestEdition, this.latestEdition));
225 this.latestEdition = latestEdition;
229 * Return the time of the last inserted update of this Sone.
231 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
233 public long getTime() {
238 * Sets the time of the last inserted update of this Sone.
241 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
242 * @return This Sone (for method chaining)
244 public Sone setTime(long time) {
250 * Returns the status of this Sone.
252 * @return The status of this Sone
254 public SoneStatus getStatus() {
259 * Sets the new status of this Sone.
262 * The new status of this Sone
264 * @throws IllegalArgumentException
265 * if {@code status} is {@code null}
267 public Sone setStatus(SoneStatus status) {
268 this.status = checkNotNull(status, "status must not be null");
273 * Returns a copy of the profile. If you want to update values in the profile
274 * of this Sone, update the values in the returned {@link Profile} and use
275 * {@link #setProfile(Profile)} to change the profile in this Sone.
277 * @return A copy of the profile
279 public Profile getProfile() {
280 return new Profile(profile);
284 * Sets the profile of this Sone. A copy of the given profile is stored so that
285 * subsequent modifications of the given profile are not reflected in this
291 public void setProfile(Profile profile) {
292 this.profile = new Profile(profile);
296 * Returns the client used by this Sone.
298 * @return The client used by this Sone, or {@code null}
300 public Client getClient() {
305 * Sets the client used by this Sone.
308 * The client used by this Sone, or {@code null}
309 * @return This Sone (for method chaining)
311 public Sone setClient(Client client) {
312 this.client = client;
317 * Returns whether this Sone is known.
319 * @return {@code true} if this Sone is known, {@code false} otherwise
321 public boolean isKnown() {
326 * Sets whether this Sone is known.
329 * {@code true} if this Sone is known, {@code false} otherwise
332 public Sone setKnown(boolean known) {
338 * Returns all friend Sones of this Sone.
340 * @return The friend Sones of this Sone
342 public List<String> getFriends() {
343 return new ArrayList<String>(friendSones);
347 * Returns whether this Sone has the given Sone as a friend Sone.
349 * @param friendSoneId
350 * The ID of the Sone to check for
351 * @return {@code true} if this Sone has the given Sone as a friend, {@code
354 public boolean hasFriend(String friendSoneId) {
355 return friendSones.contains(friendSoneId);
359 * Adds the given Sone as a friend Sone.
362 * The friend Sone to add
363 * @return This Sone (for method chaining)
365 public Sone addFriend(String friendSone) {
366 if (!friendSone.equals(id)) {
367 friendSones.add(friendSone);
373 * Removes the given Sone as a friend Sone.
375 * @param friendSoneId
376 * The ID of the friend Sone to remove
377 * @return This Sone (for method chaining)
379 public Sone removeFriend(String friendSoneId) {
380 friendSones.remove(friendSoneId);
385 * Returns the list of posts of this Sone, sorted by time, newest first.
387 * @return All posts of this Sone
389 public List<Post> getPosts() {
390 List<Post> sortedPosts;
391 synchronized (this) {
392 sortedPosts = new ArrayList<Post>(posts);
394 Collections.sort(sortedPosts, Post.TIME_COMPARATOR);
399 * Sets all posts of this Sone at once.
402 * The new (and only) posts of this Sone
403 * @return This Sone (for method chaining)
405 public Sone setPosts(Collection<Post> posts) {
406 synchronized (this) {
408 this.posts.addAll(posts);
414 * Adds the given post to this Sone. The post will not be added if its {@link
415 * Post#getSone() Sone} is not this Sone.
420 public void addPost(Post post) {
421 if (post.getSone().equals(this) && posts.add(post)) {
422 logger.log(Level.FINEST, String.format("Adding %s to “%s”.", post, getName()));
427 * Removes the given post from this Sone.
432 public void removePost(Post post) {
433 if (post.getSone().equals(this)) {
439 * Returns all replies this Sone made.
441 * @return All replies this Sone made
443 public Set<PostReply> getReplies() {
444 return Collections.unmodifiableSet(replies);
448 * Sets all replies of this Sone at once.
451 * The new (and only) replies of this Sone
452 * @return This Sone (for method chaining)
454 public Sone setReplies(Collection<PostReply> replies) {
455 this.replies.clear();
456 this.replies.addAll(replies);
461 * Adds a reply to this Sone. If the given reply was not made by this Sone,
462 * nothing is added to this Sone.
467 public void addReply(PostReply reply) {
468 if (reply.getSone().equals(this)) {
474 * Removes a reply from this Sone.
477 * The reply to remove
479 public void removeReply(PostReply reply) {
480 if (reply.getSone().equals(this)) {
481 replies.remove(reply);
486 * Returns the IDs of all liked posts.
488 * @return All liked posts’ IDs
490 public Set<String> getLikedPostIds() {
491 return Collections.unmodifiableSet(likedPostIds);
495 * Sets the IDs of all liked posts.
497 * @param likedPostIds
498 * All liked posts’ IDs
499 * @return This Sone (for method chaining)
501 public Sone setLikePostIds(Set<String> likedPostIds) {
502 this.likedPostIds.clear();
503 this.likedPostIds.addAll(likedPostIds);
508 * Checks whether the given post ID is liked by this Sone.
512 * @return {@code true} if this Sone likes the given post, {@code false}
515 public boolean isLikedPostId(String postId) {
516 return likedPostIds.contains(postId);
520 * Adds the given post ID to the list of posts this Sone likes.
524 * @return This Sone (for method chaining)
526 public Sone addLikedPostId(String postId) {
527 likedPostIds.add(postId);
532 * Removes the given post ID from the list of posts this Sone likes.
536 * @return This Sone (for method chaining)
538 public Sone removeLikedPostId(String postId) {
539 likedPostIds.remove(postId);
544 * Returns the IDs of all liked replies.
546 * @return All liked replies’ IDs
548 public Set<String> getLikedReplyIds() {
549 return Collections.unmodifiableSet(likedReplyIds);
553 * Sets the IDs of all liked replies.
555 * @param likedReplyIds
556 * All liked replies’ IDs
557 * @return This Sone (for method chaining)
559 public Sone setLikeReplyIds(Set<String> likedReplyIds) {
560 this.likedReplyIds.clear();
561 this.likedReplyIds.addAll(likedReplyIds);
566 * Checks whether the given reply ID is liked by this Sone.
569 * The ID of the reply
570 * @return {@code true} if this Sone likes the given reply, {@code false}
573 public boolean isLikedReplyId(String replyId) {
574 return likedReplyIds.contains(replyId);
578 * Adds the given reply ID to the list of replies this Sone likes.
581 * The ID of the reply
582 * @return This Sone (for method chaining)
584 public Sone addLikedReplyId(String replyId) {
585 likedReplyIds.add(replyId);
590 * Removes the given post ID from the list of replies this Sone likes.
593 * The ID of the reply
594 * @return This Sone (for method chaining)
596 public Sone removeLikedReplyId(String replyId) {
597 likedReplyIds.remove(replyId);
602 * Returns the root album that contains all visible albums of this Sone.
604 * @return The root album of this Sone
606 public Album getRootAlbum() {
611 * Returns Sone-specific options.
613 * @return The options of this Sone
615 public SoneOptions getOptions() {
620 * Sets the options of this Sone.
623 * The options of this Sone
625 /* TODO - remove this method again, maybe add an option provider */
626 public void setOptions(SoneOptions options) {
627 this.options = options;
631 // FINGERPRINTABLE METHODS
636 public synchronized String getFingerprint() {
637 Hasher hash = Hashing.sha256().newHasher();
638 hash.putString(profile.getFingerprint());
640 hash.putString("Posts(");
641 for (Post post : getPosts()) {
642 hash.putString("Post(").putString(post.getId()).putString(")");
646 List<PostReply> replies = new ArrayList<PostReply>(getReplies());
647 Collections.sort(replies, Reply.TIME_COMPARATOR);
648 hash.putString("Replies(");
649 for (PostReply reply : replies) {
650 hash.putString("Reply(").putString(reply.getId()).putString(")");
654 List<String> likedPostIds = new ArrayList<String>(getLikedPostIds());
655 Collections.sort(likedPostIds);
656 hash.putString("LikedPosts(");
657 for (String likedPostId : likedPostIds) {
658 hash.putString("Post(").putString(likedPostId).putString(")");
662 List<String> likedReplyIds = new ArrayList<String>(getLikedReplyIds());
663 Collections.sort(likedReplyIds);
664 hash.putString("LikedReplies(");
665 for (String likedReplyId : likedReplyIds) {
666 hash.putString("Reply(").putString(likedReplyId).putString(")");
670 hash.putString("Albums(");
671 for (Album album : rootAlbum.getAlbums()) {
672 if (!Album.NOT_EMPTY.apply(album)) {
675 hash.putString(album.getFingerprint());
679 return hash.hash().toString();
683 // INTERFACE Comparable<Sone>
688 public int compareTo(Sone sone) {
689 return NICE_NAME_COMPARATOR.compare(this, sone);
698 public int hashCode() {
699 return id.hashCode();
704 public boolean equals(Object object) {
705 if (!(object instanceof Sone)) {
708 return ((Sone) object).getId().equals(id);
713 public String toString() {
714 return getClass().getName() + "[identity=" + identity + ",friends(" + friendSones.size() + "),posts(" + posts.size() + "),replies(" + replies.size() + "),albums(" + getRootAlbum().getAlbums().size() + ")]";