X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdatabase%2Fmemory%2FConfigurationLoader.java;h=268f1a203ae4b228e5944374e250dea27f94000d;hb=179e7da4d8d8a474d0b622d60b5f5d32d6ab4052;hp=078b5f4042c9c941cc5fc25f22ac52f3dfe8238f;hpb=b6de5505ae5d2f835a235de6e9f23cb83affe4b7;p=Sone.git
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 078b5f4..268f1a2 100644
--- a/src/main/java/net/pterodactylus/sone/database/memory/ConfigurationLoader.java
+++ b/src/main/java/net/pterodactylus/sone/database/memory/ConfigurationLoader.java
@@ -1,51 +1,131 @@
package net.pterodactylus.sone.database.memory;
+import static java.util.logging.Level.WARNING;
+
+import java.util.Collection;
+import java.util.HashMap;
import java.util.HashSet;
+import java.util.Map;
+import java.util.Map.Entry;
import java.util.Set;
+import java.util.logging.Logger;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
import net.pterodactylus.util.config.Configuration;
+import net.pterodactylus.util.config.ConfigurationException;
/**
* Helper class for interacting with a {@link Configuration}.
- *
- * @author David âBombeâ Roden
*/
public class ConfigurationLoader {
+ private static final Logger logger = Logger.getLogger(ConfigurationLoader.class.getName());
private final Configuration configuration;
public ConfigurationLoader(Configuration configuration) {
this.configuration = configuration;
}
+ public synchronized Set loadFriends(String localSoneId) {
+ return loadIds("Sone/" + localSoneId + "/Friends");
+ }
+
+ public void saveFriends(String soneId, Collection friends) {
+ saveIds("Sone/" + soneId + "/Friends", friends);
+ }
+
public synchronized Set loadKnownPosts() {
- Set knownPosts = new HashSet();
- int postCounter = 0;
+ return loadIds("KnownPosts");
+ }
+
+ public synchronized Set loadKnownPostReplies() {
+ return loadIds("KnownReplies");
+ }
+
+ public synchronized Set loadBookmarkedPosts() {
+ return loadIds("Bookmarks/Post");
+ }
+
+ @Nullable
+ public synchronized Long getSoneFollowingTime(@Nonnull String soneId) {
+ return loadSoneFollowingTimes().get(soneId);
+ }
+
+ public synchronized void removeSoneFollowingTime(@Nonnull String soneId) {
+ Map soneFollowingTimes = loadSoneFollowingTimes();
+ soneFollowingTimes.remove(soneId);
+ storeSoneFollowingTimes(soneFollowingTimes);
+ }
+
+ public synchronized void setSoneFollowingTime(@Nonnull String soneId, long time) {
+ Map soneFollowingTimes = loadSoneFollowingTimes();
+ soneFollowingTimes.put(soneId, time);
+ storeSoneFollowingTimes(soneFollowingTimes);
+ }
+
+ private synchronized Map loadSoneFollowingTimes() {
+ Map soneFollowingTimes = new HashMap<>();
+ int soneCounter = 0;
while (true) {
- String knownPostId = configuration
- .getStringValue("KnownPosts/" + postCounter++ + "/ID")
- .getValue(null);
- if (knownPostId == null) {
+ String soneId = configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").getValue(null);
+ if (soneId == null) {
break;
}
- knownPosts.add(knownPostId);
+ soneFollowingTimes.put(soneId, configuration.getLongValue("SoneFollowingTimes/" + soneCounter++ + "/Time").getValue(null));
}
- return knownPosts;
+ return soneFollowingTimes;
}
- public synchronized Set loadKnownPostReplies() {
- Set knownPostReplies = new HashSet();
- int replyCounter = 0;
+ private synchronized void storeSoneFollowingTimes(Map soneFollowingTimes) {
+ int soneCounter = 0;
+ try {
+ for (Entry soneFollowingTime : soneFollowingTimes.entrySet()) {
+ configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").setValue(soneFollowingTime.getKey());
+ configuration.getLongValue("SoneFollowingTimes/" + soneCounter + "/Time").setValue(soneFollowingTime.getValue());
+ ++soneCounter;
+ }
+ configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").setValue(null);
+ } catch (ConfigurationException ce1) {
+ logger.log(WARNING, "Could not save Sone following times!", ce1);
+ }
+ }
+
+ private Set loadIds(String prefix) {
+ Set ids = new HashSet<>();
+ int idCounter = 0;
while (true) {
- String knownReplyId = configuration
- .getStringValue("KnownReplies/" + replyCounter++ + "/ID")
+ String id = configuration
+ .getStringValue(prefix + "/" + idCounter++ + "/ID")
.getValue(null);
- if (knownReplyId == null) {
+ if (id == null) {
break;
}
- knownPostReplies.add(knownReplyId);
+ ids.add(id);
+ }
+ return ids;
+ }
+
+ public synchronized void saveBookmarkedPosts(
+ Set bookmarkedPosts) {
+ saveIds("Bookmarks/Post", bookmarkedPosts);
+ }
+
+ private void saveIds(String prefix, Collection ids) {
+ try {
+ int idCounter = 0;
+ for (String id : ids) {
+ 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);
}
- return knownPostReplies;
}
}