|
9 | 9 |
|
10 | 10 | import com.rezzedup.util.constants.types.Primitives; |
11 | 11 | import com.rezzedup.util.valuables.Adapter; |
12 | | -import com.rezzedup.util.valuables.Deserializer; |
13 | | -import com.rezzedup.util.valuables.Serializer; |
| 12 | +import com.rezzedup.util.valuables.Adapts; |
| 13 | +import org.bukkit.Material; |
| 14 | +import org.bukkit.Sound; |
14 | 15 | import pl.tlinkowski.annotation.basic.NullOr; |
15 | 16 |
|
| 17 | +import java.time.Instant; |
16 | 18 | import java.util.ArrayList; |
17 | 19 | import java.util.List; |
18 | 20 | import java.util.Map; |
19 | 21 | import java.util.Optional; |
20 | | -import java.util.UUID; |
21 | 22 | import java.util.function.Function; |
22 | 23 |
|
23 | | -final class YamlAdapters |
| 24 | +public class YamlAdapters |
24 | 25 | { |
25 | 26 | private YamlAdapters() { throw new UnsupportedOperationException(); } |
26 | 27 |
|
27 | | - static final Adapter<Object, String> STRING = |
28 | | - simple(serialized -> Optional.of(String.valueOf(serialized))); |
29 | | - |
30 | | - static final Adapter<Object, Boolean> BOOLEAN = |
31 | | - simple(serialized -> |
32 | | - (serialized instanceof Boolean) |
33 | | - ? Optional.of((Boolean) serialized) |
34 | | - : Optional.empty() |
| 28 | + private static <V> Adapter<Object, List<V>> list(Function<Object, @NullOr V> conversion) |
| 29 | + { |
| 30 | + return Adapter.of( |
| 31 | + serialized -> |
| 32 | + { |
| 33 | + if (!(serialized instanceof List)) { return Optional.empty(); } |
| 34 | + |
| 35 | + List<?> existing = (List<?>) serialized; |
| 36 | + List<V> values = new ArrayList<>(); |
| 37 | + |
| 38 | + for (Object obj : existing) |
| 39 | + { |
| 40 | + @NullOr V converted = conversion.apply(obj); |
| 41 | + if (converted != null) { values.add(converted); } |
| 42 | + } |
| 43 | + |
| 44 | + return Optional.of(values); |
| 45 | + }, |
| 46 | + Optional::of |
35 | 47 | ); |
| 48 | + } |
36 | 49 |
|
37 | | - static final Adapter<Object, Integer> INTEGER = number(Number::intValue); |
38 | | - |
39 | | - static final Adapter<Object, Long> LONG = number(Number::longValue); |
40 | | - |
41 | | - static final Adapter<Object, Float> FLOAT = number(Number::floatValue); |
| 50 | + public static <E extends Enum<E>> Adapter<Object, E> ofEnum(Class<E> type) |
| 51 | + { |
| 52 | + return Adapter.of( |
| 53 | + serialized -> Adapts.string().intoEnum(type).deserialize(String.valueOf(serialized)), |
| 54 | + deserialized -> Optional.of(String.valueOf(deserialized)) |
| 55 | + ); |
| 56 | + } |
42 | 57 |
|
43 | | - static final Adapter<Object, Double> DOUBLE = number(Number::doubleValue); |
| 58 | + public static <V> Adapter<Object, V> ofParsed(Function<String, V> parser) |
| 59 | + { |
| 60 | + return Adapter.of( |
| 61 | + serialized -> { |
| 62 | + try { return Optional.of(parser.apply(String.valueOf(serialized))); } |
| 63 | + catch (RuntimeException ignored) { return Optional.empty(); } |
| 64 | + }, |
| 65 | + deserialized -> Optional.of(String.valueOf(deserialized)) |
| 66 | + ); |
| 67 | + } |
44 | 68 |
|
45 | | - static final Adapter<Object, List<String>> STRING_LIST = |
| 69 | + public static final Adapter<Object, List<String>> STRING_LIST = |
46 | 70 | list(serialized -> |
47 | 71 | (serialized instanceof String || Primitives.isBoxed(serialized)) |
48 | 72 | ? String.valueOf(serialized) |
49 | 73 | : null |
50 | 74 | ); |
51 | 75 |
|
52 | | - static final Adapter<Object, List<Map<?, ?>>> MAP_LIST = |
| 76 | + public static final Adapter<Object, List<Map<?, ?>>> MAP_LIST = |
53 | 77 | list(serialized -> (serialized instanceof Map<?, ?>) ? (Map<?, ?>) serialized : null); |
54 | 78 |
|
55 | | - static final Adapter<Object, UUID> U_UID = |
56 | | - convert( |
57 | | - serialized -> { |
58 | | - try { return Optional.of(UUID.fromString(String.valueOf(serialized))); } |
59 | | - catch (IllegalArgumentException ignored) { return Optional.empty(); } |
60 | | - }, |
61 | | - deserialized -> Optional.of(deserialized.toString()) |
62 | | - ); |
63 | | - |
64 | | - // |
65 | | - // Factories |
66 | | - // |
67 | | - |
68 | | - static <V> Adapter<Object, V> convert(Deserializer<Object, V> deserializer, Serializer<V, Object> serializer) |
69 | | - { |
70 | | - return Adapter.of(deserializer, serializer); |
71 | | - } |
| 79 | + public static final Adapter<Object, Sound> SOUND = ofEnum(Sound.class); |
72 | 80 |
|
73 | | - static <V> Adapter<Object, V> simple(Deserializer<Object, V> deserializer) |
74 | | - { |
75 | | - return convert(deserializer, Optional::of); |
76 | | - } |
| 81 | + public static final Adapter<Object, Material> MATERIAL = ofEnum(Material.class); |
77 | 82 |
|
78 | | - static <V extends Number> Adapter<Object, V> number(Function<Number, V> conversion) |
79 | | - { |
80 | | - return simple(val -> |
81 | | - (val instanceof Number) |
82 | | - ? Optional.of(conversion.apply((Number) val)) |
83 | | - : Optional.empty() |
84 | | - ); |
85 | | - } |
86 | | - |
87 | | - static <V> Adapter<Object, List<V>> list(Function<Object, @NullOr V> conversion) |
88 | | - { |
89 | | - return simple(serialized -> |
90 | | - { |
91 | | - if (!(serialized instanceof List)) { return Optional.empty(); } |
92 | | - |
93 | | - List<?> existing = (List<?>) serialized; |
94 | | - List<V> values = new ArrayList<>(); |
95 | | - |
96 | | - for (Object obj : existing) |
97 | | - { |
98 | | - @NullOr V converted = conversion.apply(obj); |
99 | | - if (converted != null) { values.add(converted); } |
100 | | - } |
101 | | - |
102 | | - return Optional.of(values); |
103 | | - }); |
104 | | - } |
| 83 | + public static final Adapter<Object, Instant> INSTANT = ofParsed(Instant::parse); |
105 | 84 | } |
0 commit comments