✅ Add tests for WOTC.getTrust
[Sone.git] / src / main / java / net / pterodactylus / sone / database / memory / MemoryBookmarkDatabase.java
1 package net.pterodactylus.sone.database.memory;
2
3 import static com.google.common.base.Optional.fromNullable;
4 import static com.google.common.collect.FluentIterable.from;
5
6 import java.util.HashSet;
7 import java.util.Set;
8 import java.util.concurrent.locks.ReadWriteLock;
9 import java.util.concurrent.locks.ReentrantReadWriteLock;
10
11 import net.pterodactylus.sone.data.Post;
12 import net.pterodactylus.sone.data.Post.EmptyPost;
13 import net.pterodactylus.sone.database.BookmarkDatabase;
14
15 import com.google.common.base.Function;
16
17 /**
18  * Memory-based {@link BookmarkDatabase} implementation.
19  */
20 public class MemoryBookmarkDatabase implements BookmarkDatabase {
21
22         private final ReadWriteLock lock = new ReentrantReadWriteLock();
23         private final MemoryDatabase memoryDatabase;
24         private final ConfigurationLoader configurationLoader;
25         private final Set<String> bookmarkedPosts = new HashSet<>();
26
27         public MemoryBookmarkDatabase(MemoryDatabase memoryDatabase,
28                         ConfigurationLoader configurationLoader) {
29                 this.memoryDatabase = memoryDatabase;
30                 this.configurationLoader = configurationLoader;
31         }
32
33         public void start() {
34                 loadBookmarkedPosts();
35         }
36
37         private void loadBookmarkedPosts() {
38                 Set<String> bookmarkedPosts = configurationLoader.loadBookmarkedPosts();
39                 lock.writeLock().lock();
40                 try {
41                         this.bookmarkedPosts.clear();
42                         this.bookmarkedPosts.addAll(bookmarkedPosts);
43                 } finally {
44                         lock.writeLock().unlock();
45                 }
46         }
47
48         public void stop() {
49                 saveBookmarkedPosts();
50         }
51
52         private void saveBookmarkedPosts() {
53                 lock.readLock().lock();
54                 try {
55                         configurationLoader.saveBookmarkedPosts(this.bookmarkedPosts);
56                 } finally {
57                         lock.readLock().unlock();
58                 }
59         }
60
61         @Override
62         public void bookmarkPost(Post post) {
63                 lock.writeLock().lock();
64                 try {
65                         bookmarkedPosts.add(post.getId());
66                         saveBookmarkedPosts();
67                 } finally {
68                         lock.writeLock().unlock();
69                 }
70         }
71
72         @Override
73         public void unbookmarkPost(Post post) {
74                 lock.writeLock().lock();
75                 try {
76                         bookmarkedPosts.remove(post.getId());
77                         saveBookmarkedPosts();
78                 } finally {
79                         lock.writeLock().unlock();
80                 }
81         }
82
83         @Override
84         public boolean isPostBookmarked(Post post) {
85                 lock.readLock().lock();
86                 try {
87                         return bookmarkedPosts.contains(post.getId());
88                 } finally {
89                         lock.readLock().unlock();
90                 }
91         }
92
93         @Override
94         public Set<Post> getBookmarkedPosts() {
95                 lock.readLock().lock();
96                 try {
97                         return from(bookmarkedPosts).transform(
98                                         new Function<String, Post>() {
99                                                 @Override
100                                                 public Post apply(String postId) {
101                                                         return fromNullable(memoryDatabase.getPost(postId))
102                                                                         .or(new EmptyPost(postId));
103                                                 }
104                                         }).toSet();
105                 } finally {
106                         lock.readLock().unlock();
107                 }
108         }
109
110 }