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 URI under which the Sone is stored in Freenet. */
72 private volatile FreenetURI requestUri;
74 /** The URI used to insert a new version of this Sone. */
75 /* This will be null for remote Sones! */
76 private volatile FreenetURI insertUri;
78 /** The latest edition of the Sone. */
79 private volatile long latestEdition;
81 /** The time of the last inserted update. */
82 private volatile long time;
84 /** The status of this Sone. */
85 private volatile SoneStatus status = SoneStatus.unknown;
87 /** The profile of this Sone. */
88 private volatile Profile profile = new Profile(this);
90 /** The client used by the Sone. */
91 private volatile Client client;
93 /** Whether this Sone is known. */
94 private volatile boolean known;
96 /** All friend Sones. */
97 private final Set<String> friendSones = new CopyOnWriteArraySet<String>();
100 private final Set<Post> posts = new CopyOnWriteArraySet<Post>();
103 private final Set<PostReply> replies = new CopyOnWriteArraySet<PostReply>();
105 /** The IDs of all liked posts. */
106 private final Set<String> likedPostIds = new CopyOnWriteArraySet<String>();
108 /** The IDs of all liked replies. */
109 private final Set<String> likedReplyIds = new CopyOnWriteArraySet<String>();
111 /** The root album containing all albums. */
112 private final Album rootAlbum;
114 /** Sone-specific options. */
115 private Options options = new Options();
118 * Creates a new Sone.
123 * {@code true} if the Sone is a local Sone, {@code false} otherwise
125 public DefaultSone(Database database, String id, boolean local) {
126 this.database = database;
129 rootAlbum = new DefaultAlbumBuilder(database, this, null).build();
136 public String getId() {
140 public Identity getIdentity() {
141 return database.getIdentity(id).get();
144 public String getName() {
145 return getIdentity().getNickname();
148 public boolean isLocal() {
152 public FreenetURI getRequestUri() {
153 return (requestUri != null) ? requestUri.setSuggestedEdition(latestEdition) : null;
156 public Sone setRequestUri(FreenetURI requestUri) {
157 if (this.requestUri == null) {
158 this.requestUri = requestUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
161 if (!this.requestUri.equalsKeypair(requestUri)) {
162 logger.log(Level.WARNING, String.format("Request URI %s tried to overwrite %s!", requestUri, this.requestUri));
168 public FreenetURI getInsertUri() {
169 return (insertUri != null) ? insertUri.setSuggestedEdition(latestEdition) : null;
172 public Sone setInsertUri(FreenetURI insertUri) {
173 if (this.insertUri == null) {
174 this.insertUri = insertUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
177 if (!this.insertUri.equalsKeypair(insertUri)) {
178 logger.log(Level.WARNING, String.format("Request URI %s tried to overwrite %s!", insertUri, this.insertUri));
184 public long getLatestEdition() {
185 return latestEdition;
188 public void setLatestEdition(long latestEdition) {
189 if (!(latestEdition > this.latestEdition)) {
190 logger.log(Level.FINE, String.format("New latest edition %d is not greater than current latest edition %d!", latestEdition, this.latestEdition));
193 this.latestEdition = latestEdition;
196 public long getTime() {
200 public Sone setTime(long time) {
205 public SoneStatus getStatus() {
209 public Sone setStatus(SoneStatus status) {
210 this.status = checkNotNull(status, "status must not be null");
214 public Profile getProfile() {
215 return new Profile(profile);
218 public void setProfile(Profile profile) {
219 this.profile = new Profile(profile);
222 public Client getClient() {
226 public Sone setClient(Client client) {
227 this.client = client;
231 public boolean isKnown() {
235 public Sone setKnown(boolean known) {
240 public List<String> getFriends() {
241 List<String> friends = new ArrayList<String>(friendSones);
245 public boolean hasFriend(String friendSoneId) {
246 return friendSones.contains(friendSoneId);
249 public Sone addFriend(String friendSone) {
250 if (!friendSone.equals(id)) {
251 friendSones.add(friendSone);
256 public Sone removeFriend(String friendSoneId) {
257 friendSones.remove(friendSoneId);
261 public List<Post> getPosts() {
262 List<Post> sortedPosts;
263 synchronized (this) {
264 sortedPosts = new ArrayList<Post>(posts);
266 Collections.sort(sortedPosts, Post.TIME_COMPARATOR);
270 public Sone setPosts(Collection<Post> posts) {
271 synchronized (this) {
273 this.posts.addAll(posts);
278 public void addPost(Post post) {
279 if (post.getSone().equals(this) && posts.add(post)) {
280 logger.log(Level.FINEST, String.format("Adding %s to “%s”.", post, getName()));
284 public void removePost(Post post) {
285 if (post.getSone().equals(this)) {
290 public Set<PostReply> getReplies() {
291 return Collections.unmodifiableSet(replies);
294 public Sone setReplies(Collection<PostReply> replies) {
295 this.replies.clear();
296 this.replies.addAll(replies);
300 public void addReply(PostReply reply) {
301 if (reply.getSone().equals(this)) {
306 public void removeReply(PostReply reply) {
307 if (reply.getSone().equals(this)) {
308 replies.remove(reply);
312 public Set<String> getLikedPostIds() {
313 return Collections.unmodifiableSet(likedPostIds);
316 public Sone setLikePostIds(Set<String> likedPostIds) {
317 this.likedPostIds.clear();
318 this.likedPostIds.addAll(likedPostIds);
322 public boolean isLikedPostId(String postId) {
323 return likedPostIds.contains(postId);
326 public Sone addLikedPostId(String postId) {
327 likedPostIds.add(postId);
331 public Sone removeLikedPostId(String postId) {
332 likedPostIds.remove(postId);
336 public Set<String> getLikedReplyIds() {
337 return Collections.unmodifiableSet(likedReplyIds);
340 public Sone setLikeReplyIds(Set<String> likedReplyIds) {
341 this.likedReplyIds.clear();
342 this.likedReplyIds.addAll(likedReplyIds);
346 public boolean isLikedReplyId(String replyId) {
347 return likedReplyIds.contains(replyId);
350 public Sone addLikedReplyId(String replyId) {
351 likedReplyIds.add(replyId);
355 public Sone removeLikedReplyId(String replyId) {
356 likedReplyIds.remove(replyId);
360 public Album getRootAlbum() {
364 public Options getOptions() {
368 /* TODO - remove this method again, maybe add an option provider */
369 public void setOptions(Options options) {
370 this.options = options;
374 public AlbumBuilder newAlbumBuilder() {
375 return new DefaultAlbumBuilder(database, this, rootAlbum.getId());
378 public PostBuilder newPostBuilder() {
379 return new DefaultPostBuilder(database, getId()) {
381 public Post build(Optional<PostCreated> postCreated) {
382 Post post = super.build(postCreated);
383 database.storePost(post);
390 public PostReplyBuilder newPostReplyBuilder(String postId) throws IllegalStateException {
391 return new DefaultPostReplyBuilder(database, getId(), postId) {
393 public PostReply build(Optional<PostReplyCreated> postReplyCreated) {
394 PostReply postReply = super.build(postReplyCreated);
395 database.storePostReply(postReply);
402 // FINGERPRINTABLE METHODS
406 public synchronized String getFingerprint() {
407 Hasher hash = Hashing.sha256().newHasher();
408 hash.putString(profile.getFingerprint());
410 hash.putString("Posts(");
411 for (Post post : getPosts()) {
412 hash.putString("Post(").putString(post.getId()).putString(")");
416 List<PostReply> replies = new ArrayList<PostReply>(getReplies());
417 Collections.sort(replies, Reply.TIME_COMPARATOR);
418 hash.putString("Replies(");
419 for (PostReply reply : replies) {
420 hash.putString("Reply(").putString(reply.getId()).putString(")");
424 List<String> likedPostIds = new ArrayList<String>(getLikedPostIds());
425 Collections.sort(likedPostIds);
426 hash.putString("LikedPosts(");
427 for (String likedPostId : likedPostIds) {
428 hash.putString("Post(").putString(likedPostId).putString(")");
432 List<String> likedReplyIds = new ArrayList<String>(getLikedReplyIds());
433 Collections.sort(likedReplyIds);
434 hash.putString("LikedReplies(");
435 for (String likedReplyId : likedReplyIds) {
436 hash.putString("Reply(").putString(likedReplyId).putString(")");
440 hash.putString("Albums(");
441 for (Album album : rootAlbum.getAlbums()) {
442 if (!Album.NOT_EMPTY.apply(album)) {
445 hash.putString(album.getFingerprint());
449 return hash.hash().toString();
453 // INTERFACE Comparable<Sone>
457 public int compareTo(Sone sone) {
458 return NICE_NAME_COMPARATOR.compare(this, sone);
466 public int hashCode() {
467 return id.hashCode();
471 public boolean equals(Object object) {
472 if (!(object instanceof Sone)) {
475 return ((Sone) object).getId().equals(id);
479 public String toString() {
480 return getClass().getName() + "[id=" + id + ",requestUri=" + requestUri + ",insertUri(" + String.valueOf(insertUri).length() + "),friends(" + friendSones.size() + "),posts(" + posts.size() + "),replies(" + replies.size() + "),albums(" + getRootAlbum().getAlbums().size() + ")]";