X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdatabase%2Fmemory%2FConfigurationLoader.java;h=0e37dabbf57e0e5702f21087bc79e587bc64de22;hb=722b47810ffbe01465f104791c9f660ae161023b;hp=84d8198b4861ba2dcf6ad46b169ae4b3b7e99ca8;hpb=ed0ac504a54683d3063c5cf37fc1a282836f8743;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 84d8198..0e37dab 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/ConfigurationLoader.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/ConfigurationLoader.java @@ -2,13 +2,19 @@ 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 net.pterodactylus.util.config.Configuration; import net.pterodactylus.util.config.ConfigurationException; +import com.google.common.base.Optional; + /** * Helper class for interacting with a {@link Configuration}. * @@ -24,10 +30,61 @@ public class ConfigurationLoader { 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 loadKnownSones() { + return loadIds("KnownSones"); + } + + public synchronized void saveKnownSones(Set knownSones) { + saveIds("KnownSones", knownSones); + } + + public synchronized Map loadSoneFollowingTimes() { + Map soneFollowingTimes = new HashMap(); + 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 soneFollowingTimes) { + try { + int counter = 0; + for (Entry 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 loadKnownPosts() { return loadIds("KnownPosts"); } + public synchronized void saveKnownPosts(Set knownPosts) { + saveIds("KnownPosts", knownPosts); + } + public synchronized Set loadKnownPostReplies() { return loadIds("KnownReplies"); } @@ -56,10 +113,10 @@ public class ConfigurationLoader { saveIds("Bookmarks/Post", bookmarkedPosts); } - private void saveIds(String prefix, Set bookmarkedPosts) { + private void saveIds(String prefix, Collection ids) { try { int idCounter = 0; - for (String id : bookmarkedPosts) { + for (String id : ids) { configuration .getStringValue(prefix + "/" + idCounter++ + "/ID") .setValue(id); @@ -72,4 +129,13 @@ public class ConfigurationLoader { } } + public long getLocalSoneTime(String localSoneId) { + Long time = configuration.getLongValue("Sone/" + localSoneId + "/Time").getValue(null); + return Optional.fromNullable(time).or(-1L); + } + + public String getLastInsertFingerprint(String localSoneId) { + return configuration.getStringValue("Sone/" + localSoneId + "/LastInsertFingerprint").getValue(""); + } + }