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 Collection<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 * Returns the list of posts of this Sone, sorted by time, newest first.
361 * @return All posts of this Sone
363 public List<Post> getPosts() {
364 List<Post> sortedPosts;
365 synchronized (this) {
366 sortedPosts = new ArrayList<Post>(posts);
368 Collections.sort(sortedPosts, Post.TIME_COMPARATOR);
373 * Sets all posts of this Sone at once.
376 * The new (and only) posts of this Sone
377 * @return This Sone (for method chaining)
379 public Sone setPosts(Collection<Post> posts) {
380 synchronized (this) {
382 this.posts.addAll(posts);
388 * Adds the given post to this Sone. The post will not be added if its {@link
389 * Post#getSone() Sone} is not this Sone.
394 public void addPost(Post post) {
395 if (post.getSone().equals(this) && posts.add(post)) {
396 logger.log(Level.FINEST, String.format("Adding %s to “%s”.", post, getName()));
401 * Removes the given post from this Sone.
406 public void removePost(Post post) {
407 if (post.getSone().equals(this)) {
413 * Returns all replies this Sone made.
415 * @return All replies this Sone made
417 public Set<PostReply> getReplies() {
418 return Collections.unmodifiableSet(replies);
422 * Sets all replies of this Sone at once.
425 * The new (and only) replies of this Sone
426 * @return This Sone (for method chaining)
428 public Sone setReplies(Collection<PostReply> replies) {
429 this.replies.clear();
430 this.replies.addAll(replies);
435 * Adds a reply to this Sone. If the given reply was not made by this Sone,
436 * nothing is added to this Sone.
441 public void addReply(PostReply reply) {
442 if (reply.getSone().equals(this)) {
448 * Removes a reply from this Sone.
451 * The reply to remove
453 public void removeReply(PostReply reply) {
454 if (reply.getSone().equals(this)) {
455 replies.remove(reply);
460 * Returns the IDs of all liked posts.
462 * @return All liked posts’ IDs
464 public Set<String> getLikedPostIds() {
465 return Collections.unmodifiableSet(likedPostIds);
469 * Sets the IDs of all liked posts.
471 * @param likedPostIds
472 * All liked posts’ IDs
473 * @return This Sone (for method chaining)
475 public Sone setLikePostIds(Set<String> likedPostIds) {
476 this.likedPostIds.clear();
477 this.likedPostIds.addAll(likedPostIds);
482 * Checks whether the given post ID is liked by this Sone.
486 * @return {@code true} if this Sone likes the given post, {@code false}
489 public boolean isLikedPostId(String postId) {
490 return likedPostIds.contains(postId);
494 * Adds the given post ID to the list of posts this Sone likes.
498 * @return This Sone (for method chaining)
500 public Sone addLikedPostId(String postId) {
501 likedPostIds.add(postId);
506 * Removes the given post ID from the list of posts this Sone likes.
510 * @return This Sone (for method chaining)
512 public Sone removeLikedPostId(String postId) {
513 likedPostIds.remove(postId);
518 * Returns the IDs of all liked replies.
520 * @return All liked replies’ IDs
522 public Set<String> getLikedReplyIds() {
523 return Collections.unmodifiableSet(likedReplyIds);
527 * Sets the IDs of all liked replies.
529 * @param likedReplyIds
530 * All liked replies’ IDs
531 * @return This Sone (for method chaining)
533 public Sone setLikeReplyIds(Set<String> likedReplyIds) {
534 this.likedReplyIds.clear();
535 this.likedReplyIds.addAll(likedReplyIds);
540 * Checks whether the given reply ID is liked by this Sone.
543 * The ID of the reply
544 * @return {@code true} if this Sone likes the given reply, {@code false}
547 public boolean isLikedReplyId(String replyId) {
548 return likedReplyIds.contains(replyId);
552 * Adds the given reply ID to the list of replies this Sone likes.
555 * The ID of the reply
556 * @return This Sone (for method chaining)
558 public Sone addLikedReplyId(String replyId) {
559 likedReplyIds.add(replyId);
564 * Removes the given post ID from the list of replies this Sone likes.
567 * The ID of the reply
568 * @return This Sone (for method chaining)
570 public Sone removeLikedReplyId(String replyId) {
571 likedReplyIds.remove(replyId);
576 * Returns the root album that contains all visible albums of this Sone.
578 * @return The root album of this Sone
580 public Album getRootAlbum() {
585 * Returns Sone-specific options.
587 * @return The options of this Sone
589 public SoneOptions getOptions() {
594 * Sets the options of this Sone.
597 * The options of this Sone
599 /* TODO - remove this method again, maybe add an option provider */
600 public void setOptions(SoneOptions options) {
601 this.options = options;
605 // FINGERPRINTABLE METHODS
610 public synchronized String getFingerprint() {
611 Hasher hash = Hashing.sha256().newHasher();
612 hash.putString(profile.getFingerprint());
614 hash.putString("Posts(");
615 for (Post post : getPosts()) {
616 hash.putString("Post(").putString(post.getId()).putString(")");
620 List<PostReply> replies = new ArrayList<PostReply>(getReplies());
621 Collections.sort(replies, Reply.TIME_COMPARATOR);
622 hash.putString("Replies(");
623 for (PostReply reply : replies) {
624 hash.putString("Reply(").putString(reply.getId()).putString(")");
628 List<String> likedPostIds = new ArrayList<String>(getLikedPostIds());
629 Collections.sort(likedPostIds);
630 hash.putString("LikedPosts(");
631 for (String likedPostId : likedPostIds) {
632 hash.putString("Post(").putString(likedPostId).putString(")");
636 List<String> likedReplyIds = new ArrayList<String>(getLikedReplyIds());
637 Collections.sort(likedReplyIds);
638 hash.putString("LikedReplies(");
639 for (String likedReplyId : likedReplyIds) {
640 hash.putString("Reply(").putString(likedReplyId).putString(")");
644 hash.putString("Albums(");
645 for (Album album : rootAlbum.getAlbums()) {
646 if (!Album.NOT_EMPTY.apply(album)) {
649 hash.putString(album.getFingerprint());
653 return hash.hash().toString();
657 // INTERFACE Comparable<Sone>
662 public int compareTo(Sone sone) {
663 return NICE_NAME_COMPARATOR.compare(this, sone);
672 public int hashCode() {
673 return id.hashCode();
678 public boolean equals(Object object) {
679 if (!(object instanceof Sone)) {
682 return ((Sone) object).getId().equals(id);
687 public String toString() {
688 return getClass().getName() + "[identity=" + identity + ",friends(" + friendSones.size() + "),posts(" + posts.size() + "),replies(" + replies.size() + "),albums(" + getRootAlbum().getAlbums().size() + ")]";