-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
MemoryDataKey.java
180 lines (151 loc) · 4.32 KB
/
MemoryDataKey.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
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
package net.citizensnpcs.api.util;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.MemoryConfiguration;
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
public class MemoryDataKey extends DataKey {
private String name;
private final ConfigurationSection root;
public MemoryDataKey() {
super("");
root = new MemoryConfiguration();
}
private MemoryDataKey(ConfigurationSection root, String path) {
super(path);
this.root = root;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
MemoryDataKey other = (MemoryDataKey) obj;
if (path == null) {
if (other.path != null) {
return false;
}
} else if (!path.equals(other.path)) {
return false;
}
return true;
}
@Override
public boolean getBoolean(String key) {
return root.getBoolean(getKeyFor(key), false);
}
@Override
public double getDouble(String key) {
return root.getDouble(getKeyFor(key), 0D);
}
@Override
public int getInt(String key) {
return root.getInt(getKeyFor(key), 0);
}
private String getKeyFor(String key) {
if (key.isEmpty())
return path;
if (key.charAt(0) == '.')
return path.isEmpty() ? key.substring(1, key.length()) : path + key;
return path.isEmpty() ? key : path + "." + key;
}
@Override
public long getLong(String key) {
return root.getLong(getKeyFor(key), 0L);
}
@Override
public Object getRaw(String key) {
return root.get(getKeyFor(key));
}
@Override
public MemoryDataKey getRelative(String relative) {
String key = getKeyFor(relative);
return new MemoryDataKey(root, key);
}
@Override
public String getString(String key) {
return root.getString(getKeyFor(key), "");
}
@Override
public Iterable<DataKey> getSubKeys() {
ConfigurationSection head = root.getConfigurationSection(path);
if (head == null)
return Collections.emptyList();
Set<String> keys = head.getKeys(false);
return Iterables.transform(keys, new Function<String, DataKey>() {
@Override
public DataKey apply(String input) {
return new MemoryDataKey(root, getKeyFor(input));
}
});
}
@Override
public Map<String, Object> getValuesDeep() {
ConfigurationSection section = root.getConfigurationSection(path);
if (section == null)
return Collections.emptyMap();
return section.getValues(true);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((path == null) ? 0 : path.hashCode());
return result;
}
@Override
public boolean keyExists(String key) {
return root.isSet(getKeyFor(key));
}
@Override
public String name() {
if (name == null) {
int idx = path.lastIndexOf('.');
name = idx == -1 ? path : path.substring(idx + 1, path.length());
}
return name;
}
@Override
public void removeKey(String key) {
set(key, null);
}
private void set(String key, Object value) {
root.set(getKeyFor(key), value);
}
@Override
public void setBoolean(String key, boolean value) {
set(key, value);
}
@Override
public void setDouble(String key, double value) {
set(key, value);
}
@Override
public void setInt(String key, int value) {
set(key, value);
}
@Override
public void setLong(String key, long value) {
set(key, value);
}
@Override
public void setRaw(String key, Object value) {
set(key, value);
}
@Override
public void setString(String key, String value) {
set(key, value);
}
@Override
public String toString() {
return "MemoryDataKey[" + path + "]";
}
}