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;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.List;
27 import java.util.concurrent.CopyOnWriteArraySet;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
31 import net.pterodactylus.sone.core.Options;
32 import net.pterodactylus.sone.data.Album;
33 import net.pterodactylus.sone.data.Client;
34 import net.pterodactylus.sone.data.Post;
35 import net.pterodactylus.sone.data.PostReply;
36 import net.pterodactylus.sone.data.Profile;
37 import net.pterodactylus.sone.data.Reply;
38 import net.pterodactylus.sone.data.Sone;
39 import net.pterodactylus.sone.database.AlbumBuilder;
40 import net.pterodactylus.sone.database.Database;
41 import net.pterodactylus.sone.database.PostBuilder;
42 import net.pterodactylus.sone.database.PostReplyBuilder;
43 import net.pterodactylus.sone.freenet.wot.Identity;
44 import net.pterodactylus.util.logging.Logging;
46 import freenet.keys.FreenetURI;
48 import com.google.common.base.Optional;
49 import com.google.common.hash.Hasher;
50 import com.google.common.hash.Hashing;
53 * Dumb, store-everything-in-memory {@link Sone} implementation.
55 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
57 public class DefaultSone implements Sone {
60 private static final Logger logger = Logging.getLogger(DefaultSone.class);
63 private final Database database;
65 /** The ID of this Sone. */
66 private final String id;
68 /** Whether the Sone is local. */
69 private final boolean local;
71 /** The identity of this Sone. */
72 private Identity identity;
74 /** The URI under which the Sone is stored in Freenet. */
75 private volatile FreenetURI requestUri;
77 /** The URI used to insert a new version of this Sone. */
78 /* This will be null for remote Sones! */
79 private volatile FreenetURI insertUri;
81 /** The latest edition of the Sone. */
82 private volatile long latestEdition;
84 /** The time of the last inserted update. */
85 private volatile long time;
87 /** The status of this Sone. */
88 private volatile SoneStatus status = SoneStatus.unknown;
90 /** The profile of this Sone. */
91 private volatile Profile profile = new Profile(this);
93 /** The client used by the Sone. */
94 private volatile Client client;
96 /** Whether this Sone is known. */
97 private volatile boolean known;
99 /** All friend Sones. */
100 private final Set<String> friendSones = new CopyOnWriteArraySet<String>();
103 private final Set<Post> posts = new CopyOnWriteArraySet<Post>();
106 private final Set<PostReply> replies = new CopyOnWriteArraySet<PostReply>();
108 /** The IDs of all liked posts. */
109 private final Set<String> likedPostIds = new CopyOnWriteArraySet<String>();
111 /** The IDs of all liked replies. */
112 private final Set<String> likedReplyIds = new CopyOnWriteArraySet<String>();
114 /** The root album containing all albums. */
115 private final Album rootAlbum;
117 /** Sone-specific options. */
118 private Options options = new Options();
121 * Creates a new Sone.
126 * {@code true} if the Sone is a local Sone, {@code false} otherwise
128 public DefaultSone(Database database, String id, boolean local) {
129 this.database = database;
132 rootAlbum = new DefaultAlbumBuilder(database, this, null).build();
139 public String getId() {
143 public Identity getIdentity() {
147 public DefaultSone setIdentity(Identity identity) throws IllegalArgumentException {
148 if (!identity.getId().equals(id)) {
149 throw new IllegalArgumentException("Identity’s ID does not match Sone’s ID!");
151 this.identity = identity;
155 public String getName() {
156 return (identity != null) ? identity.getNickname() : null;
159 public boolean isLocal() {
163 public FreenetURI getRequestUri() {
164 return (requestUri != null) ? requestUri.setSuggestedEdition(latestEdition) : null;
167 public Sone setRequestUri(FreenetURI requestUri) {
168 if (this.requestUri == null) {
169 this.requestUri = requestUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
172 if (!this.requestUri.equalsKeypair(requestUri)) {
173 logger.log(Level.WARNING, String.format("Request URI %s tried to overwrite %s!", requestUri, this.requestUri));
179 public FreenetURI getInsertUri() {
180 return (insertUri != null) ? insertUri.setSuggestedEdition(latestEdition) : null;
183 public Sone setInsertUri(FreenetURI insertUri) {
184 if (this.insertUri == null) {
185 this.insertUri = insertUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
188 if (!this.insertUri.equalsKeypair(insertUri)) {
189 logger.log(Level.WARNING, String.format("Request URI %s tried to overwrite %s!", insertUri, this.insertUri));
195 public long getLatestEdition() {
196 return latestEdition;
199 public void setLatestEdition(long latestEdition) {
200 if (!(latestEdition > this.latestEdition)) {
201 logger.log(Level.FINE, String.format("New latest edition %d is not greater than current latest edition %d!", latestEdition, this.latestEdition));
204 this.latestEdition = latestEdition;
207 public long getTime() {
211 public Sone setTime(long time) {
216 public SoneStatus getStatus() {
220 public Sone setStatus(SoneStatus status) {
221 this.status = checkNotNull(status, "status must not be null");
225 public Profile getProfile() {
226 return new Profile(profile);
229 public void setProfile(Profile profile) {
230 this.profile = new Profile(profile);
233 public Client getClient() {
237 public Sone setClient(Client client) {
238 this.client = client;
242 public boolean isKnown() {
246 public Sone setKnown(boolean known) {
251 public List<String> getFriends() {
252 List<String> friends = new ArrayList<String>(friendSones);
256 public boolean hasFriend(String friendSoneId) {
257 return friendSones.contains(friendSoneId);
260 public Sone addFriend(String friendSone) {
261 if (!friendSone.equals(id)) {
262 friendSones.add(friendSone);
267 public Sone removeFriend(String friendSoneId) {
268 friendSones.remove(friendSoneId);
272 public List<Post> getPosts() {
273 List<Post> sortedPosts;
274 synchronized (this) {
275 sortedPosts = new ArrayList<Post>(posts);
277 Collections.sort(sortedPosts, Post.TIME_COMPARATOR);
281 public Sone setPosts(Collection<Post> posts) {
282 synchronized (this) {
284 this.posts.addAll(posts);
289 public void addPost(Post post) {
290 if (post.getSone().equals(this) && posts.add(post)) {
291 logger.log(Level.FINEST, String.format("Adding %s to “%s”.", post, getName()));
295 public void removePost(Post post) {
296 if (post.getSone().equals(this)) {
301 public Set<PostReply> getReplies() {
302 return Collections.unmodifiableSet(replies);
305 public Sone setReplies(Collection<PostReply> replies) {
306 this.replies.clear();
307 this.replies.addAll(replies);
311 public void addReply(PostReply reply) {
312 if (reply.getSone().equals(this)) {
317 public void removeReply(PostReply reply) {
318 if (reply.getSone().equals(this)) {
319 replies.remove(reply);
323 public Set<String> getLikedPostIds() {
324 return Collections.unmodifiableSet(likedPostIds);
327 public Sone setLikePostIds(Set<String> likedPostIds) {
328 this.likedPostIds.clear();
329 this.likedPostIds.addAll(likedPostIds);
333 public boolean isLikedPostId(String postId) {
334 return likedPostIds.contains(postId);
337 public Sone addLikedPostId(String postId) {
338 likedPostIds.add(postId);
342 public Sone removeLikedPostId(String postId) {
343 likedPostIds.remove(postId);
347 public Set<String> getLikedReplyIds() {
348 return Collections.unmodifiableSet(likedReplyIds);
351 public Sone setLikeReplyIds(Set<String> likedReplyIds) {
352 this.likedReplyIds.clear();
353 this.likedReplyIds.addAll(likedReplyIds);
357 public boolean isLikedReplyId(String replyId) {
358 return likedReplyIds.contains(replyId);
361 public Sone addLikedReplyId(String replyId) {
362 likedReplyIds.add(replyId);
366 public Sone removeLikedReplyId(String replyId) {
367 likedReplyIds.remove(replyId);
371 public Album getRootAlbum() {
375 public Options getOptions() {
379 /* TODO - remove this method again, maybe add an option provider */
380 public void setOptions(Options options) {
381 this.options = options;
385 public AlbumBuilder newAlbumBuilder() {
386 return new DefaultAlbumBuilder(database, this, rootAlbum.getId());
389 public PostBuilder newPostBuilder() {
390 return new DefaultPostBuilder(database, getId()) {
392 public Post build(Optional<PostCreated> postCreated) {
393 Post post = super.build(postCreated);
394 database.storePost(post);
401 public PostReplyBuilder newPostReplyBuilder(String postId) throws IllegalStateException {
402 return new DefaultPostReplyBuilder(database, getId(), postId) {
404 public PostReply build(Optional<PostReplyCreated> postReplyCreated) {
405 PostReply postReply = super.build(postReplyCreated);
406 database.storePostReply(postReply);
413 // FINGERPRINTABLE METHODS
417 public synchronized String getFingerprint() {
418 Hasher hash = Hashing.sha256().newHasher();
419 hash.putString(profile.getFingerprint());
421 hash.putString("Posts(");
422 for (Post post : getPosts()) {
423 hash.putString("Post(").putString(post.getId()).putString(")");
427 List<PostReply> replies = new ArrayList<PostReply>(getReplies());
428 Collections.sort(replies, Reply.TIME_COMPARATOR);
429 hash.putString("Replies(");
430 for (PostReply reply : replies) {
431 hash.putString("Reply(").putString(reply.getId()).putString(")");
435 List<String> likedPostIds = new ArrayList<String>(getLikedPostIds());
436 Collections.sort(likedPostIds);
437 hash.putString("LikedPosts(");
438 for (String likedPostId : likedPostIds) {
439 hash.putString("Post(").putString(likedPostId).putString(")");
443 List<String> likedReplyIds = new ArrayList<String>(getLikedReplyIds());
444 Collections.sort(likedReplyIds);
445 hash.putString("LikedReplies(");
446 for (String likedReplyId : likedReplyIds) {
447 hash.putString("Reply(").putString(likedReplyId).putString(")");
451 hash.putString("Albums(");
452 for (Album album : rootAlbum.getAlbums()) {
453 if (!Album.NOT_EMPTY.apply(album)) {
456 hash.putString(album.getFingerprint());
460 return hash.hash().toString();
464 // INTERFACE Comparable<Sone>
468 public int compareTo(Sone sone) {
469 return NICE_NAME_COMPARATOR.compare(this, sone);
477 public int hashCode() {
478 return id.hashCode();
482 public boolean equals(Object object) {
483 if (!(object instanceof Sone)) {
486 return ((Sone) object).getId().equals(id);
490 public String toString() {
491 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() + ")]";