-
-
Notifications
You must be signed in to change notification settings - Fork 346
/
KeyFactory.java
389 lines (328 loc) · 12.5 KB
/
KeyFactory.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
/*
* This file is part of SpongeAPI, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.api.data.key;
import com.google.common.base.Objects;
import com.google.common.reflect.TypeToken;
import org.spongepowered.api.data.DataQuery;
import org.spongepowered.api.data.DataView;
import org.spongepowered.api.data.value.BaseValue;
import org.spongepowered.api.data.value.mutable.ListValue;
import org.spongepowered.api.data.value.mutable.MapValue;
import org.spongepowered.api.data.value.mutable.OptionalValue;
import org.spongepowered.api.data.value.mutable.SetValue;
import org.spongepowered.api.data.value.mutable.Value;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import javax.annotation.Nullable;
/**
* A factory of {@link Key}s, useful for both the implementation of SpongeAPI,
* and for plugins wishing to provide their own {@link Key}s without having
* to remain afraid of having to cast back and forth.
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public final class KeyFactory {
private KeyFactory() {}
/**
* Creates a new {@link Key} with the provided <code>E</code> element
* class and <code>V</code> {@link Value} class along with the provided
* default {@link DataQuery} to be used with the generated {@link Key}.
*
* <p>Note that {@link Key}s are not registered, but it is recommended
* to avoid generating {@link Key}s of potentially conflicting
* {@link DataQuery}(s).</p>
*
* @param <E> The type of element
* @param <V> The inferred return type
* @param elementToken The element class
* @param valueToken The value class
* @param query The query
* @return The generated key
*/
public static <E, V extends BaseValue<E>> Key<V> makeSingleKey(final TypeToken<E> elementToken, final TypeToken<V> valueToken,
final DataQuery query, final String id, final String name) {
return new Key<V>() {
@Nullable private String string;
@Override
public String getId() {
return id;
}
@Override
public String getName() {
return name;
}
private final int hash = Objects.hashCode(elementToken, valueToken, query);
@SuppressWarnings("rawtypes")
@Override
public TypeToken<V> getValueToken() {
return valueToken;
}
@Override
public TypeToken<?> getElementToken() {
return elementToken;
}
@Override
public DataQuery getQuery() {
return query;
}
@Override
public int hashCode() {
return this.hash;
}
@Override
public String toString() {
if (this.string == null) {
this.string = "Key{Value:" + valueToken.getRawType().getSimpleName() + "<"
+ elementToken.getRawType().getSimpleName() + ">, Query: "
+ query.toString() + "}";
}
return this.string;
}
};
}
/**
* Creates a new {@link Key} based on a {@link ListValue} of a type
* <code>E</code> element along with the provided {@link DataQuery}.
*
* @param <E> The type of element
* @param elementToken The element class
* @param query The query to access the data
* @return The generated key
*/
public static <E> Key<ListValue<E>> makeListKey(final TypeToken<? extends List<E>> elementToken, final TypeToken<ListValue<E>> valueToken,
final DataQuery query, final String id, final String name) {
return new Key<ListValue<E>>() {
@Nullable private String string;
@Override
public String getId() {
return id;
}
@Override
public String getName() {
return name;
}
private final int hash = Objects.hashCode(ListValue.class, elementToken, query);
@SuppressWarnings("rawtypes")
@Override
public TypeToken<ListValue<E>> getValueToken() {
return valueToken;
}
@Override
public TypeToken<?> getElementToken() {
return elementToken;
}
@Override
public DataQuery getQuery() {
return query;
}
@Override
public int hashCode() {
return this.hash;
}
@Override
public String toString() {
if (this.string == null) {
this.string = "Key{Value:ListValue<" + elementToken.toString() + ">, Query: " + query.toString() + "}";
}
return this.string;
}
};
}
/**
* Creates a new {@link Key} based on a {@link SetValue} of a type
* <code>E</code> element along with the provided {@link DataQuery}.
*
* @param <E> The type of element
* @param elementToken The element class
* @param valueToken
*@param query The query to access the data @return The generated key
*/
public static <E> Key<SetValue<E>> makeSetKey(final TypeToken<? extends Set<E>> elementToken, TypeToken<SetValue<E>> valueToken,
final DataQuery query, final String id, final String name) {
return new Key<SetValue<E>>() {
@Nullable private String string;
@Override
public String getId() {
return id;
}
@Override
public String getName() {
return name;
}
private final int hash = Objects.hashCode(ListValue.class, elementToken, query);
@SuppressWarnings("rawtypes")
@Override
public TypeToken<SetValue<E>> getValueToken() {
return valueToken;
}
@Override
public TypeToken<?> getElementToken() {
return elementToken;
}
@Override
public DataQuery getQuery() {
return query;
}
@Override
public int hashCode() {
return this.hash;
}
@Override
public String toString() {
if (this.string == null) {
this.string = "Key{Value:" + "SetValue<" + elementToken.toString() + ">, Query: " + query.toString() + "}";
}
return this.string;
}
};
}
/**
* Creates a new {@link Key} based on a {@link MapValue} of the types
* <code>K</code> keys and <code>V</code> values with the provided
* {@link DataQuery} for accessing the {@link Map} in {@link DataView}s.
*
* @param <K> The type of keys
* @param <V> The type of values
* @param elementToken The element token
* @param valueToken The value class of the map
* @param query The query
* @return The generated key
*/
public static <K, V> Key<MapValue<K, V>> makeMapKey(final TypeToken<Map<K, V>> elementToken, final TypeToken<MapValue<K, V>> valueToken, final DataQuery query, final String id, final String name) {
return new Key<MapValue<K, V>>() {
@Nullable private String string;
@Override
public String getId() {
return id;
}
@Override
public String getName() {
return name;
}
private final int hash = Objects.hashCode(elementToken, valueToken, query);
@SuppressWarnings("rawtypes")
@Override
public TypeToken<MapValue<K, V>> getValueToken() {
return valueToken;
}
@SuppressWarnings("rawtypes")
@Override
public TypeToken<?> getElementToken() {
return elementToken;
}
@Override
public DataQuery getQuery() {
return query;
}
@Override
public int hashCode() {
return this.hash;
}
@Override
public String toString() {
if (this.string == null) {
this.string = "Key{Value:" + "MapValue<" + elementToken.toString() + ","
+ valueToken.toString() + ">, Query: " + query.toString() + "}";
}
return this.string;
}
};
}
/**
* Creates a new {@link Key} based on an {@link OptionalValue} of the type
* <code>E</code> element type with the provided {@link DataQuery} for
* accessing the optionally null value in {@link DataView}s.
*
* @param <E> The element type
* @param elementToken The element class
* @param valueToken
* @param query The query
* @return The generated key
*/
public static <E> Key<OptionalValue<E>> makeOptionalKey(final TypeToken<Optional<E>> elementToken, TypeToken<OptionalValue<E>> valueToken,
final DataQuery query, final String id,
final String name) {
return new Key<OptionalValue<E>>() {
@Nullable private String string;
@Override
public String getId() {
return id;
}
@Override
public String getName() {
return name;
}
private final int hash = Objects.hashCode(Optional.class, elementToken, query);
@Override
public TypeToken<OptionalValue<E>> getValueToken() {
return valueToken;
}
@Override
public TypeToken<?> getElementToken() {
return elementToken;
}
@Override
public DataQuery getQuery() {
return query;
}
@Override
public int hashCode() {
return this.hash;
}
@Override
public String toString() {
if (this.string == null) {
this.string = "Key{Value:" + "OptionalValue<" + elementToken.toString() + ">, Query: " + query.toString() + "}";
}
return this.string;
}
};
}
static <E, V extends BaseValue<E>> Key<V> fake(final String keyName) {
return new Key<V>() {
@Override
public String getId() {
throw new UnsupportedOperationException("Key " + keyName + " is not implemented");
}
@Override
public String getName() {
throw new UnsupportedOperationException("Key " + keyName + " is not implemented");
}
@Override
public TypeToken<V> getValueToken() {
throw new UnsupportedOperationException("Key " + keyName + " is not implemented");
}
@Override
public TypeToken<?> getElementToken() {
throw new UnsupportedOperationException("Key " + keyName + " is not implemented");
}
@Override
public DataQuery getQuery() {
throw new UnsupportedOperationException("Key " + keyName + " is not implemented");
}
};
}
}