Refactor loading of IDs.
[Sone.git] / src / main / java / net / pterodactylus / sone / database / memory / ConfigurationLoader.java
1 package net.pterodactylus.sone.database.memory;
2
3 import java.util.HashSet;
4 import java.util.Set;
5
6 import net.pterodactylus.util.config.Configuration;
7
8 /**
9  * Helper class for interacting with a {@link Configuration}.
10  *
11  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
12  */
13 public class ConfigurationLoader {
14
15         private final Configuration configuration;
16
17         public ConfigurationLoader(Configuration configuration) {
18                 this.configuration = configuration;
19         }
20
21         public synchronized Set<String> loadKnownPosts() {
22                 return loadIds("KnownPosts");
23         }
24
25         public synchronized Set<String> loadKnownPostReplies() {
26                 return loadIds("KnownReplies");
27         }
28
29         public synchronized Set<String> loadBookmarkedPosts() {
30                 return loadIds("Bookmarks/Post");
31         }
32
33         private Set<String> loadIds(String prefix) {
34                 Set<String> ids = new HashSet<String>();
35                 int idCounter = 0;
36                 while (true) {
37                         String id = configuration
38                                         .getStringValue(prefix + "/" + idCounter++ + "/ID")
39                                         .getValue(null);
40                         if (id == null) {
41                                 break;
42                         }
43                         ids.add(id);
44                 }
45                 return ids;
46         }
47
48 }