From 723de3522aa5c04634932f05bf4075304979c7a9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 4 Nov 2014 20:01:39 +0100 Subject: [PATCH] Add method to save bookmarked posts to configuration loader. --- .../sone/database/memory/ConfigurationLoader.java | 27 ++++++++++++++++++++++ .../database/memory/MemoryBookmarkDatabase.java | 13 +++++++++++ .../sone/database/memory/MemoryDatabase.java | 1 + .../database/memory/ConfigurationLoaderTest.java | 24 +++++++++++++++++++ 4 files changed, 65 insertions(+) diff --git a/src/main/java/net/pterodactylus/sone/database/memory/ConfigurationLoader.java b/src/main/java/net/pterodactylus/sone/database/memory/ConfigurationLoader.java index b9ac859..fe23153 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/ConfigurationLoader.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/ConfigurationLoader.java @@ -1,9 +1,13 @@ package net.pterodactylus.sone.database.memory; +import static java.util.logging.Level.WARNING; + import java.util.HashSet; import java.util.Set; +import java.util.logging.Logger; import net.pterodactylus.util.config.Configuration; +import net.pterodactylus.util.config.ConfigurationException; /** * Helper class for interacting with a {@link Configuration}. @@ -12,6 +16,8 @@ import net.pterodactylus.util.config.Configuration; */ public class ConfigurationLoader { + private static final Logger logger = + Logger.getLogger(ConfigurationLoader.class.getName()); private final Configuration configuration; public ConfigurationLoader(Configuration configuration) { @@ -45,4 +51,25 @@ public class ConfigurationLoader { return ids; } + public synchronized void saveBookmarkedPosts( + Set bookmarkedPosts) { + saveIds("Bookmarks/Post", bookmarkedPosts); + } + + private void saveIds(String prefix, Set bookmarkedPosts) { + try { + int idCounter = 0; + for (String id : bookmarkedPosts) { + configuration + .getStringValue(prefix + "/" + idCounter++ + "/ID") + .setValue(id); + } + configuration + .getStringValue(prefix + "/" + idCounter + "/ID") + .setValue(null); + } catch (ConfigurationException ce1) { + logger.log(WARNING, "Could not save bookmarked posts!", ce1); + } + } + } 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 4c99c53..34ff024 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryBookmarkDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryBookmarkDatabase.java @@ -45,6 +45,19 @@ public class MemoryBookmarkDatabase implements BookmarkDatabase { } } + public void stop() { + saveBookmarkedPosts(); + } + + private void saveBookmarkedPosts() { + lock.readLock().lock(); + try { + configurationLoader.saveBookmarkedPosts(this.bookmarkedPosts); + } finally { + lock.readLock().unlock(); + } + } + @Override public void bookmarkPost(Post post) { lock.writeLock().lock(); diff --git a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java index 18a5718..0f795eb 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java @@ -168,6 +168,7 @@ public class MemoryDatabase extends AbstractService implements Database { @Override protected void doStop() { try { + memoryBookmarkDatabase.stop(); save(); notifyStopped(); } catch (DatabaseException de1) { diff --git a/src/test/java/net/pterodactylus/sone/database/memory/ConfigurationLoaderTest.java b/src/test/java/net/pterodactylus/sone/database/memory/ConfigurationLoaderTest.java index de357e9..55b66ee 100644 --- a/src/test/java/net/pterodactylus/sone/database/memory/ConfigurationLoaderTest.java +++ b/src/test/java/net/pterodactylus/sone/database/memory/ConfigurationLoaderTest.java @@ -1,14 +1,18 @@ package net.pterodactylus.sone.database.memory; +import static java.util.Arrays.asList; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.nullValue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import java.util.HashSet; import java.util.Set; import net.pterodactylus.sone.TestValue; import net.pterodactylus.util.config.Configuration; +import net.pterodactylus.util.config.ConfigurationException; import org.junit.Test; @@ -60,4 +64,24 @@ public class ConfigurationLoaderTest { assertThat(knownPosts, containsInAnyOrder("Post1", "Post2")); } + @Test + public void loaderCanSaveBookmarkedPosts() throws ConfigurationException { + final TestValue post1 = new TestValue(null); + final TestValue post2 = new TestValue(null); + final TestValue post3 = new TestValue(null); + when(configuration.getStringValue("Bookmarks/Post/0/ID")) + .thenReturn(post1); + when(configuration.getStringValue("Bookmarks/Post/1/ID")) + .thenReturn(post2); + when(configuration.getStringValue("Bookmarks/Post/2/ID")) + .thenReturn(post3); + HashSet originalPosts = + new HashSet(asList("Post1", "Post2")); + configurationLoader.saveBookmarkedPosts(originalPosts); + HashSet extractedPosts = new HashSet( + asList(post1.getValue(), post2.getValue())); + assertThat(extractedPosts, containsInAnyOrder("Post1", "Post2")); + assertThat(post3.getValue(), nullValue()); + } + } -- 2.7.4