X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=inline;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdatabase%2Fmemory%2FMemoryBookmarkDatabase.java;h=1a4e1193327c3c660a4df5473e40ecc625b91da5;hb=ae0140e054f0e2cb4e0385951224a625cdf7f9a2;hp=34ff024d0838c2d39480ff5e86da7f92338ae989;hpb=723de3522aa5c04634932f05bf4075304979c7a9;p=Sone.git
diff --git a/src/main/java/net/pterodactylus/sone/database/memory/MemoryBookmarkDatabase.java b/src/main/java/net/pterodactylus/sone/database/memory/MemoryBookmarkDatabase.java
index 34ff024..1a4e119 100644
--- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryBookmarkDatabase.java
+++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryBookmarkDatabase.java
@@ -1,5 +1,6 @@
package net.pterodactylus.sone.database.memory;
+import static com.google.common.base.Optional.fromNullable;
import static com.google.common.collect.FluentIterable.from;
import java.util.HashSet;
@@ -8,21 +9,20 @@ import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import net.pterodactylus.sone.data.Post;
+import net.pterodactylus.sone.data.Post.EmptyPost;
import net.pterodactylus.sone.database.BookmarkDatabase;
import com.google.common.base.Function;
/**
* Memory-based {@link BookmarkDatabase} implementation.
- *
- * @author David âBombeâ Roden
*/
public class MemoryBookmarkDatabase implements BookmarkDatabase {
private final ReadWriteLock lock = new ReentrantReadWriteLock();
private final MemoryDatabase memoryDatabase;
private final ConfigurationLoader configurationLoader;
- private final Set bookmarkedPosts = new HashSet();
+ private final Set bookmarkedPosts = new HashSet<>();
public MemoryBookmarkDatabase(MemoryDatabase memoryDatabase,
ConfigurationLoader configurationLoader) {
@@ -63,6 +63,7 @@ public class MemoryBookmarkDatabase implements BookmarkDatabase {
lock.writeLock().lock();
try {
bookmarkedPosts.add(post.getId());
+ saveBookmarkedPosts();
} finally {
lock.writeLock().unlock();
}
@@ -73,6 +74,7 @@ public class MemoryBookmarkDatabase implements BookmarkDatabase {
lock.writeLock().lock();
try {
bookmarkedPosts.remove(post.getId());
+ saveBookmarkedPosts();
} finally {
lock.writeLock().unlock();
}
@@ -92,11 +94,12 @@ public class MemoryBookmarkDatabase implements BookmarkDatabase {
public Set getBookmarkedPosts() {
lock.readLock().lock();
try {
- return from(bookmarkedPosts).transformAndConcat(
- new Function>() {
+ return from(bookmarkedPosts).transform(
+ new Function() {
@Override
- public Iterable apply(String postId) {
- return memoryDatabase.getPost(postId).asSet();
+ public Post apply(String postId) {
+ return fromNullable(memoryDatabase.getPost(postId))
+ .or(new EmptyPost(postId));
}
}).toSet();
} finally {