2 * Sone - SoneImpl.java - Copyright © 2010–2013 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.LocalSone;
37 import net.pterodactylus.sone.data.Post;
38 import net.pterodactylus.sone.data.PostReply;
39 import net.pterodactylus.sone.data.Profile;
40 import net.pterodactylus.sone.data.Reply;
41 import net.pterodactylus.sone.data.Sone;
42 import net.pterodactylus.sone.data.SoneOptions;
43 import net.pterodactylus.sone.data.SoneOptions.DefaultSoneOptions;
44 import net.pterodactylus.sone.database.Database;
45 import net.pterodactylus.sone.freenet.wot.Identity;
46 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
48 import freenet.keys.FreenetURI;
50 import com.google.common.hash.Hasher;
51 import com.google.common.hash.Hashing;
54 * {@link Sone} implementation.
56 * Operations that modify the Sone need to synchronize on the Sone in question.
58 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
60 public class SoneImpl implements LocalSone {
63 private static final Logger logger = getLogger("Sone.Data");
66 private final Database database;
68 /** The ID of this Sone. */
69 private final String id;
71 /** Whether the Sone is local. */
72 private final boolean local;
74 /** The identity of this Sone. */
75 private final Identity identity;
77 /** The latest edition of the Sone. */
78 private volatile long latestEdition;
80 /** The time of the last inserted update. */
81 private volatile long time;
83 /** The status of this Sone. */
84 private volatile SoneStatus status = SoneStatus.unknown;
86 /** The profile of this Sone. */
87 private volatile Profile profile = new Profile(this);
89 /** The client used by the Sone. */
90 private volatile Client client;
92 /** Whether this Sone is known. */
93 private volatile boolean known;
96 private final Set<Post> posts = new CopyOnWriteArraySet<Post>();
99 private final Set<PostReply> replies = new CopyOnWriteArraySet<PostReply>();
101 /** The IDs of all liked posts. */
102 private final Set<String> likedPostIds = new CopyOnWriteArraySet<String>();
104 /** The IDs of all liked replies. */
105 private final Set<String> likedReplyIds = new CopyOnWriteArraySet<String>();
107 /** The root album containing all albums. */
108 private final Album rootAlbum = new AlbumImpl(this);
110 /** Sone-specific options. */
111 private SoneOptions options = new DefaultSoneOptions();
114 * Creates a new Sone.
116 * @param database The database
118 * The identity of the Sone
120 * {@code true} if the Sone is a local Sone, {@code false} otherwise
122 public SoneImpl(Database database, Identity identity, boolean local) {
123 this.database = database;
124 this.id = identity.getId();
125 this.identity = identity;
134 * Returns the identity of this Sone.
136 * @return The identity of this Sone
138 public String getId() {
143 * Returns the identity of this Sone.
145 * @return The identity of this Sone
147 public Identity getIdentity() {
152 * Returns the name of this Sone.
154 * @return The name of this Sone
156 public String getName() {
157 return (identity != null) ? identity.getNickname() : null;
161 * Returns whether this Sone is a local Sone.
163 * @return {@code true} if this Sone is a local Sone, {@code false} otherwise
165 public boolean isLocal() {
170 * Returns the request URI of this Sone.
172 * @return The request URI of this Sone
174 public FreenetURI getRequestUri() {
176 return new FreenetURI(getIdentity().getRequestUri())
179 .setMetaString(new String[0])
180 .setSuggestedEdition(latestEdition);
181 } catch (MalformedURLException e) {
182 throw new IllegalStateException(
183 format("Identity %s's request URI is incorrect.",
189 * Returns the insert URI of this Sone.
191 * @return The insert URI of this Sone
193 public FreenetURI getInsertUri() {
198 return new FreenetURI(((OwnIdentity) getIdentity()).getInsertUri())
200 .setMetaString(new String[0])
201 .setSuggestedEdition(latestEdition);
202 } catch (MalformedURLException e) {
203 throw new IllegalStateException(format("Own identity %s's insert URI is incorrect.", getIdentity()), e);
208 * Returns the latest edition of this Sone.
210 * @return The latest edition of this Sone
212 public long getLatestEdition() {
213 return latestEdition;
217 * Sets the latest edition of this Sone. If the given latest edition is not
218 * greater than the current latest edition, the latest edition of this Sone is
221 * @param latestEdition
222 * The latest edition of this Sone
224 public void setLatestEdition(long latestEdition) {
225 if (!(latestEdition > this.latestEdition)) {
226 logger.log(Level.FINE, String.format("New latest edition %d is not greater than current latest edition %d!", latestEdition, this.latestEdition));
229 this.latestEdition = latestEdition;
233 * Return the time of the last inserted update of this Sone.
235 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
237 public long getTime() {
242 * Sets the time of the last inserted update of this Sone.
245 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
246 * @return This Sone (for method chaining)
248 public Sone setTime(long time) {
254 * Returns the status of this Sone.
256 * @return The status of this Sone
258 public SoneStatus getStatus() {
263 * Sets the new status of this Sone.
266 * The new status of this Sone
268 * @throws IllegalArgumentException
269 * if {@code status} is {@code null}
271 public Sone setStatus(SoneStatus status) {
272 this.status = checkNotNull(status, "status must not be null");
277 * Returns a copy of the profile. If you want to update values in the profile
278 * of this Sone, update the values in the returned {@link Profile} and use
279 * {@link #setProfile(Profile)} to change the profile in this Sone.
281 * @return A copy of the profile
283 public Profile getProfile() {
284 return new Profile(profile);
288 * Sets the profile of this Sone. A copy of the given profile is stored so that
289 * subsequent modifications of the given profile are not reflected in this
295 public void setProfile(Profile profile) {
296 this.profile = new Profile(profile);
300 * Returns the client used by this Sone.
302 * @return The client used by this Sone, or {@code null}
304 public Client getClient() {
309 * Sets the client used by this Sone.
312 * The client used by this Sone, or {@code null}
313 * @return This Sone (for method chaining)
315 public Sone setClient(Client client) {
316 this.client = client;
321 * Returns whether this Sone is known.
323 * @return {@code true} if this Sone is known, {@code false} otherwise
325 public boolean isKnown() {
330 * Sets whether this Sone is known.
333 * {@code true} if this Sone is known, {@code false} otherwise
336 public Sone setKnown(boolean known) {
342 * Returns all friend Sones of this Sone.
344 * @return The friend Sones of this Sone
346 public Collection<String> getFriends() {
347 return database.getFriends(this);
351 * Returns whether this Sone has the given Sone as a friend Sone.
353 * @param friendSoneId
354 * The ID of the Sone to check for
355 * @return {@code true} if this Sone has the given Sone as a friend, {@code
358 public boolean hasFriend(String friendSoneId) {
359 return database.isFriend(this, friendSoneId);
363 * Returns the list of posts of this Sone, sorted by time, newest first.
365 * @return All posts of this Sone
367 public List<Post> getPosts() {
368 List<Post> sortedPosts;
369 synchronized (this) {
370 sortedPosts = new ArrayList<Post>(posts);
372 Collections.sort(sortedPosts, Post.TIME_COMPARATOR);
377 * Sets all posts of this Sone at once.
380 * The new (and only) posts of this Sone
381 * @return This Sone (for method chaining)
383 public Sone setPosts(Collection<Post> posts) {
384 synchronized (this) {
386 this.posts.addAll(posts);
392 * Adds the given post to this Sone. The post will not be added if its {@link
393 * Post#getSone() Sone} is not this Sone.
398 public void addPost(Post post) {
399 if (post.getSone().equals(this) && posts.add(post)) {
400 logger.log(Level.FINEST, String.format("Adding %s to “%s”.", post, getName()));
405 * Removes the given post from this Sone.
410 public void removePost(Post post) {
411 if (post.getSone().equals(this)) {
417 * Returns all replies this Sone made.
419 * @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)
432 public Sone setReplies(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(PostReply reply) {
446 if (reply.getSone().equals(this)) {
452 * Removes a reply from this Sone.
455 * The reply to remove
457 public void removeReply(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
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)
479 public Sone setLikePostIds(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(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)
504 public Sone addLikedPostId(String postId) {
505 likedPostIds.add(postId);
510 * Removes the given post ID from the list of posts this Sone likes.
514 * @return This Sone (for method chaining)
516 public Sone removeLikedPostId(String postId) {
517 likedPostIds.remove(postId);
522 * Returns the IDs of all liked replies.
524 * @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)
537 public Sone setLikeReplyIds(Set<String> likedReplyIds) {
538 this.likedReplyIds.clear();
539 this.likedReplyIds.addAll(likedReplyIds);
544 * Checks whether the given reply ID is liked by this Sone.
547 * The ID of the reply
548 * @return {@code true} if this Sone likes the given reply, {@code false}
551 public boolean isLikedReplyId(String replyId) {
552 return likedReplyIds.contains(replyId);
556 * Adds the given reply ID to the list of replies this Sone likes.
559 * The ID of the reply
560 * @return This Sone (for method chaining)
562 public Sone addLikedReplyId(String replyId) {
563 likedReplyIds.add(replyId);
568 * Removes the given post ID from the list of replies this Sone likes.
571 * The ID of the reply
572 * @return This Sone (for method chaining)
574 public Sone removeLikedReplyId(String replyId) {
575 likedReplyIds.remove(replyId);
580 * Returns the root album that contains all visible albums of this Sone.
582 * @return The root album of this Sone
584 public Album getRootAlbum() {
589 * Returns Sone-specific options.
591 * @return The options of this Sone
593 public SoneOptions getOptions() {
598 * Sets the options of this Sone.
601 * The options of this Sone
603 /* TODO - remove this method again, maybe add an option provider */
604 public void setOptions(SoneOptions options) {
605 this.options = options;
609 // FINGERPRINTABLE METHODS
614 public synchronized String getFingerprint() {
615 Hasher hash = Hashing.sha256().newHasher();
616 hash.putString(profile.getFingerprint());
618 hash.putString("Posts(");
619 for (Post post : getPosts()) {
620 hash.putString("Post(").putString(post.getId()).putString(")");
624 List<PostReply> replies = new ArrayList<PostReply>(getReplies());
625 Collections.sort(replies, Reply.TIME_COMPARATOR);
626 hash.putString("Replies(");
627 for (PostReply reply : replies) {
628 hash.putString("Reply(").putString(reply.getId()).putString(")");
632 List<String> likedPostIds = new ArrayList<String>(getLikedPostIds());
633 Collections.sort(likedPostIds);
634 hash.putString("LikedPosts(");
635 for (String likedPostId : likedPostIds) {
636 hash.putString("Post(").putString(likedPostId).putString(")");
640 List<String> likedReplyIds = new ArrayList<String>(getLikedReplyIds());
641 Collections.sort(likedReplyIds);
642 hash.putString("LikedReplies(");
643 for (String likedReplyId : likedReplyIds) {
644 hash.putString("Reply(").putString(likedReplyId).putString(")");
648 hash.putString("Albums(");
649 for (Album album : rootAlbum.getAlbums()) {
650 if (!Album.NOT_EMPTY.apply(album)) {
653 hash.putString(album.getFingerprint());
657 return hash.hash().toString();
661 // INTERFACE Comparable<Sone>
666 public int compareTo(Sone sone) {
667 return NICE_NAME_COMPARATOR.compare(this, sone);
676 public int hashCode() {
677 return id.hashCode();
682 public boolean equals(Object object) {
683 if (!(object instanceof Sone)) {
686 return ((Sone) object).getId().equals(id);
691 public String toString() {
692 return getClass().getName() + "[identity=" + identity + ",posts(" + posts.size() + "),replies(" + replies.size() + "),albums(" + getRootAlbum().getAlbums().size() + ")]";