2 * Sone - SoneImpl.java - Copyright © 2010–2016 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 javax.annotation.Nonnull;
35 import javax.annotation.Nullable;
37 import net.pterodactylus.sone.data.Album;
38 import net.pterodactylus.sone.data.Client;
39 import net.pterodactylus.sone.data.Post;
40 import net.pterodactylus.sone.data.PostReply;
41 import net.pterodactylus.sone.data.Profile;
42 import net.pterodactylus.sone.data.Reply;
43 import net.pterodactylus.sone.data.Sone;
44 import net.pterodactylus.sone.data.SoneOptions;
45 import net.pterodactylus.sone.data.SoneOptions.DefaultSoneOptions;
46 import net.pterodactylus.sone.database.Database;
47 import net.pterodactylus.sone.freenet.wot.Identity;
48 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
50 import freenet.keys.FreenetURI;
52 import com.google.common.hash.Hasher;
53 import com.google.common.hash.Hashing;
56 * {@link Sone} implementation.
58 * Operations that modify the Sone need to synchronize on the Sone in question.
60 public class SoneImpl implements Sone {
63 private static final Logger logger = getLogger(SoneImpl.class.getName());
66 private final Database database;
68 /** The ID of this Sone. */
69 private final String id;
71 /** Whether the Sone is local. */
72 private final boolean local;
74 /** The identity of this Sone. */
75 private final Identity identity;
77 /** The latest edition of the Sone. */
78 private volatile long latestEdition;
80 /** The time of the last inserted update. */
81 private volatile long time;
83 /** The status of this Sone. */
84 private volatile SoneStatus status = SoneStatus.unknown;
86 /** The profile of this Sone. */
87 private volatile Profile profile = new Profile(this);
89 /** The client used by the Sone. */
90 private volatile Client client;
92 /** Whether this Sone is known. */
93 private volatile boolean known;
96 private final Set<Post> posts = new CopyOnWriteArraySet<>();
99 private final Set<PostReply> replies = new CopyOnWriteArraySet<>();
101 /** The IDs of all liked posts. */
102 private final Set<String> likedPostIds = new CopyOnWriteArraySet<>();
104 /** The IDs of all liked replies. */
105 private final Set<String> likedReplyIds = new CopyOnWriteArraySet<>();
107 /** The root album containing all albums. */
108 private final Album rootAlbum = new AlbumImpl(this);
110 /** Sone-specific options. */
111 private SoneOptions options = new DefaultSoneOptions();
114 * Creates a new Sone.
116 * @param database The database
118 * The identity of the Sone
120 * {@code true} if the Sone is a local Sone, {@code false} otherwise
122 public SoneImpl(Database database, Identity identity, boolean local) {
123 this.database = database;
124 this.id = identity.getId();
125 this.identity = identity;
134 * Returns the identity of this Sone.
136 * @return The identity of this Sone
139 public String getId() {
144 * Returns the identity of this Sone.
146 * @return The identity of this Sone
149 public Identity getIdentity() {
154 * Returns the name of this Sone.
156 * @return The name of this Sone
159 public String getName() {
160 return (identity != null) ? identity.getNickname() : null;
164 * Returns whether this Sone is a local Sone.
166 * @return {@code true} if this Sone is a local Sone, {@code false} otherwise
168 public boolean isLocal() {
173 * Returns the request URI of this Sone.
175 * @return The request URI of this Sone
178 public FreenetURI getRequestUri() {
180 return new FreenetURI(getIdentity().getRequestUri())
183 .setMetaString(new String[0])
184 .setSuggestedEdition(latestEdition);
185 } catch (MalformedURLException e) {
186 throw new IllegalStateException(
187 format("Identity %s's request URI is incorrect.",
193 * Returns the insert URI of this Sone.
195 * @return The insert URI of this Sone
198 public FreenetURI getInsertUri() {
203 return new FreenetURI(((OwnIdentity) getIdentity()).getInsertUri())
205 .setMetaString(new String[0])
206 .setSuggestedEdition(latestEdition);
207 } catch (MalformedURLException e) {
208 throw new IllegalStateException(format("Own identity %s's insert URI is incorrect.", getIdentity()), e);
213 * Returns the latest edition of this Sone.
215 * @return The latest edition of this Sone
217 public long getLatestEdition() {
218 return latestEdition;
222 * Sets the latest edition of this Sone. If the given latest edition is not
223 * greater than the current latest edition, the latest edition of this Sone is
226 * @param latestEdition
227 * The latest edition of this Sone
229 public void setLatestEdition(long latestEdition) {
230 if (!(latestEdition > this.latestEdition)) {
231 logger.log(Level.FINE, String.format("New latest edition %d is not greater than current latest edition %d!", latestEdition, this.latestEdition));
234 this.latestEdition = latestEdition;
238 * Return the time of the last inserted update of this Sone.
240 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
242 public long getTime() {
247 * Sets the time of the last inserted update of this Sone.
250 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
251 * @return This Sone (for method chaining)
254 public Sone setTime(long time) {
260 * Returns the status of this Sone.
262 * @return The status of this Sone
265 public SoneStatus getStatus() {
270 * Sets the new status of this Sone.
273 * The new status of this Sone
275 * @throws IllegalArgumentException
276 * if {@code status} is {@code null}
279 public Sone setStatus(@Nonnull SoneStatus status) {
280 this.status = checkNotNull(status, "status must not be null");
285 * Returns a copy of the profile. If you want to update values in the profile
286 * of this Sone, update the values in the returned {@link Profile} and use
287 * {@link #setProfile(Profile)} to change the profile in this Sone.
289 * @return A copy of the profile
292 public Profile getProfile() {
293 return new Profile(profile);
297 * Sets the profile of this Sone. A copy of the given profile is stored so that
298 * subsequent modifications of the given profile are not reflected in this
304 public void setProfile(@Nonnull Profile profile) {
305 this.profile = new Profile(profile);
309 * Returns the client used by this Sone.
311 * @return The client used by this Sone, or {@code null}
314 public Client getClient() {
319 * Sets the client used by this Sone.
322 * The client used by this Sone, or {@code null}
323 * @return This Sone (for method chaining)
326 public Sone setClient(@Nullable Client client) {
327 this.client = client;
332 * Returns whether this Sone is known.
334 * @return {@code true} if this Sone is known, {@code false} otherwise
336 public boolean isKnown() {
341 * Sets whether this Sone is known.
344 * {@code true} if this Sone is known, {@code false} otherwise
348 public Sone setKnown(boolean known) {
354 * Returns all friend Sones of this Sone.
356 * @return The friend Sones of this Sone
359 public Collection<String> getFriends() {
360 return database.getFriends(this);
364 * Returns whether this Sone has the given Sone as a friend Sone.
366 * @param friendSoneId
367 * The ID of the Sone to check for
368 * @return {@code true} if this Sone has the given Sone as a friend, {@code
371 public boolean hasFriend(@Nonnull String friendSoneId) {
372 return database.isFriend(this, friendSoneId);
376 * Returns the list of posts of this Sone, sorted by time, newest first.
378 * @return All posts of this Sone
381 public List<Post> getPosts() {
382 List<Post> sortedPosts;
383 synchronized (this) {
384 sortedPosts = new ArrayList<>(posts);
386 Collections.sort(sortedPosts, Post.NEWEST_FIRST);
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)
398 public Sone setPosts(@Nonnull Collection<Post> posts) {
399 synchronized (this) {
401 this.posts.addAll(posts);
407 * Adds the given post to this Sone. The post will not be added if its {@link
408 * Post#getSone() Sone} is not this Sone.
413 public void addPost(@Nonnull Post post) {
414 if (post.getSone().equals(this) && posts.add(post)) {
415 logger.log(Level.FINEST, String.format("Adding %s to “%s”.", post, getName()));
420 * Removes the given post from this Sone.
425 public void removePost(@Nonnull Post post) {
426 if (post.getSone().equals(this)) {
432 * Returns all replies this Sone made.
434 * @return All replies this Sone made
437 public Set<PostReply> getReplies() {
438 return Collections.unmodifiableSet(replies);
442 * Sets all replies of this Sone at once.
445 * The new (and only) replies of this Sone
446 * @return This Sone (for method chaining)
449 public Sone setReplies(@Nonnull Collection<PostReply> replies) {
450 this.replies.clear();
451 this.replies.addAll(replies);
456 * Adds a reply to this Sone. If the given reply was not made by this Sone,
457 * nothing is added to this Sone.
462 public void addReply(@Nonnull PostReply reply) {
463 if (reply.getSone().equals(this)) {
469 * Removes a reply from this Sone.
472 * The reply to remove
474 public void removeReply(@Nonnull PostReply reply) {
475 if (reply.getSone().equals(this)) {
476 replies.remove(reply);
481 * Returns the IDs of all liked posts.
483 * @return All liked posts’ IDs
486 public Set<String> getLikedPostIds() {
487 return Collections.unmodifiableSet(likedPostIds);
491 * Sets the IDs of all liked posts.
493 * @param likedPostIds
494 * All liked posts’ IDs
495 * @return This Sone (for method chaining)
498 public Sone setLikePostIds(@Nonnull Set<String> likedPostIds) {
499 this.likedPostIds.clear();
500 this.likedPostIds.addAll(likedPostIds);
505 * Checks whether the given post ID is liked by this Sone.
509 * @return {@code true} if this Sone likes the given post, {@code false}
512 public boolean isLikedPostId(@Nonnull String postId) {
513 return likedPostIds.contains(postId);
517 * Adds the given post ID to the list of posts this Sone likes.
521 * @return This Sone (for method chaining)
524 public Sone addLikedPostId(@Nonnull String postId) {
525 likedPostIds.add(postId);
530 * Removes the given post ID from the list of posts this Sone likes.
535 public void removeLikedPostId(@Nonnull String postId) {
536 likedPostIds.remove(postId);
540 * Returns the IDs of all liked replies.
542 * @return All liked replies’ IDs
545 public Set<String> getLikedReplyIds() {
546 return Collections.unmodifiableSet(likedReplyIds);
550 * Sets the IDs of all liked replies.
552 * @param likedReplyIds
553 * All liked replies’ IDs
554 * @return This Sone (for method chaining)
557 public Sone setLikeReplyIds(@Nonnull Set<String> likedReplyIds) {
558 this.likedReplyIds.clear();
559 this.likedReplyIds.addAll(likedReplyIds);
564 * Checks whether the given reply ID is liked by this Sone.
567 * The ID of the reply
568 * @return {@code true} if this Sone likes the given reply, {@code false}
571 public boolean isLikedReplyId(@Nonnull String replyId) {
572 return likedReplyIds.contains(replyId);
576 * Adds the given reply ID to the list of replies this Sone likes.
579 * The ID of the reply
580 * @return This Sone (for method chaining)
583 public Sone addLikedReplyId(@Nonnull String replyId) {
584 likedReplyIds.add(replyId);
589 * Removes the given post ID from the list of replies this Sone likes.
592 * The ID of the reply
594 public void removeLikedReplyId(@Nonnull String replyId) {
595 likedReplyIds.remove(replyId);
599 * Returns the root album that contains all visible albums of this Sone.
601 * @return The root album of this Sone
604 public Album getRootAlbum() {
609 * Returns Sone-specific options.
611 * @return The options of this Sone
614 public SoneOptions getOptions() {
619 * Sets the options of this Sone.
622 * The options of this Sone
624 /* TODO - remove this method again, maybe add an option provider */
625 public void setOptions(@Nonnull SoneOptions options) {
626 this.options = options;
630 // FINGERPRINTABLE METHODS
635 public synchronized String getFingerprint() {
636 Hasher hash = Hashing.sha256().newHasher();
637 hash.putString(profile.getFingerprint());
639 hash.putString("Posts(");
640 for (Post post : getPosts()) {
641 hash.putString("Post(").putString(post.getId()).putString(")");
645 List<PostReply> replies = new ArrayList<>(getReplies());
646 Collections.sort(replies, Reply.TIME_COMPARATOR);
647 hash.putString("Replies(");
648 for (PostReply reply : replies) {
649 hash.putString("Reply(").putString(reply.getId()).putString(")");
653 List<String> likedPostIds = new ArrayList<>(getLikedPostIds());
654 Collections.sort(likedPostIds);
655 hash.putString("LikedPosts(");
656 for (String likedPostId : likedPostIds) {
657 hash.putString("Post(").putString(likedPostId).putString(")");
661 List<String> likedReplyIds = new ArrayList<>(getLikedReplyIds());
662 Collections.sort(likedReplyIds);
663 hash.putString("LikedReplies(");
664 for (String likedReplyId : likedReplyIds) {
665 hash.putString("Reply(").putString(likedReplyId).putString(")");
669 hash.putString("Albums(");
670 for (Album album : rootAlbum.getAlbums()) {
671 if (!Album.NOT_EMPTY.apply(album)) {
674 hash.putString(album.getFingerprint());
678 return hash.hash().toString();
682 // INTERFACE Comparable<Sone>
687 public int compareTo(Sone sone) {
688 return NICE_NAME_COMPARATOR.compare(this, sone);
697 public int hashCode() {
698 return id.hashCode();
703 public boolean equals(Object object) {
704 if (!(object instanceof Sone)) {
707 return ((Sone) object).getId().equals(id);
712 public String toString() {
713 return getClass().getName() + "[identity=" + identity + ",posts(" + posts.size() + "),replies(" + replies.size() + "),albums(" + getRootAlbum().getAlbums().size() + ")]";