-
Notifications
You must be signed in to change notification settings - Fork 429
Expand file tree
/
Copy pathConfigSection.java
More file actions
774 lines (700 loc) · 21 KB
/
ConfigSection.java
File metadata and controls
774 lines (700 loc) · 21 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
package cn.nukkit.utils;
import java.util.*;
/**
* Created by fromgate on 26.04.2016.
*/
public class ConfigSection extends LinkedHashMap<String, Object> {
/**
* Empty ConfigSection constructor
*/
public ConfigSection() {
super();
}
/**
* Constructor of ConfigSection that contains initial key/value data
*
* @param key
* @param value
*/
public ConfigSection(String key, Object value) {
this();
this.set(key, value);
}
/**
* Constructor of ConfigSection, based on values stored in map.
*
* @param map
*/
public ConfigSection(LinkedHashMap<String, Object> map) {
this();
if (map == null || map.isEmpty()) return;
for (Map.Entry<String, Object> entry : map.entrySet()) {
if (entry.getValue() instanceof LinkedHashMap) {
super.put(entry.getKey(), new ConfigSection((LinkedHashMap) entry.getValue()));
} else if (entry.getValue() instanceof List) {
super.put(entry.getKey(), parseList((List) entry.getValue()));
} else {
super.put(entry.getKey(), entry.getValue());
}
}
}
private List parseList(List list) {
List<Object> newList = new ArrayList<>();
for (Object o : list) {
if (o instanceof LinkedHashMap) {
newList.add(new ConfigSection((LinkedHashMap) o));
} else {
newList.add(o);
}
}
return newList;
}
/**
* Get root section as LinkedHashMap
*
* @return
*/
public Map<String, Object> getAllMap() {
return new LinkedHashMap<>(this);
}
/**
* Get new instance of config section
*
* @return
*/
public ConfigSection getAll() {
return new ConfigSection(this);
}
/**
* Get object by key. If section does not contain value, return null
*/
public Object get(String key) {
return this.get(key, null);
}
/**
* Get object by key. If section does not contain value, return default value
*
* @param key
* @param defaultValue
* @return
*/
public <T> T get(String key, T defaultValue) {
if (key == null || key.isEmpty()) return defaultValue;
if (super.containsKey(key)) return (T) super.get(key);
String[] keys = key.split("\\.", 2);
if (!super.containsKey(keys[0])) return defaultValue;
Object value = super.get(keys[0]);
if (value instanceof ConfigSection) {
ConfigSection section = (ConfigSection) value;
return section.get(keys[1], defaultValue);
}
return defaultValue;
}
/**
* Store value into config section
*
* @param key
* @param value
*/
public void set(String key, Object value) {
String[] subKeys = key.split("\\.", 2);
if (subKeys.length > 1) {
ConfigSection childSection = new ConfigSection();
if (this.containsKey(subKeys[0]) && super.get(subKeys[0]) instanceof ConfigSection)
childSection = (ConfigSection) super.get(subKeys[0]);
childSection.set(subKeys[1], value);
super.put(subKeys[0], childSection);
} else super.put(subKeys[0], value);
}
/**
* Check type of section element defined by key. Return true this element is ConfigSection
*
* @param key
* @return
*/
public boolean isSection(String key) {
Object value = this.get(key);
return value instanceof ConfigSection;
}
/**
* Get config section element defined by key
*
* @param key
* @return
*/
public ConfigSection getSection(String key) {
return this.get(key, new ConfigSection());
}
//@formatter:off
/**
* Get all ConfigSections in root path.
* Example config:
* a1:
* b1:
* c1:
* c2:
* a2:
* b2:
* c3:
* c4:
* a3: true
* a4: "hello"
* a5: 100
* <p>
* getSections() will return new ConfigSection, that contains sections a1 and a2 only.
*
* @return
*/
//@formatter:on
public ConfigSection getSections() {
return getSections(null);
}
/**
* Get sections (and only sections) from provided path
*
* @param key - config section path, if null or empty root path will used.
* @return
*/
public ConfigSection getSections(String key) {
ConfigSection sections = new ConfigSection();
ConfigSection parent = key == null || key.isEmpty() ? this.getAll() : getSection(key);
if (parent == null) return sections;
parent.forEach((key1, value) -> {
if (value instanceof ConfigSection)
sections.put(key1, value);
});
return sections;
}
/**
* Get int value of config section element
*
* @param key - key (inside) current section (default value equals to 0)
* @return
*/
public int getInt(String key) {
return this.getInt(key, 0);
}
/**
* Get int value of config section element
*
* @param key - key (inside) current section
* @param defaultValue - default value that will returned if section element is not exists
* @return
*/
public int getInt(String key, int defaultValue) {
return this.get(key, ((Number) defaultValue)).intValue();
}
/**
* Check type of section element defined by key. Return true this element is Integer
*
* @param key
* @return
*/
public boolean isInt(String key) {
Object val = get(key);
return val instanceof Integer;
}
/**
* Get long value of config section element
*
* @param key - key (inside) current section
* @return
*/
public long getLong(String key) {
return this.getLong(key, 0);
}
/**
* Get long value of config section element
*
* @param key - key (inside) current section
* @param defaultValue - default value that will returned if section element is not exists
* @return
*/
public long getLong(String key, long defaultValue) {
return this.get(key, ((Number) defaultValue)).longValue();
}
/**
* Check type of section element defined by key. Return true this element is Long
*
* @param key
* @return
*/
public boolean isLong(String key) {
Object val = get(key);
return val instanceof Long;
}
/**
* Get double value of config section element
*
* @param key - key (inside) current section
* @return
*/
public double getDouble(String key) {
return this.getDouble(key, 0);
}
/**
* Get double value of config section element
*
* @param key - key (inside) current section
* @param defaultValue - default value that will returned if section element is not exists
* @return
*/
public double getDouble(String key, double defaultValue) {
return this.get(key, ((Number) defaultValue)).doubleValue();
}
/**
* Check type of section element defined by key. Return true this element is Double
*
* @param key
* @return
*/
public boolean isDouble(String key) {
Object val = get(key);
return val instanceof Double;
}
/**
* Get String value of config section element
*
* @param key - key (inside) current section
* @return
*/
public String getString(String key) {
return this.getString(key, "");
}
/**
* Get String value of config section element
*
* @param key - key (inside) current section
* @param defaultValue - default value that will returned if section element is not exists
* @return
*/
public String getString(String key, String defaultValue) {
Object result = this.get(key, defaultValue);
return String.valueOf(result);
}
/**
* Check type of section element defined by key. Return true this element is String
*
* @param key
* @return
*/
public boolean isString(String key) {
Object val = get(key);
return val instanceof String;
}
/**
* Get boolean value of config section element
*
* @param key - key (inside) current section
* @return
*/
public boolean getBoolean(String key) {
return this.getBoolean(key, false);
}
/**
* Get boolean value of config section element
*
* @param key - key (inside) current section
* @param defaultValue - default value that will returned if section element is not exists
* @return
*/
public boolean getBoolean(String key, boolean defaultValue) {
return this.get(key, defaultValue);
}
/**
* Check type of section element defined by key. Return true this element is Integer
*
* @param key
* @return
*/
public boolean isBoolean(String key) {
Object val = get(key);
return val instanceof Boolean;
}
/**
* Get List value of config section element
*
* @param key - key (inside) current section
* @return
*/
public List getList(String key) {
return this.getList(key, null);
}
/**
* Get List value of config section element
*
* @param key - key (inside) current section
* @param defaultList - default value that will returned if section element is not exists
* @return
*/
public List getList(String key, List defaultList) {
return this.get(key, defaultList);
}
/**
* Check type of section element defined by key. Return true this element is List
*
* @param key
* @return
*/
public boolean isList(String key) {
Object val = get(key);
return val instanceof List;
}
/**
* Get Map value of config section element
*
* @param key - key (inside) current section
* @return
*/
public Map getMap(String key) {
return this.getMap(key, null);
}
/**
* Get Map value of config section element
*
* @param key - key (inside) current section
* @param defaultList - default value that will returned if section element is not exists
* @return
*/
public Map getMap(String key, List defaultList) {
return (Map) this.get(key, defaultList);
}
/**
* Check type of section element defined by key. Return true this element is Map
*
* @param key
* @return
*/
public boolean isMap(String key) {
Object val = get(key);
return val instanceof Map;
}
/**
* Get String List value of config section element
*
* @param key - key (inside) current section
* @return
*/
public List<String> getStringList(String key) {
List value = this.getList(key);
if (value == null) {
return new ArrayList<>(0);
}
List<String> result = new ArrayList<>();
for (Object o : value) {
if (o instanceof String || o instanceof Number || o instanceof Boolean || o instanceof Character) {
result.add(String.valueOf(o));
}
}
return result;
}
/**
* Get Integer List value of config section element
*
* @param key - key (inside) current section
* @return
*/
public List<Integer> getIntegerList(String key) {
List<?> list = getList(key);
if (list == null) {
return new ArrayList<>(0);
}
List<Integer> result = new ArrayList<>();
for (Object object : list) {
if (object instanceof Integer) {
result.add((Integer) object);
} else if (object instanceof String) {
try {
result.add(Integer.valueOf((String) object));
} catch (Exception ex) {
//ignore
}
} else if (object instanceof Character) {
result.add((int) (Character) object);
} else if (object instanceof Number) {
result.add(((Number) object).intValue());
}
}
return result;
}
/**
* Get Boolean List value of config section element
*
* @param key - key (inside) current section
* @return
*/
public List<Boolean> getBooleanList(String key) {
List<?> list = getList(key);
if (list == null) {
return new ArrayList<>(0);
}
List<Boolean> result = new ArrayList<>();
for (Object object : list) {
if (object instanceof Boolean) {
result.add((Boolean) object);
} else if (object instanceof String) {
if (Boolean.TRUE.toString().equals(object)) {
result.add(true);
} else if (Boolean.FALSE.toString().equals(object)) {
result.add(false);
}
}
}
return result;
}
/**
* Get Double List value of config section element
*
* @param key - key (inside) current section
* @return
*/
public List<Double> getDoubleList(String key) {
List<?> list = getList(key);
if (list == null) {
return new ArrayList<>(0);
}
List<Double> result = new ArrayList<>();
for (Object object : list) {
if (object instanceof Double) {
result.add((Double) object);
} else if (object instanceof String) {
try {
result.add(Double.valueOf((String) object));
} catch (Exception ex) {
//ignore
}
} else if (object instanceof Character) {
result.add((double) (Character) object);
} else if (object instanceof Number) {
result.add(((Number) object).doubleValue());
}
}
return result;
}
/**
* Get Float List value of config section element
*
* @param key - key (inside) current section
* @return
*/
public List<Float> getFloatList(String key) {
List<?> list = getList(key);
if (list == null) {
return new ArrayList<>(0);
}
List<Float> result = new ArrayList<>();
for (Object object : list) {
if (object instanceof Float) {
result.add((Float) object);
} else if (object instanceof String) {
try {
result.add(Float.valueOf((String) object));
} catch (Exception ex) {
//ignore
}
} else if (object instanceof Character) {
result.add((float) (Character) object);
} else if (object instanceof Number) {
result.add(((Number) object).floatValue());
}
}
return result;
}
/**
* Get Long List value of config section element
*
* @param key - key (inside) current section
* @return
*/
public List<Long> getLongList(String key) {
List<?> list = getList(key);
if (list == null) {
return new ArrayList<>(0);
}
List<Long> result = new ArrayList<>();
for (Object object : list) {
if (object instanceof Long) {
result.add((Long) object);
} else if (object instanceof String) {
try {
result.add(Long.valueOf((String) object));
} catch (Exception ex) {
//ignore
}
} else if (object instanceof Character) {
result.add((long) (Character) object);
} else if (object instanceof Number) {
result.add(((Number) object).longValue());
}
}
return result;
}
/**
* Get Byte List value of config section element
*
* @param key - key (inside) current section
* @return
*/
public List<Byte> getByteList(String key) {
List<?> list = getList(key);
if (list == null) {
return new ArrayList<>(0);
}
List<Byte> result = new ArrayList<>();
for (Object object : list) {
if (object instanceof Byte) {
result.add((Byte) object);
} else if (object instanceof String) {
try {
result.add(Byte.valueOf((String) object));
} catch (Exception ex) {
//ignore
}
} else if (object instanceof Character) {
result.add((byte) ((Character) object).charValue());
} else if (object instanceof Number) {
result.add(((Number) object).byteValue());
}
}
return result;
}
/**
* Get Character List value of config section element
*
* @param key - key (inside) current section
* @return
*/
public List<Character> getCharacterList(String key) {
List<?> list = getList(key);
if (list == null) {
return new ArrayList<>(0);
}
List<Character> result = new ArrayList<>();
for (Object object : list) {
if (object instanceof Character) {
result.add((Character) object);
} else if (object instanceof String) {
String str = (String) object;
if (str.length() == 1) {
result.add(str.charAt(0));
}
} else if (object instanceof Number) {
result.add((char) ((Number) object).intValue());
}
}
return result;
}
/**
* Get Short List value of config section element
*
* @param key - key (inside) current section
* @return
*/
public List<Short> getShortList(String key) {
List<?> list = getList(key);
if (list == null) {
return new ArrayList<>(0);
}
List<Short> result = new ArrayList<>();
for (Object object : list) {
if (object instanceof Short) {
result.add((Short) object);
} else if (object instanceof String) {
try {
result.add(Short.valueOf((String) object));
} catch (Exception ex) {
//ignore
}
} else if (object instanceof Character) {
result.add((short) ((Character) object).charValue());
} else if (object instanceof Number) {
result.add(((Number) object).shortValue());
}
}
return result;
}
/**
* Get Map List value of config section element
*
* @param key - key (inside) current section
* @return
*/
public List<Map> getMapList(String key) {
List<Map> list = getList(key);
List<Map> result = new ArrayList<>();
if (list == null) {
return result;
}
for (Object object : list) {
if (object instanceof Map) {
result.add((Map) object);
}
}
return result;
}
/**
* Check existence of config section element
*
* @param key
* @param ignoreCase
* @return
*/
public boolean exists(String key, boolean ignoreCase) {
if (ignoreCase) key = key.toLowerCase();
for (String existKey : this.getKeys(true)) {
if (ignoreCase) existKey = existKey.toLowerCase();
if (existKey.equals(key)) return true;
}
return false;
}
/**
* Check existence of config section element
*
* @param key
* @return
*/
public boolean exists(String key) {
return exists(key, false);
}
/**
* Remove config section element
*
* @param key
*/
public void remove(String key) {
if (key == null || key.isEmpty()) return;
if (super.containsKey(key)) super.remove(key);
else if (this.containsKey(".")) {
String[] keys = key.split("\\.", 2);
if (super.get(keys[0]) instanceof ConfigSection) {
ConfigSection section = (ConfigSection) super.get(keys[0]);
section.remove(keys[1]);
}
}
}
/**
* Get all keys
*
* @param child - true = include child keys
* @return
*/
public Set<String> getKeys(boolean child) {
Set<String> keys = new LinkedHashSet<>();
this.forEach((key, value) -> {
keys.add(key);
if (value instanceof ConfigSection) {
if (child)
((ConfigSection) value).getKeys(true).forEach(childKey -> keys.add(key + "." + childKey));
}
});
return keys;
}
/**
* Get all keys
*
* @return
*/
public Set<String> getKeys() {
return this.getKeys(true);
}
}