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())
195 .setMetaString(new String[0])
196 .setSuggestedEdition(latestEdition);
197 } catch (MalformedURLException e) {
198 throw new IllegalStateException(format("Own identity %s's insert URI is incorrect.", getIdentity()), e);
203 * Returns the latest edition of this Sone.
205 * @return The latest edition of this Sone
207 public long getLatestEdition() {
208 return latestEdition;
212 * Sets the latest edition of this Sone. If the given latest edition is not
213 * greater than the current latest edition, the latest edition of this Sone is
216 * @param latestEdition
217 * The latest edition of this Sone
219 public void setLatestEdition(long latestEdition) {
220 if (!(latestEdition > this.latestEdition)) {
221 logger.log(Level.FINE, String.format("New latest edition %d is not greater than current latest edition %d!", latestEdition, this.latestEdition));
224 this.latestEdition = latestEdition;
228 * Return the time of the last inserted update of this Sone.
230 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
232 public long getTime() {
237 * Sets the time of the last inserted update of this Sone.
240 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
241 * @return This Sone (for method chaining)
243 public Sone setTime(long time) {
249 * Returns the status of this Sone.
251 * @return The status of this Sone
253 public SoneStatus getStatus() {
258 * Sets the new status of this Sone.
261 * The new status of this Sone
263 * @throws IllegalArgumentException
264 * if {@code status} is {@code null}
266 public Sone setStatus(SoneStatus status) {
267 this.status = checkNotNull(status, "status must not be null");
272 * Returns a copy of the profile. If you want to update values in the profile
273 * of this Sone, update the values in the returned {@link Profile} and use
274 * {@link #setProfile(Profile)} to change the profile in this Sone.
276 * @return A copy of the profile
278 public Profile getProfile() {
279 return new Profile(profile);
283 * Sets the profile of this Sone. A copy of the given profile is stored so that
284 * subsequent modifications of the given profile are not reflected in this
290 public void setProfile(Profile profile) {
291 this.profile = new Profile(profile);
295 * Returns the client used by this Sone.
297 * @return The client used by this Sone, or {@code null}
299 public Client getClient() {
304 * Sets the client used by this Sone.
307 * The client used by this Sone, or {@code null}
308 * @return This Sone (for method chaining)
310 public Sone setClient(Client client) {
311 this.client = client;
316 * Returns whether this Sone is known.
318 * @return {@code true} if this Sone is known, {@code false} otherwise
320 public boolean isKnown() {
325 * Sets whether this Sone is known.
328 * {@code true} if this Sone is known, {@code false} otherwise
331 public Sone setKnown(boolean known) {
337 * Returns all friend Sones of this Sone.
339 * @return The friend Sones of this Sone
341 public List<String> getFriends() {
342 return new ArrayList<String>(friendSones);
346 * Returns whether this Sone has the given Sone as a friend Sone.
348 * @param friendSoneId
349 * The ID of the Sone to check for
350 * @return {@code true} if this Sone has the given Sone as a friend, {@code
353 public boolean hasFriend(String friendSoneId) {
354 return friendSones.contains(friendSoneId);
358 * Adds the given Sone as a friend Sone.
361 * The friend Sone to add
362 * @return This Sone (for method chaining)
364 public Sone addFriend(String friendSone) {
365 if (!friendSone.equals(id)) {
366 friendSones.add(friendSone);
372 * Removes the given Sone as a friend Sone.
374 * @param friendSoneId
375 * The ID of the friend Sone to remove
376 * @return This Sone (for method chaining)
378 public Sone removeFriend(String friendSoneId) {
379 friendSones.remove(friendSoneId);
384 * Returns the list of posts of this Sone, sorted by time, newest first.
386 * @return All posts of this Sone
388 public List<Post> getPosts() {
389 List<Post> sortedPosts;
390 synchronized (this) {
391 sortedPosts = new ArrayList<Post>(posts);
393 Collections.sort(sortedPosts, Post.TIME_COMPARATOR);
398 * Sets all posts of this Sone at once.
401 * The new (and only) posts of this Sone
402 * @return This Sone (for method chaining)
404 public Sone setPosts(Collection<Post> posts) {
405 synchronized (this) {
407 this.posts.addAll(posts);
413 * Adds the given post to this Sone. The post will not be added if its {@link
414 * Post#getSone() Sone} is not this Sone.
419 public void addPost(Post post) {
420 if (post.getSone().equals(this) && posts.add(post)) {
421 logger.log(Level.FINEST, String.format("Adding %s to “%s”.", post, getName()));
426 * Removes the given post from this Sone.
431 public void removePost(Post post) {
432 if (post.getSone().equals(this)) {
438 * Returns all replies this Sone made.
440 * @return All replies this Sone made
442 public Set<PostReply> getReplies() {
443 return Collections.unmodifiableSet(replies);
447 * Sets all replies of this Sone at once.
450 * The new (and only) replies of this Sone
451 * @return This Sone (for method chaining)
453 public Sone setReplies(Collection<PostReply> replies) {
454 this.replies.clear();
455 this.replies.addAll(replies);
460 * Adds a reply to this Sone. If the given reply was not made by this Sone,
461 * nothing is added to this Sone.
466 public void addReply(PostReply reply) {
467 if (reply.getSone().equals(this)) {
473 * Removes a reply from this Sone.
476 * The reply to remove
478 public void removeReply(PostReply reply) {
479 if (reply.getSone().equals(this)) {
480 replies.remove(reply);
485 * Returns the IDs of all liked posts.
487 * @return All liked posts’ IDs
489 public Set<String> getLikedPostIds() {
490 return Collections.unmodifiableSet(likedPostIds);
494 * Sets the IDs of all liked posts.
496 * @param likedPostIds
497 * All liked posts’ IDs
498 * @return This Sone (for method chaining)
500 public Sone setLikePostIds(Set<String> likedPostIds) {
501 this.likedPostIds.clear();
502 this.likedPostIds.addAll(likedPostIds);
507 * Checks whether the given post ID is liked by this Sone.
511 * @return {@code true} if this Sone likes the given post, {@code false}
514 public boolean isLikedPostId(String postId) {
515 return likedPostIds.contains(postId);
519 * Adds the given post ID to the list of posts this Sone likes.
523 * @return This Sone (for method chaining)
525 public Sone addLikedPostId(String postId) {
526 likedPostIds.add(postId);
531 * Removes the given post ID from the list of posts this Sone likes.
535 * @return This Sone (for method chaining)
537 public Sone removeLikedPostId(String postId) {
538 likedPostIds.remove(postId);
543 * Returns the IDs of all liked replies.
545 * @return All liked replies’ IDs
547 public Set<String> getLikedReplyIds() {
548 return Collections.unmodifiableSet(likedReplyIds);
552 * Sets the IDs of all liked replies.
554 * @param likedReplyIds
555 * All liked replies’ IDs
556 * @return This Sone (for method chaining)
558 public Sone setLikeReplyIds(Set<String> likedReplyIds) {
559 this.likedReplyIds.clear();
560 this.likedReplyIds.addAll(likedReplyIds);
565 * Checks whether the given reply ID is liked by this Sone.
568 * The ID of the reply
569 * @return {@code true} if this Sone likes the given reply, {@code false}
572 public boolean isLikedReplyId(String replyId) {
573 return likedReplyIds.contains(replyId);
577 * Adds the given reply ID to the list of replies this Sone likes.
580 * The ID of the reply
581 * @return This Sone (for method chaining)
583 public Sone addLikedReplyId(String replyId) {
584 likedReplyIds.add(replyId);
589 * Removes the given post ID from the list of replies this Sone likes.
592 * The ID of the reply
593 * @return This Sone (for method chaining)
595 public Sone removeLikedReplyId(String replyId) {
596 likedReplyIds.remove(replyId);
601 * Returns the root album that contains all visible albums of this Sone.
603 * @return The root album of this Sone
605 public Album getRootAlbum() {
610 * Returns Sone-specific options.
612 * @return The options of this Sone
614 public SoneOptions getOptions() {
619 * Sets the options of this Sone.
622 * The options of this Sone
624 /* TODO - remove this method again, maybe add an option provider */
625 public void setOptions(SoneOptions options) {
626 this.options = options;
630 // FINGERPRINTABLE METHODS
635 public synchronized String getFingerprint() {
636 Hasher hash = Hashing.sha256().newHasher();
637 hash.putString(profile.getFingerprint());
639 hash.putString("Posts(");
640 for (Post post : getPosts()) {
641 hash.putString("Post(").putString(post.getId()).putString(")");
645 List<PostReply> replies = new ArrayList<PostReply>(getReplies());
646 Collections.sort(replies, Reply.TIME_COMPARATOR);
647 hash.putString("Replies(");
648 for (PostReply reply : replies) {
649 hash.putString("Reply(").putString(reply.getId()).putString(")");
653 List<String> likedPostIds = new ArrayList<String>(getLikedPostIds());
654 Collections.sort(likedPostIds);
655 hash.putString("LikedPosts(");
656 for (String likedPostId : likedPostIds) {
657 hash.putString("Post(").putString(likedPostId).putString(")");
661 List<String> likedReplyIds = new ArrayList<String>(getLikedReplyIds());
662 Collections.sort(likedReplyIds);
663 hash.putString("LikedReplies(");
664 for (String likedReplyId : likedReplyIds) {
665 hash.putString("Reply(").putString(likedReplyId).putString(")");
669 hash.putString("Albums(");
670 for (Album album : rootAlbum.getAlbums()) {
671 if (!Album.NOT_EMPTY.apply(album)) {
674 hash.putString(album.getFingerprint());
678 return hash.hash().toString();
682 // INTERFACE Comparable<Sone>
687 public int compareTo(Sone sone) {
688 return NICE_NAME_COMPARATOR.compare(this, sone);
697 public int hashCode() {
698 return id.hashCode();
703 public boolean equals(Object object) {
704 if (!(object instanceof Sone)) {
707 return ((Sone) object).getId().equals(id);
712 public String toString() {
713 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() + ")]";