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 java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.List;
25 import java.util.concurrent.CopyOnWriteArraySet;
26 import java.util.logging.Level;
28 import net.pterodactylus.sone.data.Post;
29 import net.pterodactylus.sone.data.PostReply;
30 import net.pterodactylus.sone.data.Sone;
31 import net.pterodactylus.sone.database.AlbumBuilder;
34 * Dumb, store-everything-in-memory {@link Sone} implementation.
36 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38 public class DefaultSone extends AbstractSone {
41 private final Set<Post> posts = new CopyOnWriteArraySet<Post>();
44 private final Set<PostReply> replies = new CopyOnWriteArraySet<PostReply>();
46 /** The IDs of all liked posts. */
47 private final Set<String> likedPostIds = new CopyOnWriteArraySet<String>();
49 /** The IDs of all liked replies. */
50 private final Set<String> likedReplyIds = new CopyOnWriteArraySet<String>();
58 * {@code true} if the Sone is a local Sone, {@code false} otherwise
60 public DefaultSone(String id, boolean local) {
69 * Returns whether this Sone is known.
71 * @return {@code true} if this Sone is known, {@code false} otherwise
73 public boolean isKnown() {
78 * Sets whether this Sone is known.
81 * {@code true} if this Sone is known, {@code false} otherwise
84 public Sone setKnown(boolean known) {
90 * Returns all friend Sones of this Sone.
92 * @return The friend Sones of this Sone
94 public List<String> getFriends() {
95 List<String> friends = new ArrayList<String>(friendSones);
100 * Returns whether this Sone has the given Sone as a friend Sone.
102 * @param friendSoneId
103 * The ID of the Sone to check for
104 * @return {@code true} if this Sone has the given Sone as a friend, {@code
107 public boolean hasFriend(String friendSoneId) {
108 return friendSones.contains(friendSoneId);
112 * Adds the given Sone as a friend Sone.
115 * The friend Sone to add
116 * @return This Sone (for method chaining)
118 public Sone addFriend(String friendSone) {
119 if (!friendSone.equals(id)) {
120 friendSones.add(friendSone);
126 * Removes the given Sone as a friend Sone.
128 * @param friendSoneId
129 * The ID of the friend Sone to remove
130 * @return This Sone (for method chaining)
132 public Sone removeFriend(String friendSoneId) {
133 friendSones.remove(friendSoneId);
138 * Returns the list of posts of this Sone, sorted by time, newest first.
140 * @return All posts of this Sone
142 public List<Post> getPosts() {
143 List<Post> sortedPosts;
144 synchronized (this) {
145 sortedPosts = new ArrayList<Post>(posts);
147 Collections.sort(sortedPosts, Post.TIME_COMPARATOR);
152 * Sets all posts of this Sone at once.
155 * The new (and only) posts of this Sone
156 * @return This Sone (for method chaining)
158 public Sone setPosts(Collection<Post> posts) {
159 synchronized (this) {
161 this.posts.addAll(posts);
167 * Adds the given post to this Sone. The post will not be added if its {@link
168 * Post#getSone() Sone} is not this Sone.
173 public void addPost(Post post) {
174 if (post.getSone().equals(this) && posts.add(post)) {
175 logger.log(Level.FINEST, String.format("Adding %s to “%s”.", post, getName()));
180 * Removes the given post from this Sone.
185 public void removePost(Post post) {
186 if (post.getSone().equals(this)) {
192 * Returns all replies this Sone made.
194 * @return All replies this Sone made
196 public Set<PostReply> getReplies() {
197 return Collections.unmodifiableSet(replies);
201 * Sets all replies of this Sone at once.
204 * The new (and only) replies of this Sone
205 * @return This Sone (for method chaining)
207 public Sone setReplies(Collection<PostReply> replies) {
208 this.replies.clear();
209 this.replies.addAll(replies);
214 * Adds a reply to this Sone. If the given reply was not made by this Sone,
215 * nothing is added to this Sone.
220 public void addReply(PostReply reply) {
221 if (reply.getSone().equals(this)) {
227 * Removes a reply from this Sone.
230 * The reply to remove
232 public void removeReply(PostReply reply) {
233 if (reply.getSone().equals(this)) {
234 replies.remove(reply);
239 * Returns the IDs of all liked posts.
241 * @return All liked posts’ IDs
243 public Set<String> getLikedPostIds() {
244 return Collections.unmodifiableSet(likedPostIds);
248 * Sets the IDs of all liked posts.
250 * @param likedPostIds
251 * All liked posts’ IDs
252 * @return This Sone (for method chaining)
254 public Sone setLikePostIds(Set<String> likedPostIds) {
255 this.likedPostIds.clear();
256 this.likedPostIds.addAll(likedPostIds);
260 public boolean isLikedPostId(String postId) {
261 return likedPostIds.contains(postId);
264 public Sone addLikedPostId(String postId) {
265 likedPostIds.add(postId);
269 public Sone removeLikedPostId(String postId) {
270 likedPostIds.remove(postId);
274 public Set<String> getLikedReplyIds() {
275 return Collections.unmodifiableSet(likedReplyIds);
278 public Sone setLikeReplyIds(Set<String> likedReplyIds) {
279 this.likedReplyIds.clear();
280 this.likedReplyIds.addAll(likedReplyIds);
284 public boolean isLikedReplyId(String replyId) {
285 return likedReplyIds.contains(replyId);
288 public Sone addLikedReplyId(String replyId) {
289 likedReplyIds.add(replyId);
293 public Sone removeLikedReplyId(String replyId) {
294 likedReplyIds.remove(replyId);
299 public AlbumBuilder newAlbumBuilder() {
300 return new DefaultAlbumBuilder(this, rootAlbum);