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.PostKt.newestPostFirst;
25 import static net.pterodactylus.sone.data.ReplyKt.newestReplyFirst;
26 import static net.pterodactylus.sone.data.SoneKt.*;
28 import java.net.MalformedURLException;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.List;
34 import java.util.concurrent.CopyOnWriteArraySet;
35 import java.util.logging.Level;
36 import java.util.logging.Logger;
38 import javax.annotation.Nonnull;
39 import javax.annotation.Nullable;
41 import net.pterodactylus.sone.data.Album;
42 import net.pterodactylus.sone.data.AlbumKt;
43 import net.pterodactylus.sone.data.Client;
44 import net.pterodactylus.sone.data.Post;
45 import net.pterodactylus.sone.data.PostReply;
46 import net.pterodactylus.sone.data.Profile;
47 import net.pterodactylus.sone.data.Sone;
48 import net.pterodactylus.sone.data.SoneOptions;
49 import net.pterodactylus.sone.data.SoneOptions.DefaultSoneOptions;
50 import net.pterodactylus.sone.database.Database;
51 import net.pterodactylus.sone.freenet.wot.Identity;
53 import freenet.keys.FreenetURI;
55 import com.google.common.hash.Hasher;
56 import com.google.common.hash.Hashing;
59 * {@link Sone} implementation.
61 * Operations that modify the Sone need to synchronize on the Sone in question.
63 public class SoneImpl implements Sone {
66 private static final Logger logger = getLogger(SoneImpl.class.getName());
69 private final Database database;
71 /** The ID of this Sone. */
72 private final String id;
74 /** Whether the Sone is local. */
75 private final boolean local;
77 /** The identity of this Sone. */
78 private final Identity identity;
80 /** The latest edition of the Sone. */
81 private volatile long latestEdition;
83 /** The time of the last inserted update. */
84 private volatile long time;
86 /** The status of this Sone. */
87 private volatile SoneStatus status = SoneStatus.unknown;
89 /** The profile of this Sone. */
90 private volatile Profile profile = new Profile(this);
92 /** The client used by the Sone. */
93 private volatile Client client;
95 /** Whether this Sone is known. */
96 private volatile boolean known;
99 private final Set<Post> posts = new CopyOnWriteArraySet<>();
102 private final Set<PostReply> replies = new CopyOnWriteArraySet<>();
104 /** The IDs of all liked posts. */
105 private final Set<String> likedPostIds = new CopyOnWriteArraySet<>();
107 /** The IDs of all liked replies. */
108 private final Set<String> likedReplyIds = new CopyOnWriteArraySet<>();
110 /** The root album containing all albums. */
111 private final Album rootAlbum = new AlbumImpl(this);
113 /** Sone-specific options. */
114 private SoneOptions options = new DefaultSoneOptions();
117 * Creates a new Sone.
119 * @param database The database
121 * The identity of the Sone
123 * {@code true} if the Sone is a local Sone, {@code false} otherwise
125 public SoneImpl(Database database, Identity identity, boolean local) {
126 this.database = database;
127 this.id = identity.getId();
128 this.identity = identity;
137 * Returns the identity of this Sone.
139 * @return The identity of this Sone
142 public String getId() {
147 * Returns the identity of this Sone.
149 * @return The identity of this Sone
152 public Identity getIdentity() {
157 * Returns the name of this Sone.
159 * @return The name of this Sone
162 public String getName() {
163 return (identity != null) ? identity.getNickname() : null;
167 * Returns whether this Sone is a local Sone.
169 * @return {@code true} if this Sone is a local Sone, {@code false} otherwise
171 public boolean isLocal() {
176 * Returns the request URI of this Sone.
178 * @return The request URI of this Sone
181 public FreenetURI getRequestUri() {
183 return new FreenetURI(getIdentity().getRequestUri())
186 .setMetaString(new String[0])
187 .setSuggestedEdition(latestEdition);
188 } catch (MalformedURLException e) {
189 throw new IllegalStateException(
190 format("Identity %s's request URI is incorrect.",
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)
237 public Sone setTime(long time) {
243 * Returns the status of this Sone.
245 * @return The status of this Sone
248 public SoneStatus getStatus() {
253 * Sets the new status of this Sone.
256 * The new status of this Sone
258 * @throws IllegalArgumentException
259 * if {@code status} is {@code null}
262 public Sone setStatus(@Nonnull SoneStatus status) {
263 this.status = checkNotNull(status, "status must not be null");
268 * Returns a copy of the profile. If you want to update values in the profile
269 * of this Sone, update the values in the returned {@link Profile} and use
270 * {@link #setProfile(Profile)} to change the profile in this Sone.
272 * @return A copy of the profile
275 public Profile getProfile() {
276 return new Profile(profile);
280 * Sets the profile of this Sone. A copy of the given profile is stored so that
281 * subsequent modifications of the given profile are not reflected in this
287 public void setProfile(@Nonnull Profile profile) {
288 this.profile = new Profile(profile);
292 * Returns the client used by this Sone.
294 * @return The client used by this Sone, or {@code null}
297 public Client getClient() {
302 * Sets the client used by this Sone.
305 * The client used by this Sone, or {@code null}
306 * @return This Sone (for method chaining)
309 public Sone setClient(@Nullable Client client) {
310 this.client = client;
315 * Returns whether this Sone is known.
317 * @return {@code true} if this Sone is known, {@code false} otherwise
319 public boolean isKnown() {
324 * Sets whether this Sone is known.
327 * {@code true} if this Sone is known, {@code false} otherwise
331 public Sone setKnown(boolean known) {
337 * Returns all friend Sones of this Sone.
339 * @return The friend Sones of this Sone
342 public Collection<String> getFriends() {
343 return database.getFriends(this);
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(@Nonnull String friendSoneId) {
355 return database.isFriend(this, friendSoneId);
359 * Returns the list of posts of this Sone, sorted by time, newest first.
361 * @return All posts of this Sone
364 public List<Post> getPosts() {
365 List<Post> sortedPosts;
366 synchronized (this) {
367 sortedPosts = new ArrayList<>(posts);
369 sortedPosts.sort(newestPostFirst());
374 * Sets all posts of this Sone at once.
377 * The new (and only) posts of this Sone
378 * @return This Sone (for method chaining)
381 public Sone setPosts(@Nonnull Collection<Post> posts) {
382 synchronized (this) {
384 this.posts.addAll(posts);
390 * Adds the given post to this Sone. The post will not be added if its {@link
391 * Post#getSone() Sone} is not this Sone.
396 public void addPost(@Nonnull Post post) {
397 if (post.getSone().equals(this) && posts.add(post)) {
398 logger.log(Level.FINEST, String.format("Adding %s to “%s”.", post, getName()));
403 * Removes the given post from this Sone.
408 public void removePost(@Nonnull Post post) {
409 if (post.getSone().equals(this)) {
415 * Returns all replies this Sone made.
417 * @return All replies this Sone made
420 public Set<PostReply> getReplies() {
421 return Collections.unmodifiableSet(replies);
425 * Sets all replies of this Sone at once.
428 * The new (and only) replies of this Sone
429 * @return This Sone (for method chaining)
432 public Sone setReplies(@Nonnull Collection<PostReply> replies) {
433 this.replies.clear();
434 this.replies.addAll(replies);
439 * Adds a reply to this Sone. If the given reply was not made by this Sone,
440 * nothing is added to this Sone.
445 public void addReply(@Nonnull PostReply reply) {
446 if (reply.getSone().equals(this)) {
452 * Removes a reply from this Sone.
455 * The reply to remove
457 public void removeReply(@Nonnull PostReply reply) {
458 if (reply.getSone().equals(this)) {
459 replies.remove(reply);
464 * Returns the IDs of all liked posts.
466 * @return All liked posts’ IDs
469 public Set<String> getLikedPostIds() {
470 return Collections.unmodifiableSet(likedPostIds);
474 * Sets the IDs of all liked posts.
476 * @param likedPostIds
477 * All liked posts’ IDs
478 * @return This Sone (for method chaining)
481 public Sone setLikePostIds(@Nonnull Set<String> likedPostIds) {
482 this.likedPostIds.clear();
483 this.likedPostIds.addAll(likedPostIds);
488 * Checks whether the given post ID is liked by this Sone.
492 * @return {@code true} if this Sone likes the given post, {@code false}
495 public boolean isLikedPostId(@Nonnull String postId) {
496 return likedPostIds.contains(postId);
500 * Adds the given post ID to the list of posts this Sone likes.
504 * @return This Sone (for method chaining)
507 public Sone addLikedPostId(@Nonnull String postId) {
508 likedPostIds.add(postId);
513 * Removes the given post ID from the list of posts this Sone likes.
518 public void removeLikedPostId(@Nonnull String postId) {
519 likedPostIds.remove(postId);
523 * Returns the IDs of all liked replies.
525 * @return All liked replies’ IDs
528 public Set<String> getLikedReplyIds() {
529 return Collections.unmodifiableSet(likedReplyIds);
533 * Sets the IDs of all liked replies.
535 * @param likedReplyIds
536 * All liked replies’ IDs
537 * @return This Sone (for method chaining)
540 public Sone setLikeReplyIds(@Nonnull Set<String> likedReplyIds) {
541 this.likedReplyIds.clear();
542 this.likedReplyIds.addAll(likedReplyIds);
547 * Checks whether the given reply ID is liked by this Sone.
550 * The ID of the reply
551 * @return {@code true} if this Sone likes the given reply, {@code false}
554 public boolean isLikedReplyId(@Nonnull String replyId) {
555 return likedReplyIds.contains(replyId);
559 * Adds the given reply ID to the list of replies this Sone likes.
562 * The ID of the reply
563 * @return This Sone (for method chaining)
566 public Sone addLikedReplyId(@Nonnull String replyId) {
567 likedReplyIds.add(replyId);
572 * Removes the given post ID from the list of replies this Sone likes.
575 * The ID of the reply
577 public void removeLikedReplyId(@Nonnull String replyId) {
578 likedReplyIds.remove(replyId);
582 * Returns the root album that contains all visible albums of this Sone.
584 * @return The root album of this Sone
587 public Album getRootAlbum() {
592 * Returns Sone-specific options.
594 * @return The options of this Sone
597 public SoneOptions getOptions() {
602 * Sets the options of this Sone.
605 * The options of this Sone
607 /* TODO - remove this method again, maybe add an option provider */
608 public void setOptions(@Nonnull SoneOptions options) {
609 this.options = options;
613 // FINGERPRINTABLE METHODS
618 public synchronized String getFingerprint() {
619 Hasher hash = Hashing.sha256().newHasher();
620 hash.putString(profile.getFingerprint(), UTF_8);
622 hash.putString("Posts(", UTF_8);
623 for (Post post : getPosts()) {
624 hash.putString("Post(", UTF_8).putString(post.getId(), UTF_8).putString(")", UTF_8);
626 hash.putString(")", UTF_8);
628 List<PostReply> replies = new ArrayList<>(getReplies());
629 replies.sort(newestReplyFirst().reversed());
630 hash.putString("Replies(", UTF_8);
631 for (PostReply reply : replies) {
632 hash.putString("Reply(", UTF_8).putString(reply.getId(), UTF_8).putString(")", UTF_8);
634 hash.putString(")", UTF_8);
636 List<String> likedPostIds = new ArrayList<>(getLikedPostIds());
637 Collections.sort(likedPostIds);
638 hash.putString("LikedPosts(", UTF_8);
639 for (String likedPostId : likedPostIds) {
640 hash.putString("Post(", UTF_8).putString(likedPostId, UTF_8).putString(")", UTF_8);
642 hash.putString(")", UTF_8);
644 List<String> likedReplyIds = new ArrayList<>(getLikedReplyIds());
645 Collections.sort(likedReplyIds);
646 hash.putString("LikedReplies(", UTF_8);
647 for (String likedReplyId : likedReplyIds) {
648 hash.putString("Reply(", UTF_8).putString(likedReplyId, UTF_8).putString(")", UTF_8);
650 hash.putString(")", UTF_8);
652 hash.putString("Albums(", UTF_8);
653 for (Album album : rootAlbum.getAlbums()) {
654 if (!AlbumKt.notEmpty().invoke(album)) {
657 hash.putString(album.getFingerprint(), UTF_8);
659 hash.putString(")", UTF_8);
661 return hash.hash().toString();
665 // INTERFACE Comparable<Sone>
670 public int compareTo(Sone sone) {
671 return niceNameComparator().compare(this, sone);
680 public int hashCode() {
681 return id.hashCode();
686 public boolean equals(Object object) {
687 if (!(object instanceof Sone)) {
690 return ((Sone) object).getId().equals(id);
695 public String toString() {
696 return getClass().getName() + "[identity=" + identity + ",posts(" + posts.size() + "),replies(" + replies.size() + "),albums(" + getRootAlbum().getAlbums().size() + ")]";