Move storage of known Sones into 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.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> loadKnownSones() {
39                 return loadIds("KnownSones");
40         }
41
42         public synchronized void saveKnownSones(Set<String> knownSones) {
43                 saveIds("KnownSones", knownSones);
44         }
45
46         public synchronized Set<String> loadKnownPosts() {
47                 return loadIds("KnownPosts");
48         }
49
50         public synchronized void saveKnownPosts(Set<String> knownPosts) {
51                 saveIds("KnownPosts", knownPosts);
52         }
53
54         public synchronized Set<String> loadKnownPostReplies() {
55                 return loadIds("KnownReplies");
56         }
57
58         public synchronized Set<String> loadBookmarkedPosts() {
59                 return loadIds("Bookmarks/Post");
60         }
61
62         private Set<String> loadIds(String prefix) {
63                 Set<String> ids = new HashSet<String>();
64                 int idCounter = 0;
65                 while (true) {
66                         String id = configuration
67                                         .getStringValue(prefix + "/" + idCounter++ + "/ID")
68                                         .getValue(null);
69                         if (id == null) {
70                                 break;
71                         }
72                         ids.add(id);
73                 }
74                 return ids;
75         }
76
77         public synchronized void saveBookmarkedPosts(
78                         Set<String> bookmarkedPosts) {
79                 saveIds("Bookmarks/Post", bookmarkedPosts);
80         }
81
82         private void saveIds(String prefix, Collection<String> ids) {
83                 try {
84                         int idCounter = 0;
85                         for (String id : ids) {
86                                 configuration
87                                                 .getStringValue(prefix + "/" + idCounter++ + "/ID")
88                                                 .setValue(id);
89                         }
90                         configuration
91                                         .getStringValue(prefix + "/" + idCounter + "/ID")
92                                         .setValue(null);
93                 } catch (ConfigurationException ce1) {
94                         logger.log(WARNING, "Could not save bookmarked posts!", ce1);
95                 }
96         }
97
98         public long getLocalSoneTime(String localSoneId) {
99                 Long time = configuration.getLongValue("Sone/" + localSoneId + "/Time").getValue(null);
100                 return Optional.fromNullable(time).or(-1L);
101         }
102
103         public String getLastInsertFingerprint(String localSoneId) {
104                 return configuration.getStringValue("Sone/" + localSoneId + "/LastInsertFingerprint").getValue("");
105         }
106
107 }