1 package net.pterodactylus.sone.database.memory;
3 import static java.util.logging.Level.WARNING;
5 import java.util.Collection;
6 import java.util.HashMap;
7 import java.util.HashSet;
9 import java.util.Map.Entry;
11 import java.util.logging.Logger;
13 import javax.annotation.Nonnull;
14 import javax.annotation.Nullable;
16 import net.pterodactylus.util.config.Configuration;
17 import net.pterodactylus.util.config.ConfigurationException;
20 * Helper class for interacting with a {@link Configuration}.
22 public class ConfigurationLoader {
24 private static final Logger logger = Logger.getLogger(ConfigurationLoader.class.getName());
25 private final Configuration configuration;
27 public ConfigurationLoader(Configuration configuration) {
28 this.configuration = configuration;
31 public synchronized Set<String> loadFriends(String localSoneId) {
32 return loadIds("Sone/" + localSoneId + "/Friends");
35 public void saveFriends(String soneId, Collection<String> friends) {
36 saveIds("Sone/" + soneId + "/Friends", friends);
39 public synchronized Set<String> loadKnownPosts() {
40 return loadIds("KnownPosts");
43 public synchronized Set<String> loadKnownPostReplies() {
44 return loadIds("KnownReplies");
47 public synchronized Set<String> loadBookmarkedPosts() {
48 return loadIds("Bookmarks/Post");
52 public synchronized Long getSoneFollowingTime(@Nonnull String soneId) {
53 return loadSoneFollowingTimes().get(soneId);
56 public synchronized void removeSoneFollowingTime(@Nonnull String soneId) {
57 Map<String, Long> soneFollowingTimes = loadSoneFollowingTimes();
58 soneFollowingTimes.remove(soneId);
59 storeSoneFollowingTimes(soneFollowingTimes);
62 public synchronized void setSoneFollowingTime(@Nonnull String soneId, long time) {
63 Map<String, Long> soneFollowingTimes = loadSoneFollowingTimes();
64 soneFollowingTimes.put(soneId, time);
65 storeSoneFollowingTimes(soneFollowingTimes);
68 private synchronized Map<String, Long> loadSoneFollowingTimes() {
69 Map<String, Long> soneFollowingTimes = new HashMap<>();
72 String soneId = configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").getValue(null);
76 soneFollowingTimes.put(soneId, configuration.getLongValue("SoneFollowingTimes/" + soneCounter++ + "/Time").getValue(null));
78 return soneFollowingTimes;
81 private synchronized void storeSoneFollowingTimes(Map<String, Long> soneFollowingTimes) {
84 for (Entry<String, Long> soneFollowingTime : soneFollowingTimes.entrySet()) {
85 configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").setValue(soneFollowingTime.getKey());
86 configuration.getLongValue("SoneFollowingTimes/" + soneCounter + "/Time").setValue(soneFollowingTime.getValue());
89 configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").setValue(null);
90 } catch (ConfigurationException ce1) {
91 logger.log(WARNING, "Could not save Sone following times!", ce1);
95 private Set<String> loadIds(String prefix) {
96 Set<String> ids = new HashSet<>();
99 String id = configuration
100 .getStringValue(prefix + "/" + idCounter++ + "/ID")
110 public synchronized void saveBookmarkedPosts(
111 Set<String> bookmarkedPosts) {
112 saveIds("Bookmarks/Post", bookmarkedPosts);
115 private void saveIds(String prefix, Collection<String> ids) {
118 for (String id : ids) {
120 .getStringValue(prefix + "/" + idCounter++ + "/ID")
124 .getStringValue(prefix + "/" + idCounter + "/ID")
126 } catch (ConfigurationException ce1) {
127 logger.log(WARNING, "Could not save bookmarked posts!", ce1);