Expose following times from FriendProvider
[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  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
23  */
24 public class ConfigurationLoader {
25
26         private static final Logger logger = Logger.getLogger(ConfigurationLoader.class.getName());
27         private final Configuration configuration;
28
29         public ConfigurationLoader(Configuration configuration) {
30                 this.configuration = configuration;
31         }
32
33         public synchronized Set<String> loadFriends(String localSoneId) {
34                 return loadIds("Sone/" + localSoneId + "/Friends");
35         }
36
37         public void saveFriends(String soneId, Collection<String> friends) {
38                 saveIds("Sone/" + soneId + "/Friends", friends);
39         }
40
41         public synchronized Set<String> loadKnownPosts() {
42                 return loadIds("KnownPosts");
43         }
44
45         public synchronized Set<String> loadKnownPostReplies() {
46                 return loadIds("KnownReplies");
47         }
48
49         public synchronized Set<String> loadBookmarkedPosts() {
50                 return loadIds("Bookmarks/Post");
51         }
52
53         @Nullable
54         public synchronized Long getSoneFollowingTime(@Nonnull String soneId) {
55                 return loadSoneFollowingTimes().get(soneId);
56         }
57
58         public synchronized void removeSoneFollowingTime(@Nonnull String soneId) {
59                 Map<String, Long> soneFollowingTimes = loadSoneFollowingTimes();
60                 soneFollowingTimes.remove(soneId);
61                 storeSoneFollowingTimes(soneFollowingTimes);
62         }
63
64         public synchronized void setSoneFollowingTime(@Nonnull String soneId, long time) {
65                 Map<String, Long> soneFollowingTimes = loadSoneFollowingTimes();
66                 soneFollowingTimes.put(soneId, time);
67                 storeSoneFollowingTimes(soneFollowingTimes);
68         }
69
70         private synchronized Map<String, Long> loadSoneFollowingTimes() {
71                 Map<String, Long> soneFollowingTimes = new HashMap<>();
72                 int soneCounter = 0;
73                 while (true) {
74                         String soneId = configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").getValue(null);
75                         if (soneId == null) {
76                                 break;
77                         }
78                         soneFollowingTimes.put(soneId, configuration.getLongValue("SoneFollowingTimes/" + soneCounter++ + "/Time").getValue(null));
79                 }
80                 return soneFollowingTimes;
81         }
82
83         private synchronized void storeSoneFollowingTimes(Map<String, Long> soneFollowingTimes) {
84                 int soneCounter = 0;
85                 try {
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());
89                                 ++soneCounter;
90                         }
91                         configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").setValue(null);
92                 } catch (ConfigurationException ce1) {
93                         logger.log(WARNING, "Could not save Sone following times!", ce1);
94                 }
95         }
96
97         private Set<String> loadIds(String prefix) {
98                 Set<String> ids = new HashSet<String>();
99                 int idCounter = 0;
100                 while (true) {
101                         String id = configuration
102                                         .getStringValue(prefix + "/" + idCounter++ + "/ID")
103                                         .getValue(null);
104                         if (id == null) {
105                                 break;
106                         }
107                         ids.add(id);
108                 }
109                 return ids;
110         }
111
112         public synchronized void saveBookmarkedPosts(
113                         Set<String> bookmarkedPosts) {
114                 saveIds("Bookmarks/Post", bookmarkedPosts);
115         }
116
117         private void saveIds(String prefix, Collection<String> ids) {
118                 try {
119                         int idCounter = 0;
120                         for (String id : ids) {
121                                 configuration
122                                                 .getStringValue(prefix + "/" + idCounter++ + "/ID")
123                                                 .setValue(id);
124                         }
125                         configuration
126                                         .getStringValue(prefix + "/" + idCounter + "/ID")
127                                         .setValue(null);
128                 } catch (ConfigurationException ce1) {
129                         logger.log(WARNING, "Could not save bookmarked posts!", ce1);
130                 }
131         }
132
133 }