-
-
Notifications
You must be signed in to change notification settings - Fork 390
/
Copy pathArithmetics.java
286 lines (245 loc) · 12.2 KB
/
Arithmetics.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
package org.skriptlang.skript.lang.arithmetic;
import ch.njol.skript.Skript;
import ch.njol.skript.SkriptAPIException;
import ch.njol.util.Pair;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnmodifiableView;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.Set;
import java.util.HashSet;
import java.util.function.Supplier;
import java.util.stream.Collectors;
public final class Arithmetics {
private static final Map<Operator, List<OperationInfo<?, ?, ?>>> operations = Collections.synchronizedMap(new HashMap<>());
private static final Map<Operator, Map<Pair<Class<?>, Class<?>>, OperationInfo<?, ?, ?>>> cachedOperations = Collections.synchronizedMap(new HashMap<>());
private static final Map<Class<?>, DifferenceInfo<?, ?>> differences = Collections.synchronizedMap(new HashMap<>());
private static final Map<Class<?>, DifferenceInfo<?, ?>> cachedDifferences = Collections.synchronizedMap(new HashMap<>());
private static final Map<Class<?>, Supplier<?>> defaultValues = Collections.synchronizedMap(new HashMap<>());
private static final Map<Class<?>, Supplier<?>> cachedDefaultValues = Collections.synchronizedMap(new HashMap<>());
public static <T> void registerOperation(Operator operator, Class<T> type, Operation<T, T, T> operation) {
registerOperation(operator, type, type, type, operation);
}
public static <L, R> void registerOperation(Operator operator, Class<L> leftClass, Class<R> rightClass, Operation<L, R, L> operation) {
registerOperation(operator, leftClass, rightClass, leftClass, operation);
}
public static <L, R> void registerOperation(Operator operator, Class<L> leftClass, Class<R> rightClass, Operation<L, R, L> operation, Operation<R, L, L> commutativeOperation) {
registerOperation(operator, leftClass, rightClass, leftClass, operation);
registerOperation(operator, rightClass, leftClass, leftClass, commutativeOperation);
}
public static <L, R, T> void registerOperation(Operator operator, Class<L> leftClass, Class<R> rightClass, Class<T> returnType, Operation<L, R, T> operation, Operation<R, L, T> commutativeOperation) {
registerOperation(operator, leftClass, rightClass, returnType, operation);
registerOperation(operator, rightClass, leftClass, returnType, commutativeOperation);
}
public static <L, R, T> void registerOperation(Operator operator, Class<L> leftClass, Class<R> rightClass, Class<T> returnType, Operation<L, R, T> operation) {
Skript.checkAcceptRegistrations();
if (exactOperationExists(operator, leftClass, rightClass))
throw new SkriptAPIException("There's already a " + operator.getName() + " operation registered for types '"
+ leftClass + "' and '" + rightClass + "'");
getOperations_i(operator).add(new OperationInfo<>(leftClass, rightClass, returnType, operation));
}
public static boolean exactOperationExists(Operator operator, Class<?> leftClass, Class<?> rightClass) {
for (OperationInfo<?, ?, ?> info : getOperations_i(operator)) {
if (info.getLeft() == leftClass && info.getRight() == rightClass)
return true;
}
return false;
}
public static boolean operationExists(Operator operator, Class<?> leftClass, Class<?> rightClass) {
return getOperationInfo(operator, leftClass, rightClass) != null;
}
private static List<OperationInfo<?, ?, ?>> getOperations_i(Operator operator) {
return operations.computeIfAbsent(operator, o -> Collections.synchronizedList(new ArrayList<>()));
}
@UnmodifiableView
public static List<OperationInfo<?, ?, ?>> getOperations(Operator operator) {
assertIsOperationsDoneLoading();
return Collections.unmodifiableList(getOperations_i(operator));
}
@SuppressWarnings({"unchecked", "rawtypes"})
public static <T> List<OperationInfo<T, ?, ?>> getOperations(Operator operator, Class<T> type) {
return (List) getOperations(operator).stream()
.filter(info -> info.getLeft().isAssignableFrom(type))
.collect(Collectors.toList());
}
@Nullable
@SuppressWarnings("unchecked")
public static <L, R, T> OperationInfo<L, R, T> getOperationInfo(Operator operator, Class<L> leftClass, Class<R> rightClass, Class<T> returnType) {
OperationInfo<L, R, ?> info = getOperationInfo(operator, leftClass, rightClass);
if (info != null && returnType.isAssignableFrom(info.getReturnType()))
return (OperationInfo<L, R, T>) info;
return null;
}
private static Map<Pair<Class<?>, Class<?>>, OperationInfo<?, ?, ?>> getCachedOperations(Operator operator) {
return cachedOperations.computeIfAbsent(operator, o -> Collections.synchronizedMap(new HashMap<>()));
}
@Nullable
@SuppressWarnings("unchecked")
public static <L, R> OperationInfo<L, R, ?> getOperationInfo(Operator operator, Class<L> leftClass, Class<R> rightClass) {
assertIsOperationsDoneLoading();
return (OperationInfo<L, R, ?>) getCachedOperations(operator).computeIfAbsent(new Pair<>(leftClass, rightClass), pair ->
getOperations(operator).stream()
.filter(info -> info.getLeft().isAssignableFrom(leftClass) && info.getRight().isAssignableFrom(rightClass))
.reduce((info, info2) -> {
if (info2.getLeft() == leftClass && info2.getRight() == rightClass)
return info2;
return info;
})
.orElse(null));
}
@Nullable
public static <L, R, T> Operation<L, R, T> getOperation(Operator operator, Class<L> leftClass, Class<R> rightClass, Class<T> returnType) {
OperationInfo<L, R, T> info = getOperationInfo(operator, leftClass, rightClass, returnType);
return info == null ? null : info.getOperation();
}
@Nullable
public static <L, R> Operation<L, R, ?> getOperation(Operator operator, Class<L> leftClass, Class<R> rightClass) {
OperationInfo<L, R, ?> info = getOperationInfo(operator, leftClass, rightClass);
return info == null ? null : info.getOperation();
}
@Nullable
public static <L, R, T> OperationInfo<L, R, T> lookupOperationInfo(Operator operator, Class<L> leftClass, Class<R> rightClass, Class<T> returnType) {
OperationInfo<L, R, ?> info = lookupOperationInfo(operator, leftClass, rightClass);
return info != null ? info.getConverted(leftClass, rightClass, returnType) : null;
}
public static <L, R> @Nullable OperationInfo<L, R, ?> lookupOperationInfo(
Operator operator,
Class<L> leftClass,
Class<R> rightClass,
Class<?> ... possibleReturnTypes
) {
OperationInfo<L, R, ?> info = lookupOperationInfo(operator, leftClass, rightClass);
if (info == null)
return null;
for (Class<?> returnType : possibleReturnTypes) {
OperationInfo<L, R, ?> convertedInfo = info.getConverted(leftClass, rightClass, returnType);
if (convertedInfo != null)
return convertedInfo;
}
return null;
}
@Nullable
@SuppressWarnings("unchecked")
public static <L, R> OperationInfo<L, R, ?> lookupOperationInfo(Operator operator, Class<L> leftClass, Class<R> rightClass) {
OperationInfo<L, R, ?> operationInfo = getOperationInfo(operator, leftClass, rightClass);
if (operationInfo != null)
return operationInfo;
return (OperationInfo<L, R, ?>) getCachedOperations(operator).computeIfAbsent(new Pair<>(leftClass, rightClass), pair -> {
for (OperationInfo<?, ?, ?> info : getOperations(operator)) {
if (!info.getLeft().isAssignableFrom(leftClass) && !info.getRight().isAssignableFrom(rightClass))
continue;
OperationInfo<L, R, ?> convertedInfo = info.getConverted(leftClass, rightClass, info.getReturnType());
if (convertedInfo != null)
return convertedInfo;
}
return null;
});
}
@SuppressWarnings("unchecked")
public static <L, R, T> T calculate(Operator operator, L left, R right, Class<T> returnType) {
Operation<L, R, T> operation = (Operation<L, R, T>) getOperation(operator, left.getClass(), right.getClass(), returnType);
return operation == null ? null : operation.calculate(left, right);
}
@SuppressWarnings("unchecked")
public static <L, R, T> T calculateUnsafe(Operator operator, L left, R right) {
Operation<L, R, T> operation = (Operation<L, R, T>) getOperation(operator, left.getClass(), right.getClass());
return operation == null ? null : operation.calculate(left, right);
}
public static <T> void registerDifference(Class<T> type, Operation<T, T, T> operation) {
registerDifference(type, type, operation);
}
public static <T, R> void registerDifference(Class<T> type, Class<R> returnType, Operation<T, T, R> operation) {
Skript.checkAcceptRegistrations();
if (exactDifferenceExists(type))
throw new IllegalArgumentException("There's already a difference registered for type '" + type + "'");
differences.put(type, new DifferenceInfo<>(type, returnType, operation));
}
public static boolean exactDifferenceExists(Class<?> type) {
return differences.containsKey(type);
}
public static boolean differenceExists(Class<?> type) {
return getDifferenceInfo(type) != null;
}
@SuppressWarnings("unchecked")
public static <T, R> DifferenceInfo<T, R> getDifferenceInfo(Class<T> type, Class<R> returnType) {
DifferenceInfo<T, ?> info = getDifferenceInfo(type);
if (info != null && returnType.isAssignableFrom(info.getReturnType()))
return (DifferenceInfo<T, R>) info;
return null;
}
@SuppressWarnings("unchecked")
public static <T> DifferenceInfo<T, ?> getDifferenceInfo(Class<T> type) {
if (Skript.isAcceptRegistrations())
throw new SkriptAPIException("Differences cannot be retrieved until Skript has finished registrations.");
return (DifferenceInfo<T, ?>) cachedDifferences.computeIfAbsent(type, c -> {
if (differences.containsKey(type))
return differences.get(type);
for (Map.Entry<Class<?>, DifferenceInfo<?, ?>> entry : differences.entrySet()) {
if (entry.getKey().isAssignableFrom(type))
return entry.getValue();
}
return null;
});
}
public static <T, R> Operation<T, T, R> getDifference(Class<T> type, Class<R> returnType) {
DifferenceInfo<T, R> info = getDifferenceInfo(type, returnType);
return info == null ? null : info.getOperation();
}
public static <T> Operation<T, T, ?> getDifference(Class<T> type) {
DifferenceInfo<T, ?> info = getDifferenceInfo(type);
return info == null ? null : info.getOperation();
}
@SuppressWarnings("unchecked")
public static <T, R> R difference(T left, T right, Class<R> returnType) {
Operation<T, T, R> operation = (Operation<T, T, R>) getDifference(left.getClass(), returnType);
return operation == null ? null : operation.calculate(left, right);
}
@SuppressWarnings("unchecked")
public static <T, R> R differenceUnsafe(T left, T right) {
Operation<T, T, R> operation = (Operation<T, T, R>) getDifference(left.getClass());
return operation == null ? null : operation.calculate(left, right);
}
public static <T> void registerDefaultValue(Class<T> type, Supplier<T> supplier) {
Skript.checkAcceptRegistrations();
if (defaultValues.containsKey(type))
throw new IllegalArgumentException("There's already a default value registered for type '" + type + "'");
defaultValues.put(type, supplier);
}
@SuppressWarnings("unchecked")
public static <R, T extends R> R getDefaultValue(Class<T> type) {
if (Skript.isAcceptRegistrations())
throw new SkriptAPIException("Default values cannot be retrieved until Skript has finished registrations.");
Supplier<R> supplier = (Supplier<R>) cachedDefaultValues.computeIfAbsent(type, c -> {
if (defaultValues.containsKey(type))
return defaultValues.get(type);
for (Map.Entry<Class<?>, Supplier<?>> entry : defaultValues.entrySet()) {
if (entry.getKey().isAssignableFrom(type))
return entry.getValue();
}
return null;
});
return supplier == null ? null : supplier.get();
}
private static void assertIsOperationsDoneLoading() {
if (Skript.isAcceptRegistrations())
throw new SkriptAPIException("Operations cannot be retrieved until Skript has finished registrations.");
}
/**
* All registered types that could be returned from a calculation using this operator.
* This is used to fetch potential return types when unknown (variable) arguments are used in a sum.
*
* @param operator The operator to test
* @return Every type this could return
*/
public static Collection<Class<?>> getAllReturnTypes(Operator operator) {
Set<Class<?>> types = new HashSet<>();
for (OperationInfo<?, ?, ?> info : getOperations_i(operator)) {
types.add(info.getReturnType());
}
return types;
}
}