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;
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.SoneOptions.DefaultSoneOptions;
35 import net.pterodactylus.sone.freenet.wot.Identity;
36 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
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 = getLogger("Sone.Data");
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 final Identity identity;
64 /** The latest edition of the Sone. */
65 private volatile long latestEdition;
67 /** The time of the last inserted update. */
68 private volatile long time;
70 /** The status of this Sone. */
71 private volatile SoneStatus status = SoneStatus.unknown;
73 /** The profile of this Sone. */
74 private volatile Profile profile = new Profile(this);
76 /** The client used by the Sone. */
77 private volatile Client client;
79 /** Whether this Sone is known. */
80 private volatile boolean known;
82 /** All friend Sones. */
83 private final Set<String> friendSones = new CopyOnWriteArraySet<String>();
86 private final Set<Post> posts = new CopyOnWriteArraySet<Post>();
89 private final Set<PostReply> replies = new CopyOnWriteArraySet<PostReply>();
91 /** The IDs of all liked posts. */
92 private final Set<String> likedPostIds = new CopyOnWriteArraySet<String>();
94 /** The IDs of all liked replies. */
95 private final Set<String> likedReplyIds = new CopyOnWriteArraySet<String>();
97 /** The root album containing all albums. */
98 private final Album rootAlbum = new AlbumImpl(this);
100 /** Sone-specific options. */
101 private SoneOptions options = new DefaultSoneOptions();
104 * Creates a new Sone.
107 * The identity of the Sone
109 * {@code true} if the Sone is a local Sone, {@code false} otherwise
111 public SoneImpl(Identity identity, boolean local) {
112 this.id = identity.getId();
113 this.identity = identity;
122 * Returns the identity of this Sone.
124 * @return The identity of this Sone
126 public String getId() {
131 * Returns the identity of this Sone.
133 * @return The identity of this Sone
135 public Identity getIdentity() {
140 * Returns the name of this Sone.
142 * @return The name of this Sone
144 public String getName() {
145 return (identity != null) ? identity.getNickname() : null;
149 * Returns whether this Sone is a local Sone.
151 * @return {@code true} if this Sone is a local Sone, {@code false} otherwise
153 public boolean isLocal() {
158 * Returns the request URI of this Sone.
160 * @return The request URI of this Sone
162 public FreenetURI getRequestUri() {
164 return new FreenetURI(getIdentity().getRequestUri())
167 .setMetaString(new String[0])
168 .setSuggestedEdition(latestEdition);
169 } catch (MalformedURLException e) {
170 throw new IllegalStateException(
171 format("Identity %s's request URI is incorrect.",
177 * Returns the insert URI of this Sone.
179 * @return The insert URI of this Sone
181 public FreenetURI getInsertUri() {
186 return new FreenetURI(((OwnIdentity) getIdentity()).getInsertUri())
188 .setMetaString(new String[0])
189 .setSuggestedEdition(latestEdition);
190 } catch (MalformedURLException e) {
191 throw new IllegalStateException(format("Own identity %s's insert URI is incorrect.", getIdentity()), e);
196 * Returns the latest edition of this Sone.
198 * @return The latest edition of this Sone
200 public long getLatestEdition() {
201 return latestEdition;
205 * Sets the latest edition of this Sone. If the given latest edition is not
206 * greater than the current latest edition, the latest edition of this Sone is
209 * @param latestEdition
210 * The latest edition of this Sone
212 public void setLatestEdition(long latestEdition) {
213 if (!(latestEdition > this.latestEdition)) {
214 logger.log(Level.FINE, String.format("New latest edition %d is not greater than current latest edition %d!", latestEdition, this.latestEdition));
217 this.latestEdition = latestEdition;
221 * Return the time of the last inserted update of this Sone.
223 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
225 public long getTime() {
230 * Sets the time of the last inserted update of this Sone.
233 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
234 * @return This Sone (for method chaining)
236 public Sone setTime(long time) {
242 * Returns the status of this Sone.
244 * @return The status of this Sone
246 public SoneStatus getStatus() {
251 * Sets the new status of this Sone.
254 * The new status of this Sone
256 * @throws IllegalArgumentException
257 * if {@code status} is {@code null}
259 public Sone setStatus(SoneStatus status) {
260 this.status = checkNotNull(status, "status must not be null");
265 * Returns a copy of the profile. If you want to update values in the profile
266 * of this Sone, update the values in the returned {@link Profile} and use
267 * {@link #setProfile(Profile)} to change the profile in this Sone.
269 * @return A copy of the profile
271 public Profile getProfile() {
272 return new Profile(profile);
276 * Sets the profile of this Sone. A copy of the given profile is stored so that
277 * subsequent modifications of the given profile are not reflected in this
283 public void setProfile(Profile profile) {
284 this.profile = new Profile(profile);
288 * Returns the client used by this Sone.
290 * @return The client used by this Sone, or {@code null}
292 public Client getClient() {
297 * Sets the client used by this Sone.
300 * The client used by this Sone, or {@code null}
301 * @return This Sone (for method chaining)
303 public Sone setClient(Client client) {
304 this.client = client;
309 * Returns whether this Sone is known.
311 * @return {@code true} if this Sone is known, {@code false} otherwise
313 public boolean isKnown() {
318 * Sets whether this Sone is known.
321 * {@code true} if this Sone is known, {@code false} otherwise
324 public Sone setKnown(boolean known) {
330 * Returns all friend Sones of this Sone.
332 * @return The friend Sones of this Sone
334 public List<String> getFriends() {
335 return new ArrayList<String>(friendSones);
339 * Returns whether this Sone has the given Sone as a friend Sone.
341 * @param friendSoneId
342 * The ID of the Sone to check for
343 * @return {@code true} if this Sone has the given Sone as a friend, {@code
346 public boolean hasFriend(String friendSoneId) {
347 return friendSones.contains(friendSoneId);
351 * Adds the given Sone as a friend Sone.
354 * The friend Sone to add
355 * @return This Sone (for method chaining)
357 public Sone addFriend(String friendSone) {
358 if (!friendSone.equals(id)) {
359 friendSones.add(friendSone);
365 * Removes the given Sone as a friend Sone.
367 * @param friendSoneId
368 * The ID of the friend Sone to remove
369 * @return This Sone (for method chaining)
371 public Sone removeFriend(String friendSoneId) {
372 friendSones.remove(friendSoneId);
377 * Returns the list of posts of this Sone, sorted by time, newest first.
379 * @return All posts of this Sone
381 public List<Post> getPosts() {
382 List<Post> sortedPosts;
383 synchronized (this) {
384 sortedPosts = new ArrayList<Post>(posts);
386 Collections.sort(sortedPosts, Post.TIME_COMPARATOR);
391 * Sets all posts of this Sone at once.
394 * The new (and only) posts of this Sone
395 * @return This Sone (for method chaining)
397 public Sone setPosts(Collection<Post> posts) {
398 synchronized (this) {
400 this.posts.addAll(posts);
406 * Adds the given post to this Sone. The post will not be added if its {@link
407 * Post#getSone() Sone} is not this Sone.
412 public void addPost(Post post) {
413 if (post.getSone().equals(this) && posts.add(post)) {
414 logger.log(Level.FINEST, String.format("Adding %s to “%s”.", post, getName()));
419 * Removes the given post from this Sone.
424 public void removePost(Post post) {
425 if (post.getSone().equals(this)) {
431 * Returns all replies this Sone made.
433 * @return All replies this Sone made
435 public Set<PostReply> getReplies() {
436 return Collections.unmodifiableSet(replies);
440 * Sets all replies of this Sone at once.
443 * The new (and only) replies of this Sone
444 * @return This Sone (for method chaining)
446 public Sone setReplies(Collection<PostReply> replies) {
447 this.replies.clear();
448 this.replies.addAll(replies);
453 * Adds a reply to this Sone. If the given reply was not made by this Sone,
454 * nothing is added to this Sone.
459 public void addReply(PostReply reply) {
460 if (reply.getSone().equals(this)) {
466 * Removes a reply from this Sone.
469 * The reply to remove
471 public void removeReply(PostReply reply) {
472 if (reply.getSone().equals(this)) {
473 replies.remove(reply);
478 * Returns the IDs of all liked posts.
480 * @return All liked posts’ IDs
482 public Set<String> getLikedPostIds() {
483 return Collections.unmodifiableSet(likedPostIds);
487 * Sets the IDs of all liked posts.
489 * @param likedPostIds
490 * All liked posts’ IDs
491 * @return This Sone (for method chaining)
493 public Sone setLikePostIds(Set<String> likedPostIds) {
494 this.likedPostIds.clear();
495 this.likedPostIds.addAll(likedPostIds);
500 * Checks whether the given post ID is liked by this Sone.
504 * @return {@code true} if this Sone likes the given post, {@code false}
507 public boolean isLikedPostId(String postId) {
508 return likedPostIds.contains(postId);
512 * Adds the given post ID to the list of posts this Sone likes.
516 * @return This Sone (for method chaining)
518 public Sone addLikedPostId(String postId) {
519 likedPostIds.add(postId);
524 * Removes the given post ID from the list of posts this Sone likes.
528 * @return This Sone (for method chaining)
530 public Sone removeLikedPostId(String postId) {
531 likedPostIds.remove(postId);
536 * Returns the IDs of all liked replies.
538 * @return All liked replies’ IDs
540 public Set<String> getLikedReplyIds() {
541 return Collections.unmodifiableSet(likedReplyIds);
545 * Sets the IDs of all liked replies.
547 * @param likedReplyIds
548 * All liked replies’ IDs
549 * @return This Sone (for method chaining)
551 public Sone setLikeReplyIds(Set<String> likedReplyIds) {
552 this.likedReplyIds.clear();
553 this.likedReplyIds.addAll(likedReplyIds);
558 * Checks whether the given reply ID is liked by this Sone.
561 * The ID of the reply
562 * @return {@code true} if this Sone likes the given reply, {@code false}
565 public boolean isLikedReplyId(String replyId) {
566 return likedReplyIds.contains(replyId);
570 * Adds the given reply ID to the list of replies this Sone likes.
573 * The ID of the reply
574 * @return This Sone (for method chaining)
576 public Sone addLikedReplyId(String replyId) {
577 likedReplyIds.add(replyId);
582 * Removes the given post ID from the list of replies this Sone likes.
585 * The ID of the reply
586 * @return This Sone (for method chaining)
588 public Sone removeLikedReplyId(String replyId) {
589 likedReplyIds.remove(replyId);
594 * Returns the root album that contains all visible albums of this Sone.
596 * @return The root album of this Sone
598 public Album getRootAlbum() {
603 * Returns Sone-specific options.
605 * @return The options of this Sone
607 public SoneOptions getOptions() {
612 * Sets the options of this Sone.
615 * The options of this Sone
617 /* TODO - remove this method again, maybe add an option provider */
618 public void setOptions(SoneOptions options) {
619 this.options = options;
623 // FINGERPRINTABLE METHODS
628 public synchronized String getFingerprint() {
629 Hasher hash = Hashing.sha256().newHasher();
630 hash.putString(profile.getFingerprint());
632 hash.putString("Posts(");
633 for (Post post : getPosts()) {
634 hash.putString("Post(").putString(post.getId()).putString(")");
638 List<PostReply> replies = new ArrayList<PostReply>(getReplies());
639 Collections.sort(replies, Reply.TIME_COMPARATOR);
640 hash.putString("Replies(");
641 for (PostReply reply : replies) {
642 hash.putString("Reply(").putString(reply.getId()).putString(")");
646 List<String> likedPostIds = new ArrayList<String>(getLikedPostIds());
647 Collections.sort(likedPostIds);
648 hash.putString("LikedPosts(");
649 for (String likedPostId : likedPostIds) {
650 hash.putString("Post(").putString(likedPostId).putString(")");
654 List<String> likedReplyIds = new ArrayList<String>(getLikedReplyIds());
655 Collections.sort(likedReplyIds);
656 hash.putString("LikedReplies(");
657 for (String likedReplyId : likedReplyIds) {
658 hash.putString("Reply(").putString(likedReplyId).putString(")");
662 hash.putString("Albums(");
663 for (Album album : rootAlbum.getAlbums()) {
664 if (!Album.NOT_EMPTY.apply(album)) {
667 hash.putString(album.getFingerprint());
671 return hash.hash().toString();
675 // INTERFACE Comparable<Sone>
680 public int compareTo(Sone sone) {
681 return NICE_NAME_COMPARATOR.compare(this, sone);
690 public int hashCode() {
691 return id.hashCode();
696 public boolean equals(Object object) {
697 if (!(object instanceof Sone)) {
700 return ((Sone) object).getId().equals(id);
705 public String toString() {
706 return getClass().getName() + "[identity=" + identity + ",friends(" + friendSones.size() + "),posts(" + posts.size() + "),replies(" + replies.size() + "),albums(" + getRootAlbum().getAlbums().size() + ")]";