2 * Sone - SoneImpl.java - Copyright © 2010–2019 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.nio.charset.StandardCharsets.UTF_8;
23 import static java.util.logging.Logger.getLogger;
25 import java.net.MalformedURLException;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.List;
31 import java.util.concurrent.CopyOnWriteArraySet;
32 import java.util.logging.Level;
33 import java.util.logging.Logger;
35 import javax.annotation.Nonnull;
36 import javax.annotation.Nullable;
38 import net.pterodactylus.sone.data.Album;
39 import net.pterodactylus.sone.data.Client;
40 import net.pterodactylus.sone.data.Post;
41 import net.pterodactylus.sone.data.PostReply;
42 import net.pterodactylus.sone.data.Profile;
43 import net.pterodactylus.sone.data.Reply;
44 import net.pterodactylus.sone.data.Sone;
45 import net.pterodactylus.sone.data.SoneOptions;
46 import net.pterodactylus.sone.data.SoneOptions.DefaultSoneOptions;
47 import net.pterodactylus.sone.database.Database;
48 import net.pterodactylus.sone.freenet.wot.Identity;
49 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
51 import freenet.keys.FreenetURI;
53 import com.google.common.hash.Hasher;
54 import com.google.common.hash.Hashing;
57 * {@link Sone} implementation.
59 * Operations that modify the Sone need to synchronize on the Sone in question.
61 public class SoneImpl implements Sone {
64 private static final Logger logger = getLogger(SoneImpl.class.getName());
67 private final Database database;
69 /** The ID of this Sone. */
70 private final String id;
72 /** Whether the Sone is local. */
73 private final boolean local;
75 /** The identity of this Sone. */
76 private final Identity identity;
78 /** The latest edition of the Sone. */
79 private volatile long latestEdition;
81 /** The time of the last inserted update. */
82 private volatile long time;
84 /** The status of this Sone. */
85 private volatile SoneStatus status = SoneStatus.unknown;
87 /** The profile of this Sone. */
88 private volatile Profile profile = new Profile(this);
90 /** The client used by the Sone. */
91 private volatile Client client;
93 /** Whether this Sone is known. */
94 private volatile boolean known;
97 private final Set<Post> posts = new CopyOnWriteArraySet<>();
100 private final Set<PostReply> replies = new CopyOnWriteArraySet<>();
102 /** The IDs of all liked posts. */
103 private final Set<String> likedPostIds = new CopyOnWriteArraySet<>();
105 /** The IDs of all liked replies. */
106 private final Set<String> likedReplyIds = new CopyOnWriteArraySet<>();
108 /** The root album containing all albums. */
109 private final Album rootAlbum = new AlbumImpl(this);
111 /** Sone-specific options. */
112 private SoneOptions options = new DefaultSoneOptions();
115 * Creates a new Sone.
117 * @param database The database
119 * The identity of the Sone
121 * {@code true} if the Sone is a local Sone, {@code false} otherwise
123 public SoneImpl(Database database, Identity identity, boolean local) {
124 this.database = database;
125 this.id = identity.getId();
126 this.identity = identity;
135 * Returns the identity of this Sone.
137 * @return The identity of this Sone
140 public String getId() {
145 * Returns the identity of this Sone.
147 * @return The identity of this Sone
150 public Identity getIdentity() {
155 * Returns the name of this Sone.
157 * @return The name of this Sone
160 public String getName() {
161 return (identity != null) ? identity.getNickname() : null;
165 * Returns whether this Sone is a local Sone.
167 * @return {@code true} if this Sone is a local Sone, {@code false} otherwise
169 public boolean isLocal() {
174 * Returns the request URI of this Sone.
176 * @return The request URI of this Sone
179 public FreenetURI getRequestUri() {
181 return new FreenetURI(getIdentity().getRequestUri())
184 .setMetaString(new String[0])
185 .setSuggestedEdition(latestEdition);
186 } catch (MalformedURLException e) {
187 throw new IllegalStateException(
188 format("Identity %s's request URI is incorrect.",
194 * Returns the insert URI of this Sone.
196 * @return The insert URI of this Sone
199 public FreenetURI getInsertUri() {
204 return new FreenetURI(((OwnIdentity) getIdentity()).getInsertUri())
206 .setMetaString(new String[0])
207 .setSuggestedEdition(latestEdition);
208 } catch (MalformedURLException e) {
209 throw new IllegalStateException(format("Own identity %s's insert URI is incorrect.", getIdentity()), e);
214 * Returns the latest edition of this Sone.
216 * @return The latest edition of this Sone
218 public long getLatestEdition() {
219 return latestEdition;
223 * Sets the latest edition of this Sone. If the given latest edition is not
224 * greater than the current latest edition, the latest edition of this Sone is
227 * @param latestEdition
228 * The latest edition of this Sone
230 public void setLatestEdition(long latestEdition) {
231 if (!(latestEdition > this.latestEdition)) {
232 logger.log(Level.FINE, String.format("New latest edition %d is not greater than current latest edition %d!", latestEdition, this.latestEdition));
235 this.latestEdition = latestEdition;
239 * Return the time of the last inserted update of this Sone.
241 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
243 public long getTime() {
248 * Sets the time of the last inserted update of this Sone.
251 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
252 * @return This Sone (for method chaining)
255 public Sone setTime(long time) {
261 * Returns the status of this Sone.
263 * @return The status of this Sone
266 public SoneStatus getStatus() {
271 * Sets the new status of this Sone.
274 * The new status of this Sone
276 * @throws IllegalArgumentException
277 * if {@code status} is {@code null}
280 public Sone setStatus(@Nonnull SoneStatus status) {
281 this.status = checkNotNull(status, "status must not be null");
286 * Returns a copy of the profile. If you want to update values in the profile
287 * of this Sone, update the values in the returned {@link Profile} and use
288 * {@link #setProfile(Profile)} to change the profile in this Sone.
290 * @return A copy of the profile
293 public Profile getProfile() {
294 return new Profile(profile);
298 * Sets the profile of this Sone. A copy of the given profile is stored so that
299 * subsequent modifications of the given profile are not reflected in this
305 public void setProfile(@Nonnull Profile profile) {
306 this.profile = new Profile(profile);
310 * Returns the client used by this Sone.
312 * @return The client used by this Sone, or {@code null}
315 public Client getClient() {
320 * Sets the client used by this Sone.
323 * The client used by this Sone, or {@code null}
324 * @return This Sone (for method chaining)
327 public Sone setClient(@Nullable Client client) {
328 this.client = client;
333 * Returns whether this Sone is known.
335 * @return {@code true} if this Sone is known, {@code false} otherwise
337 public boolean isKnown() {
342 * Sets whether this Sone is known.
345 * {@code true} if this Sone is known, {@code false} otherwise
349 public Sone setKnown(boolean known) {
355 * Returns all friend Sones of this Sone.
357 * @return The friend Sones of this Sone
360 public Collection<String> getFriends() {
361 return database.getFriends(this);
365 * Returns whether this Sone has the given Sone as a friend Sone.
367 * @param friendSoneId
368 * The ID of the Sone to check for
369 * @return {@code true} if this Sone has the given Sone as a friend, {@code
372 public boolean hasFriend(@Nonnull String friendSoneId) {
373 return database.isFriend(this, friendSoneId);
377 * Returns the list of posts of this Sone, sorted by time, newest first.
379 * @return All posts of this Sone
382 public List<Post> getPosts() {
383 List<Post> sortedPosts;
384 synchronized (this) {
385 sortedPosts = new ArrayList<>(posts);
387 Collections.sort(sortedPosts, Post.NEWEST_FIRST);
392 * Sets all posts of this Sone at once.
395 * The new (and only) posts of this Sone
396 * @return This Sone (for method chaining)
399 public Sone setPosts(@Nonnull Collection<Post> posts) {
400 synchronized (this) {
402 this.posts.addAll(posts);
408 * Adds the given post to this Sone. The post will not be added if its {@link
409 * Post#getSone() Sone} is not this Sone.
414 public void addPost(@Nonnull Post post) {
415 if (post.getSone().equals(this) && posts.add(post)) {
416 logger.log(Level.FINEST, String.format("Adding %s to “%s”.", post, getName()));
421 * Removes the given post from this Sone.
426 public void removePost(@Nonnull Post post) {
427 if (post.getSone().equals(this)) {
433 * Returns all replies this Sone made.
435 * @return All replies this Sone made
438 public Set<PostReply> getReplies() {
439 return Collections.unmodifiableSet(replies);
443 * Sets all replies of this Sone at once.
446 * The new (and only) replies of this Sone
447 * @return This Sone (for method chaining)
450 public Sone setReplies(@Nonnull Collection<PostReply> replies) {
451 this.replies.clear();
452 this.replies.addAll(replies);
457 * Adds a reply to this Sone. If the given reply was not made by this Sone,
458 * nothing is added to this Sone.
463 public void addReply(@Nonnull PostReply reply) {
464 if (reply.getSone().equals(this)) {
470 * Removes a reply from this Sone.
473 * The reply to remove
475 public void removeReply(@Nonnull PostReply reply) {
476 if (reply.getSone().equals(this)) {
477 replies.remove(reply);
482 * Returns the IDs of all liked posts.
484 * @return All liked posts’ IDs
487 public Set<String> getLikedPostIds() {
488 return Collections.unmodifiableSet(likedPostIds);
492 * Sets the IDs of all liked posts.
494 * @param likedPostIds
495 * All liked posts’ IDs
496 * @return This Sone (for method chaining)
499 public Sone setLikePostIds(@Nonnull Set<String> likedPostIds) {
500 this.likedPostIds.clear();
501 this.likedPostIds.addAll(likedPostIds);
506 * Checks whether the given post ID is liked by this Sone.
510 * @return {@code true} if this Sone likes the given post, {@code false}
513 public boolean isLikedPostId(@Nonnull String postId) {
514 return likedPostIds.contains(postId);
518 * Adds the given post ID to the list of posts this Sone likes.
522 * @return This Sone (for method chaining)
525 public Sone addLikedPostId(@Nonnull String postId) {
526 likedPostIds.add(postId);
531 * Removes the given post ID from the list of posts this Sone likes.
536 public void removeLikedPostId(@Nonnull String postId) {
537 likedPostIds.remove(postId);
541 * Returns the IDs of all liked replies.
543 * @return All liked replies’ IDs
546 public Set<String> getLikedReplyIds() {
547 return Collections.unmodifiableSet(likedReplyIds);
551 * Sets the IDs of all liked replies.
553 * @param likedReplyIds
554 * All liked replies’ IDs
555 * @return This Sone (for method chaining)
558 public Sone setLikeReplyIds(@Nonnull Set<String> likedReplyIds) {
559 this.likedReplyIds.clear();
560 this.likedReplyIds.addAll(likedReplyIds);
565 * Checks whether the given reply ID is liked by this Sone.
568 * The ID of the reply
569 * @return {@code true} if this Sone likes the given reply, {@code false}
572 public boolean isLikedReplyId(@Nonnull String replyId) {
573 return likedReplyIds.contains(replyId);
577 * Adds the given reply ID to the list of replies this Sone likes.
580 * The ID of the reply
581 * @return This Sone (for method chaining)
584 public Sone addLikedReplyId(@Nonnull 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
595 public void removeLikedReplyId(@Nonnull String replyId) {
596 likedReplyIds.remove(replyId);
600 * Returns the root album that contains all visible albums of this Sone.
602 * @return The root album of this Sone
605 public Album getRootAlbum() {
610 * Returns Sone-specific options.
612 * @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(@Nonnull 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(), UTF_8);
640 hash.putString("Posts(", UTF_8);
641 for (Post post : getPosts()) {
642 hash.putString("Post(", UTF_8).putString(post.getId(), UTF_8).putString(")", UTF_8);
644 hash.putString(")", UTF_8);
646 List<PostReply> replies = new ArrayList<>(getReplies());
647 Collections.sort(replies, Reply.TIME_COMPARATOR);
648 hash.putString("Replies(", UTF_8);
649 for (PostReply reply : replies) {
650 hash.putString("Reply(", UTF_8).putString(reply.getId(), UTF_8).putString(")", UTF_8);
652 hash.putString(")", UTF_8);
654 List<String> likedPostIds = new ArrayList<>(getLikedPostIds());
655 Collections.sort(likedPostIds);
656 hash.putString("LikedPosts(", UTF_8);
657 for (String likedPostId : likedPostIds) {
658 hash.putString("Post(", UTF_8).putString(likedPostId, UTF_8).putString(")", UTF_8);
660 hash.putString(")", UTF_8);
662 List<String> likedReplyIds = new ArrayList<>(getLikedReplyIds());
663 Collections.sort(likedReplyIds);
664 hash.putString("LikedReplies(", UTF_8);
665 for (String likedReplyId : likedReplyIds) {
666 hash.putString("Reply(", UTF_8).putString(likedReplyId, UTF_8).putString(")", UTF_8);
668 hash.putString(")", UTF_8);
670 hash.putString("Albums(", UTF_8);
671 for (Album album : rootAlbum.getAlbums()) {
672 if (!Album.NOT_EMPTY.apply(album)) {
675 hash.putString(album.getFingerprint(), UTF_8);
677 hash.putString(")", UTF_8);
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 + ",posts(" + posts.size() + "),replies(" + replies.size() + "),albums(" + getRootAlbum().getAlbums().size() + ")]";