Move management of Sone following times to database.
[Sone.git] / src / main / java / net / pterodactylus / sone / database / memory / ConfigurationLoader.java
index fa8a362..0e37dab 100644 (file)
@@ -3,7 +3,10 @@ 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;
 
@@ -43,6 +46,37 @@ public class ConfigurationLoader {
                saveIds("KnownSones", knownSones);
        }
 
+       public synchronized Map<String, Long> loadSoneFollowingTimes() {
+               Map<String, Long> soneFollowingTimes = new HashMap<String, Long>();
+               int counter = 0;
+               while (true) {
+                       String soneId = configuration.getStringValue("SoneFollowingTimes/" + counter + "/Sone").getValue(null);
+                       if (soneId == null) {
+                               break;
+                       }
+                       long followingTime = configuration.getLongValue("SoneFollowingTimes/" + counter + "/Time").getValue(Long.MAX_VALUE);
+                       soneFollowingTimes.put(soneId, followingTime);
+                       counter++;
+               }
+               return soneFollowingTimes;
+       }
+
+       public synchronized void saveSoneFollowingTimes(Map<String, Long> soneFollowingTimes) {
+               try {
+                       int counter = 0;
+                       for (Entry<String, Long> soneFollowingTime : soneFollowingTimes.entrySet()) {
+                               configuration.getStringValue("SoneFollowingTimes/" + counter + "/Sone")
+                                               .setValue(soneFollowingTime.getKey());
+                               configuration.getLongValue("SoneFollowingTimes/" + counter + "/Time")
+                                               .setValue(soneFollowingTime.getValue());
+                               counter++;
+                       }
+                       configuration.getStringValue("SoneFollowingTimes/" + counter + "/Sone").setValue(null);
+               } catch (ConfigurationException ce1) {
+                       logger.log(WARNING, "Could not save Sone following times!", ce1);
+               }
+       }
+
        public synchronized Set<String> loadKnownPosts() {
                return loadIds("KnownPosts");
        }