2 * Sone - SoneImpl.java - Copyright © 2010–2016 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.util.logging.Logger.getLogger;
24 import java.net.MalformedURLException;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.List;
30 import java.util.concurrent.CopyOnWriteArraySet;
31 import java.util.logging.Level;
32 import java.util.logging.Logger;
34 import net.pterodactylus.sone.data.Album;
35 import net.pterodactylus.sone.data.Client;
36 import net.pterodactylus.sone.data.Post;
37 import net.pterodactylus.sone.data.PostReply;
38 import net.pterodactylus.sone.data.Profile;
39 import net.pterodactylus.sone.data.Reply;
40 import net.pterodactylus.sone.data.Sone;
41 import net.pterodactylus.sone.data.SoneOptions;
42 import net.pterodactylus.sone.data.SoneOptions.DefaultSoneOptions;
43 import net.pterodactylus.sone.database.Database;
44 import net.pterodactylus.sone.freenet.wot.Identity;
45 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
47 import freenet.keys.FreenetURI;
49 import com.google.common.hash.Hasher;
50 import com.google.common.hash.Hashing;
53 * {@link Sone} implementation.
55 * Operations that modify the Sone need to synchronize on the Sone in question.
57 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
59 public class SoneImpl implements Sone {
62 private static final Logger logger = getLogger(SoneImpl.class.getName());
65 private final Database database;
67 /** The ID of this Sone. */
68 private final String id;
70 /** Whether the Sone is local. */
71 private final boolean local;
73 /** The identity of this Sone. */
74 private final Identity identity;
76 /** The latest edition of the Sone. */
77 private volatile long latestEdition;
79 /** The time of the last inserted update. */
80 private volatile long time;
82 /** The status of this Sone. */
83 private volatile SoneStatus status = SoneStatus.unknown;
85 /** The profile of this Sone. */
86 private volatile Profile profile = new Profile(this);
88 /** The client used by the Sone. */
89 private volatile Client client;
91 /** Whether this Sone is known. */
92 private volatile boolean known;
95 private final Set<Post> posts = new CopyOnWriteArraySet<Post>();
98 private final Set<PostReply> replies = new CopyOnWriteArraySet<PostReply>();
100 /** The IDs of all liked posts. */
101 private final Set<String> likedPostIds = new CopyOnWriteArraySet<String>();
103 /** The IDs of all liked replies. */
104 private final Set<String> likedReplyIds = new CopyOnWriteArraySet<String>();
106 /** The root album containing all albums. */
107 private final Album rootAlbum = new AlbumImpl(this);
109 /** Sone-specific options. */
110 private SoneOptions options = new DefaultSoneOptions();
113 * Creates a new Sone.
115 * @param database The database
117 * The identity of the Sone
119 * {@code true} if the Sone is a local Sone, {@code false} otherwise
121 public SoneImpl(Database database, Identity identity, boolean local) {
122 this.database = database;
123 this.id = identity.getId();
124 this.identity = identity;
133 * Returns the identity of this Sone.
135 * @return The identity of this Sone
137 public String getId() {
142 * Returns the identity of this Sone.
144 * @return The identity of this Sone
146 public Identity getIdentity() {
151 * Returns the name of this Sone.
153 * @return The name of this Sone
155 public String getName() {
156 return (identity != null) ? identity.getNickname() : null;
160 * Returns whether this Sone is a local Sone.
162 * @return {@code true} if this Sone is a local Sone, {@code false} otherwise
164 public boolean isLocal() {
169 * Returns the request URI of this Sone.
171 * @return The request URI of this Sone
173 public FreenetURI getRequestUri() {
175 return new FreenetURI(getIdentity().getRequestUri())
178 .setMetaString(new String[0])
179 .setSuggestedEdition(latestEdition);
180 } catch (MalformedURLException e) {
181 throw new IllegalStateException(
182 format("Identity %s's request URI is incorrect.",
188 * Returns the insert URI of this Sone.
190 * @return The insert URI of this Sone
192 public FreenetURI getInsertUri() {
197 return new FreenetURI(((OwnIdentity) getIdentity()).getInsertUri())
199 .setMetaString(new String[0])
200 .setSuggestedEdition(latestEdition);
201 } catch (MalformedURLException e) {
202 throw new IllegalStateException(format("Own identity %s's insert URI is incorrect.", getIdentity()), e);
207 * Returns the latest edition of this Sone.
209 * @return The latest edition of this Sone
211 public long getLatestEdition() {
212 return latestEdition;
216 * Sets the latest edition of this Sone. If the given latest edition is not
217 * greater than the current latest edition, the latest edition of this Sone is
220 * @param latestEdition
221 * The latest edition of this Sone
223 public void setLatestEdition(long latestEdition) {
224 if (!(latestEdition > this.latestEdition)) {
225 logger.log(Level.FINE, String.format("New latest edition %d is not greater than current latest edition %d!", latestEdition, this.latestEdition));
228 this.latestEdition = latestEdition;
232 * Return the time of the last inserted update of this Sone.
234 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
236 public long getTime() {
241 * Sets the time of the last inserted update of this Sone.
244 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
245 * @return This Sone (for method chaining)
247 public Sone setTime(long time) {
253 * Returns the status of this Sone.
255 * @return The status of this Sone
257 public SoneStatus getStatus() {
262 * Sets the new status of this Sone.
265 * The new status of this Sone
267 * @throws IllegalArgumentException
268 * if {@code status} is {@code null}
270 public Sone setStatus(SoneStatus status) {
271 this.status = checkNotNull(status, "status must not be null");
276 * Returns a copy of the profile. If you want to update values in the profile
277 * of this Sone, update the values in the returned {@link Profile} and use
278 * {@link #setProfile(Profile)} to change the profile in this Sone.
280 * @return A copy of the profile
282 public Profile getProfile() {
283 return new Profile(profile);
287 * Sets the profile of this Sone. A copy of the given profile is stored so that
288 * subsequent modifications of the given profile are not reflected in this
294 public void setProfile(Profile profile) {
295 this.profile = new Profile(profile);
299 * Returns the client used by this Sone.
301 * @return The client used by this Sone, or {@code null}
303 public Client getClient() {
308 * Sets the client used by this Sone.
311 * The client used by this Sone, or {@code null}
312 * @return This Sone (for method chaining)
314 public Sone setClient(Client client) {
315 this.client = client;
320 * Returns whether this Sone is known.
322 * @return {@code true} if this Sone is known, {@code false} otherwise
324 public boolean isKnown() {
329 * Sets whether this Sone is known.
332 * {@code true} if this Sone is known, {@code false} otherwise
335 public Sone setKnown(boolean known) {
341 * Returns all friend Sones of this Sone.
343 * @return The friend Sones of this Sone
345 public Collection<String> getFriends() {
346 return database.getFriends(this);
350 * Returns whether this Sone has the given Sone as a friend Sone.
352 * @param friendSoneId
353 * The ID of the Sone to check for
354 * @return {@code true} if this Sone has the given Sone as a friend, {@code
357 public boolean hasFriend(String friendSoneId) {
358 return database.isFriend(this, friendSoneId);
362 * Returns the list of posts of this Sone, sorted by time, newest first.
364 * @return All posts of this Sone
366 public List<Post> getPosts() {
367 List<Post> sortedPosts;
368 synchronized (this) {
369 sortedPosts = new ArrayList<Post>(posts);
371 Collections.sort(sortedPosts, Post.NEWEST_FIRST);
376 * Sets all posts of this Sone at once.
379 * The new (and only) posts of this Sone
380 * @return This Sone (for method chaining)
382 public Sone setPosts(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(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(Post post) {
410 if (post.getSone().equals(this)) {
416 * Returns all replies this Sone made.
418 * @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)
431 public Sone setReplies(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(PostReply reply) {
445 if (reply.getSone().equals(this)) {
451 * Removes a reply from this Sone.
454 * The reply to remove
456 public void removeReply(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
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)
478 public Sone setLikePostIds(Set<String> likedPostIds) {
479 this.likedPostIds.clear();
480 this.likedPostIds.addAll(likedPostIds);
485 * Checks whether the given post ID is liked by this Sone.
489 * @return {@code true} if this Sone likes the given post, {@code false}
492 public boolean isLikedPostId(String postId) {
493 return likedPostIds.contains(postId);
497 * Adds the given post ID to the list of posts this Sone likes.
501 * @return This Sone (for method chaining)
503 public Sone addLikedPostId(String postId) {
504 likedPostIds.add(postId);
509 * Removes the given post ID from the list of posts this Sone likes.
513 * @return This Sone (for method chaining)
515 public Sone removeLikedPostId(String postId) {
516 likedPostIds.remove(postId);
521 * Returns the IDs of all liked replies.
523 * @return All liked replies’ IDs
525 public Set<String> getLikedReplyIds() {
526 return Collections.unmodifiableSet(likedReplyIds);
530 * Sets the IDs of all liked replies.
532 * @param likedReplyIds
533 * All liked replies’ IDs
534 * @return This Sone (for method chaining)
536 public Sone setLikeReplyIds(Set<String> likedReplyIds) {
537 this.likedReplyIds.clear();
538 this.likedReplyIds.addAll(likedReplyIds);
543 * Checks whether the given reply ID is liked by this Sone.
546 * The ID of the reply
547 * @return {@code true} if this Sone likes the given reply, {@code false}
550 public boolean isLikedReplyId(String replyId) {
551 return likedReplyIds.contains(replyId);
555 * Adds the given reply ID to the list of replies this Sone likes.
558 * The ID of the reply
559 * @return This Sone (for method chaining)
561 public Sone addLikedReplyId(String replyId) {
562 likedReplyIds.add(replyId);
567 * Removes the given post ID from the list of replies this Sone likes.
570 * The ID of the reply
571 * @return This Sone (for method chaining)
573 public Sone removeLikedReplyId(String replyId) {
574 likedReplyIds.remove(replyId);
579 * Returns the root album that contains all visible albums of this Sone.
581 * @return The root album of this Sone
583 public Album getRootAlbum() {
588 * Returns Sone-specific options.
590 * @return The options of this Sone
592 public SoneOptions getOptions() {
597 * Sets the options of this Sone.
600 * The options of this Sone
602 /* TODO - remove this method again, maybe add an option provider */
603 public void setOptions(SoneOptions options) {
604 this.options = options;
608 // FINGERPRINTABLE METHODS
613 public synchronized String getFingerprint() {
614 Hasher hash = Hashing.sha256().newHasher();
615 hash.putString(profile.getFingerprint());
617 hash.putString("Posts(");
618 for (Post post : getPosts()) {
619 hash.putString("Post(").putString(post.getId()).putString(")");
623 List<PostReply> replies = new ArrayList<PostReply>(getReplies());
624 Collections.sort(replies, Reply.TIME_COMPARATOR);
625 hash.putString("Replies(");
626 for (PostReply reply : replies) {
627 hash.putString("Reply(").putString(reply.getId()).putString(")");
631 List<String> likedPostIds = new ArrayList<String>(getLikedPostIds());
632 Collections.sort(likedPostIds);
633 hash.putString("LikedPosts(");
634 for (String likedPostId : likedPostIds) {
635 hash.putString("Post(").putString(likedPostId).putString(")");
639 List<String> likedReplyIds = new ArrayList<String>(getLikedReplyIds());
640 Collections.sort(likedReplyIds);
641 hash.putString("LikedReplies(");
642 for (String likedReplyId : likedReplyIds) {
643 hash.putString("Reply(").putString(likedReplyId).putString(")");
647 hash.putString("Albums(");
648 for (Album album : rootAlbum.getAlbums()) {
649 if (!Album.NOT_EMPTY.apply(album)) {
652 hash.putString(album.getFingerprint());
656 return hash.hash().toString();
660 // INTERFACE Comparable<Sone>
665 public int compareTo(Sone sone) {
666 return NICE_NAME_COMPARATOR.compare(this, sone);
675 public int hashCode() {
676 return id.hashCode();
681 public boolean equals(Object object) {
682 if (!(object instanceof Sone)) {
685 return ((Sone) object).getId().equals(id);
690 public String toString() {
691 return getClass().getName() + "[identity=" + identity + ",posts(" + posts.size() + "),replies(" + replies.size() + "),albums(" + getRootAlbum().getAlbums().size() + ")]";