-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
MapTagFlagTracker.java
152 lines (135 loc) · 5.06 KB
/
MapTagFlagTracker.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
package com.denizenscript.denizencore.flags;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.MapTag;
import com.denizenscript.denizencore.objects.core.TimeTag;
import com.denizenscript.denizencore.tags.TagContext;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.text.StringHolder;
import java.util.*;
public class MapTagFlagTracker extends AbstractFlagTracker {
public MapTag map;
public MapTagFlagTracker() {
this.map = new MapTag();
}
public MapTagFlagTracker(MapTag map) {
this.map = map;
doClean(map);
}
public MapTagFlagTracker(String mapTagValue, TagContext context) {
this(MapTag.valueOf(mapTagValue, context));
}
public static StringHolder valueString = new StringHolder("__value");
public static StringHolder expirationString = new StringHolder("__expiration");
public static boolean isExpired(ObjectTag expirationObj) {
if (expirationObj == null) {
return false;
}
if (TimeTag.now().millis() > ((TimeTag) expirationObj).millis()) {
return true;
}
return false;
}
@Override
public ObjectTag getFlagValue(String key) {
List<String> splitKey = CoreUtilities.split(key, '.');
String endKey = splitKey.get(splitKey.size() - 1);
MapTag map = this.map;
for (int i = 0; i < splitKey.size() - 1; i++) {
MapTag subMap = (MapTag) map.getObject(splitKey.get(i));
if (subMap == null) {
return null;
}
ObjectTag subValue = subMap.map.get(valueString);
if (!(subValue instanceof MapTag)) {
return null;
}
map = (MapTag) subValue;
}
MapTag obj = (MapTag) map.getObject(endKey);
if (obj == null) {
return null;
}
ObjectTag value = obj.map.get(valueString);
if (value == null) {
return null;
}
if (isExpired(obj.map.get(expirationString))) {
return null;
}
if (value instanceof MapTag) {
return deflaggedSubMap((MapTag) value);
}
return value;
}
public MapTag deflaggedSubMap(MapTag map) {
MapTag toReturn = new MapTag();
for (Map.Entry<StringHolder, ObjectTag> pair : map.map.entrySet()) {
MapTag subMap = (MapTag) pair.getValue();
if (isExpired(subMap.map.get(expirationString))) {
continue;
}
ObjectTag subValue = subMap.map.get(valueString);
if (subValue instanceof MapTag) {
subValue = deflaggedSubMap((MapTag) subValue);
}
toReturn.map.put(pair.getKey(), subValue);
}
return toReturn;
}
@Override
public TimeTag getFlagExpirationTime(String key) {
return null;
}
@Override
public Collection<String> listAllFlags() {
ArrayList<String> keys = new ArrayList<>(map.map.size());
for (StringHolder string : map.map.keySet()) {
keys.add(string.str);
}
return keys;
}
public void doClean(MapTag map) {
ArrayList<StringHolder> toRemove = new ArrayList<>();
for (Map.Entry<StringHolder, ObjectTag> entry : map.map.entrySet()) {
if (isExpired(((MapTag) entry.getValue()).map.get(expirationString))) {
toRemove.add(entry.getKey());
}
else {
ObjectTag subValue = ((MapTag) entry.getValue()).map.get(valueString);
if (subValue instanceof MapTag) {
doClean((MapTag) subValue);
}
}
}
for (StringHolder str : toRemove) {
map.map.remove(str);
}
}
@Override
public void setFlag(String key, ObjectTag value, TimeTag expiration) {
List<String> splitKey = CoreUtilities.split(key, '.');
String endKey = splitKey.get(splitKey.size() - 1);
for (int i = 0; i < splitKey.size() - 1; i++) {
MapTag flagMap = (MapTag) map.getObject(splitKey.get(i));
if (flagMap == null) {
flagMap = new MapTag();
map.putObject(splitKey.get(i), flagMap);
}
ObjectTag innerMapTag = flagMap.map.get(valueString);
flagMap.map.remove(expirationString);
if (!(innerMapTag instanceof MapTag)) {
innerMapTag = new MapTag();
flagMap.map.put(valueString, innerMapTag);
}
map = (MapTag) innerMapTag;
}
MapTag resultMap = new MapTag();
resultMap.map.put(valueString, value);
resultMap.map.put(expirationString, expiration);
map.putObject(endKey, resultMap);
}
@Override
public String toString() {
return map.toString();
}
}