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() {
170 return (requestUri != null) ? requestUri.setSuggestedEdition(latestEdition) : null;
174 * Sets the request URI of this Sone.
177 * The request URI of this Sone
178 * @return This Sone (for method chaining)
180 public Sone setRequestUri(FreenetURI requestUri) {
181 if (this.requestUri == null) {
182 this.requestUri = requestUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
185 if (!this.requestUri.equalsKeypair(requestUri)) {
186 logger.log(Level.WARNING, String.format("Request URI %s tried to overwrite %s!", requestUri, this.requestUri));
193 * Returns the insert URI of this Sone.
195 * @return The insert URI of this Sone
197 public FreenetURI getInsertUri() {
202 return new FreenetURI(((OwnIdentity) getIdentity()).getInsertUri()).setDocName("Sone").setMetaString(new String[0]);
203 } catch (MalformedURLException e) {
204 throw new IllegalStateException(format("Own identity %s's insert URI is incorrect.", getIdentity()), e);
209 * Returns the latest edition of this Sone.
211 * @return The latest edition of this Sone
213 public long getLatestEdition() {
214 return latestEdition;
218 * Sets the latest edition of this Sone. If the given latest edition is not
219 * greater than the current latest edition, the latest edition of this Sone is
222 * @param latestEdition
223 * The latest edition of this Sone
225 public void setLatestEdition(long latestEdition) {
226 if (!(latestEdition > this.latestEdition)) {
227 logger.log(Level.FINE, String.format("New latest edition %d is not greater than current latest edition %d!", latestEdition, this.latestEdition));
230 this.latestEdition = latestEdition;
234 * Return the time of the last inserted update of this Sone.
236 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
238 public long getTime() {
243 * Sets the time of the last inserted update of this Sone.
246 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
247 * @return This Sone (for method chaining)
249 public Sone setTime(long time) {
255 * Returns the status of this Sone.
257 * @return The status of this Sone
259 public SoneStatus getStatus() {
264 * Sets the new status of this Sone.
267 * The new status of this Sone
269 * @throws IllegalArgumentException
270 * if {@code status} is {@code null}
272 public Sone setStatus(SoneStatus status) {
273 this.status = checkNotNull(status, "status must not be null");
278 * Returns a copy of the profile. If you want to update values in the profile
279 * of this Sone, update the values in the returned {@link Profile} and use
280 * {@link #setProfile(Profile)} to change the profile in this Sone.
282 * @return A copy of the profile
284 public Profile getProfile() {
285 return new Profile(profile);
289 * Sets the profile of this Sone. A copy of the given profile is stored so that
290 * subsequent modifications of the given profile are not reflected in this
296 public void setProfile(Profile profile) {
297 this.profile = new Profile(profile);
301 * Returns the client used by this Sone.
303 * @return The client used by this Sone, or {@code null}
305 public Client getClient() {
310 * Sets the client used by this Sone.
313 * The client used by this Sone, or {@code null}
314 * @return This Sone (for method chaining)
316 public Sone setClient(Client client) {
317 this.client = client;
322 * Returns whether this Sone is known.
324 * @return {@code true} if this Sone is known, {@code false} otherwise
326 public boolean isKnown() {
331 * Sets whether this Sone is known.
334 * {@code true} if this Sone is known, {@code false} otherwise
337 public Sone setKnown(boolean known) {
343 * Returns all friend Sones of this Sone.
345 * @return The friend Sones of this Sone
347 public List<String> getFriends() {
348 return new ArrayList<String>(friendSones);
352 * Returns whether this Sone has the given Sone as a friend Sone.
354 * @param friendSoneId
355 * The ID of the Sone to check for
356 * @return {@code true} if this Sone has the given Sone as a friend, {@code
359 public boolean hasFriend(String friendSoneId) {
360 return friendSones.contains(friendSoneId);
364 * Adds the given Sone as a friend Sone.
367 * The friend Sone to add
368 * @return This Sone (for method chaining)
370 public Sone addFriend(String friendSone) {
371 if (!friendSone.equals(id)) {
372 friendSones.add(friendSone);
378 * Removes the given Sone as a friend Sone.
380 * @param friendSoneId
381 * The ID of the friend Sone to remove
382 * @return This Sone (for method chaining)
384 public Sone removeFriend(String friendSoneId) {
385 friendSones.remove(friendSoneId);
390 * Returns the list of posts of this Sone, sorted by time, newest first.
392 * @return All posts of this Sone
394 public List<Post> getPosts() {
395 List<Post> sortedPosts;
396 synchronized (this) {
397 sortedPosts = new ArrayList<Post>(posts);
399 Collections.sort(sortedPosts, Post.TIME_COMPARATOR);
404 * Sets all posts of this Sone at once.
407 * The new (and only) posts of this Sone
408 * @return This Sone (for method chaining)
410 public Sone setPosts(Collection<Post> posts) {
411 synchronized (this) {
413 this.posts.addAll(posts);
419 * Adds the given post to this Sone. The post will not be added if its {@link
420 * Post#getSone() Sone} is not this Sone.
425 public void addPost(Post post) {
426 if (post.getSone().equals(this) && posts.add(post)) {
427 logger.log(Level.FINEST, String.format("Adding %s to “%s”.", post, getName()));
432 * Removes the given post from this Sone.
437 public void removePost(Post post) {
438 if (post.getSone().equals(this)) {
444 * Returns all replies this Sone made.
446 * @return All replies this Sone made
448 public Set<PostReply> getReplies() {
449 return Collections.unmodifiableSet(replies);
453 * Sets all replies of this Sone at once.
456 * The new (and only) replies of this Sone
457 * @return This Sone (for method chaining)
459 public Sone setReplies(Collection<PostReply> replies) {
460 this.replies.clear();
461 this.replies.addAll(replies);
466 * Adds a reply to this Sone. If the given reply was not made by this Sone,
467 * nothing is added to this Sone.
472 public void addReply(PostReply reply) {
473 if (reply.getSone().equals(this)) {
479 * Removes a reply from this Sone.
482 * The reply to remove
484 public void removeReply(PostReply reply) {
485 if (reply.getSone().equals(this)) {
486 replies.remove(reply);
491 * Returns the IDs of all liked posts.
493 * @return All liked posts’ IDs
495 public Set<String> getLikedPostIds() {
496 return Collections.unmodifiableSet(likedPostIds);
500 * Sets the IDs of all liked posts.
502 * @param likedPostIds
503 * All liked posts’ IDs
504 * @return This Sone (for method chaining)
506 public Sone setLikePostIds(Set<String> likedPostIds) {
507 this.likedPostIds.clear();
508 this.likedPostIds.addAll(likedPostIds);
513 * Checks whether the given post ID is liked by this Sone.
517 * @return {@code true} if this Sone likes the given post, {@code false}
520 public boolean isLikedPostId(String postId) {
521 return likedPostIds.contains(postId);
525 * Adds the given post ID to the list of posts this Sone likes.
529 * @return This Sone (for method chaining)
531 public Sone addLikedPostId(String postId) {
532 likedPostIds.add(postId);
537 * Removes the given post ID from the list of posts this Sone likes.
541 * @return This Sone (for method chaining)
543 public Sone removeLikedPostId(String postId) {
544 likedPostIds.remove(postId);
549 * Returns the IDs of all liked replies.
551 * @return All liked replies’ IDs
553 public Set<String> getLikedReplyIds() {
554 return Collections.unmodifiableSet(likedReplyIds);
558 * Sets the IDs of all liked replies.
560 * @param likedReplyIds
561 * All liked replies’ IDs
562 * @return This Sone (for method chaining)
564 public Sone setLikeReplyIds(Set<String> likedReplyIds) {
565 this.likedReplyIds.clear();
566 this.likedReplyIds.addAll(likedReplyIds);
571 * Checks whether the given reply ID is liked by this Sone.
574 * The ID of the reply
575 * @return {@code true} if this Sone likes the given reply, {@code false}
578 public boolean isLikedReplyId(String replyId) {
579 return likedReplyIds.contains(replyId);
583 * Adds the given reply ID to the list of replies this Sone likes.
586 * The ID of the reply
587 * @return This Sone (for method chaining)
589 public Sone addLikedReplyId(String replyId) {
590 likedReplyIds.add(replyId);
595 * Removes the given post ID from the list of replies this Sone likes.
598 * The ID of the reply
599 * @return This Sone (for method chaining)
601 public Sone removeLikedReplyId(String replyId) {
602 likedReplyIds.remove(replyId);
607 * Returns the root album that contains all visible albums of this Sone.
609 * @return The root album of this Sone
611 public Album getRootAlbum() {
616 * Returns Sone-specific options.
618 * @return The options of this Sone
620 public SoneOptions getOptions() {
625 * Sets the options of this Sone.
628 * The options of this Sone
630 /* TODO - remove this method again, maybe add an option provider */
631 public void setOptions(SoneOptions options) {
632 this.options = options;
636 // FINGERPRINTABLE METHODS
641 public synchronized String getFingerprint() {
642 Hasher hash = Hashing.sha256().newHasher();
643 hash.putString(profile.getFingerprint());
645 hash.putString("Posts(");
646 for (Post post : getPosts()) {
647 hash.putString("Post(").putString(post.getId()).putString(")");
651 List<PostReply> replies = new ArrayList<PostReply>(getReplies());
652 Collections.sort(replies, Reply.TIME_COMPARATOR);
653 hash.putString("Replies(");
654 for (PostReply reply : replies) {
655 hash.putString("Reply(").putString(reply.getId()).putString(")");
659 List<String> likedPostIds = new ArrayList<String>(getLikedPostIds());
660 Collections.sort(likedPostIds);
661 hash.putString("LikedPosts(");
662 for (String likedPostId : likedPostIds) {
663 hash.putString("Post(").putString(likedPostId).putString(")");
667 List<String> likedReplyIds = new ArrayList<String>(getLikedReplyIds());
668 Collections.sort(likedReplyIds);
669 hash.putString("LikedReplies(");
670 for (String likedReplyId : likedReplyIds) {
671 hash.putString("Reply(").putString(likedReplyId).putString(")");
675 hash.putString("Albums(");
676 for (Album album : rootAlbum.getAlbums()) {
677 if (!Album.NOT_EMPTY.apply(album)) {
680 hash.putString(album.getFingerprint());
684 return hash.hash().toString();
688 // INTERFACE Comparable<Sone>
693 public int compareTo(Sone sone) {
694 return NICE_NAME_COMPARATOR.compare(this, sone);
703 public int hashCode() {
704 return id.hashCode();
709 public boolean equals(Object object) {
710 if (!(object instanceof Sone)) {
713 return ((Sone) object).getId().equals(id);
718 public String toString() {
719 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() + ")]";