X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdatabase%2Fmemory%2FMemoryBookmarkDatabase.java;h=1a4e1193327c3c660a4df5473e40ecc625b91da5;hb=23e09dfc13a0cc6dfc7d7b8cc09c78acc6984ec7;hp=78c8a4f093b421465ae8271f564bad4b276a2a12;hpb=36b8e20651c14970ca30a1b8ed43af219b367bc9;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 78c8a4f..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) {
@@ -45,13 +45,16 @@ public class MemoryBookmarkDatabase implements BookmarkDatabase {
}
}
- @Override
- public void bookmarkPost(String postId) {
- lock.writeLock().lock();
+ public void stop() {
+ saveBookmarkedPosts();
+ }
+
+ private void saveBookmarkedPosts() {
+ lock.readLock().lock();
try {
- bookmarkedPosts.add(postId);
+ configurationLoader.saveBookmarkedPosts(this.bookmarkedPosts);
} finally {
- lock.writeLock().unlock();
+ lock.readLock().unlock();
}
}
@@ -60,6 +63,7 @@ public class MemoryBookmarkDatabase implements BookmarkDatabase {
lock.writeLock().lock();
try {
bookmarkedPosts.add(post.getId());
+ saveBookmarkedPosts();
} finally {
lock.writeLock().unlock();
}
@@ -70,6 +74,7 @@ public class MemoryBookmarkDatabase implements BookmarkDatabase {
lock.writeLock().lock();
try {
bookmarkedPosts.remove(post.getId());
+ saveBookmarkedPosts();
} finally {
lock.writeLock().unlock();
}
@@ -89,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 {