661bfe81c3b1656bf1cbcde22691e00ecb843b1b
[Sone.git] / src / main / java / net / pterodactylus / sone / database / memory / ConfigurationLoader.java
1 package net.pterodactylus.sone.database.memory;
2
3 import static java.util.logging.Level.WARNING;
4
5 import java.util.Collection;
6 import java.util.HashMap;
7 import java.util.HashSet;
8 import java.util.Map;
9 import java.util.Map.Entry;
10 import java.util.Set;
11 import java.util.logging.Logger;
12
13 import javax.annotation.Nonnull;
14 import javax.annotation.Nullable;
15
16 import net.pterodactylus.util.config.Configuration;
17 import net.pterodactylus.util.config.ConfigurationException;
18
19 /**
20  * Helper class for interacting with a {@link Configuration}.
21  */
22 public class ConfigurationLoader {
23
24         private static final Logger logger = Logger.getLogger(ConfigurationLoader.class.getName());
25         private final Configuration configuration;
26
27         public ConfigurationLoader(Configuration configuration) {
28                 this.configuration = configuration;
29         }
30
31         public synchronized Set<String> loadFriends(String localSoneId) {
32                 return loadIds("Sone/" + localSoneId + "/Friends");
33         }
34
35         public void saveFriends(String soneId, Collection<String> friends) {
36                 saveIds("Sone/" + soneId + "/Friends", friends);
37         }
38
39         public synchronized Set<String> loadKnownPosts() {
40                 return loadIds("KnownPosts");
41         }
42
43         public synchronized Set<String> loadKnownPostReplies() {
44                 return loadIds("KnownReplies");
45         }
46
47         public synchronized Set<String> loadBookmarkedPosts() {
48                 return loadIds("Bookmarks/Post");
49         }
50
51         @Nullable
52         public synchronized Long getSoneFollowingTime(@Nonnull String soneId) {
53                 return loadSoneFollowingTimes().get(soneId);
54         }
55
56         public synchronized void removeSoneFollowingTime(@Nonnull String soneId) {
57                 Map<String, Long> soneFollowingTimes = loadSoneFollowingTimes();
58                 soneFollowingTimes.remove(soneId);
59                 storeSoneFollowingTimes(soneFollowingTimes);
60         }
61
62         public synchronized void setSoneFollowingTime(@Nonnull String soneId, long time) {
63                 Map<String, Long> soneFollowingTimes = loadSoneFollowingTimes();
64                 soneFollowingTimes.put(soneId, time);
65                 storeSoneFollowingTimes(soneFollowingTimes);
66         }
67
68         private synchronized Map<String, Long> loadSoneFollowingTimes() {
69                 Map<String, Long> soneFollowingTimes = new HashMap<>();
70                 int soneCounter = 0;
71                 while (true) {
72                         String soneId = configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").getValue(null);
73                         if (soneId == null) {
74                                 break;
75                         }
76                         soneFollowingTimes.put(soneId, configuration.getLongValue("SoneFollowingTimes/" + soneCounter++ + "/Time").getValue(null));
77                 }
78                 return soneFollowingTimes;
79         }
80
81         private synchronized void storeSoneFollowingTimes(Map<String, Long> soneFollowingTimes) {
82                 int soneCounter = 0;
83                 try {
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());
87                                 ++soneCounter;
88                         }
89                         configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").setValue(null);
90                 } catch (ConfigurationException ce1) {
91                         logger.log(WARNING, "Could not save Sone following times!", ce1);
92                 }
93         }
94
95         private Set<String> loadIds(String prefix) {
96                 Set<String> ids = new HashSet<String>();
97                 int idCounter = 0;
98                 while (true) {
99                         String id = configuration
100                                         .getStringValue(prefix + "/" + idCounter++ + "/ID")
101                                         .getValue(null);
102                         if (id == null) {
103                                 break;
104                         }
105                         ids.add(id);
106                 }
107                 return ids;
108         }
109
110         public synchronized void saveBookmarkedPosts(
111                         Set<String> bookmarkedPosts) {
112                 saveIds("Bookmarks/Post", bookmarkedPosts);
113         }
114
115         private void saveIds(String prefix, Collection<String> ids) {
116                 try {
117                         int idCounter = 0;
118                         for (String id : ids) {
119                                 configuration
120                                                 .getStringValue(prefix + "/" + idCounter++ + "/ID")
121                                                 .setValue(id);
122                         }
123                         configuration
124                                         .getStringValue(prefix + "/" + idCounter + "/ID")
125                                         .setValue(null);
126                 } catch (ConfigurationException ce1) {
127                         logger.log(WARNING, "Could not save bookmarked posts!", ce1);
128                 }
129         }
130
131 }