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 final 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 final 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, long time, Client client) {
123 this.database = database;
124 this.id = identity.getId();
125 this.identity = identity;
128 this.client = client;
136 * Returns the identity of this Sone.
138 * @return The identity of this Sone
140 public String getId() {
145 * Returns the identity of this Sone.
147 * @return The identity of this Sone
149 public Identity getIdentity() {
154 * Returns the name of this Sone.
156 * @return The name of this Sone
158 public String getName() {
159 return (identity != null) ? identity.getNickname() : null;
163 * Returns whether this Sone is a local Sone.
165 * @return {@code true} if this Sone is a local Sone, {@code false} otherwise
167 public boolean isLocal() {
172 * Returns the request URI of this Sone.
174 * @return The request URI of this Sone
176 public FreenetURI getRequestUri() {
178 return new FreenetURI(getIdentity().getRequestUri())
181 .setMetaString(new String[0])
182 .setSuggestedEdition(latestEdition);
183 } catch (MalformedURLException e) {
184 throw new IllegalStateException(
185 format("Identity %s's request URI is incorrect.",
191 * Returns the insert URI of this Sone.
193 * @return The insert URI of this Sone
195 public FreenetURI getInsertUri() {
200 return new FreenetURI(((OwnIdentity) getIdentity()).getInsertUri())
202 .setMetaString(new String[0])
203 .setSuggestedEdition(latestEdition);
204 } catch (MalformedURLException e) {
205 throw new IllegalStateException(format("Own identity %s's insert URI is incorrect.", getIdentity()), e);
210 * Returns the latest edition of this Sone.
212 * @return The latest edition of this Sone
214 public long getLatestEdition() {
215 return latestEdition;
219 * Sets the latest edition of this Sone. If the given latest edition is not
220 * greater than the current latest edition, the latest edition of this Sone is
223 * @param latestEdition
224 * The latest edition of this Sone
226 public void setLatestEdition(long latestEdition) {
227 if (!(latestEdition > this.latestEdition)) {
228 logger.log(Level.FINE, String.format("New latest edition %d is not greater than current latest edition %d!", latestEdition, this.latestEdition));
231 this.latestEdition = latestEdition;
235 * Return the time of the last inserted update of this Sone.
237 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
239 public long getTime() {
244 * Returns the status of this Sone.
246 * @return The status of this Sone
248 public SoneStatus getStatus() {
253 * Sets the new status of this Sone.
256 * The new status of this Sone
258 * @throws IllegalArgumentException
259 * if {@code status} is {@code null}
261 public Sone setStatus(SoneStatus status) {
262 this.status = checkNotNull(status, "status must not be null");
267 * Returns a copy of the profile. If you want to update values in the profile
268 * of this Sone, update the values in the returned {@link Profile} and use
269 * {@link #setProfile(Profile)} to change the profile in this Sone.
271 * @return A copy of the profile
273 public Profile getProfile() {
274 return new Profile(profile);
278 * Sets the profile of this Sone. A copy of the given profile is stored so that
279 * subsequent modifications of the given profile are not reflected in this
285 public void setProfile(Profile profile) {
286 this.profile = new Profile(profile);
290 * Returns the client used by this Sone.
292 * @return The client used by this Sone, or {@code null}
294 public Client getClient() {
299 * Sets the client used by this Sone.
302 * The client used by this Sone, or {@code null}
303 * @return This Sone (for method chaining)
305 public Sone setClient(Client client) {
310 * Returns whether this Sone is known.
312 * @return {@code true} if this Sone is known, {@code false} otherwise
314 public boolean isKnown() {
319 * Sets whether this Sone is known.
322 * {@code true} if this Sone is known, {@code false} otherwise
325 public Sone setKnown(boolean known) {
331 * Returns all friend Sones of this Sone.
333 * @return The friend Sones of this Sone
335 public Collection<String> getFriends() {
336 return database.getFriends(this);
340 * Returns whether this Sone has the given Sone as a friend Sone.
342 * @param friendSoneId
343 * The ID of the Sone to check for
344 * @return {@code true} if this Sone has the given Sone as a friend, {@code
347 public boolean hasFriend(String friendSoneId) {
348 return database.isFriend(this, friendSoneId);
352 * Returns the list of posts of this Sone, sorted by time, newest first.
354 * @return All posts of this Sone
356 public List<Post> getPosts() {
357 List<Post> sortedPosts;
358 synchronized (this) {
359 sortedPosts = new ArrayList<Post>(posts);
361 Collections.sort(sortedPosts, Post.TIME_COMPARATOR);
366 * Sets all posts of this Sone at once.
369 * The new (and only) posts of this Sone
370 * @return This Sone (for method chaining)
372 public Sone setPosts(Collection<Post> posts) {
373 synchronized (this) {
375 this.posts.addAll(posts);
381 * Adds the given post to this Sone. The post will not be added if its {@link
382 * Post#getSone() Sone} is not this Sone.
387 public void addPost(Post post) {
388 if (post.getSone().equals(this) && posts.add(post)) {
389 logger.log(Level.FINEST, String.format("Adding %s to “%s”.", post, getName()));
394 * Removes the given post from this Sone.
399 public void removePost(Post post) {
400 if (post.getSone().equals(this)) {
406 * Returns all replies this Sone made.
408 * @return All replies this Sone made
410 public Set<PostReply> getReplies() {
411 return Collections.unmodifiableSet(replies);
415 * Sets all replies of this Sone at once.
418 * The new (and only) replies of this Sone
419 * @return This Sone (for method chaining)
421 public Sone setReplies(Collection<PostReply> replies) {
422 this.replies.clear();
423 this.replies.addAll(replies);
428 * Adds a reply to this Sone. If the given reply was not made by this Sone,
429 * nothing is added to this Sone.
434 public void addReply(PostReply reply) {
435 if (reply.getSone().equals(this)) {
441 * Removes a reply from this Sone.
444 * The reply to remove
446 public void removeReply(PostReply reply) {
447 if (reply.getSone().equals(this)) {
448 replies.remove(reply);
453 * Returns the IDs of all liked posts.
455 * @return All liked posts’ IDs
457 public Set<String> getLikedPostIds() {
458 return Collections.unmodifiableSet(likedPostIds);
462 * Sets the IDs of all liked posts.
464 * @param likedPostIds
465 * All liked posts’ IDs
466 * @return This Sone (for method chaining)
468 public Sone setLikePostIds(Set<String> likedPostIds) {
469 this.likedPostIds.clear();
470 this.likedPostIds.addAll(likedPostIds);
475 * Checks whether the given post ID is liked by this Sone.
479 * @return {@code true} if this Sone likes the given post, {@code false}
482 public boolean isLikedPostId(String postId) {
483 return likedPostIds.contains(postId);
487 * Adds the given post ID to the list of posts this Sone likes.
491 * @return This Sone (for method chaining)
493 public Sone addLikedPostId(String postId) {
494 likedPostIds.add(postId);
499 * Removes the given post ID from the list of posts this Sone likes.
503 * @return This Sone (for method chaining)
505 public Sone removeLikedPostId(String postId) {
506 likedPostIds.remove(postId);
511 * Returns the IDs of all liked replies.
513 * @return All liked replies’ IDs
515 public Set<String> getLikedReplyIds() {
516 return Collections.unmodifiableSet(likedReplyIds);
520 * Sets the IDs of all liked replies.
522 * @param likedReplyIds
523 * All liked replies’ IDs
524 * @return This Sone (for method chaining)
526 public Sone setLikeReplyIds(Set<String> likedReplyIds) {
527 this.likedReplyIds.clear();
528 this.likedReplyIds.addAll(likedReplyIds);
533 * Checks whether the given reply ID is liked by this Sone.
536 * The ID of the reply
537 * @return {@code true} if this Sone likes the given reply, {@code false}
540 public boolean isLikedReplyId(String replyId) {
541 return likedReplyIds.contains(replyId);
545 * Adds the given reply ID to the list of replies this Sone likes.
548 * The ID of the reply
549 * @return This Sone (for method chaining)
551 public Sone addLikedReplyId(String replyId) {
552 likedReplyIds.add(replyId);
557 * Removes the given post ID from the list of replies this Sone likes.
560 * The ID of the reply
561 * @return This Sone (for method chaining)
563 public Sone removeLikedReplyId(String replyId) {
564 likedReplyIds.remove(replyId);
569 * Returns the root album that contains all visible albums of this Sone.
571 * @return The root album of this Sone
573 public Album getRootAlbum() {
578 * Returns Sone-specific options.
580 * @return The options of this Sone
582 public SoneOptions getOptions() {
587 * Sets the options of this Sone.
590 * The options of this Sone
592 /* TODO - remove this method again, maybe add an option provider */
593 public void setOptions(SoneOptions options) {
594 this.options = options;
598 // FINGERPRINTABLE METHODS
603 public synchronized String getFingerprint() {
604 Hasher hash = Hashing.sha256().newHasher();
605 hash.putString(profile.getFingerprint());
607 hash.putString("Posts(");
608 for (Post post : getPosts()) {
609 hash.putString("Post(").putString(post.getId()).putString(")");
613 List<PostReply> replies = new ArrayList<PostReply>(getReplies());
614 Collections.sort(replies, Reply.TIME_COMPARATOR);
615 hash.putString("Replies(");
616 for (PostReply reply : replies) {
617 hash.putString("Reply(").putString(reply.getId()).putString(")");
621 List<String> likedPostIds = new ArrayList<String>(getLikedPostIds());
622 Collections.sort(likedPostIds);
623 hash.putString("LikedPosts(");
624 for (String likedPostId : likedPostIds) {
625 hash.putString("Post(").putString(likedPostId).putString(")");
629 List<String> likedReplyIds = new ArrayList<String>(getLikedReplyIds());
630 Collections.sort(likedReplyIds);
631 hash.putString("LikedReplies(");
632 for (String likedReplyId : likedReplyIds) {
633 hash.putString("Reply(").putString(likedReplyId).putString(")");
637 hash.putString("Albums(");
638 for (Album album : rootAlbum.getAlbums()) {
639 if (!Album.NOT_EMPTY.apply(album)) {
642 hash.putString(album.getFingerprint());
646 return hash.hash().toString();
650 // INTERFACE Comparable<Sone>
655 public int compareTo(Sone sone) {
656 return NICE_NAME_COMPARATOR.compare(this, sone);
665 public int hashCode() {
666 return id.hashCode();
671 public boolean equals(Object object) {
672 if (!(object instanceof Sone)) {
675 return ((Sone) object).getId().equals(id);
680 public String toString() {
681 return getClass().getName() + "[identity=" + identity + ",posts(" + posts.size() + "),replies(" + replies.size() + "),albums(" + getRootAlbum().getAlbums().size() + ")]";