1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package mk.fsgrep.util;
26
27
28 import java.io.*;
29 import java.util.*;
30
31
32 /***
33 * A simple persistent data store using the Java Properties framework.
34 * This gives crude support for collection storage by key prefix
35 * identification.
36 *
37 * @author Murali Krishnan
38 *
39 */
40 public class PersistentData extends Properties {
41
42
43
44
45
46 /***
47 * The singleton instance.
48 */
49 private static PersistentData sInstance = null;
50
51
52 /***
53 * The file where this object is persisted.
54 */
55 protected static final File DATA_FILE = new File(AppProperties.APP_DIR + File.separator + "pdata.properties");
56
57
58
59
60
61 /***
62 * @return the singleton, created lazily.
63 */
64 public static synchronized PersistentData getInstance() {
65 if (sInstance == null) {
66 sInstance = new PersistentData();
67 }
68
69 return sInstance;
70 }
71
72
73
74
75
76
77
78
79
80
81 /***
82 * The default constructor loads the file.
83 */
84 protected PersistentData() {
85 load();
86 }
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102 /***
103 * Read the data file to populate this object.
104 * It should only be called by the (default) constructor.
105 */
106 protected synchronized void load() {
107 if (DATA_FILE.exists()) {
108 try {
109 FileInputStream fis = new FileInputStream(DATA_FILE);
110 load(fis);
111 fis.close();
112 } catch (IOException ioe) {
113 System.err.println("Could not read persistent data.");
114 ioe.printStackTrace();
115 }
116 } else {
117 System.out.println("Creating default persistent data file.");
118 store();
119 }
120 }
121
122
123
124
125
126 /***
127 * Lookup the value for the given key and convert it to a boolean.
128 * If it is not found, return false.
129 *
130 * @return The property value converted to a boolean value.
131 * @param key The association key.
132 */
133 public boolean getBooleanProperty(String key) {
134 String value = getProperty(key);
135
136 boolean result = Boolean.TRUE.toString().equals(value);
137
138 return result;
139 }
140
141 /***
142 * Save this objects values to the file.
143 */
144 public synchronized void store() {
145 AppProperties.ensureAppDir();
146
147 try {
148 FileOutputStream fos = new FileOutputStream(DATA_FILE);
149 store(fos, "fsgrep persistent data");
150 fos.close();
151 } catch (IOException ioe) {
152 System.err.println("Could not save persistent data.");
153 ioe.printStackTrace();
154 }
155 }
156
157
158 /***
159 * Return the sorted set of all keys that have the give prefix.
160 *
161 * @param prefix The string begining of the keys to be extracted.
162 * @return all the property keys with the given prefix.
163 */
164 public synchronized Collection<String> getKeysPrefixed(String prefix) {
165
166
167 Collection<String> result = new TreeSet<String>();
168
169 for (Object o : keySet()) {
170 String key = (String) o;
171 if (key.startsWith(prefix)) {
172 result.add(key);
173 }
174 }
175 return result;
176 }
177
178
179 /***
180 * Remove all keys that have the give prefix.
181 *
182 * @param prefix The string begining of the keys to be removed.
183 */
184 public synchronized void removeKeysPrefixed(String prefix) {
185
186
187 Collection<String> keys = getKeysPrefixed(prefix);
188
189 for (String key : keys) {
190 remove(key);
191 }
192 }
193
194
195 /***
196 * Save the key/value mapping where the key is a prefix/index.
197 *
198 * @param prefix The key prefix.
199 * @param index The index (numeric) part of the key.
200 * @param value The value associated with the key.
201 */
202 public void put(String prefix, int index, String value) {
203 StringBuffer indexPart = new StringBuffer(String.valueOf(index));
204 while (indexPart.length() < 3) {
205 indexPart.insert(0, "0");
206 }
207
208 String key = prefix + indexPart;
209
210 setProperty(key, value);
211 }
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228 }