Add tests for saving Sone following times
[Sone.git] / src / main / java / net / pterodactylus / sone / database / memory / ConfigurationLoader.java
index 0f55575..bb82ee2 100644 (file)
@@ -3,10 +3,16 @@ 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;
 
@@ -44,6 +50,50 @@ 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<String, Long> soneFollowingTimes = loadSoneFollowingTimes();
+               soneFollowingTimes.remove(soneId);
+               storeSoneFollowingTimes(soneFollowingTimes);
+       }
+
+       public synchronized void setSoneFollowingTime(@Nonnull String soneId, long time) {
+               Map<String, Long> soneFollowingTimes = loadSoneFollowingTimes();
+               soneFollowingTimes.put(soneId, time);
+               storeSoneFollowingTimes(soneFollowingTimes);
+       }
+
+       private synchronized Map<String, Long> loadSoneFollowingTimes() {
+               Map<String, Long> 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(Long.MAX_VALUE));
+               }
+               return soneFollowingTimes;
+       }
+
+       private synchronized void storeSoneFollowingTimes(Map<String, Long> soneFollowingTimes) {
+               int soneCounter = 0;
+               try {
+                       for (Entry<String, Long> 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<String> loadIds(String prefix) {
                Set<String> ids = new HashSet<String>();
                int idCounter = 0;