Move management of Sone following times to database.
[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 net.pterodactylus.util.config.Configuration;
14 import net.pterodactylus.util.config.ConfigurationException;
15
16 import com.google.common.base.Optional;
17
18 /**
19  * Helper class for interacting with a {@link Configuration}.
20  *
21  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
22  */
23 public class ConfigurationLoader {
24
25         private static final Logger logger =
26                         Logger.getLogger("Sone.Database.Memory.Configuration");
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> loadKnownSones() {
42                 return loadIds("KnownSones");
43         }
44
45         public synchronized void saveKnownSones(Set<String> knownSones) {
46                 saveIds("KnownSones", knownSones);
47         }
48
49         public synchronized Map<String, Long> loadSoneFollowingTimes() {
50                 Map<String, Long> soneFollowingTimes = new HashMap<String, Long>();
51                 int counter = 0;
52                 while (true) {
53                         String soneId = configuration.getStringValue("SoneFollowingTimes/" + counter + "/Sone").getValue(null);
54                         if (soneId == null) {
55                                 break;
56                         }
57                         long followingTime = configuration.getLongValue("SoneFollowingTimes/" + counter + "/Time").getValue(Long.MAX_VALUE);
58                         soneFollowingTimes.put(soneId, followingTime);
59                         counter++;
60                 }
61                 return soneFollowingTimes;
62         }
63
64         public synchronized void saveSoneFollowingTimes(Map<String, Long> soneFollowingTimes) {
65                 try {
66                         int counter = 0;
67                         for (Entry<String, Long> soneFollowingTime : soneFollowingTimes.entrySet()) {
68                                 configuration.getStringValue("SoneFollowingTimes/" + counter + "/Sone")
69                                                 .setValue(soneFollowingTime.getKey());
70                                 configuration.getLongValue("SoneFollowingTimes/" + counter + "/Time")
71                                                 .setValue(soneFollowingTime.getValue());
72                                 counter++;
73                         }
74                         configuration.getStringValue("SoneFollowingTimes/" + counter + "/Sone").setValue(null);
75                 } catch (ConfigurationException ce1) {
76                         logger.log(WARNING, "Could not save Sone following times!", ce1);
77                 }
78         }
79
80         public synchronized Set<String> loadKnownPosts() {
81                 return loadIds("KnownPosts");
82         }
83
84         public synchronized void saveKnownPosts(Set<String> knownPosts) {
85                 saveIds("KnownPosts", knownPosts);
86         }
87
88         public synchronized Set<String> loadKnownPostReplies() {
89                 return loadIds("KnownReplies");
90         }
91
92         public synchronized Set<String> loadBookmarkedPosts() {
93                 return loadIds("Bookmarks/Post");
94         }
95
96         private Set<String> loadIds(String prefix) {
97                 Set<String> ids = new HashSet<String>();
98                 int idCounter = 0;
99                 while (true) {
100                         String id = configuration
101                                         .getStringValue(prefix + "/" + idCounter++ + "/ID")
102                                         .getValue(null);
103                         if (id == null) {
104                                 break;
105                         }
106                         ids.add(id);
107                 }
108                 return ids;
109         }
110
111         public synchronized void saveBookmarkedPosts(
112                         Set<String> bookmarkedPosts) {
113                 saveIds("Bookmarks/Post", bookmarkedPosts);
114         }
115
116         private void saveIds(String prefix, Collection<String> ids) {
117                 try {
118                         int idCounter = 0;
119                         for (String id : ids) {
120                                 configuration
121                                                 .getStringValue(prefix + "/" + idCounter++ + "/ID")
122                                                 .setValue(id);
123                         }
124                         configuration
125                                         .getStringValue(prefix + "/" + idCounter + "/ID")
126                                         .setValue(null);
127                 } catch (ConfigurationException ce1) {
128                         logger.log(WARNING, "Could not save bookmarked posts!", ce1);
129                 }
130         }
131
132         public long getLocalSoneTime(String localSoneId) {
133                 Long time = configuration.getLongValue("Sone/" + localSoneId + "/Time").getValue(null);
134                 return Optional.fromNullable(time).or(-1L);
135         }
136
137         public String getLastInsertFingerprint(String localSoneId) {
138                 return configuration.getStringValue("Sone/" + localSoneId + "/LastInsertFingerprint").getValue("");
139         }
140
141 }