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 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 final 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;
107 /** Sone-specific options. */
108 private Options options = new Options();
111 * Creates a new Sone.
116 * {@code true} if the Sone is a local Sone, {@code false} otherwise
118 public DefaultSone(Database database, String id, boolean local, Client client) {
119 this.database = database;
122 this.client = client;
123 rootAlbum = new DefaultAlbumBuilder(database, this, null).build();
130 public String getId() {
134 public Identity getIdentity() {
135 return database.getIdentity(id).get();
138 public String getName() {
139 return getIdentity().getNickname();
142 public boolean isLocal() {
146 public long getLatestEdition() {
147 return latestEdition;
150 public void setLatestEdition(long latestEdition) {
151 if (!(latestEdition > this.latestEdition)) {
152 logger.log(Level.FINE, String.format("New latest edition %d is not greater than current latest edition %d!", latestEdition, this.latestEdition));
155 this.latestEdition = latestEdition;
158 public long getTime() {
162 public Sone setTime(long time) {
167 public SoneStatus getStatus() {
171 public Sone setStatus(SoneStatus status) {
172 this.status = checkNotNull(status, "status must not be null");
176 public Profile getProfile() {
177 return new Profile(profile);
180 public void setProfile(Profile profile) {
181 this.profile = new Profile(profile);
184 public Client getClient() {
188 public boolean isKnown() {
192 public Sone setKnown(boolean known) {
197 public List<String> getFriends() {
198 List<String> friends = new ArrayList<String>(friendSones);
202 public boolean hasFriend(String friendSoneId) {
203 return friendSones.contains(friendSoneId);
206 public Sone addFriend(String friendSone) {
207 if (!friendSone.equals(id)) {
208 friendSones.add(friendSone);
213 public Sone removeFriend(String friendSoneId) {
214 friendSones.remove(friendSoneId);
218 public List<Post> getPosts() {
219 List<Post> sortedPosts;
220 synchronized (this) {
221 sortedPosts = new ArrayList<Post>(posts);
223 Collections.sort(sortedPosts, Post.TIME_COMPARATOR);
227 public Sone setPosts(Collection<Post> posts) {
228 synchronized (this) {
230 this.posts.addAll(posts);
235 public void addPost(Post post) {
236 if (post.getSone().equals(this) && posts.add(post)) {
237 logger.log(Level.FINEST, String.format("Adding %s to “%s”.", post, getName()));
241 public void removePost(Post post) {
242 if (post.getSone().equals(this)) {
247 public Set<PostReply> getReplies() {
248 return Collections.unmodifiableSet(replies);
251 public Sone setReplies(Collection<PostReply> replies) {
252 this.replies.clear();
253 this.replies.addAll(replies);
257 public void addReply(PostReply reply) {
258 if (reply.getSone().equals(this)) {
263 public void removeReply(PostReply reply) {
264 if (reply.getSone().equals(this)) {
265 replies.remove(reply);
269 public Set<String> getLikedPostIds() {
270 return Collections.unmodifiableSet(likedPostIds);
273 public Sone setLikePostIds(Set<String> likedPostIds) {
274 this.likedPostIds.clear();
275 this.likedPostIds.addAll(likedPostIds);
279 public boolean isLikedPostId(String postId) {
280 return likedPostIds.contains(postId);
283 public Sone removeLikedPostId(String postId) {
284 likedPostIds.remove(postId);
288 public Set<String> getLikedReplyIds() {
289 return Collections.unmodifiableSet(likedReplyIds);
292 public Sone setLikeReplyIds(Set<String> likedReplyIds) {
293 this.likedReplyIds.clear();
294 this.likedReplyIds.addAll(likedReplyIds);
298 public boolean isLikedReplyId(String replyId) {
299 return likedReplyIds.contains(replyId);
302 public Sone addLikedReplyId(String replyId) {
303 likedReplyIds.add(replyId);
307 public Sone removeLikedReplyId(String replyId) {
308 likedReplyIds.remove(replyId);
312 public Album getRootAlbum() {
316 public Options getOptions() {
320 /* TODO - remove this method again, maybe add an option provider */
321 public void setOptions(Options options) {
322 this.options = options;
326 public AlbumBuilder newAlbumBuilder() {
327 return new DefaultAlbumBuilder(database, this, rootAlbum.getId());
330 public PostBuilder newPostBuilder() {
331 return new DefaultPostBuilder(database, getId()) {
333 public Post build(Optional<PostCreated> postCreated) {
334 Post post = super.build(postCreated);
335 database.storePost(post);
342 public PostReplyBuilder newPostReplyBuilder(String postId) throws IllegalStateException {
343 return new DefaultPostReplyBuilder(database, getId(), postId) {
345 public PostReply build(Optional<PostReplyCreated> postReplyCreated) {
346 PostReply postReply = super.build(postReplyCreated);
347 database.storePostReply(postReply);
353 public Modifier modify() {
354 return new Modifier() {
355 private long latestEdition = DefaultSone.this.latestEdition;
357 public Modifier setLatestEdition(long latestEdition) {
358 this.latestEdition = latestEdition;
363 public Sone update() {
364 DefaultSone.this.latestEdition = latestEdition;
365 return DefaultSone.this;
371 // FINGERPRINTABLE METHODS
375 public synchronized String getFingerprint() {
376 Hasher hash = Hashing.sha256().newHasher();
377 hash.putString(profile.getFingerprint());
379 hash.putString("Posts(");
380 for (Post post : getPosts()) {
381 hash.putString("Post(").putString(post.getId()).putString(")");
385 List<PostReply> replies = new ArrayList<PostReply>(getReplies());
386 Collections.sort(replies, Reply.TIME_COMPARATOR);
387 hash.putString("Replies(");
388 for (PostReply reply : replies) {
389 hash.putString("Reply(").putString(reply.getId()).putString(")");
393 List<String> likedPostIds = new ArrayList<String>(getLikedPostIds());
394 Collections.sort(likedPostIds);
395 hash.putString("LikedPosts(");
396 for (String likedPostId : likedPostIds) {
397 hash.putString("Post(").putString(likedPostId).putString(")");
401 List<String> likedReplyIds = new ArrayList<String>(getLikedReplyIds());
402 Collections.sort(likedReplyIds);
403 hash.putString("LikedReplies(");
404 for (String likedReplyId : likedReplyIds) {
405 hash.putString("Reply(").putString(likedReplyId).putString(")");
409 hash.putString("Albums(");
410 for (Album album : rootAlbum.getAlbums()) {
411 if (!Album.NOT_EMPTY.apply(album)) {
414 hash.putString(album.getFingerprint());
418 return hash.hash().toString();
422 // INTERFACE Comparable<Sone>
426 public int compareTo(Sone sone) {
427 return NICE_NAME_COMPARATOR.compare(this, sone);
435 public int hashCode() {
436 return id.hashCode();
440 public boolean equals(Object object) {
441 if (!(object instanceof Sone)) {
444 return ((Sone) object).getId().equals(id);
448 public String toString() {
449 return getClass().getName() + "[id=" + id + ",friends(" + friendSones.size() + "),posts(" + posts.size() + "),replies(" + replies.size() + "),albums(" + getRootAlbum().getAlbums().size() + ")]";