--- /dev/null
+package net.pterodactylus.sone.database.memory;
+
+import static com.google.common.collect.FluentIterable.from;
+
+import java.util.HashSet;
+import java.util.Set;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+import net.pterodactylus.sone.data.Post;
+import net.pterodactylus.sone.database.BookmarkDatabase;
+
+import com.google.common.base.Function;
+
+/**
+ * Memory-based {@link BookmarkDatabase} implementation.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class MemoryBookmarkDatabase implements BookmarkDatabase {
+
+ private final ReadWriteLock lock = new ReentrantReadWriteLock();
+ private final MemoryDatabase memoryDatabase;
+ private final Set<String> bookmarkedPosts = new HashSet<String>();
+
+ public MemoryBookmarkDatabase(MemoryDatabase memoryDatabase) {
+ this.memoryDatabase = memoryDatabase;
+ }
+
+ @Override
+ public void bookmarkPost(String postId) {
+ lock.writeLock().lock();
+ try {
+ bookmarkedPosts.add(postId);
+ } finally {
+ lock.writeLock().unlock();
+ }
+ }
+
+ @Override
+ public void bookmarkPost(Post post) {
+ lock.writeLock().lock();
+ try {
+ bookmarkedPosts.add(post.getId());
+ } finally {
+ lock.writeLock().unlock();
+ }
+ }
+
+ @Override
+ public void unbookmarkPost(Post post) {
+ lock.writeLock().lock();
+ try {
+ bookmarkedPosts.remove(post.getId());
+ } finally {
+ lock.writeLock().unlock();
+ }
+ }
+
+ @Override
+ public boolean isPostBookmarked(Post post) {
+ lock.readLock().lock();
+ try {
+ return bookmarkedPosts.contains(post.getId());
+ } finally {
+ lock.readLock().unlock();
+ }
+ }
+
+ @Override
+ public Set<Post> getBookmarkedPosts() {
+ lock.readLock().lock();
+ try {
+ return from(bookmarkedPosts).transformAndConcat(
+ new Function<String, Iterable<Post>>() {
+ @Override
+ public Iterable<Post> apply(String postId) {
+ return memoryDatabase.getPost(postId).asSet();
+ }
+ }).toSet();
+ } finally {
+ lock.readLock().unlock();
+ }
+ }
+
+}
--- /dev/null
+package net.pterodactylus.sone.database.memory;
+
+import static com.google.common.base.Optional.fromNullable;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.contains;
+import static org.hamcrest.Matchers.is;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import net.pterodactylus.sone.data.Post;
+
+import com.google.common.base.Optional;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
+/**
+ * Unit test for {@link MemoryBookmarkDatabase}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class MemoryBookmarkDatabaseTest {
+
+ private final MemoryDatabase memoryDatabase = mock(MemoryDatabase.class);
+ private final MemoryBookmarkDatabase bookmarkDatabase =
+ new MemoryBookmarkDatabase(memoryDatabase);
+ private final Map<String, Post> posts = new HashMap<String, Post>();
+
+ @Before
+ public void setupMemoryDatabase() {
+ when(memoryDatabase.getPost(anyString())).thenAnswer(
+ new Answer<Optional<Post>>() {
+ @Override
+ public Optional<Post> answer(
+ InvocationOnMock invocation) {
+ return fromNullable(
+ posts.get(invocation.getArguments()[0]));
+ }
+ });
+ }
+
+ @Before
+ public void setupPosts() {
+ createPost("PostId1");
+ createPost("PostId2");
+ }
+
+ private void createPost(String postId) {
+ Post post = mock(Post.class);
+ when(post.getId()).thenReturn(postId);
+ posts.put(postId, post);
+ }
+
+ @Test
+ public void bookmarkDatabaseRetainsBookmarkedPosts() {
+ Set<Post> allPosts = new HashSet<Post>(posts.values());
+ for (Post post : allPosts) {
+ bookmarkDatabase.bookmarkPost(post);
+ }
+ assertThat(bookmarkDatabase.getBookmarkedPosts(), is(allPosts));
+ for (Post post : allPosts) {
+ assertThat(bookmarkDatabase.isPostBookmarked(post), is(true));
+ }
+ }
+
+ @Test
+ public void removingABookmarkRemovesTheCorrectBookmark() {
+ Set<Post> allPosts = new HashSet<Post>(posts.values());
+ for (Post post : allPosts) {
+ bookmarkDatabase.bookmarkPost(post);
+ }
+ Post randomPost = posts.values().iterator().next();
+ bookmarkDatabase.unbookmarkPost(randomPost);
+ allPosts.remove(randomPost);
+ assertThat(bookmarkDatabase.getBookmarkedPosts(), is(allPosts));
+ for (Post post : posts.values()) {
+ assertThat(bookmarkDatabase.isPostBookmarked(post),
+ is(!post.equals(randomPost)));
+ }
+ }
+
+ @Test
+ public void addingABookmarkByIdBookmarksTheCorrectPost() {
+ Post randomPost = posts.values().iterator().next();
+ bookmarkDatabase.bookmarkPost(randomPost.getId());
+ assertThat(bookmarkDatabase.getBookmarkedPosts(),
+ contains(randomPost));
+ }
+
+}