return knownPostReplies;
}
+ public synchronized Set<String> loadBookmarkedPosts() {
+ Set<String> bookmarkedPosts = new HashSet<String>();
+ int postCounter = 0;
+ while (true) {
+ String bookmarkedPostId = configuration
+ .getStringValue("Bookmarks/Post/" + postCounter++ + "/ID")
+ .getValue(null);
+ if (bookmarkedPostId == null) {
+ break;
+ }
+ bookmarkedPosts.add(bookmarkedPostId);
+ }
+ return bookmarkedPosts;
+ }
+
}
private final ReadWriteLock lock = new ReentrantReadWriteLock();
private final MemoryDatabase memoryDatabase;
+ private final ConfigurationLoader configurationLoader;
private final Set<String> bookmarkedPosts = new HashSet<String>();
- public MemoryBookmarkDatabase(MemoryDatabase memoryDatabase) {
+ public MemoryBookmarkDatabase(MemoryDatabase memoryDatabase,
+ ConfigurationLoader configurationLoader) {
this.memoryDatabase = memoryDatabase;
+ this.configurationLoader = configurationLoader;
+ }
+
+ public void start() {
+ loadBookmarkedPosts();
+ }
+
+ private void loadBookmarkedPosts() {
+ Set<String> bookmarkedPosts = configurationLoader.loadBookmarkedPosts();
+ lock.writeLock().lock();
+ try {
+ this.bookmarkedPosts.clear();
+ this.bookmarkedPosts.addAll(bookmarkedPosts);
+ } finally {
+ lock.writeLock().unlock();
+ }
}
@Override
private final Map<String, Image> allImages = new HashMap<String, Image>();
private final Multimap<String, Image> soneImages = HashMultimap.create();
- private final MemoryBookmarkDatabase memoryBookmarkDatabase =
- new MemoryBookmarkDatabase(this);
+ private final MemoryBookmarkDatabase memoryBookmarkDatabase;
/**
* Creates a new memory database.
this.soneProvider = soneProvider;
this.configuration = configuration;
this.configurationLoader = new ConfigurationLoader(configuration);
+ memoryBookmarkDatabase =
+ new MemoryBookmarkDatabase(this, configurationLoader);
}
//
/** {@inheritDocs} */
@Override
protected void doStart() {
+ memoryBookmarkDatabase.start();
loadKnownPosts();
loadKnownPostReplies();
notifyStarted();
containsInAnyOrder("PostReply1", "PostReply2"));
}
+ @Test
+ public void loaderCanLoadBookmarkedPosts() {
+ when(configuration.getStringValue("Bookmarks/Post/0/ID"))
+ .thenReturn(new TestValue<String>("Post2"));
+ when(configuration.getStringValue("Bookmarks/Post/1/ID"))
+ .thenReturn(new TestValue<String>("Post1"));
+ when(configuration.getStringValue("Bookmarks/Post/2/ID"))
+ .thenReturn(new TestValue<String>(null));
+ Set<String> knownPosts = configurationLoader.loadBookmarkedPosts();
+ assertThat(knownPosts, containsInAnyOrder("Post1", "Post2"));
+ }
+
}
public class MemoryBookmarkDatabaseTest {
private final MemoryDatabase memoryDatabase = mock(MemoryDatabase.class);
+ private final ConfigurationLoader configurationLoader = mock(ConfigurationLoader.class);
private final MemoryBookmarkDatabase bookmarkDatabase =
- new MemoryBookmarkDatabase(memoryDatabase);
+ new MemoryBookmarkDatabase(memoryDatabase, configurationLoader);
private final Map<String, Post> posts = new HashMap<String, Post>();
@Before