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.Client;
41 import net.pterodactylus.sone.data.Post;
42 import net.pterodactylus.sone.data.PostReply;
43 import net.pterodactylus.sone.data.Profile;
44 import net.pterodactylus.sone.data.Reply;
45 import net.pterodactylus.sone.data.Sone;
46 import net.pterodactylus.sone.data.SoneOptions;
47 import net.pterodactylus.sone.data.SoneOptions.DefaultSoneOptions;
48 import net.pterodactylus.sone.database.Database;
49 import net.pterodactylus.sone.freenet.wot.Identity;
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 latest edition of this Sone.
196 * @return The latest edition of this Sone
198 public long getLatestEdition() {
199 return latestEdition;
203 * Sets the latest edition of this Sone. If the given latest edition is not
204 * greater than the current latest edition, the latest edition of this Sone is
207 * @param latestEdition
208 * The latest edition of this Sone
210 public void setLatestEdition(long latestEdition) {
211 if (!(latestEdition > this.latestEdition)) {
212 logger.log(Level.FINE, String.format("New latest edition %d is not greater than current latest edition %d!", latestEdition, this.latestEdition));
215 this.latestEdition = latestEdition;
219 * Return the time of the last inserted update of this Sone.
221 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
223 public long getTime() {
228 * Sets the time of the last inserted update of this Sone.
231 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
232 * @return This Sone (for method chaining)
235 public Sone setTime(long time) {
241 * Returns the status of this Sone.
243 * @return The status of this Sone
246 public SoneStatus getStatus() {
251 * Sets the new status of this Sone.
254 * The new status of this Sone
256 * @throws IllegalArgumentException
257 * if {@code status} is {@code null}
260 public Sone setStatus(@Nonnull SoneStatus status) {
261 this.status = checkNotNull(status, "status must not be null");
266 * Returns a copy of the profile. If you want to update values in the profile
267 * of this Sone, update the values in the returned {@link Profile} and use
268 * {@link #setProfile(Profile)} to change the profile in this Sone.
270 * @return A copy of the profile
273 public Profile getProfile() {
274 return new Profile(profile);
278 * Sets the profile of this Sone. A copy of the given profile is stored so that
279 * subsequent modifications of the given profile are not reflected in this
285 public void setProfile(@Nonnull Profile profile) {
286 this.profile = new Profile(profile);
290 * Returns the client used by this Sone.
292 * @return The client used by this Sone, or {@code null}
295 public Client getClient() {
300 * Sets the client used by this Sone.
303 * The client used by this Sone, or {@code null}
304 * @return This Sone (for method chaining)
307 public Sone setClient(@Nullable Client client) {
308 this.client = client;
313 * Returns whether this Sone is known.
315 * @return {@code true} if this Sone is known, {@code false} otherwise
317 public boolean isKnown() {
322 * Sets whether this Sone is known.
325 * {@code true} if this Sone is known, {@code false} otherwise
329 public Sone setKnown(boolean known) {
335 * Returns all friend Sones of this Sone.
337 * @return The friend Sones of this Sone
340 public Collection<String> getFriends() {
341 return database.getFriends(this);
345 * Returns whether this Sone has the given Sone as a friend Sone.
347 * @param friendSoneId
348 * The ID of the Sone to check for
349 * @return {@code true} if this Sone has the given Sone as a friend, {@code
352 public boolean hasFriend(@Nonnull String friendSoneId) {
353 return database.isFriend(this, friendSoneId);
357 * Returns the list of posts of this Sone, sorted by time, newest first.
359 * @return All posts of this Sone
362 public List<Post> getPosts() {
363 List<Post> sortedPosts;
364 synchronized (this) {
365 sortedPosts = new ArrayList<>(posts);
367 Collections.sort(sortedPosts, Post.NEWEST_FIRST);
372 * Sets all posts of this Sone at once.
375 * The new (and only) posts of this Sone
376 * @return This Sone (for method chaining)
379 public Sone setPosts(@Nonnull Collection<Post> posts) {
380 synchronized (this) {
382 this.posts.addAll(posts);
388 * Adds the given post to this Sone. The post will not be added if its {@link
389 * Post#getSone() Sone} is not this Sone.
394 public void addPost(@Nonnull Post post) {
395 if (post.getSone().equals(this) && posts.add(post)) {
396 logger.log(Level.FINEST, String.format("Adding %s to “%s”.", post, getName()));
401 * Removes the given post from this Sone.
406 public void removePost(@Nonnull Post post) {
407 if (post.getSone().equals(this)) {
413 * Returns all replies this Sone made.
415 * @return All replies this Sone made
418 public Set<PostReply> getReplies() {
419 return Collections.unmodifiableSet(replies);
423 * Sets all replies of this Sone at once.
426 * The new (and only) replies of this Sone
427 * @return This Sone (for method chaining)
430 public Sone setReplies(@Nonnull Collection<PostReply> replies) {
431 this.replies.clear();
432 this.replies.addAll(replies);
437 * Adds a reply to this Sone. If the given reply was not made by this Sone,
438 * nothing is added to this Sone.
443 public void addReply(@Nonnull PostReply reply) {
444 if (reply.getSone().equals(this)) {
450 * Removes a reply from this Sone.
453 * The reply to remove
455 public void removeReply(@Nonnull PostReply reply) {
456 if (reply.getSone().equals(this)) {
457 replies.remove(reply);
462 * Returns the IDs of all liked posts.
464 * @return All liked posts’ IDs
467 public Set<String> getLikedPostIds() {
468 return Collections.unmodifiableSet(likedPostIds);
472 * Sets the IDs of all liked posts.
474 * @param likedPostIds
475 * All liked posts’ IDs
476 * @return This Sone (for method chaining)
479 public Sone setLikePostIds(@Nonnull Set<String> likedPostIds) {
480 this.likedPostIds.clear();
481 this.likedPostIds.addAll(likedPostIds);
486 * Checks whether the given post ID is liked by this Sone.
490 * @return {@code true} if this Sone likes the given post, {@code false}
493 public boolean isLikedPostId(@Nonnull String postId) {
494 return likedPostIds.contains(postId);
498 * Adds the given post ID to the list of posts this Sone likes.
502 * @return This Sone (for method chaining)
505 public Sone addLikedPostId(@Nonnull String postId) {
506 likedPostIds.add(postId);
511 * Removes the given post ID from the list of posts this Sone likes.
516 public void removeLikedPostId(@Nonnull String postId) {
517 likedPostIds.remove(postId);
521 * Returns the IDs of all liked replies.
523 * @return All liked replies’ IDs
526 public Set<String> getLikedReplyIds() {
527 return Collections.unmodifiableSet(likedReplyIds);
531 * Sets the IDs of all liked replies.
533 * @param likedReplyIds
534 * All liked replies’ IDs
535 * @return This Sone (for method chaining)
538 public Sone setLikeReplyIds(@Nonnull Set<String> likedReplyIds) {
539 this.likedReplyIds.clear();
540 this.likedReplyIds.addAll(likedReplyIds);
545 * Checks whether the given reply ID is liked by this Sone.
548 * The ID of the reply
549 * @return {@code true} if this Sone likes the given reply, {@code false}
552 public boolean isLikedReplyId(@Nonnull String replyId) {
553 return likedReplyIds.contains(replyId);
557 * Adds the given reply ID to the list of replies this Sone likes.
560 * The ID of the reply
561 * @return This Sone (for method chaining)
564 public Sone addLikedReplyId(@Nonnull String replyId) {
565 likedReplyIds.add(replyId);
570 * Removes the given post ID from the list of replies this Sone likes.
573 * The ID of the reply
575 public void removeLikedReplyId(@Nonnull String replyId) {
576 likedReplyIds.remove(replyId);
580 * Returns the root album that contains all visible albums of this Sone.
582 * @return The root album of this Sone
585 public Album getRootAlbum() {
590 * Returns Sone-specific options.
592 * @return The options of this Sone
595 public SoneOptions getOptions() {
600 * Sets the options of this Sone.
603 * The options of this Sone
605 /* TODO - remove this method again, maybe add an option provider */
606 public void setOptions(@Nonnull SoneOptions options) {
607 this.options = options;
611 // FINGERPRINTABLE METHODS
616 public synchronized String getFingerprint() {
617 Hasher hash = Hashing.sha256().newHasher();
618 hash.putString(profile.getFingerprint(), UTF_8);
620 hash.putString("Posts(", UTF_8);
621 for (Post post : getPosts()) {
622 hash.putString("Post(", UTF_8).putString(post.getId(), UTF_8).putString(")", UTF_8);
624 hash.putString(")", UTF_8);
626 List<PostReply> replies = new ArrayList<>(getReplies());
627 Collections.sort(replies, Reply.TIME_COMPARATOR);
628 hash.putString("Replies(", UTF_8);
629 for (PostReply reply : replies) {
630 hash.putString("Reply(", UTF_8).putString(reply.getId(), UTF_8).putString(")", UTF_8);
632 hash.putString(")", UTF_8);
634 List<String> likedPostIds = new ArrayList<>(getLikedPostIds());
635 Collections.sort(likedPostIds);
636 hash.putString("LikedPosts(", UTF_8);
637 for (String likedPostId : likedPostIds) {
638 hash.putString("Post(", UTF_8).putString(likedPostId, UTF_8).putString(")", UTF_8);
640 hash.putString(")", UTF_8);
642 List<String> likedReplyIds = new ArrayList<>(getLikedReplyIds());
643 Collections.sort(likedReplyIds);
644 hash.putString("LikedReplies(", UTF_8);
645 for (String likedReplyId : likedReplyIds) {
646 hash.putString("Reply(", UTF_8).putString(likedReplyId, UTF_8).putString(")", UTF_8);
648 hash.putString(")", UTF_8);
650 hash.putString("Albums(", UTF_8);
651 for (Album album : rootAlbum.getAlbums()) {
652 if (!Album.NOT_EMPTY.apply(album)) {
655 hash.putString(album.getFingerprint(), UTF_8);
657 hash.putString(")", UTF_8);
659 return hash.hash().toString();
663 // INTERFACE Comparable<Sone>
668 public int compareTo(Sone sone) {
669 return niceNameComparator().compare(this, sone);
678 public int hashCode() {
679 return id.hashCode();
684 public boolean equals(Object object) {
685 if (!(object instanceof Sone)) {
688 return ((Sone) object).getId().equals(id);
693 public String toString() {
694 return getClass().getName() + "[identity=" + identity + ",posts(" + posts.size() + "),replies(" + replies.size() + "),albums(" + getRootAlbum().getAlbums().size() + ")]";