ModifierSet.java
8.87 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.scene7.is.util.Enum
* com.scene7.is.util.KeyValuePair
* com.scene7.is.util.XMLConvertible
* com.scene7.is.util.XMLUtil
* com.scene7.is.util.text.Formatter
* com.scene7.is.util.text.ObjectPrinter
* com.scene7.is.util.text.ParameterException
* org.jetbrains.annotations.NotNull
* org.jetbrains.annotations.Nullable
*/
package com.scene7.is.ps.provider;
import com.scene7.is.ps.provider.ModifierSetIterator;
import com.scene7.is.ps.provider.defs.ModifierEnum;
import com.scene7.is.util.Enum;
import com.scene7.is.util.KeyValuePair;
import com.scene7.is.util.XMLConvertible;
import com.scene7.is.util.XMLUtil;
import com.scene7.is.util.text.Formatter;
import com.scene7.is.util.text.ObjectPrinter;
import com.scene7.is.util.text.ParameterException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.AbstractCollection;
import java.util.Iterator;
import java.util.List;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
public class ModifierSet
extends AbstractCollection<KeyValuePair<ModifierEnum<?>, ?>>
implements XMLConvertible {
public static final int OVERRIDE_NONE = 0;
public static final int OVERRIDE_ALL = Integer.MAX_VALUE;
private Object[] values;
private short[] scores;
private short maxScore;
private boolean frozen;
private boolean shared;
public ModifierSet() {
this.values = new Object[ModifierEnum.VALUE_LIST.size()];
this.scores = new short[ModifierEnum.VALUE_LIST.size()];
}
public void finalize() throws Throwable {
Object.super.finalize();
}
public ModifierSet(ModifierSet src) {
this.setAll(src);
}
public <T> void set(ModifierEnum<T> modifier, @Nullable T value) {
assert (!this.frozen);
this.unshare();
int id = modifier.id;
if (value != null) {
this.values[id] = value;
this.scores[id] = this.maxScore = (short)(this.maxScore + 1);
} else {
this.values[id] = null;
this.scores[id] = 0;
}
}
public void setAll(ModifierSet src) {
if (src.frozen || src.shared) {
this.values = src.values;
this.scores = src.scores;
this.shared = true;
} else {
this.values = new Object[ModifierEnum.VALUE_LIST.size()];
this.scores = new short[ModifierEnum.VALUE_LIST.size()];
System.arraycopy(src.values, 0, this.values, 0, src.values.length);
System.arraycopy(src.scores, 0, this.scores, 0, src.scores.length);
}
this.maxScore = src.maxScore;
}
public void freeze() {
assert (!this.frozen);
this.frozen = true;
}
public void merge(ModifierSet src, int baseScore) {
assert (!this.frozen);
if (src.maxScore == 0) {
return;
}
int base = baseScore > this.maxScore ? this.maxScore : baseScore;
this.unshare();
for (int i = 0; i < this.values.length; ++i) {
short thisScore;
short srcScore = (short)(src.scores[i] + base);
short s = thisScore = this.scores[i] <= base ? this.scores[i] : (short)(this.scores[i] + src.maxScore);
if (srcScore != base && srcScore >= thisScore) {
this.scores[i] = srcScore;
this.values[i] = src.values[i];
continue;
}
this.scores[i] = thisScore;
}
this.maxScore = (short)(this.maxScore + src.maxScore);
}
public Object clone() {
return new ModifierSet(this);
}
public <T> boolean equal(ModifierEnum<T> modifier, T expected) {
Object actual = this.get(modifier, null);
if (expected == null) {
return actual == null;
}
return expected.equals(actual);
}
@NotNull
public <T> T get(ModifierEnum<T> modifier) throws ParameterException {
int id = modifier.id;
Object value = this.values[id];
if (value != null) {
return (T)value;
}
throw new ParameterException(1, modifier.name, null, null);
}
public <T> T get(ModifierEnum<T> modifier, T defaultValue) {
int id = modifier.id;
Object value = this.values[id];
if (value != null) {
return (T)value;
}
return defaultValue;
}
@NotNull
public <T> T getOrDie(ModifierEnum<T> modifier) {
int id = modifier.id;
Object value = this.values[id];
assert (value != null);
return (T)value;
}
@Deprecated
public <T> T get(ModifierEnum<T> modifier, int index) throws ParameterException {
int id = modifier.id;
assert (this.values[id] instanceof Object[]);
Object[] obj = (Object[])this.values[id];
assert (index < obj.length);
Object value = obj[index];
if (value != null) {
return (T)value;
}
throw new ParameterException(1, modifier.name, null, null);
}
public <T> T get(ModifierEnum<T> modifier, int index, T defaultValue) {
int id = modifier.id;
Object[] obj = (Object[])this.values[id];
if (obj == null || index >= obj.length) {
return defaultValue;
}
Object value = obj[index];
if (value != null) {
return (T)value;
}
return defaultValue;
}
public Object getOrDie(ModifierEnum<Object[]> modifier, int index) {
int id = modifier.id;
assert (this.values[id] instanceof Object[]);
Object[] obj = (Object[])this.values[id];
assert (index < obj.length);
Object value = obj[index];
assert (value != null);
return value;
}
public int getScore(ModifierEnum modifier) {
return this.scores[modifier.id];
}
public boolean contains(ModifierEnum modifier) {
return this.values[modifier.id] != null;
}
@NotNull
@Override
public String toString() {
StringWriter buf = new StringWriter();
PrintWriter out = new PrintWriter(buf);
for (ModifierEnum value : ModifierEnum.VALUE_LIST) {
if (this.values[value.id] == null) continue;
String key = value.name;
if (key.startsWith("$")) {
key = "I_" + key.substring(1);
}
out.print(key + "{" + this.scores[value.id] + "}: ");
ObjectPrinter.print((PrintWriter)out, (Object)Formatter.indent((String)String.valueOf(this.values[value.id]), (int)4));
out.println();
}
out.close();
return buf.toString();
}
public Element toXMLElement(Document document) {
Element root = document.createElement("modifier-set");
for (ModifierEnum value : ModifierEnum.VALUE_LIST) {
if (this.values[value.id] == null) continue;
String key = value.name;
if (key.startsWith("$")) {
key = "I_" + key.substring(1);
}
Element modifierElement = document.createElement("modifier");
modifierElement.setAttribute("name", key);
modifierElement.setAttribute("score", String.valueOf(this.scores[value.id]));
Object obj = this.values[value.id];
if (obj instanceof Number || obj instanceof String || obj instanceof Boolean) {
modifierElement.setAttribute("value", String.valueOf(obj));
modifierElement.setAttribute("type", obj.getClass().getName());
} else if (obj instanceof Enum) {
modifierElement.setAttribute("value", "[" + String.valueOf(obj) + "]");
modifierElement.setAttribute("type", obj.getClass().getName());
} else {
Element modifierValue = XMLUtil.createElement((Document)document, (Object)obj);
modifierElement.appendChild(modifierValue);
}
root.appendChild(modifierElement);
}
return root;
}
private void unshare() {
if (this.shared) {
Object[] oldValues = this.values;
short[] oldScores = this.scores;
this.values = new Object[ModifierEnum.VALUE_LIST.size()];
this.scores = new short[ModifierEnum.VALUE_LIST.size()];
System.arraycopy(oldValues, 0, this.values, 0, this.values.length);
System.arraycopy(oldScores, 0, this.scores, 0, this.scores.length);
this.shared = false;
}
}
@Override
public int size() {
int count = 0;
for (KeyValuePair element : this) {
++count;
}
return count;
}
@NotNull
@Override
public Iterator<KeyValuePair<ModifierEnum<?>, ?>> iterator() {
return new ModifierSetIterator(this);
}
}