Move post-related database functionality into its own class.
[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.HashSet;
7 import java.util.Set;
8 import java.util.logging.Logger;
9
10 import net.pterodactylus.util.config.Configuration;
11 import net.pterodactylus.util.config.ConfigurationException;
12
13 import com.google.common.base.Optional;
14
15 /**
16  * Helper class for interacting with a {@link Configuration}.
17  *
18  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
19  */
20 public class ConfigurationLoader {
21
22         private static final Logger logger =
23                         Logger.getLogger("Sone.Database.Memory.Configuration");
24         private final Configuration configuration;
25
26         public ConfigurationLoader(Configuration configuration) {
27                 this.configuration = configuration;
28         }
29
30         public synchronized Set<String> loadFriends(String localSoneId) {
31                 return loadIds("Sone/" + localSoneId + "/Friends");
32         }
33
34         public void saveFriends(String soneId, Collection<String> friends) {
35                 saveIds("Sone/" + soneId + "/Friends", friends);
36         }
37
38         public synchronized Set<String> loadKnownPosts() {
39                 return loadIds("KnownPosts");
40         }
41
42         public synchronized void saveKnownPosts(Set<String> knownPosts) {
43                 saveIds("KnownPosts", knownPosts);
44         }
45
46         public synchronized Set<String> loadKnownPostReplies() {
47                 return loadIds("KnownReplies");
48         }
49
50         public synchronized Set<String> loadBookmarkedPosts() {
51                 return loadIds("Bookmarks/Post");
52         }
53
54         private Set<String> loadIds(String prefix) {
55                 Set<String> ids = new HashSet<String>();
56                 int idCounter = 0;
57                 while (true) {
58                         String id = configuration
59                                         .getStringValue(prefix + "/" + idCounter++ + "/ID")
60                                         .getValue(null);
61                         if (id == null) {
62                                 break;
63                         }
64                         ids.add(id);
65                 }
66                 return ids;
67         }
68
69         public synchronized void saveBookmarkedPosts(
70                         Set<String> bookmarkedPosts) {
71                 saveIds("Bookmarks/Post", bookmarkedPosts);
72         }
73
74         private void saveIds(String prefix, Collection<String> ids) {
75                 try {
76                         int idCounter = 0;
77                         for (String id : ids) {
78                                 configuration
79                                                 .getStringValue(prefix + "/" + idCounter++ + "/ID")
80                                                 .setValue(id);
81                         }
82                         configuration
83                                         .getStringValue(prefix + "/" + idCounter + "/ID")
84                                         .setValue(null);
85                 } catch (ConfigurationException ce1) {
86                         logger.log(WARNING, "Could not save bookmarked posts!", ce1);
87                 }
88         }
89
90         public long getLocalSoneTime(String localSoneId) {
91                 Long time = configuration.getLongValue("Sone/" + localSoneId + "/Time").getValue(null);
92                 return Optional.fromNullable(time).or(-1L);
93         }
94
95         public String getLastInsertFingerprint(String localSoneId) {
96                 return configuration.getStringValue("Sone/" + localSoneId + "/LastInsertFingerprint").getValue("");
97         }
98
99 }