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 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
24 public class ConfigurationLoader {
26 private static final Logger logger = Logger.getLogger(ConfigurationLoader.class.getName());
27 private final Configuration configuration;
29 public ConfigurationLoader(Configuration configuration) {
30 this.configuration = configuration;
33 public synchronized Set<String> loadFriends(String localSoneId) {
34 return loadIds("Sone/" + localSoneId + "/Friends");
37 public void saveFriends(String soneId, Collection<String> friends) {
38 saveIds("Sone/" + soneId + "/Friends", friends);
41 public synchronized Set<String> loadKnownPosts() {
42 return loadIds("KnownPosts");
45 public synchronized Set<String> loadKnownPostReplies() {
46 return loadIds("KnownReplies");
49 public synchronized Set<String> loadBookmarkedPosts() {
50 return loadIds("Bookmarks/Post");
54 public synchronized Long getSoneFollowingTime(@Nonnull String soneId) {
55 return loadSoneFollowingTimes().get(soneId);
58 public synchronized void removeSoneFollowingTime(@Nonnull String soneId) {
59 Map<String, Long> soneFollowingTimes = loadSoneFollowingTimes();
60 soneFollowingTimes.remove(soneId);
61 storeSoneFollowingTimes(soneFollowingTimes);
64 public synchronized void setSoneFollowingTime(@Nonnull String soneId, long time) {
65 Map<String, Long> soneFollowingTimes = loadSoneFollowingTimes();
66 soneFollowingTimes.put(soneId, time);
67 storeSoneFollowingTimes(soneFollowingTimes);
70 private synchronized Map<String, Long> loadSoneFollowingTimes() {
71 Map<String, Long> soneFollowingTimes = new HashMap<>();
74 String soneId = configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").getValue(null);
78 soneFollowingTimes.put(soneId, configuration.getLongValue("SoneFollowingTimes/" + soneCounter++ + "/Time").getValue(null));
80 return soneFollowingTimes;
83 private synchronized void storeSoneFollowingTimes(Map<String, Long> soneFollowingTimes) {
86 for (Entry<String, Long> soneFollowingTime : soneFollowingTimes.entrySet()) {
87 configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").setValue(soneFollowingTime.getKey());
88 configuration.getLongValue("SoneFollowingTimes/" + soneCounter + "/Time").setValue(soneFollowingTime.getValue());
91 configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").setValue(null);
92 } catch (ConfigurationException ce1) {
93 logger.log(WARNING, "Could not save Sone following times!", ce1);
97 private Set<String> loadIds(String prefix) {
98 Set<String> ids = new HashSet<String>();
101 String id = configuration
102 .getStringValue(prefix + "/" + idCounter++ + "/ID")
112 public synchronized void saveBookmarkedPosts(
113 Set<String> bookmarkedPosts) {
114 saveIds("Bookmarks/Post", bookmarkedPosts);
117 private void saveIds(String prefix, Collection<String> ids) {
120 for (String id : ids) {
122 .getStringValue(prefix + "/" + idCounter++ + "/ID")
126 .getStringValue(prefix + "/" + idCounter + "/ID")
128 } catch (ConfigurationException ce1) {
129 logger.log(WARNING, "Could not save bookmarked posts!", ce1);