X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdatabase%2Fmemory%2FConfigurationLoader.java;h=268f1a203ae4b228e5944374e250dea27f94000d;hb=23e09dfc13a0cc6dfc7d7b8cc09c78acc6984ec7;hp=0f5557595b869d61bd0609a9078364cbcab9fc7c;hpb=3a7092e48f27cba6286946442783f539ad73a911;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 0f55575..268f1a2 100644
--- a/src/main/java/net/pterodactylus/sone/database/memory/ConfigurationLoader.java
+++ b/src/main/java/net/pterodactylus/sone/database/memory/ConfigurationLoader.java
@@ -3,17 +3,21 @@ 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 {
@@ -44,8 +48,52 @@ public class ConfigurationLoader {
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 soneId = configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").getValue(null);
+ if (soneId == null) {
+ break;
+ }
+ soneFollowingTimes.put(soneId, configuration.getLongValue("SoneFollowingTimes/" + soneCounter++ + "/Time").getValue(null));
+ }
+ return soneFollowingTimes;
+ }
+
+ 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();
+ Set ids = new HashSet<>();
int idCounter = 0;
while (true) {
String id = configuration