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.Reply;
48 import net.pterodactylus.sone.data.Sone;
49 import net.pterodactylus.sone.data.SoneOptions;
50 import net.pterodactylus.sone.data.SoneOptions.DefaultSoneOptions;
51 import net.pterodactylus.sone.database.Database;
52 import net.pterodactylus.sone.freenet.wot.Identity;
54 import freenet.keys.FreenetURI;
56 import com.google.common.hash.Hasher;
57 import com.google.common.hash.Hashing;
60 * {@link Sone} implementation.
62 * Operations that modify the Sone need to synchronize on the Sone in question.
64 public class SoneImpl implements Sone {
67 private static final Logger logger = getLogger(SoneImpl.class.getName());
70 private final Database database;
72 /** The ID of this Sone. */
73 private final String id;
75 /** Whether the Sone is local. */
76 private final boolean local;
78 /** The identity of this Sone. */
79 private final Identity identity;
81 /** The latest edition of the Sone. */
82 private volatile long latestEdition;
84 /** The time of the last inserted update. */
85 private volatile long time;
87 /** The status of this Sone. */
88 private volatile SoneStatus status = SoneStatus.unknown;
90 /** The profile of this Sone. */
91 private volatile Profile profile = new Profile(this);
93 /** The client used by the Sone. */
94 private volatile Client client;
96 /** Whether this Sone is known. */
97 private volatile boolean known;
100 private final Set<Post> posts = new CopyOnWriteArraySet<>();
103 private final Set<PostReply> replies = new CopyOnWriteArraySet<>();
105 /** The IDs of all liked posts. */
106 private final Set<String> likedPostIds = new CopyOnWriteArraySet<>();
108 /** The IDs of all liked replies. */
109 private final Set<String> likedReplyIds = new CopyOnWriteArraySet<>();
111 /** The root album containing all albums. */
112 private final Album rootAlbum = new AlbumImpl(this);
114 /** Sone-specific options. */
115 private SoneOptions options = new DefaultSoneOptions();
118 * Creates a new Sone.
120 * @param database The database
122 * The identity of the Sone
124 * {@code true} if the Sone is a local Sone, {@code false} otherwise
126 public SoneImpl(Database database, Identity identity, boolean local) {
127 this.database = database;
128 this.id = identity.getId();
129 this.identity = identity;
138 * Returns the identity of this Sone.
140 * @return The identity of this Sone
143 public String getId() {
148 * Returns the identity of this Sone.
150 * @return The identity of this Sone
153 public Identity getIdentity() {
158 * Returns the name of this Sone.
160 * @return The name of this Sone
163 public String getName() {
164 return (identity != null) ? identity.getNickname() : null;
168 * Returns whether this Sone is a local Sone.
170 * @return {@code true} if this Sone is a local Sone, {@code false} otherwise
172 public boolean isLocal() {
177 * Returns the request URI of this Sone.
179 * @return The request URI of this Sone
182 public FreenetURI getRequestUri() {
184 return new FreenetURI(getIdentity().getRequestUri())
187 .setMetaString(new String[0])
188 .setSuggestedEdition(latestEdition);
189 } catch (MalformedURLException e) {
190 throw new IllegalStateException(
191 format("Identity %s's request URI is incorrect.",
197 * Returns the latest edition of this Sone.
199 * @return The latest edition of this Sone
201 public long getLatestEdition() {
202 return latestEdition;
206 * Sets the latest edition of this Sone. If the given latest edition is not
207 * greater than the current latest edition, the latest edition of this Sone is
210 * @param latestEdition
211 * The latest edition of this Sone
213 public void setLatestEdition(long latestEdition) {
214 if (!(latestEdition > this.latestEdition)) {
215 logger.log(Level.FINE, String.format("New latest edition %d is not greater than current latest edition %d!", latestEdition, this.latestEdition));
218 this.latestEdition = latestEdition;
222 * Return the time of the last inserted update of this Sone.
224 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
226 public long getTime() {
231 * Sets the time of the last inserted update of this Sone.
234 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
235 * @return This Sone (for method chaining)
238 public Sone setTime(long time) {
244 * Returns the status of this Sone.
246 * @return The status of this Sone
249 public SoneStatus getStatus() {
254 * Sets the new status of this Sone.
257 * The new status of this Sone
259 * @throws IllegalArgumentException
260 * if {@code status} is {@code null}
263 public Sone setStatus(@Nonnull SoneStatus status) {
264 this.status = checkNotNull(status, "status must not be null");
269 * Returns a copy of the profile. If you want to update values in the profile
270 * of this Sone, update the values in the returned {@link Profile} and use
271 * {@link #setProfile(Profile)} to change the profile in this Sone.
273 * @return A copy of the profile
276 public Profile getProfile() {
277 return new Profile(profile);
281 * Sets the profile of this Sone. A copy of the given profile is stored so that
282 * subsequent modifications of the given profile are not reflected in this
288 public void setProfile(@Nonnull Profile profile) {
289 this.profile = new Profile(profile);
293 * Returns the client used by this Sone.
295 * @return The client used by this Sone, or {@code null}
298 public Client getClient() {
303 * Sets the client used by this Sone.
306 * The client used by this Sone, or {@code null}
307 * @return This Sone (for method chaining)
310 public Sone setClient(@Nullable Client client) {
311 this.client = client;
316 * Returns whether this Sone is known.
318 * @return {@code true} if this Sone is known, {@code false} otherwise
320 public boolean isKnown() {
325 * Sets whether this Sone is known.
328 * {@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
343 public Collection<String> getFriends() {
344 return database.getFriends(this);
348 * Returns whether this Sone has the given Sone as a friend Sone.
350 * @param friendSoneId
351 * The ID of the Sone to check for
352 * @return {@code true} if this Sone has the given Sone as a friend, {@code
355 public boolean hasFriend(@Nonnull String friendSoneId) {
356 return database.isFriend(this, friendSoneId);
360 * Returns the list of posts of this Sone, sorted by time, newest first.
362 * @return All posts of this Sone
365 public List<Post> getPosts() {
366 List<Post> sortedPosts;
367 synchronized (this) {
368 sortedPosts = new ArrayList<>(posts);
370 sortedPosts.sort(newestPostFirst());
375 * Sets all posts of this Sone at once.
378 * The new (and only) posts of this Sone
379 * @return This Sone (for method chaining)
382 public Sone setPosts(@Nonnull Collection<Post> posts) {
383 synchronized (this) {
385 this.posts.addAll(posts);
391 * Adds the given post to this Sone. The post will not be added if its {@link
392 * Post#getSone() Sone} is not this Sone.
397 public void addPost(@Nonnull Post post) {
398 if (post.getSone().equals(this) && posts.add(post)) {
399 logger.log(Level.FINEST, String.format("Adding %s to “%s”.", post, getName()));
404 * Removes the given post from this Sone.
409 public void removePost(@Nonnull Post post) {
410 if (post.getSone().equals(this)) {
416 * Returns all replies this Sone made.
418 * @return All replies this Sone made
421 public Set<PostReply> getReplies() {
422 return Collections.unmodifiableSet(replies);
426 * Sets all replies of this Sone at once.
429 * The new (and only) replies of this Sone
430 * @return This Sone (for method chaining)
433 public Sone setReplies(@Nonnull Collection<PostReply> replies) {
434 this.replies.clear();
435 this.replies.addAll(replies);
440 * Adds a reply to this Sone. If the given reply was not made by this Sone,
441 * nothing is added to this Sone.
446 public void addReply(@Nonnull PostReply reply) {
447 if (reply.getSone().equals(this)) {
453 * Removes a reply from this Sone.
456 * The reply to remove
458 public void removeReply(@Nonnull PostReply reply) {
459 if (reply.getSone().equals(this)) {
460 replies.remove(reply);
465 * Returns the IDs of all liked posts.
467 * @return All liked posts’ IDs
470 public Set<String> getLikedPostIds() {
471 return Collections.unmodifiableSet(likedPostIds);
475 * Sets the IDs of all liked posts.
477 * @param likedPostIds
478 * All liked posts’ IDs
479 * @return This Sone (for method chaining)
482 public Sone setLikePostIds(@Nonnull Set<String> likedPostIds) {
483 this.likedPostIds.clear();
484 this.likedPostIds.addAll(likedPostIds);
489 * Checks whether the given post ID is liked by this Sone.
493 * @return {@code true} if this Sone likes the given post, {@code false}
496 public boolean isLikedPostId(@Nonnull String postId) {
497 return likedPostIds.contains(postId);
501 * Adds the given post ID to the list of posts this Sone likes.
505 * @return This Sone (for method chaining)
508 public Sone addLikedPostId(@Nonnull String postId) {
509 likedPostIds.add(postId);
514 * Removes the given post ID from the list of posts this Sone likes.
519 public void removeLikedPostId(@Nonnull String postId) {
520 likedPostIds.remove(postId);
524 * Returns the IDs of all liked replies.
526 * @return All liked replies’ IDs
529 public Set<String> getLikedReplyIds() {
530 return Collections.unmodifiableSet(likedReplyIds);
534 * Sets the IDs of all liked replies.
536 * @param likedReplyIds
537 * All liked replies’ IDs
538 * @return This Sone (for method chaining)
541 public Sone setLikeReplyIds(@Nonnull Set<String> likedReplyIds) {
542 this.likedReplyIds.clear();
543 this.likedReplyIds.addAll(likedReplyIds);
548 * Checks whether the given reply ID is liked by this Sone.
551 * The ID of the reply
552 * @return {@code true} if this Sone likes the given reply, {@code false}
555 public boolean isLikedReplyId(@Nonnull String replyId) {
556 return likedReplyIds.contains(replyId);
560 * Adds the given reply ID to the list of replies this Sone likes.
563 * The ID of the reply
564 * @return This Sone (for method chaining)
567 public Sone addLikedReplyId(@Nonnull String replyId) {
568 likedReplyIds.add(replyId);
573 * Removes the given post ID from the list of replies this Sone likes.
576 * The ID of the reply
578 public void removeLikedReplyId(@Nonnull String replyId) {
579 likedReplyIds.remove(replyId);
583 * Returns the root album that contains all visible albums of this Sone.
585 * @return The root album of this Sone
588 public Album getRootAlbum() {
593 * Returns Sone-specific options.
595 * @return The options of this Sone
598 public SoneOptions getOptions() {
603 * Sets the options of this Sone.
606 * The options of this Sone
608 /* TODO - remove this method again, maybe add an option provider */
609 public void setOptions(@Nonnull SoneOptions options) {
610 this.options = options;
614 // FINGERPRINTABLE METHODS
619 public synchronized String getFingerprint() {
620 Hasher hash = Hashing.sha256().newHasher();
621 hash.putString(profile.getFingerprint(), UTF_8);
623 hash.putString("Posts(", UTF_8);
624 for (Post post : getPosts()) {
625 hash.putString("Post(", UTF_8).putString(post.getId(), UTF_8).putString(")", UTF_8);
627 hash.putString(")", UTF_8);
629 List<PostReply> replies = new ArrayList<>(getReplies());
630 replies.sort(newestReplyFirst().reversed());
631 hash.putString("Replies(", UTF_8);
632 for (PostReply reply : replies) {
633 hash.putString("Reply(", UTF_8).putString(reply.getId(), UTF_8).putString(")", UTF_8);
635 hash.putString(")", UTF_8);
637 List<String> likedPostIds = new ArrayList<>(getLikedPostIds());
638 Collections.sort(likedPostIds);
639 hash.putString("LikedPosts(", UTF_8);
640 for (String likedPostId : likedPostIds) {
641 hash.putString("Post(", UTF_8).putString(likedPostId, UTF_8).putString(")", UTF_8);
643 hash.putString(")", UTF_8);
645 List<String> likedReplyIds = new ArrayList<>(getLikedReplyIds());
646 Collections.sort(likedReplyIds);
647 hash.putString("LikedReplies(", UTF_8);
648 for (String likedReplyId : likedReplyIds) {
649 hash.putString("Reply(", UTF_8).putString(likedReplyId, UTF_8).putString(")", UTF_8);
651 hash.putString(")", UTF_8);
653 hash.putString("Albums(", UTF_8);
654 for (Album album : rootAlbum.getAlbums()) {
655 if (!AlbumKt.notEmpty().invoke(album)) {
658 hash.putString(album.getFingerprint(), UTF_8);
660 hash.putString(")", UTF_8);
662 return hash.hash().toString();
666 // INTERFACE Comparable<Sone>
671 public int compareTo(Sone sone) {
672 return niceNameComparator().compare(this, sone);
681 public int hashCode() {
682 return id.hashCode();
687 public boolean equals(Object object) {
688 if (!(object instanceof Sone)) {
691 return ((Sone) object).getId().equals(id);
696 public String toString() {
697 return getClass().getName() + "[identity=" + identity + ",posts(" + posts.size() + "),replies(" + replies.size() + "),albums(" + getRootAlbum().getAlbums().size() + ")]";