-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfiguration.java
More file actions
333 lines (272 loc) · 11.9 KB
/
Configuration.java
File metadata and controls
333 lines (272 loc) · 11.9 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package com.vanillage.utils.configuration;
import java.io.File;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Predicate;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.vanillage.utils.configuration.exceptions.ConfigurationException;
import com.vanillage.utils.configuration.exceptions.ConfigurationParserException;
import com.vanillage.utils.configuration.exceptions.ConfigurationTransformerException;
import com.vanillage.utils.configuration.xml.XmlConfigurationParser;
import com.vanillage.utils.configuration.xml.XmlConfigurationTransformer;
public final class Configuration {
private static final Logger logger = Logger.getLogger(Configuration.class.getName());
public static final Logger getLogger() {
return logger;
}
private static final Object deepCopyValueMaps(Object object) {
if (object instanceof Map) {
Map<Object, Object> copy = new LinkedHashMap<>();
((Map<?, ?>) object).forEach((key, value) -> copy.put(key, deepCopyValueMaps(value)));
return copy;
}
return object;
}
private static final Object deepCopyValueMapsIfAbsent(Object source, Object destination, boolean nullTreatedAsValue) {
if (!nullTreatedAsValue && destination == null) {
return deepCopyValueMaps(source);
}
if (source instanceof Map && destination instanceof Map) {
@SuppressWarnings("unchecked")
Map<Object, Object> destinationMap = (Map<Object, Object>) destination;
for (Entry<?, ?> sourceEntry : ((Map<?, ?>) source).entrySet()) {
if (nullTreatedAsValue ? destinationMap.containsKey(sourceEntry.getKey()) : destinationMap.get(sourceEntry.getKey()) != null) {
if (sourceEntry.getValue() instanceof Map) {
deepCopyValueMapsIfAbsent(sourceEntry.getValue(), destinationMap.get(sourceEntry.getKey()), nullTreatedAsValue);
}
} else {
destinationMap.put(sourceEntry.getKey(), deepCopyValueMaps(sourceEntry.getValue()));
}
}
}
return destination;
}
private final Logger log;
private Object content = new LinkedHashMap<>();
private final Configuration defaultConfiguration;
private final Configuration exampleConfiguration;
private final ConfigurationTransformer configurationTransformer;
private final ConfigurationParser configurationParser;
private final ConfigurationOptions configurationOptions;
public Configuration() {
this(logger);
}
public Configuration(Logger log) {
this(log, new XmlConfigurationTransformer(), new XmlConfigurationParser());
}
public Configuration(ConfigurationTransformer configurationTransformer, ConfigurationParser configurationParser) {
this(logger, configurationTransformer, configurationParser);
}
public Configuration(Logger log, ConfigurationTransformer configurationTransformer, ConfigurationParser configurationParser) {
this(log, configurationTransformer, configurationParser, new ConfigurationOptions());
}
private Configuration(Logger log, ConfigurationTransformer configurationTransformer, ConfigurationParser configurationParser, ConfigurationOptions configurationOptions) {
this(log, new Configuration(log, null, null, configurationTransformer, configurationParser, configurationOptions), new Configuration(log, null, null, configurationTransformer, configurationParser, configurationOptions), configurationTransformer, configurationParser, configurationOptions);
}
private Configuration(Logger log, Configuration defaultConfiguration, Configuration exampleConfiguration, ConfigurationTransformer configurationTransformer, ConfigurationParser configurationParser, ConfigurationOptions configurationOptions) {
if (log == null) {
throw new IllegalArgumentException("log cannot be null");
}
if (configurationTransformer == null) {
throw new IllegalArgumentException("configurationTransformer cannot be null");
}
if (configurationParser == null) {
throw new IllegalArgumentException("configurationParser cannot be null");
}
this.log = log;
this.defaultConfiguration = defaultConfiguration;
this.exampleConfiguration = exampleConfiguration;
this.configurationTransformer = configurationTransformer;
this.configurationParser = configurationParser;
this.configurationOptions = configurationOptions;
}
public Logger getLog() {
return log;
}
public Object getContent() {
return content;
}
public void setContent(Object content) {
this.content = content;
}
public Configuration getDefaultConfiguration() {
return defaultConfiguration;
}
public Configuration getExampleConfiguration() {
return exampleConfiguration;
}
public ConfigurationTransformer getConfigurationTransformer() {
return configurationTransformer;
}
public ConfigurationParser getConfigurationParser() {
return configurationParser;
}
public ConfigurationOptions getConfigurationOptions() {
return configurationOptions;
}
public void createSilent(File file) {
try {
create(file);
} catch (ConfigurationException e) {
log.log(Level.SEVERE, "Error while handling file '" + file.getPath() + "'", e);
}
}
public void create(File file) throws ConfigurationException {
try {
load(file);
} finally {
justSave(file);
}
}
public void loadSilent(File file) {
try {
load(file);
} catch (ConfigurationParserException e) {
log.log(Level.SEVERE, "Error while loading file '" + file.getPath() + "'", e);
}
}
public void load(File file) throws ConfigurationParserException {
if (file == null) {
throw new IllegalArgumentException("file cannot be null");
}
try {
content = configurationParser.parse(file);
} catch (ConfigurationParserException e) {
if (exampleConfiguration != null) {
content = deepCopyValueMaps(exampleConfiguration.getContent());
} else {
content = new LinkedHashMap<>();
}
throw e;
}
}
public void saveSilent(File file) {
try {
save(file);
} catch (ConfigurationTransformerException e) {
log.log(Level.SEVERE, "Error while saving file '" + file.getPath() + "'", e);
}
}
public void save(File file) throws ConfigurationTransformerException {
if (file == null) {
throw new IllegalArgumentException("file cannot be null");
}
if (!file.isFile() && exampleConfiguration != null) {
content = deepCopyValueMapsIfAbsent(exampleConfiguration.getContent(), content, configurationOptions.isNullTreatedAsValue());
}
justSave(file);
}
private void justSave(File file) throws ConfigurationTransformerException {
if (configurationOptions.isDefaultConfigurationAddedOnSave() && defaultConfiguration != null) {
content = deepCopyValueMapsIfAbsent(defaultConfiguration.getContent(), content, configurationOptions.isNullTreatedAsValue());
}
file.getAbsoluteFile().getParentFile().mkdirs();
configurationTransformer.transform(content, file);
}
public Object get(Predicate<Object> predicate, Object... path) { // get, retrieve, lookup, search
if (path == null) {
throw new IllegalArgumentException("path cannot be null");
}
Map<?, ?> previous = null;
Object object = content;
for (int i = 0; i < path.length; i++) {
if (object instanceof Map) {
previous = (Map<?, ?>) object;
object = previous.get(path[i]);
} else {
return defaultConfiguration == null || predicate == null || predicate.test(NoValue.INSTANCE) ? NoValue.INSTANCE : defaultConfiguration.get(predicate, path);
}
}
if (object == null && (!configurationOptions.isNullTreatedAsValue() || previous != null && /*if (predicate != null && !predicate.test(null) && !predicate.test(NoValue.INSTANCE)) {return defaultConfiguration == null ? NoValue.INSTANCE : defaultConfiguration.get(predicate, path);}*/!previous.containsKey(path[path.length - 1]))) {
object = NoValue.INSTANCE;
}
return predicate == null || predicate.test(object) ? object : defaultConfiguration == null ? NoValue.INSTANCE : defaultConfiguration.get(predicate, path);
}
@SuppressWarnings("unchecked")
public Object set(Object value, Object... path) { // insert, set, create, construct, add, put
if (path == null) {
throw new IllegalArgumentException("path cannot be null");
}
if (path.length == 0) {
Object oldContent = content;
content = value;
return oldContent == null && !configurationOptions.isNullTreatedAsValue() ? NoValue.INSTANCE : oldContent;
}
@SuppressWarnings("rawtypes")
Map map;
Object object = content;
boolean noValue = false;
if (object instanceof Map) {
map = (Map<?, ?>) object;
} else {
map = new LinkedHashMap<>();
content = map;
noValue = true;
}
for (int i = 0; i < path.length - 1; i++) {
if (!noValue && (object = map.get(path[i])) instanceof Map) {
map = (Map<?, ?>) object;
} else {
Map<?, ?> newMap = new LinkedHashMap<>();
map.put(path[i], newMap);
map = newMap;
noValue = true;
}
}
if (configurationOptions.isNullTreatedAsValue()) {
if (noValue) {
map.put(path[path.length - 1], value);
return NoValue.INSTANCE;
} else {
if (map.containsKey(path[path.length - 1])) {
return map.put(path[path.length - 1], value);
} else {
map.put(path[path.length - 1], value);
return NoValue.INSTANCE;
}
}
} else {
object = map.put(path[path.length - 1], value);
return object == null ? NoValue.INSTANCE : object;
}
}
public Object clear(Object... path) { // delete, remove, reset, clear, erase
if (path == null) {
throw new IllegalArgumentException("path cannot be null");
}
Map<?, ?> previous = null;
Object object = content;
for (int i = 0; i < path.length; i++) {
if (object instanceof Map) {
previous = (Map<?, ?>) object;
object = previous.get(path[i]);
} else {
return NoValue.INSTANCE;
}
}
if (object == null && (!configurationOptions.isNullTreatedAsValue() || previous != null && !previous.containsKey(path[path.length - 1]))) {
object = NoValue.INSTANCE;
}
if (previous == null) { // can be merged with the if statement above
content = new LinkedHashMap<>();
} else {
previous.remove(path[path.length - 1]); // not necessary if previous doesn't contain the path
}
return object;
}
/*public boolean exists(Object... path) { // exists, contains, isSet
return false;
}
public Object insertIfAbsent(Object value, Object... path) { // insert, set, create, construct, add, put
return null;
}*/
public static enum NoValue {
INSTANCE;
@Override
public final String toString() {
return "NO_VALUE_INSTANCE";
}
}
}