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.collect.FluentIterable;
51 import com.google.common.hash.Hasher;
52 import com.google.common.hash.Hashing;
55 * {@link Sone} implementation.
57 * Operations that modify the Sone need to synchronize on the Sone in question.
59 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
61 public class SoneImpl implements LocalSone {
64 private static final Logger logger = getLogger("Sone.Data");
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 final 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 final Client client;
93 /** Whether this Sone is known. */
94 private volatile boolean known;
97 private final Set<Post> posts = new CopyOnWriteArraySet<Post>();
100 private final Set<PostReply> replies = new CopyOnWriteArraySet<PostReply>();
102 /** The IDs of all liked posts. */
103 private final Set<String> likedPostIds = new CopyOnWriteArraySet<String>();
105 /** The IDs of all liked replies. */
106 private final Set<String> likedReplyIds = new CopyOnWriteArraySet<String>();
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, long time, Client client) {
124 this.database = database;
125 this.id = identity.getId();
126 this.identity = identity;
129 this.client = client;
137 * Returns the identity of this Sone.
139 * @return The identity of this Sone
141 public String getId() {
146 * Returns the identity of this Sone.
148 * @return The identity of this Sone
150 public Identity getIdentity() {
155 * Returns the name of this Sone.
157 * @return The name of this Sone
159 public String getName() {
160 return (identity != null) ? identity.getNickname() : null;
164 * Returns whether this Sone is a local Sone.
166 * @return {@code true} if this Sone is a local Sone, {@code false} otherwise
168 public boolean isLocal() {
173 * Returns the request URI of this Sone.
175 * @return The request URI of this Sone
177 public FreenetURI getRequestUri() {
179 return new FreenetURI(getIdentity().getRequestUri())
182 .setMetaString(new String[0])
183 .setSuggestedEdition(latestEdition);
184 } catch (MalformedURLException e) {
185 throw new IllegalStateException(
186 format("Identity %s's request URI is incorrect.",
192 * Returns the insert URI of this Sone.
194 * @return The insert URI of this Sone
196 public FreenetURI getInsertUri() {
201 return new FreenetURI(((OwnIdentity) getIdentity()).getInsertUri())
203 .setMetaString(new String[0])
204 .setSuggestedEdition(latestEdition);
205 } catch (MalformedURLException e) {
206 throw new IllegalStateException(format("Own identity %s's insert URI is incorrect.", getIdentity()), e);
211 * Returns the latest edition of this Sone.
213 * @return The latest edition of this Sone
215 public long getLatestEdition() {
216 return latestEdition;
220 * Sets the latest edition of this Sone. If the given latest edition is not
221 * greater than the current latest edition, the latest edition of this Sone is
224 * @param latestEdition
225 * The latest edition of this Sone
227 public void setLatestEdition(long latestEdition) {
228 if (!(latestEdition > this.latestEdition)) {
229 logger.log(Level.FINE, String.format("New latest edition %d is not greater than current latest edition %d!", latestEdition, this.latestEdition));
232 this.latestEdition = latestEdition;
236 * Return the time of the last inserted update of this Sone.
238 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
240 public long getTime() {
245 * Returns the status of this Sone.
247 * @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}
262 public Sone setStatus(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
274 public Profile getProfile() {
275 return new Profile(profile);
279 * Sets the profile of this Sone. A copy of the given profile is stored so that
280 * subsequent modifications of the given profile are not reflected in this
286 public void setProfile(Profile profile) {
287 this.profile = new Profile(profile);
291 * Returns the client used by this Sone.
293 * @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)
306 public Sone setClient(Client client) {
311 * Returns whether this Sone is known.
313 * @return {@code true} if this Sone is known, {@code false} otherwise
315 public boolean isKnown() {
320 * Sets whether this Sone is known.
323 * {@code true} if this Sone is known, {@code false} otherwise
326 public Sone setKnown(boolean known) {
332 * Returns all friend Sones of this Sone.
334 * @return The friend Sones of this Sone
336 public Collection<String> getFriends() {
337 return database.getFriends(this);
341 * Returns whether this Sone has the given Sone as a friend Sone.
343 * @param friendSoneId
344 * The ID of the Sone to check for
345 * @return {@code true} if this Sone has the given Sone as a friend, {@code
348 public boolean hasFriend(String friendSoneId) {
349 return database.isFriend(this, friendSoneId);
353 * Returns the list of posts of this Sone, sorted by time, newest first.
355 * @return All posts of this Sone
357 public List<Post> getPosts() {
358 synchronized (this) {
359 return FluentIterable.from(posts).toSortedList(Post.TIME_COMPARATOR);
364 * Sets all posts of this Sone at once.
367 * The new (and only) posts of this Sone
368 * @return This Sone (for method chaining)
370 public Sone setPosts(Collection<Post> posts) {
371 synchronized (this) {
373 this.posts.addAll(posts);
379 * Adds the given post to this Sone. The post will not be added if its {@link
380 * Post#getSone() Sone} is not this Sone.
385 public void addPost(Post post) {
386 if (post.getSone().equals(this) && posts.add(post)) {
387 logger.log(Level.FINEST, String.format("Adding %s to “%s”.", post, getName()));
392 * Removes the given post from this Sone.
397 public void removePost(Post post) {
398 if (post.getSone().equals(this)) {
404 * Returns all replies this Sone made.
406 * @return All replies this Sone made
408 public Set<PostReply> getReplies() {
409 return Collections.unmodifiableSet(replies);
413 * Sets all replies of this Sone at once.
416 * The new (and only) replies of this Sone
417 * @return This Sone (for method chaining)
419 public Sone setReplies(Collection<PostReply> replies) {
420 this.replies.clear();
421 this.replies.addAll(replies);
426 * Adds a reply to this Sone. If the given reply was not made by this Sone,
427 * nothing is added to this Sone.
432 public void addReply(PostReply reply) {
433 if (reply.getSone().equals(this)) {
439 * Removes a reply from this Sone.
442 * The reply to remove
444 public void removeReply(PostReply reply) {
445 if (reply.getSone().equals(this)) {
446 replies.remove(reply);
451 * Returns the IDs of all liked posts.
453 * @return All liked posts’ IDs
455 public Set<String> getLikedPostIds() {
456 return Collections.unmodifiableSet(likedPostIds);
460 * Sets the IDs of all liked posts.
462 * @param likedPostIds
463 * All liked posts’ IDs
464 * @return This Sone (for method chaining)
466 public Sone setLikePostIds(Set<String> likedPostIds) {
467 this.likedPostIds.clear();
468 this.likedPostIds.addAll(likedPostIds);
473 * Checks whether the given post ID is liked by this Sone.
477 * @return {@code true} if this Sone likes the given post, {@code false}
480 public boolean isLikedPostId(String postId) {
481 return likedPostIds.contains(postId);
485 * Adds the given post ID to the list of posts this Sone likes.
489 * @return This Sone (for method chaining)
491 public Sone addLikedPostId(String postId) {
492 likedPostIds.add(postId);
497 * Removes the given post ID from the list of posts this Sone likes.
501 * @return This Sone (for method chaining)
503 public Sone removeLikedPostId(String postId) {
504 likedPostIds.remove(postId);
509 * Returns the IDs of all liked replies.
511 * @return All liked replies’ IDs
513 public Set<String> getLikedReplyIds() {
514 return Collections.unmodifiableSet(likedReplyIds);
518 * Sets the IDs of all liked replies.
520 * @param likedReplyIds
521 * All liked replies’ IDs
522 * @return This Sone (for method chaining)
524 public Sone setLikeReplyIds(Set<String> likedReplyIds) {
525 this.likedReplyIds.clear();
526 this.likedReplyIds.addAll(likedReplyIds);
531 * Checks whether the given reply ID is liked by this Sone.
534 * The ID of the reply
535 * @return {@code true} if this Sone likes the given reply, {@code false}
538 public boolean isLikedReplyId(String replyId) {
539 return likedReplyIds.contains(replyId);
543 * Adds the given reply ID to the list of replies this Sone likes.
546 * The ID of the reply
547 * @return This Sone (for method chaining)
549 public Sone addLikedReplyId(String replyId) {
550 likedReplyIds.add(replyId);
555 * Removes the given post ID from the list of replies this Sone likes.
558 * The ID of the reply
559 * @return This Sone (for method chaining)
561 public Sone removeLikedReplyId(String replyId) {
562 likedReplyIds.remove(replyId);
567 * Returns the root album that contains all visible albums of this Sone.
569 * @return The root album of this Sone
571 public Album getRootAlbum() {
576 * Returns Sone-specific options.
578 * @return The options of this Sone
580 public SoneOptions getOptions() {
585 * Sets the options of this Sone.
588 * The options of this Sone
590 /* TODO - remove this method again, maybe add an option provider */
591 public void setOptions(SoneOptions options) {
592 this.options = options;
596 // FINGERPRINTABLE METHODS
601 public synchronized String getFingerprint() {
602 Hasher hash = Hashing.sha256().newHasher();
603 hash.putString(profile.getFingerprint());
605 hash.putString("Posts(");
606 for (Post post : getPosts()) {
607 hash.putString("Post(").putString(post.getId()).putString(")");
611 List<PostReply> replies = new ArrayList<PostReply>(getReplies());
612 Collections.sort(replies, Reply.TIME_COMPARATOR);
613 hash.putString("Replies(");
614 for (PostReply reply : replies) {
615 hash.putString("Reply(").putString(reply.getId()).putString(")");
619 List<String> likedPostIds = new ArrayList<String>(getLikedPostIds());
620 Collections.sort(likedPostIds);
621 hash.putString("LikedPosts(");
622 for (String likedPostId : likedPostIds) {
623 hash.putString("Post(").putString(likedPostId).putString(")");
627 List<String> likedReplyIds = new ArrayList<String>(getLikedReplyIds());
628 Collections.sort(likedReplyIds);
629 hash.putString("LikedReplies(");
630 for (String likedReplyId : likedReplyIds) {
631 hash.putString("Reply(").putString(likedReplyId).putString(")");
635 hash.putString("Albums(");
636 for (Album album : rootAlbum.getAlbums()) {
637 if (!Album.NOT_EMPTY.apply(album)) {
640 hash.putString(album.getFingerprint());
644 return hash.hash().toString();
648 // INTERFACE Comparable<Sone>
653 public int compareTo(Sone sone) {
654 return NICE_NAME_COMPARATOR.compare(this, sone);
663 public int hashCode() {
664 return id.hashCode();
669 public boolean equals(Object object) {
670 if (!(object instanceof Sone)) {
673 return ((Sone) object).getId().equals(id);
678 public String toString() {
679 return getClass().getName() + "[identity=" + identity + ",posts(" + posts.size() + "),replies(" + replies.size() + "),albums(" + getRootAlbum().getAlbums().size() + ")]";