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;
20 import static com.google.common.base.Preconditions.checkNotNull;
21 import static java.lang.String.format;
23 import java.net.MalformedURLException;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.List;
29 import java.util.concurrent.CopyOnWriteArraySet;
30 import java.util.logging.Level;
31 import java.util.logging.Logger;
33 import net.pterodactylus.sone.data.SoneOptions.DefaultSoneOptions;
34 import net.pterodactylus.sone.freenet.wot.Identity;
35 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
36 import net.pterodactylus.util.logging.Logging;
38 import freenet.keys.FreenetURI;
40 import com.google.common.hash.Hasher;
41 import com.google.common.hash.Hashing;
44 * {@link Sone} implementation.
46 * Operations that modify the Sone need to synchronize on the Sone in question.
48 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
50 public class SoneImpl implements Sone {
53 private static final Logger logger = Logging.getLogger(SoneImpl.class);
55 /** The ID of this Sone. */
56 private final String id;
58 /** Whether the Sone is local. */
59 private final boolean local;
61 /** The identity of this Sone. */
62 private final Identity identity;
64 /** The URI under which the Sone is stored in Freenet. */
65 private volatile FreenetURI requestUri;
67 /** The URI used to insert a new version of this Sone. */
68 /* This will be null for remote Sones! */
69 private volatile FreenetURI insertUri;
71 /** The latest edition of the Sone. */
72 private volatile long latestEdition;
74 /** The time of the last inserted update. */
75 private volatile long time;
77 /** The status of this Sone. */
78 private volatile SoneStatus status = SoneStatus.unknown;
80 /** The profile of this Sone. */
81 private volatile Profile profile = new Profile(this);
83 /** The client used by the Sone. */
84 private volatile Client client;
86 /** Whether this Sone is known. */
87 private volatile boolean known;
89 /** All friend Sones. */
90 private final Set<String> friendSones = new CopyOnWriteArraySet<String>();
93 private final Set<Post> posts = new CopyOnWriteArraySet<Post>();
96 private final Set<PostReply> replies = new CopyOnWriteArraySet<PostReply>();
98 /** The IDs of all liked posts. */
99 private final Set<String> likedPostIds = new CopyOnWriteArraySet<String>();
101 /** The IDs of all liked replies. */
102 private final Set<String> likedReplyIds = new CopyOnWriteArraySet<String>();
104 /** The root album containing all albums. */
105 private final Album rootAlbum = new AlbumImpl(this);
107 /** Sone-specific options. */
108 private SoneOptions options = new DefaultSoneOptions();
111 * Creates a new Sone.
114 * The identity of the Sone
116 * {@code true} if the Sone is a local Sone, {@code false} otherwise
118 public SoneImpl(Identity identity, boolean local) {
119 this.id = identity.getId();
120 this.identity = identity;
129 * Returns the identity of this Sone.
131 * @return The identity of this Sone
133 public String getId() {
138 * Returns the identity of this Sone.
140 * @return The identity of this Sone
142 public Identity getIdentity() {
147 * Returns the name of this Sone.
149 * @return The name of this Sone
151 public String getName() {
152 return (identity != null) ? identity.getNickname() : null;
156 * Returns whether this Sone is a local Sone.
158 * @return {@code true} if this Sone is a local Sone, {@code false} otherwise
160 public boolean isLocal() {
165 * Returns the request URI of this Sone.
167 * @return The request URI of this Sone
169 public FreenetURI getRequestUri() {
171 return new FreenetURI(getIdentity().getRequestUri())
174 .setMetaString(new String[0])
175 .setSuggestedEdition(latestEdition);
176 } catch (MalformedURLException e) {
177 throw new IllegalStateException(
178 format("Identity %s's request URI is incorrect.",
184 * Returns the insert URI of this Sone.
186 * @return The insert URI of this Sone
188 public FreenetURI getInsertUri() {
193 return new FreenetURI(((OwnIdentity) getIdentity()).getInsertUri()).setDocName("Sone").setMetaString(new String[0]);
194 } catch (MalformedURLException e) {
195 throw new IllegalStateException(format("Own identity %s's insert URI is incorrect.", getIdentity()), e);
200 * Returns the latest edition of this Sone.
202 * @return The latest edition of this Sone
204 public long getLatestEdition() {
205 return latestEdition;
209 * Sets the latest edition of this Sone. If the given latest edition is not
210 * greater than the current latest edition, the latest edition of this Sone is
213 * @param latestEdition
214 * The latest edition of this Sone
216 public void setLatestEdition(long latestEdition) {
217 if (!(latestEdition > this.latestEdition)) {
218 logger.log(Level.FINE, String.format("New latest edition %d is not greater than current latest edition %d!", latestEdition, this.latestEdition));
221 this.latestEdition = latestEdition;
225 * Return the time of the last inserted update of this Sone.
227 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
229 public long getTime() {
234 * Sets the time of the last inserted update of this Sone.
237 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
238 * @return This Sone (for method chaining)
240 public Sone setTime(long time) {
246 * Returns the status of this Sone.
248 * @return The status of this Sone
250 public SoneStatus getStatus() {
255 * Sets the new status of this Sone.
258 * The new status of this Sone
260 * @throws IllegalArgumentException
261 * if {@code status} is {@code null}
263 public Sone setStatus(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
275 public Profile getProfile() {
276 return new Profile(profile);
280 * Sets the profile of this Sone. A copy of the given profile is stored so that
281 * subsequent modifications of the given profile are not reflected in this
287 public void setProfile(Profile profile) {
288 this.profile = new Profile(profile);
292 * Returns the client used by this Sone.
294 * @return The client used by this Sone, or {@code null}
296 public Client getClient() {
301 * Sets the client used by this Sone.
304 * The client used by this Sone, or {@code null}
305 * @return This Sone (for method chaining)
307 public Sone setClient(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
328 public Sone setKnown(boolean known) {
334 * Returns all friend Sones of this Sone.
336 * @return The friend Sones of this Sone
338 public List<String> getFriends() {
339 return new ArrayList<String>(friendSones);
343 * Returns whether this Sone has the given Sone as a friend Sone.
345 * @param friendSoneId
346 * The ID of the Sone to check for
347 * @return {@code true} if this Sone has the given Sone as a friend, {@code
350 public boolean hasFriend(String friendSoneId) {
351 return friendSones.contains(friendSoneId);
355 * Adds the given Sone as a friend Sone.
358 * The friend Sone to add
359 * @return This Sone (for method chaining)
361 public Sone addFriend(String friendSone) {
362 if (!friendSone.equals(id)) {
363 friendSones.add(friendSone);
369 * Removes the given Sone as a friend Sone.
371 * @param friendSoneId
372 * The ID of the friend Sone to remove
373 * @return This Sone (for method chaining)
375 public Sone removeFriend(String friendSoneId) {
376 friendSones.remove(friendSoneId);
381 * Returns the list of posts of this Sone, sorted by time, newest first.
383 * @return All posts of this Sone
385 public List<Post> getPosts() {
386 List<Post> sortedPosts;
387 synchronized (this) {
388 sortedPosts = new ArrayList<Post>(posts);
390 Collections.sort(sortedPosts, Post.TIME_COMPARATOR);
395 * Sets all posts of this Sone at once.
398 * The new (and only) posts of this Sone
399 * @return This Sone (for method chaining)
401 public Sone setPosts(Collection<Post> posts) {
402 synchronized (this) {
404 this.posts.addAll(posts);
410 * Adds the given post to this Sone. The post will not be added if its {@link
411 * Post#getSone() Sone} is not this Sone.
416 public void addPost(Post post) {
417 if (post.getSone().equals(this) && posts.add(post)) {
418 logger.log(Level.FINEST, String.format("Adding %s to “%s”.", post, getName()));
423 * Removes the given post from this Sone.
428 public void removePost(Post post) {
429 if (post.getSone().equals(this)) {
435 * Returns all replies this Sone made.
437 * @return All replies this Sone made
439 public Set<PostReply> getReplies() {
440 return Collections.unmodifiableSet(replies);
444 * Sets all replies of this Sone at once.
447 * The new (and only) replies of this Sone
448 * @return This Sone (for method chaining)
450 public Sone setReplies(Collection<PostReply> replies) {
451 this.replies.clear();
452 this.replies.addAll(replies);
457 * Adds a reply to this Sone. If the given reply was not made by this Sone,
458 * nothing is added to this Sone.
463 public void addReply(PostReply reply) {
464 if (reply.getSone().equals(this)) {
470 * Removes a reply from this Sone.
473 * The reply to remove
475 public void removeReply(PostReply reply) {
476 if (reply.getSone().equals(this)) {
477 replies.remove(reply);
482 * Returns the IDs of all liked posts.
484 * @return All liked posts’ IDs
486 public Set<String> getLikedPostIds() {
487 return Collections.unmodifiableSet(likedPostIds);
491 * Sets the IDs of all liked posts.
493 * @param likedPostIds
494 * All liked posts’ IDs
495 * @return This Sone (for method chaining)
497 public Sone setLikePostIds(Set<String> likedPostIds) {
498 this.likedPostIds.clear();
499 this.likedPostIds.addAll(likedPostIds);
504 * Checks whether the given post ID is liked by this Sone.
508 * @return {@code true} if this Sone likes the given post, {@code false}
511 public boolean isLikedPostId(String postId) {
512 return likedPostIds.contains(postId);
516 * Adds the given post ID to the list of posts this Sone likes.
520 * @return This Sone (for method chaining)
522 public Sone addLikedPostId(String postId) {
523 likedPostIds.add(postId);
528 * Removes the given post ID from the list of posts this Sone likes.
532 * @return This Sone (for method chaining)
534 public Sone removeLikedPostId(String postId) {
535 likedPostIds.remove(postId);
540 * Returns the IDs of all liked replies.
542 * @return All liked replies’ IDs
544 public Set<String> getLikedReplyIds() {
545 return Collections.unmodifiableSet(likedReplyIds);
549 * Sets the IDs of all liked replies.
551 * @param likedReplyIds
552 * All liked replies’ IDs
553 * @return This Sone (for method chaining)
555 public Sone setLikeReplyIds(Set<String> likedReplyIds) {
556 this.likedReplyIds.clear();
557 this.likedReplyIds.addAll(likedReplyIds);
562 * Checks whether the given reply ID is liked by this Sone.
565 * The ID of the reply
566 * @return {@code true} if this Sone likes the given reply, {@code false}
569 public boolean isLikedReplyId(String replyId) {
570 return likedReplyIds.contains(replyId);
574 * Adds the given reply ID to the list of replies this Sone likes.
577 * The ID of the reply
578 * @return This Sone (for method chaining)
580 public Sone addLikedReplyId(String replyId) {
581 likedReplyIds.add(replyId);
586 * Removes the given post ID from the list of replies this Sone likes.
589 * The ID of the reply
590 * @return This Sone (for method chaining)
592 public Sone removeLikedReplyId(String replyId) {
593 likedReplyIds.remove(replyId);
598 * Returns the root album that contains all visible albums of this Sone.
600 * @return The root album of this Sone
602 public Album getRootAlbum() {
607 * Returns Sone-specific options.
609 * @return The options of this Sone
611 public SoneOptions getOptions() {
616 * Sets the options of this Sone.
619 * The options of this Sone
621 /* TODO - remove this method again, maybe add an option provider */
622 public void setOptions(SoneOptions options) {
623 this.options = options;
627 // FINGERPRINTABLE METHODS
632 public synchronized String getFingerprint() {
633 Hasher hash = Hashing.sha256().newHasher();
634 hash.putString(profile.getFingerprint());
636 hash.putString("Posts(");
637 for (Post post : getPosts()) {
638 hash.putString("Post(").putString(post.getId()).putString(")");
642 List<PostReply> replies = new ArrayList<PostReply>(getReplies());
643 Collections.sort(replies, Reply.TIME_COMPARATOR);
644 hash.putString("Replies(");
645 for (PostReply reply : replies) {
646 hash.putString("Reply(").putString(reply.getId()).putString(")");
650 List<String> likedPostIds = new ArrayList<String>(getLikedPostIds());
651 Collections.sort(likedPostIds);
652 hash.putString("LikedPosts(");
653 for (String likedPostId : likedPostIds) {
654 hash.putString("Post(").putString(likedPostId).putString(")");
658 List<String> likedReplyIds = new ArrayList<String>(getLikedReplyIds());
659 Collections.sort(likedReplyIds);
660 hash.putString("LikedReplies(");
661 for (String likedReplyId : likedReplyIds) {
662 hash.putString("Reply(").putString(likedReplyId).putString(")");
666 hash.putString("Albums(");
667 for (Album album : rootAlbum.getAlbums()) {
668 if (!Album.NOT_EMPTY.apply(album)) {
671 hash.putString(album.getFingerprint());
675 return hash.hash().toString();
679 // INTERFACE Comparable<Sone>
684 public int compareTo(Sone sone) {
685 return NICE_NAME_COMPARATOR.compare(this, sone);
694 public int hashCode() {
695 return id.hashCode();
700 public boolean equals(Object object) {
701 if (!(object instanceof Sone)) {
704 return ((Sone) object).getId().equals(id);
709 public String toString() {
710 return getClass().getName() + "[identity=" + identity + ",requestUri=" + requestUri + ",insertUri(" + String.valueOf(insertUri).length() + "),friends(" + friendSones.size() + "),posts(" + posts.size() + "),replies(" + replies.size() + "),albums(" + getRootAlbum().getAlbums().size() + ")]";