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.HashSet;
29 import java.util.List;
31 import java.util.concurrent.CopyOnWriteArraySet;
32 import java.util.logging.Level;
33 import java.util.logging.Logger;
35 import net.pterodactylus.sone.data.Album;
36 import net.pterodactylus.sone.data.Client;
37 import net.pterodactylus.sone.data.LocalSone;
38 import net.pterodactylus.sone.data.Post;
39 import net.pterodactylus.sone.data.PostReply;
40 import net.pterodactylus.sone.data.Profile;
41 import net.pterodactylus.sone.data.Reply;
42 import net.pterodactylus.sone.data.Sone;
43 import net.pterodactylus.sone.data.SoneOptions;
44 import net.pterodactylus.sone.data.SoneOptions.DefaultSoneOptions;
45 import net.pterodactylus.sone.database.Database;
46 import net.pterodactylus.sone.freenet.wot.Identity;
47 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
49 import freenet.keys.FreenetURI;
51 import com.google.common.collect.FluentIterable;
52 import com.google.common.hash.Hasher;
53 import com.google.common.hash.Hashing;
56 * {@link Sone} implementation.
58 * Operations that modify the Sone need to synchronize on the Sone in question.
60 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
62 public class SoneImpl implements LocalSone {
65 private static final Logger logger = getLogger("Sone.Data");
68 private final Database database;
70 /** The ID of this Sone. */
71 private final String id;
73 /** Whether the Sone is local. */
74 private final boolean local;
76 /** The identity of this Sone. */
77 private final Identity identity;
79 /** The latest edition of the Sone. */
80 private volatile long latestEdition;
82 /** The time of the last inserted update. */
83 private final long time;
85 /** The status of this Sone. */
86 private volatile SoneStatus status = SoneStatus.unknown;
88 /** The profile of this Sone. */
89 private volatile Profile profile = new Profile(this);
91 /** The client used by the Sone. */
92 private final Client client;
94 /** Whether this Sone is known. */
95 private volatile boolean known;
98 private final Collection<Post> posts = new HashSet<Post>();
101 private final Set<PostReply> replies = new CopyOnWriteArraySet<PostReply>();
103 /** The IDs of all liked posts. */
104 private final Set<String> likedPostIds = new CopyOnWriteArraySet<String>();
106 /** The IDs of all liked replies. */
107 private final Set<String> likedReplyIds = new CopyOnWriteArraySet<String>();
109 /** The root album containing all albums. */
110 private final Album rootAlbum = new AlbumImpl(this);
112 /** Sone-specific options. */
113 private SoneOptions options = new DefaultSoneOptions();
116 * Creates a new Sone.
118 * @param database The database
120 * The identity of the Sone
122 * {@code true} if the Sone is a local Sone, {@code false} otherwise
124 public SoneImpl(Database database, Identity identity, boolean local, long time, Client client, Collection<Post> posts) {
125 this.database = database;
126 this.id = identity.getId();
127 this.identity = identity;
130 this.client = client;
131 this.posts.addAll(posts);
139 * Returns the identity of this Sone.
141 * @return The identity of this Sone
143 public String getId() {
148 * Returns the identity of this Sone.
150 * @return The identity of this Sone
152 public Identity getIdentity() {
157 * Returns the name of this Sone.
159 * @return The name of this Sone
161 public String getName() {
162 return (identity != null) ? identity.getNickname() : null;
166 * Returns whether this Sone is a local Sone.
168 * @return {@code true} if this Sone is a local Sone, {@code false} otherwise
170 public boolean isLocal() {
175 * Returns the request URI of this Sone.
177 * @return The request URI of this Sone
179 public FreenetURI getRequestUri() {
181 return new FreenetURI(getIdentity().getRequestUri())
184 .setMetaString(new String[0])
185 .setSuggestedEdition(latestEdition);
186 } catch (MalformedURLException e) {
187 throw new IllegalStateException(
188 format("Identity %s's request URI is incorrect.",
194 * Returns the insert URI of this Sone.
196 * @return The insert URI of this Sone
198 public FreenetURI getInsertUri() {
203 return new FreenetURI(((OwnIdentity) getIdentity()).getInsertUri())
205 .setMetaString(new String[0])
206 .setSuggestedEdition(latestEdition);
207 } catch (MalformedURLException e) {
208 throw new IllegalStateException(format("Own identity %s's insert URI is incorrect.", getIdentity()), e);
213 * Returns the latest edition of this Sone.
215 * @return The latest edition of this Sone
217 public long getLatestEdition() {
218 return latestEdition;
222 * Sets the latest edition of this Sone. If the given latest edition is not
223 * greater than the current latest edition, the latest edition of this Sone is
226 * @param latestEdition
227 * The latest edition of this Sone
229 public void setLatestEdition(long latestEdition) {
230 if (!(latestEdition > this.latestEdition)) {
231 logger.log(Level.FINE, String.format("New latest edition %d is not greater than current latest edition %d!", latestEdition, this.latestEdition));
234 this.latestEdition = latestEdition;
238 * Return the time of the last inserted update of this Sone.
240 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
242 public long getTime() {
247 * Returns the status of this Sone.
249 * @return The status of this Sone
251 public SoneStatus getStatus() {
256 * Sets the new status of this Sone.
259 * The new status of this Sone
261 * @throws IllegalArgumentException
262 * if {@code status} is {@code null}
264 public Sone setStatus(SoneStatus status) {
265 this.status = checkNotNull(status, "status must not be null");
270 * Returns a copy of the profile. If you want to update values in the profile
271 * of this Sone, update the values in the returned {@link Profile} and use
272 * {@link #setProfile(Profile)} to change the profile in this Sone.
274 * @return A copy of the profile
276 public Profile getProfile() {
277 return new Profile(profile);
281 * Sets the profile of this Sone. A copy of the given profile is stored so that
282 * subsequent modifications of the given profile are not reflected in this
288 public void setProfile(Profile profile) {
289 this.profile = new Profile(profile);
293 * Returns the client used by this Sone.
295 * @return The client used by this Sone, or {@code null}
297 public Client getClient() {
302 * Sets the client used by this Sone.
305 * The client used by this Sone, or {@code null}
306 * @return This Sone (for method chaining)
308 public Sone setClient(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 Collection<String> getFriends() {
339 return database.getFriends(this);
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 database.isFriend(this, friendSoneId);
355 * Returns the list of posts of this Sone, sorted by time, newest first.
357 * @return All posts of this Sone
359 public List<Post> getPosts() {
360 synchronized (this) {
361 return FluentIterable.from(posts).toSortedList(Post.TIME_COMPARATOR);
366 * Returns all replies this Sone made.
368 * @return All replies this Sone made
370 public Set<PostReply> getReplies() {
371 return Collections.unmodifiableSet(replies);
375 * Sets all replies of this Sone at once.
378 * The new (and only) replies of this Sone
379 * @return This Sone (for method chaining)
381 public Sone setReplies(Collection<PostReply> replies) {
382 this.replies.clear();
383 this.replies.addAll(replies);
388 * Adds a reply to this Sone. If the given reply was not made by this Sone,
389 * nothing is added to this Sone.
394 public void addReply(PostReply reply) {
395 if (reply.getSone().equals(this)) {
401 * Removes a reply from this Sone.
404 * The reply to remove
406 public void removeReply(PostReply reply) {
407 if (reply.getSone().equals(this)) {
408 replies.remove(reply);
413 * Returns the IDs of all liked posts.
415 * @return All liked posts’ IDs
417 public Set<String> getLikedPostIds() {
418 return Collections.unmodifiableSet(likedPostIds);
422 * Sets the IDs of all liked posts.
424 * @param likedPostIds
425 * All liked posts’ IDs
426 * @return This Sone (for method chaining)
428 public Sone setLikePostIds(Set<String> likedPostIds) {
429 this.likedPostIds.clear();
430 this.likedPostIds.addAll(likedPostIds);
435 * Checks whether the given post ID is liked by this Sone.
439 * @return {@code true} if this Sone likes the given post, {@code false}
442 public boolean isLikedPostId(String postId) {
443 return likedPostIds.contains(postId);
447 * Adds the given post ID to the list of posts this Sone likes.
451 * @return This Sone (for method chaining)
453 public Sone addLikedPostId(String postId) {
454 likedPostIds.add(postId);
459 * Removes the given post ID from the list of posts this Sone likes.
463 * @return This Sone (for method chaining)
465 public Sone removeLikedPostId(String postId) {
466 likedPostIds.remove(postId);
471 * Returns the IDs of all liked replies.
473 * @return All liked replies’ IDs
475 public Set<String> getLikedReplyIds() {
476 return Collections.unmodifiableSet(likedReplyIds);
480 * Sets the IDs of all liked replies.
482 * @param likedReplyIds
483 * All liked replies’ IDs
484 * @return This Sone (for method chaining)
486 public Sone setLikeReplyIds(Set<String> likedReplyIds) {
487 this.likedReplyIds.clear();
488 this.likedReplyIds.addAll(likedReplyIds);
493 * Checks whether the given reply ID is liked by this Sone.
496 * The ID of the reply
497 * @return {@code true} if this Sone likes the given reply, {@code false}
500 public boolean isLikedReplyId(String replyId) {
501 return likedReplyIds.contains(replyId);
505 * Adds the given reply ID to the list of replies this Sone likes.
508 * The ID of the reply
509 * @return This Sone (for method chaining)
511 public Sone addLikedReplyId(String replyId) {
512 likedReplyIds.add(replyId);
517 * Removes the given post ID from the list of replies this Sone likes.
520 * The ID of the reply
521 * @return This Sone (for method chaining)
523 public Sone removeLikedReplyId(String replyId) {
524 likedReplyIds.remove(replyId);
529 * Returns the root album that contains all visible albums of this Sone.
531 * @return The root album of this Sone
533 public Album getRootAlbum() {
538 * Returns Sone-specific options.
540 * @return The options of this Sone
542 public SoneOptions getOptions() {
547 * Sets the options of this Sone.
550 * The options of this Sone
552 /* TODO - remove this method again, maybe add an option provider */
553 public void setOptions(SoneOptions options) {
554 this.options = options;
558 // FINGERPRINTABLE METHODS
563 public synchronized String getFingerprint() {
564 Hasher hash = Hashing.sha256().newHasher();
565 hash.putString(profile.getFingerprint());
567 hash.putString("Posts(");
568 for (Post post : getPosts()) {
569 hash.putString("Post(").putString(post.getId()).putString(")");
573 List<PostReply> replies = new ArrayList<PostReply>(getReplies());
574 Collections.sort(replies, Reply.TIME_COMPARATOR);
575 hash.putString("Replies(");
576 for (PostReply reply : replies) {
577 hash.putString("Reply(").putString(reply.getId()).putString(")");
581 List<String> likedPostIds = new ArrayList<String>(getLikedPostIds());
582 Collections.sort(likedPostIds);
583 hash.putString("LikedPosts(");
584 for (String likedPostId : likedPostIds) {
585 hash.putString("Post(").putString(likedPostId).putString(")");
589 List<String> likedReplyIds = new ArrayList<String>(getLikedReplyIds());
590 Collections.sort(likedReplyIds);
591 hash.putString("LikedReplies(");
592 for (String likedReplyId : likedReplyIds) {
593 hash.putString("Reply(").putString(likedReplyId).putString(")");
597 hash.putString("Albums(");
598 for (Album album : rootAlbum.getAlbums()) {
599 if (!Album.NOT_EMPTY.apply(album)) {
602 hash.putString(album.getFingerprint());
606 return hash.hash().toString();
610 // INTERFACE Comparable<Sone>
615 public int compareTo(Sone sone) {
616 return NICE_NAME_COMPARATOR.compare(this, sone);
625 public int hashCode() {
626 return id.hashCode();
631 public boolean equals(Object object) {
632 if (!(object instanceof Sone)) {
635 return ((Sone) object).getId().equals(id);
640 public String toString() {
641 return getClass().getName() + "[identity=" + identity + ",posts(" + posts.size() + "),replies(" + replies.size() + "),albums(" + getRootAlbum().getAlbums().size() + ")]";