-
-
Notifications
You must be signed in to change notification settings - Fork 449
Expand file tree
/
Copy pathClassInfo.java
More file actions
552 lines (477 loc) · 15 KB
/
Copy pathClassInfo.java
File metadata and controls
552 lines (477 loc) · 15 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
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
package ch.njol.skript.classes;
import ch.njol.skript.SkriptAPIException;
import ch.njol.skript.expressions.base.EventValueExpression;
import ch.njol.skript.lang.Debuggable;
import ch.njol.skript.lang.DefaultExpression;
import ch.njol.skript.lang.util.SimpleLiteral;
import ch.njol.skript.localization.Noun;
import ch.njol.skript.registrations.Classes;
import ch.njol.util.coll.iterator.ArrayIterator;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.bukkit.event.Event;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import org.skriptlang.skript.addon.SkriptAddon;
import org.skriptlang.skript.lang.properties.Property;
import org.skriptlang.skript.lang.properties.Property.PropertyInfo;
import org.skriptlang.skript.lang.properties.handlers.base.PropertyHandler;
import java.util.*;
import java.util.function.Supplier;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
/**
* @author Peter Güttinger
* @param <T> The class this info is for
*/
@SuppressFBWarnings("DM_STRING_VOID_CTOR")
public class ClassInfo<T> implements Debuggable {
private final Class<T> c;
private final String codeName;
private final Noun name;
@Nullable
private DefaultExpression<T> defaultExpression = null;
@Nullable
private Parser<? extends T> parser = null;
@Nullable
private Cloner<T> cloner = null;
Pattern @Nullable [] userInputPatterns = null;
@Nullable
private Changer<? super T> changer = null;
@Nullable
private Supplier<Iterator<T>> supplier = null;
@Nullable
private Serializer<? super T> serializer = null;
@Nullable
private Class<?> serializeAs = null;
@Nullable
private Class<?> mathRelativeType = null;
@Nullable
private String docName = null;
private String @Nullable [] description = null;
private String @Nullable [] usage = null;
private String @Nullable [] examples = null;
@Nullable
private String since = null;
private String @Nullable [] requiredPlugins = null;
/**
* Overrides documentation id assigned from class name.
*/
@Nullable
private String documentationId = null;
/**
* @param c The class
* @param codeName The name used in patterns
*/
public ClassInfo(final Class<T> c, final String codeName) {
this.c = c;
if (!isValidCodeName(codeName))
throw new IllegalArgumentException("Code names for classes must be lowercase and only consist of latin letters and arabic numbers");
this.codeName = codeName;
name = new Noun("types." + codeName);
}
public static boolean isValidCodeName(final String name) {
return name.matches("(?:any-)?[a-z0-9]+");
}
// === FACTORY METHODS ===
/**
* @param parser A parser to parse values of this class or null if not applicable
*/
public ClassInfo<T> parser(final Parser<? extends T> parser) {
assert this.parser == null;
this.parser = parser;
return this;
}
/**
* @param cloner A {@link Cloner} to clone values when setting variables
* or passing function arguments.
*/
public ClassInfo<T> cloner(Cloner<T> cloner) {
assert this.cloner == null;
this.cloner = cloner;
return this;
}
/**
* @param userInputPatterns <u>Regex</u> patterns to match this class, e.g. in the expressions loop-[type], random [type] out of ..., or as command arguments. These patterns
* must be english and match singular and plural.
* @throws PatternSyntaxException If any of the patterns' syntaxes is invalid
*/
public ClassInfo<T> user(final String... userInputPatterns) throws PatternSyntaxException {
assert this.userInputPatterns == null;
this.userInputPatterns = new Pattern[userInputPatterns.length];
for (int i = 0; i < userInputPatterns.length; i++) {
assert this.userInputPatterns != null;
this.userInputPatterns[i] = Pattern.compile(userInputPatterns[i]);
}
return this;
}
/**
* @param defaultExpression The default (event) value of this class or null if not applicable
* @see EventValueExpression
* @see SimpleLiteral
*/
public ClassInfo<T> defaultExpression(final DefaultExpression<T> defaultExpression) {
if (!defaultExpression.isDefault())
throw new IllegalArgumentException("defaultExpression.isDefault() must return true for the default expression of a class");
this.defaultExpression = defaultExpression;
return this;
}
/**
* Used for dynamically getting all the possible values of a class
*
* @param supplier The supplier of the values
* @return This ClassInfo object
* @see ClassInfo#supplier(Object[])
*/
public ClassInfo<T> supplier(Supplier<Iterator<T>> supplier) {
if (this.supplier != null)
throw new SkriptAPIException("supplier of this class is already set");
this.supplier = supplier;
return this;
}
/**
* Used for getting all the possible constants of a class
*
* @param values The array of the values
* @return This ClassInfo object
* @see ClassInfo#supplier(Supplier)
*/
public ClassInfo<T> supplier(T[] values) {
return supplier(() -> new ArrayIterator<>(values));
}
public ClassInfo<T> serializer(final Serializer<? super T> serializer) {
assert this.serializer == null;
if (serializeAs != null)
throw new IllegalStateException("Can't set a serializer if this class is set to be serialized as another one");
this.serializer = serializer;
serializer.register(this);
return this;
}
public ClassInfo<T> serializeAs(final Class<?> serializeAs) {
assert this.serializeAs == null;
if (serializer != null)
throw new IllegalStateException("Can't set this class to be serialized as another one if a serializer is already set");
this.serializeAs = serializeAs;
return this;
}
public ClassInfo<T> changer(final Changer<? super T> changer) {
assert this.changer == null;
this.changer = changer;
return this;
}
/**
* Use this as {@link #name(String)} to suppress warnings about missing documentation.
*/
public final static String NO_DOC = new String();
/**
* Only used for Skript's documentation.
*
* @param name
* @return This ClassInfo object
*/
public ClassInfo<T> name(final String name) {
assert this.docName == null;
this.docName = name;
return this;
}
/**
* Only used for Skript's documentation.
*
* @param description
* @return This ClassInfo object
*/
public ClassInfo<T> description(final String... description) {
assert this.description == null;
this.description = description;
return this;
}
/**
* Only used for Skript's documentation.
*
* @param usage
* @return This ClassInfo object
*/
public ClassInfo<T> usage(final String... usage) {
assert this.usage == null;
this.usage = usage;
return this;
}
/**
* Only used for Skript's documentation.
*
* @param examples
* @return This ClassInfo object
*/
public ClassInfo<T> examples(final String... examples) {
assert this.examples == null;
this.examples = examples;
return this;
}
/**
* Only used for Skript's documentation.
*
* @param since
* @return This ClassInfo object
*/
public ClassInfo<T> since(final String since) {
assert this.since == null;
this.since = since;
return this;
}
/**
* Other plugin dependencies for this ClassInfo.
*
* Only used for Skript's documentation.
*
* @param pluginNames
* @return This ClassInfo object
*/
public ClassInfo<T> requiredPlugins(final String... pluginNames) {
assert this.requiredPlugins == null;
this.requiredPlugins = pluginNames;
return this;
}
/**
* Overrides default documentation id, which is assigned from class name.
* This is especially useful for inner classes whose names are useless without
* parent class name as a context.
* @param id Documentation id override.
* @return This ClassInfo object.
*/
public ClassInfo<T> documentationId(String id) {
assert this.documentationId == null;
this.documentationId = id;
return this;
}
// === GETTERS ===
public Class<T> getC() {
return c;
}
public Noun getName() {
return name;
}
public String getCodeName() {
return codeName;
}
@Nullable
public DefaultExpression<T> getDefaultExpression() {
return defaultExpression;
}
@Nullable
public Parser<? extends T> getParser() {
return parser;
}
@Nullable
public Cloner<? extends T> getCloner() {
return cloner;
}
/**
* Clones the given object using {@link ClassInfo#cloner},
* returning the given object if no {@link Cloner} is registered.
*/
public T clone(T t) {
return cloner == null ? t : cloner.clone(t);
}
public Pattern @Nullable [] getUserInputPatterns() {
return userInputPatterns;
}
/**
* Checks whether the given input matches any of the user input patterns.
*
* @param input The user input string to be checked against the patterns.
* @return true if the input matches any of the patterns, false otherwise.
*/
public boolean matchesUserInput(String input) {
if (userInputPatterns == null)
return false;
for (Pattern typePattern : userInputPatterns) {
if (typePattern.matcher(input).matches()) {
return true;
}
}
return false;
}
@Nullable
public Changer<? super T> getChanger() {
return changer;
}
@Nullable
public Supplier<Iterator<T>> getSupplier() {
if (supplier == null && c.isEnum())
supplier = () -> new ArrayIterator<>(c.getEnumConstants());
return supplier;
}
@Nullable
public Serializer<? super T> getSerializer() {
return serializer;
}
@Nullable
public Class<?> getSerializeAs() {
return serializeAs;
}
@Nullable
public String[] getDescription() {
return description;
}
@Nullable
public String[] getUsage() {
return usage;
}
@Nullable
public String[] getExamples() {
return examples;
}
@Nullable
public String getSince() {
return since;
}
@Nullable
public String getDocName() {
return docName;
}
@Nullable
public String[] getRequiredPlugins() {
return requiredPlugins;
}
/**
* Gets overridden documentation id of this this type. If no override has
* been set, null is returned and the caller may try to derive this from
* name of {@code #getC()}.
* @return Documentation id override, or null.
*/
@Nullable
public String getDocumentationID() {
return documentationId;
}
public boolean hasDocs() {
return getDocName() != null && !ClassInfo.NO_DOC.equals(getDocName());
}
// === ORDERING ===
@Nullable
private Set<String> before;
private final Set<String> after = new HashSet<>();
/**
* Sets one or more classes that this class should occur before in the class info list. This only affects the order in which classes are parsed if it's unknown of which type
* the parsed string is.
* <p>
* Please note that subclasses will always be registered before superclasses, no matter what is defined here or in {@link #after(String...)}.
* <p>
* This list can safely contain classes that may not exist.
*
* @param before
* @return this ClassInfo
*/
public ClassInfo<T> before(final String... before) {
assert this.before == null;
this.before = new HashSet<>(Arrays.asList(before));
return this;
}
/**
* Sets one or more classes that this class should occur after in the class info list. This only affects the order in which classes are parsed if it's unknown of which type
* the parsed string is.
* <p>
* Please note that subclasses will always be registered before superclasses, no matter what is defined here or in {@link #before(String...)}.
* <p>
* This list can safely contain classes that may not exist.
*
* @param after
* @return this ClassInfo
*/
public ClassInfo<T> after(final String... after) {
this.after.addAll(Arrays.asList(after));
return this;
}
/**
* @return Set of classes that should be after this one. May return null.
*/
@Nullable
public Set<String> before() {
return before;
}
/**
* @return Set of classes that should be before this one. Never returns null.
*/
public Set<String> after() {
return after;
}
// === GENERAL ===
@Override
@NotNull
public String toString() {
return getName().getSingular();
}
public String toString(final int flags) {
return getName().toString(flags);
}
@Override
@NotNull
public String toString(final @Nullable Event event, final boolean debug) {
if (debug)
return codeName + " (" + c.getCanonicalName() + ")";
return getName().getSingular();
}
private final Map<Property<?>, PropertyInfo<?>> propertyInfos = new HashMap<>();
private final Map<Property<?>, PropertyDocs> propertyDocumentation = new HashMap<>();
@ApiStatus.Experimental
public record PropertyDocs(Property<?> property, String description, SkriptAddon provider) {}
/**
* Registers this class as having the given property, using the given property handler.
* @param property The property this class should have
* @param description A short description of the property for documentation
* @param handler The handler for this property
* @return This ClassInfo object
* @param <Handler> The type of the property handler
* @throws IllegalStateException If this property is already registered for this class
*/
@ApiStatus.Experimental
public <Handler extends PropertyHandler<T>> ClassInfo<T> property(Property<? super Handler> property, String description, SkriptAddon addon, @NotNull Handler handler) {
if (propertyInfos.containsKey(property)) {
throw new IllegalStateException("Property " + property.name() + " is already registered for the " + c.getName() + " type.");
}
propertyInfos.put(property, new PropertyInfo<>(property, handler));
Classes.hasProperty(property, this);
propertyDocumentation.put(property, new PropertyDocs(property, description, addon));
return this;
}
/**
* Checks whether this class already has the given property registered.
* @param property The property to check
* @return True if this class has the property, false otherwise
*/
@ApiStatus.Experimental
public boolean hasProperty(Property<?> property) {
return propertyInfos.containsKey(property);
}
/**
* @return An unmodifiable collection of all the properties this class has.
*/
@ApiStatus.Experimental
public @Unmodifiable Collection<Property<?>> getAllProperties() {
return Collections.unmodifiableCollection(propertyInfos.keySet());
}
/**
* Gets the property info for the given property, or null if this class does not have the property.
* @param property The property to get the info for
* @return The property info, or null if this class does not have the property
* @param <Handler> The type of the property handler
*/
@ApiStatus.Experimental
public <Handler extends PropertyHandler<?>> @Nullable PropertyInfo<Handler> getPropertyInfo(Property<Handler> property) {
if (!propertyInfos.containsKey(property)) {
return null;
}
//noinspection unchecked
return (PropertyInfo<Handler>) propertyInfos.get(property);
}
/**
* Gets the type-specific documentation for the given property, or null if this type does not have the property.
* Meant to be used for documentation.
* @param property The property to get the documentation for
* @return The documentation, or null if this type does not have the property
*/
@ApiStatus.Experimental
public PropertyDocs getPropertyDocumentation(Property<?> property) {
return propertyDocumentation.get(property);
}
}