-
Notifications
You must be signed in to change notification settings - Fork 0
/
INISettings.java
95 lines (79 loc) · 2.4 KB
/
INISettings.java
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
package chkrr00k.persister;
//"proudly" made by chkrr00k
// LGPL licence
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javafx.util.Pair;
public class INISettings {
private Map<String, Map<String, String>> storage;
public INISettings(Map<String, List<Pair<String, String>>> input) {
this.storage = new HashMap<String, Map<String, String>>();
for(String key : input.keySet()){
this.storage.put(key, this.toMap(input.get(key)));
}
}
public INISettings() {
this.storage = new HashMap<String, Map<String, String>>();
}
private Map<String, String> toMap(List<Pair<String, String>> list) {
Map<String, String> result = new HashMap<String, String>();
for(Pair<String, String> pair : list){
result.put(pair.getKey(), pair.getValue());
}
return result;
}
public Map<String, String> getSettings(String field) {
if(this.storage.containsKey(field)){
return this.storage.get(field);
}else{
return null;
}
}
public String getSetting(String field, String key) {
if(this.storage.containsKey(field) && this.storage.get(field).containsKey(key)){
return this.storage.get(field).get(key);
}else{
return null;
}
}
public void addSettings(String field, Map<String, String> values) {
if(this.storage.containsKey(field)){
this.storage.get(field).putAll(values);
}else{
this.storage.put(field, values);
}
}
public void addSetting(String field, String key, String value) {
if(this.storage.containsKey(field)){
this.storage.get(field).put(key, value);
}else{
Map<String, String> newMap = new HashMap<String, String>();
newMap.put(key, value);
this.storage.put(field, newMap);
}
}
public void removeSettings(String field) {
if(this.storage.containsKey(field)){
this.storage.remove(field);
}
}
public void removeSetting(String field, String key) {
if(this.storage.containsKey(field)){
this.storage.get(field).remove(key);
}
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
String ls = System.lineSeparator();
for(String key : this.storage.keySet()){
builder.append("[" + key + "]" + ls);
for(String valKey : this.storage.get(key).keySet()){
builder.append(valKey + "=" + this.storage.get(key).get(valKey) + ls);
}
builder.append(ls);
}
return builder.toString();
}
}