2 * Sone - SoneImpl.java - Copyright © 2010–2020 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;
24 import static net.pterodactylus.sone.data.SoneKt.*;
26 import java.net.MalformedURLException;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.Collections;
30 import java.util.List;
32 import java.util.concurrent.CopyOnWriteArraySet;
33 import java.util.logging.Level;
34 import java.util.logging.Logger;
36 import javax.annotation.Nonnull;
37 import javax.annotation.Nullable;
39 import net.pterodactylus.sone.data.Album;
40 import net.pterodactylus.sone.data.AlbumsKt;
41 import net.pterodactylus.sone.data.Client;
42 import net.pterodactylus.sone.data.Post;
43 import net.pterodactylus.sone.data.PostReply;
44 import net.pterodactylus.sone.data.Profile;
45 import net.pterodactylus.sone.data.Reply;
46 import net.pterodactylus.sone.data.Sone;
47 import net.pterodactylus.sone.data.SoneOptions;
48 import net.pterodactylus.sone.data.SoneOptions.DefaultSoneOptions;
49 import net.pterodactylus.sone.database.Database;
50 import net.pterodactylus.sone.freenet.wot.Identity;
52 import freenet.keys.FreenetURI;
54 import com.google.common.hash.Hasher;
55 import com.google.common.hash.Hashing;
58 * {@link Sone} implementation.
60 * Operations that modify the Sone need to synchronize on the Sone in question.
62 public class SoneImpl implements Sone {
65 private static final Logger logger = getLogger(SoneImpl.class.getName());
68 private final Database database;
70 /** The ID of this Sone. */
71 private final String id;
73 /** Whether the Sone is local. */
74 private final boolean local;
76 /** The identity of this Sone. */
77 private final Identity identity;
79 /** The latest edition of the Sone. */
80 private volatile long latestEdition;
82 /** The time of the last inserted update. */
83 private volatile long time;
85 /** The status of this Sone. */
86 private volatile SoneStatus status = SoneStatus.unknown;
88 /** The profile of this Sone. */
89 private volatile Profile profile = new Profile(this);
91 /** The client used by the Sone. */
92 private volatile Client client;
94 /** Whether this Sone is known. */
95 private volatile boolean known;
98 private final Set<Post> posts = new CopyOnWriteArraySet<>();
101 private final Set<PostReply> replies = new CopyOnWriteArraySet<>();
103 /** The IDs of all liked posts. */
104 private final Set<String> likedPostIds = new CopyOnWriteArraySet<>();
106 /** The IDs of all liked replies. */
107 private final Set<String> likedReplyIds = new CopyOnWriteArraySet<>();
109 /** The root album containing all albums. */
110 private final Album rootAlbum = new AlbumImpl(this);
112 /** Sone-specific options. */
113 private SoneOptions options = new DefaultSoneOptions();
116 * Creates a new Sone.
118 * @param database The database
120 * The identity of the Sone
122 * {@code true} if the Sone is a local Sone, {@code false} otherwise
124 public SoneImpl(Database database, Identity identity, boolean local) {
125 this.database = database;
126 this.id = identity.getId();
127 this.identity = identity;
136 * Returns the identity of this Sone.
138 * @return The identity of this Sone
141 public String getId() {
146 * Returns the identity of this Sone.
148 * @return The identity of this Sone
151 public Identity getIdentity() {
156 * Returns the name of this Sone.
158 * @return The name of this Sone
161 public String getName() {
162 return (identity != null) ? identity.getNickname() : null;
166 * Returns whether this Sone is a local Sone.
168 * @return {@code true} if this Sone is a local Sone, {@code false} otherwise
170 public boolean isLocal() {
175 * Returns the request URI of this Sone.
177 * @return The request URI of this Sone
180 public FreenetURI getRequestUri() {
182 return new FreenetURI(getIdentity().getRequestUri())
185 .setMetaString(new String[0])
186 .setSuggestedEdition(latestEdition);
187 } catch (MalformedURLException e) {
188 throw new IllegalStateException(
189 format("Identity %s's request URI is incorrect.",
195 * Returns the latest edition of this Sone.
197 * @return The latest edition of this Sone
199 public long getLatestEdition() {
200 return latestEdition;
204 * Sets the latest edition of this Sone. If the given latest edition is not
205 * greater than the current latest edition, the latest edition of this Sone is
208 * @param latestEdition
209 * The latest edition of this Sone
211 public void setLatestEdition(long latestEdition) {
212 if (!(latestEdition > this.latestEdition)) {
213 logger.log(Level.FINE, String.format("New latest edition %d is not greater than current latest edition %d!", latestEdition, this.latestEdition));
216 this.latestEdition = latestEdition;
220 * Return the time of the last inserted update of this Sone.
222 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
224 public long getTime() {
229 * Sets the time of the last inserted update of this Sone.
232 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
233 * @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
247 public SoneStatus getStatus() {
252 * Sets the new status of this Sone.
255 * The new status of this Sone
257 * @throws IllegalArgumentException
258 * if {@code status} is {@code null}
261 public Sone setStatus(@Nonnull SoneStatus status) {
262 this.status = checkNotNull(status, "status must not be null");
267 * Returns a copy of the profile. If you want to update values in the profile
268 * of this Sone, update the values in the returned {@link Profile} and use
269 * {@link #setProfile(Profile)} to change the profile in this Sone.
271 * @return A copy of the profile
274 public Profile getProfile() {
275 return new Profile(profile);
279 * Sets the profile of this Sone. A copy of the given profile is stored so that
280 * subsequent modifications of the given profile are not reflected in this
286 public void setProfile(@Nonnull Profile profile) {
287 this.profile = new Profile(profile);
291 * Returns the client used by this Sone.
293 * @return The client used by this Sone, or {@code null}
296 public Client getClient() {
301 * Sets the client used by this Sone.
304 * The client used by this Sone, or {@code null}
305 * @return This Sone (for method chaining)
308 public Sone setClient(@Nullable Client client) {
309 this.client = client;
314 * Returns whether this Sone is known.
316 * @return {@code true} if this Sone is known, {@code false} otherwise
318 public boolean isKnown() {
323 * Sets whether this Sone is known.
326 * {@code true} if this Sone is known, {@code false} otherwise
330 public Sone setKnown(boolean known) {
336 * Returns all friend Sones of this Sone.
338 * @return The friend Sones of this Sone
341 public Collection<String> getFriends() {
342 return database.getFriends(this);
346 * Returns whether this Sone has the given Sone as a friend Sone.
348 * @param friendSoneId
349 * The ID of the Sone to check for
350 * @return {@code true} if this Sone has the given Sone as a friend, {@code
353 public boolean hasFriend(@Nonnull String friendSoneId) {
354 return database.isFriend(this, friendSoneId);
358 * Returns the list of posts of this Sone, sorted by time, newest first.
360 * @return All posts of this Sone
363 public List<Post> getPosts() {
364 List<Post> sortedPosts;
365 synchronized (this) {
366 sortedPosts = new ArrayList<>(posts);
368 Collections.sort(sortedPosts, Post.NEWEST_FIRST);
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)
380 public Sone setPosts(@Nonnull Collection<Post> posts) {
381 synchronized (this) {
383 this.posts.addAll(posts);
389 * Adds the given post to this Sone. The post will not be added if its {@link
390 * Post#getSone() Sone} is not this Sone.
395 public void addPost(@Nonnull Post post) {
396 if (post.getSone().equals(this) && posts.add(post)) {
397 logger.log(Level.FINEST, String.format("Adding %s to “%s”.", post, getName()));
402 * Removes the given post from this Sone.
407 public void removePost(@Nonnull Post post) {
408 if (post.getSone().equals(this)) {
414 * Returns all replies this Sone made.
416 * @return All replies this Sone made
419 public Set<PostReply> getReplies() {
420 return Collections.unmodifiableSet(replies);
424 * Sets all replies of this Sone at once.
427 * The new (and only) replies of this Sone
428 * @return This Sone (for method chaining)
431 public Sone setReplies(@Nonnull Collection<PostReply> replies) {
432 this.replies.clear();
433 this.replies.addAll(replies);
438 * Adds a reply to this Sone. If the given reply was not made by this Sone,
439 * nothing is added to this Sone.
444 public void addReply(@Nonnull PostReply reply) {
445 if (reply.getSone().equals(this)) {
451 * Removes a reply from this Sone.
454 * The reply to remove
456 public void removeReply(@Nonnull PostReply reply) {
457 if (reply.getSone().equals(this)) {
458 replies.remove(reply);
463 * Returns the IDs of all liked posts.
465 * @return All liked posts’ IDs
468 public Set<String> getLikedPostIds() {
469 return Collections.unmodifiableSet(likedPostIds);
473 * Sets the IDs of all liked posts.
475 * @param likedPostIds
476 * All liked posts’ IDs
477 * @return This Sone (for method chaining)
480 public Sone setLikePostIds(@Nonnull Set<String> likedPostIds) {
481 this.likedPostIds.clear();
482 this.likedPostIds.addAll(likedPostIds);
487 * Checks whether the given post ID is liked by this Sone.
491 * @return {@code true} if this Sone likes the given post, {@code false}
494 public boolean isLikedPostId(@Nonnull String postId) {
495 return likedPostIds.contains(postId);
499 * Adds the given post ID to the list of posts this Sone likes.
503 * @return This Sone (for method chaining)
506 public Sone addLikedPostId(@Nonnull String postId) {
507 likedPostIds.add(postId);
512 * Removes the given post ID from the list of posts this Sone likes.
517 public void removeLikedPostId(@Nonnull String postId) {
518 likedPostIds.remove(postId);
522 * Returns the IDs of all liked replies.
524 * @return All liked replies’ IDs
527 public Set<String> getLikedReplyIds() {
528 return Collections.unmodifiableSet(likedReplyIds);
532 * Sets the IDs of all liked replies.
534 * @param likedReplyIds
535 * All liked replies’ IDs
536 * @return This Sone (for method chaining)
539 public Sone setLikeReplyIds(@Nonnull Set<String> likedReplyIds) {
540 this.likedReplyIds.clear();
541 this.likedReplyIds.addAll(likedReplyIds);
546 * Checks whether the given reply ID is liked by this Sone.
549 * The ID of the reply
550 * @return {@code true} if this Sone likes the given reply, {@code false}
553 public boolean isLikedReplyId(@Nonnull String replyId) {
554 return likedReplyIds.contains(replyId);
558 * Adds the given reply ID to the list of replies this Sone likes.
561 * The ID of the reply
562 * @return This Sone (for method chaining)
565 public Sone addLikedReplyId(@Nonnull String replyId) {
566 likedReplyIds.add(replyId);
571 * Removes the given post ID from the list of replies this Sone likes.
574 * The ID of the reply
576 public void removeLikedReplyId(@Nonnull String replyId) {
577 likedReplyIds.remove(replyId);
581 * Returns the root album that contains all visible albums of this Sone.
583 * @return The root album of this Sone
586 public Album getRootAlbum() {
591 * Returns Sone-specific options.
593 * @return The options of this Sone
596 public SoneOptions getOptions() {
601 * Sets the options of this Sone.
604 * The options of this Sone
606 /* TODO - remove this method again, maybe add an option provider */
607 public void setOptions(@Nonnull SoneOptions options) {
608 this.options = options;
612 // FINGERPRINTABLE METHODS
617 public synchronized String getFingerprint() {
618 Hasher hash = Hashing.sha256().newHasher();
619 hash.putString(profile.getFingerprint(), UTF_8);
621 hash.putString("Posts(", UTF_8);
622 for (Post post : getPosts()) {
623 hash.putString("Post(", UTF_8).putString(post.getId(), UTF_8).putString(")", UTF_8);
625 hash.putString(")", UTF_8);
627 List<PostReply> replies = new ArrayList<>(getReplies());
628 Collections.sort(replies, Reply.TIME_COMPARATOR);
629 hash.putString("Replies(", UTF_8);
630 for (PostReply reply : replies) {
631 hash.putString("Reply(", UTF_8).putString(reply.getId(), UTF_8).putString(")", UTF_8);
633 hash.putString(")", UTF_8);
635 List<String> likedPostIds = new ArrayList<>(getLikedPostIds());
636 Collections.sort(likedPostIds);
637 hash.putString("LikedPosts(", UTF_8);
638 for (String likedPostId : likedPostIds) {
639 hash.putString("Post(", UTF_8).putString(likedPostId, UTF_8).putString(")", UTF_8);
641 hash.putString(")", UTF_8);
643 List<String> likedReplyIds = new ArrayList<>(getLikedReplyIds());
644 Collections.sort(likedReplyIds);
645 hash.putString("LikedReplies(", UTF_8);
646 for (String likedReplyId : likedReplyIds) {
647 hash.putString("Reply(", UTF_8).putString(likedReplyId, UTF_8).putString(")", UTF_8);
649 hash.putString(")", UTF_8);
651 hash.putString("Albums(", UTF_8);
652 for (Album album : rootAlbum.getAlbums()) {
653 if (!AlbumsKt.notEmpty().invoke(album)) {
656 hash.putString(album.getFingerprint(), UTF_8);
658 hash.putString(")", UTF_8);
660 return hash.hash().toString();
664 // INTERFACE Comparable<Sone>
669 public int compareTo(Sone sone) {
670 return niceNameComparator().compare(this, sone);
679 public int hashCode() {
680 return id.hashCode();
685 public boolean equals(Object object) {
686 if (!(object instanceof Sone)) {
689 return ((Sone) object).getId().equals(id);
694 public String toString() {
695 return getClass().getName() + "[identity=" + identity + ",posts(" + posts.size() + "),replies(" + replies.size() + "),albums(" + getRootAlbum().getAlbums().size() + ")]";