-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
CoreUtilities.java
422 lines (374 loc) · 14 KB
/
CoreUtilities.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
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
package net.aufdemrand.denizencore.utilities;
import net.aufdemrand.denizencore.objects.*;
import net.aufdemrand.denizencore.objects.properties.Property;
import net.aufdemrand.denizencore.objects.properties.PropertyParser;
import net.aufdemrand.denizencore.scripts.queues.ScriptQueue;
import net.aufdemrand.denizencore.tags.Attribute;
import net.aufdemrand.denizencore.tags.TagContext;
import net.aufdemrand.denizencore.utilities.debugging.dB;
import java.io.File;
import java.io.FilenameFilter;
import java.math.BigDecimal;
import java.util.*;
public class CoreUtilities {
public static String stringifyNullPass(Object obj) {
return obj == null ? null : obj.toString();
}
public static dObject fixType(dObject input, TagContext context) {
if (input instanceof Element) {
return ObjectFetcher.pickObjectFor(input.toString(), context);
}
return input;
}
public static void autoPropertyMechanism(dObject object, Mechanism mechanism) {
PropertyParser.ClassPropertiesInfo properties = PropertyParser.propertiesByClass.get(object.getClass());
if (properties == null) {
return;
}
PropertyParser.PropertyGetter specificGetter = properties.propertiesByMechanism.get(mechanism.getName());
if (specificGetter != null) {
Property prop = specificGetter.get(object);
if (prop == null) {
return;
}
prop.adjust(mechanism);
return;
}
for (PropertyParser.PropertyGetter listGetter : properties.propertiesAnyMechs) {
Property prop = listGetter.get(object);
if (prop != null) {
prop.adjust(mechanism);
if (mechanism.fulfilled()) {
return;
}
}
}
}
public static dObject autoPropertyTagObject(dObject object, Attribute attribute) {
if (attribute.isComplete()) {
return null;
}
PropertyParser.ClassPropertiesInfo properties = PropertyParser.propertiesByClass.get(object.getClass());
if (properties == null) {
return null;
}
PropertyParser.PropertyGetter specificGetter = properties.propertiesByTag.get(attribute.getAttributeWithoutContext(1));
if (specificGetter != null) {
Property prop = specificGetter.get(object);
if (prop == null) {
return null;
}
return prop.getObjectAttribute(attribute);
}
for (PropertyParser.PropertyGetter listGetter : properties.propertiesAnyTags) {
Property prop = listGetter.get(object);
if (prop != null) {
dObject returned = prop.getObjectAttribute(attribute);
if (returned != null) {
return returned;
}
}
}
return null;
}
public static String autoPropertyTag(dObject object, Attribute attribute) {
if (attribute.isComplete()) {
return null;
}
PropertyParser.ClassPropertiesInfo properties = PropertyParser.propertiesByClass.get(object.getClass());
if (properties == null) {
return null;
}
PropertyParser.PropertyGetter specificGetter = properties.propertiesByTag.get(attribute.getAttributeWithoutContext(1));
if (specificGetter != null) {
Property prop = specificGetter.get(object);
if (prop == null) {
return null;
}
return prop.getAttribute(attribute);
}
for (PropertyParser.PropertyGetter listGetter : properties.propertiesAnyTags) {
Property prop = listGetter.get(object);
if (prop != null) {
String returned = prop.getAttribute(attribute);
if (returned != null) {
return returned;
}
}
}
return null;
}
public static dObject autoAttrib(Property inp, Attribute attribute) {
if (attribute.isComplete()) {
return null;
}
return inp.getObjectAttribute(attribute);
}
public static dObject autoAttribTyped(dObject inp, Attribute attribute) {
return autoAttrib(fixType(inp, attribute.context), attribute);
}
public static dObject autoAttrib(dObject inp, Attribute attribute) {
if (inp == null) {
dB.echoError("Tag parse failed (null return) for tag <" + attribute.toString() + ">!");
return null;
}
if (attribute.isComplete()) {
return inp;
}
return inp.getObjectAttribute(attribute);
}
public static <T extends dObject> T asType(dObject inp, Class<T> type, TagContext context) {
if (inp.getClass() == type) {
return (T) inp;
}
if (type == Element.class) {
return (T) new Element(inp.toString());
}
if (inp instanceof dObject.ObjectAttributable) {
T temp = ((dObject.ObjectAttributable) inp).asObjectType(type, context);
if (temp != null) {
return temp;
}
}
return ObjectFetcher.getObjectFrom(type, inp.toString(), context);
}
public static abstract class TypeComparisonRunnable {
public abstract boolean canBecome(dObject inp);
}
public final static Map<Class<? extends dObject>, TypeComparisonRunnable> typeCheckers = new HashMap<>();
static {
registerTypeAsTrueAlways(Element.class);
registerTypeAsTrueAlways(dList.class);
registerTypeAsNoOtherTypeCode(dScript.class, "s");
registerTypeAsNoOtherTypeCode(Duration.class, "d");
registerTypeAsNoOtherTypeCode(CustomObject.class, "custom");
registerTypeAsNoOtherTypeCode(ScriptQueue.class, "q");
}
public static void registerTypeAsNoOtherTypeCode(Class<? extends dObject> type, final String knownCode) {
typeCheckers.put(type, new TypeComparisonRunnable() {
@Override
public boolean canBecome(dObject inp) {
String simple = inp.identifySimple();
int atIndex = simple.indexOf('@');
if (atIndex != -1) {
String code = simple.substring(0, atIndex);
if (!code.equals(knownCode)) {
return false;
}
}
return true;
}
});
}
public static void registerTypeAsTrueAlways(Class<? extends dObject> type) {
typeCheckers.put(type, new TypeComparisonRunnable() {
@Override
public boolean canBecome(dObject inp) {
return true;
}
});
}
public static boolean canPossiblyBeType(dObject inp, Class<? extends dObject> type) {
if (inp.getClass() == type) {
return true;
}
TypeComparisonRunnable comp = typeCheckers.get(type);
if (comp != null && !comp.canBecome(inp)) {
return false;
}
return ObjectFetcher.checkMatch(type, inp.toString());
}
static Random random = new Random();
public static Random getRandom() {
return random;
}
static FilenameFilter scriptsFilter;
static {
scriptsFilter = new FilenameFilter() {
public boolean accept(File file, String fileName) {
if (fileName.startsWith(".")) {
return false;
}
String ext = fileName.substring(fileName.lastIndexOf('.') + 1);
return ext.equalsIgnoreCase("YML") || ext.equalsIgnoreCase("DSCRIPT");
}
};
}
public static String bigDecToString(BigDecimal input) {
String temp = input.toString();
if (temp.endsWith(".0")) {
return temp.substring(0, temp.length() - 2);
}
return temp;
}
public static String doubleToString(double input) {
String temp = String.valueOf(input);
if (temp.endsWith(".0")) {
return temp.substring(0, temp.length() - 2);
}
return temp;
}
/**
* Lists all files in the given directory.
*
* @param dir The directory to search in
* @return A {@link java.io.File} collection
*/
public static List<File> listDScriptFiles(File dir) {
List<File> files = new ArrayList<>();
File[] entries = dir.listFiles();
for (File file : entries) {
// Add file
if (scriptsFilter == null || scriptsFilter.accept(dir, file.getName())) {
files.add(file);
}
// Add subdirectories
if (file.isDirectory()) {
files.addAll(listDScriptFiles(file));
}
}
return files;
}
public static List<String> split(String str, char c) {
List<String> strings = new ArrayList<>();
int start = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == c) {
strings.add(str.substring(start, i));
start = i + 1;
}
}
strings.add(str.substring(start, str.length()));
return strings;
}
public static String concat(List<String> str, String split) {
StringBuilder sb = new StringBuilder();
if (str.size() > 0) {
sb.append(str.get(0));
}
for (int i = 1; i < str.size(); i++) {
sb.append(split).append(str.get(i));
}
return sb.toString();
}
public static List<String> split(String str, char c, int max) {
List<String> strings = new ArrayList<>();
int start = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == c) {
strings.add(str.substring(start, i));
start = i + 1;
if (strings.size() + 1 == max) {
break;
}
}
}
strings.add(str.substring(start, str.length()));
if (dB.verbose) {
dB.log("Splitting " + str + " around " + c + " limited to " + max + " returns " + concat(strings, ":::"));
}
return strings;
}
public static String toLowerCase(String input) {
char[] data = input.toCharArray();
for (int i = 0; i < data.length; i++) {
if (data[i] >= 'A' && data[i] <= 'Z') {
data[i] -= 'A' - 'a';
}
}
return new String(data);
}
public static String getXthArg(int argc, String args) {
char[] data = args.toCharArray();
StringBuilder nArg = new StringBuilder();
int arg = 0;
int x = 0;
for (int i = 0; i < data.length; i++) {
if (data[i] == ' ') {
arg++;
if (arg > argc) {
return nArg.toString();
}
}
else if (arg == argc) {
nArg.append(data[i]);
}
}
return nArg.toString();
}
public static boolean xthArgEquals(int argc, String args, String input) {
char[] data = args.toCharArray();
char[] data2 = input.toCharArray();
int arg = 0;
int x = 0;
for (int i = 0; i < data.length; i++) {
if (data[i] == ' ') {
arg++;
}
else if (arg == argc) {
if (x == data2.length) {
return false;
}
if (data2[x++] != data[i]) {
return false;
}
}
}
return x == data2.length;
}
public static String getClosestOption(List<String> strs, String opt) {
int minDist = Integer.MAX_VALUE;
opt = CoreUtilities.toLowerCase(opt);
String closest = "";
for (String cmd : strs) {
String comp = CoreUtilities.toLowerCase(cmd);
int distance = getLevenshteinDistance(opt, comp);
if (minDist > distance) {
minDist = distance;
closest = cmd;
}
}
return closest;
}
public static int getLevenshteinDistance(String s, String t) {
if (s == null || t == null) {
throw new IllegalArgumentException("Strings must not be null");
}
int n = s.length(); // length of s
int m = t.length(); // length of t
if (n == 0) {
return m;
}
else if (m == 0) {
return n;
}
int[] p = new int[n + 1]; // 'previous' cost array, horizontally
int[] d = new int[n + 1]; // cost array, horizontally
int[] _d; // placeholder to assist in swapping p and d
// indexes into strings s and t
int i; // iterates through s
int j; // iterates through t
char t_j; // jth character of t
int cost; // cost
for (i = 0; i <= n; i++) {
p[i] = i;
}
for (j = 1; j <= m; j++) {
t_j = t.charAt(j - 1);
d[0] = j;
for (i = 1; i <= n; i++) {
cost = s.charAt(i - 1) == t_j ? 0 : 1;
// minimum of cell to the left+1, to the top+1, diagonally left
// and up +cost
d[i] = Math.min(Math.min(d[i - 1] + 1, p[i] + 1), p[i - 1] + cost);
}
// copy current distance counts to 'previous row' distance counts
_d = p;
p = d;
d = _d;
}
// our last action in the above loop was to switch d and p, so p now
// actually has the most recent cost counts
return p[n];
}
}