|
13 | 13 | import core.nbt.serialization.adapter.StringAdapter; |
14 | 14 | import core.nbt.serialization.adapter.UUIDAdapter; |
15 | 15 | import core.nbt.tag.Tag; |
| 16 | +import org.jspecify.annotations.NonNull; |
16 | 17 | import org.jspecify.annotations.NullMarked; |
17 | 18 |
|
18 | 19 | import java.io.File; |
|
24 | 25 | import java.util.UUID; |
25 | 26 |
|
26 | 27 | @NullMarked |
27 | | -class Serializer implements TagDeserializationContext, TagSerializationContext { |
| 28 | +sealed class Serializer implements TagDeserializationContext, TagSerializationContext permits NBT { |
28 | 29 | private final Map<Class<?>, TagDeserializer<?>> hierarchyDeserializers = new HashMap<>(); |
29 | 30 | private final Map<Class<?>, TagSerializer<?>> hierarchySerializers = new HashMap<>(); |
30 | 31 |
|
31 | 32 | private final Map<Type, TagDeserializer<?>> deserializers = new HashMap<>(); |
32 | 33 | private final Map<Type, TagSerializer<?>> serializers = new HashMap<>(); |
33 | 34 |
|
34 | | - public Serializer() { |
| 35 | + protected Serializer() { |
35 | 36 | registerTypeAdapter(Boolean.class, BooleanAdapter.INSTANCE); |
36 | 37 | registerTypeAdapter(Byte.class, ByteAdapter.INSTANCE); |
37 | 38 | registerTypeAdapter(Double.class, DoubleAdapter.INSTANCE); |
@@ -81,20 +82,20 @@ public Tag serialize(Object object) throws ParserException { |
81 | 82 | @Override |
82 | 83 | @SuppressWarnings("unchecked") |
83 | 84 | public Tag serialize(Object object, Class<?> type) throws ParserException { |
84 | | - var serializer = (TagSerializer<Object>) hierarchySerializers.get(object.getClass()); |
| 85 | + var serializer = (TagSerializer<@NonNull Object>) hierarchySerializers.get(object.getClass()); |
85 | 86 | if (serializer != null) return serializer.serialize(object, this); |
86 | 87 | return hierarchySerializers.entrySet().stream() |
87 | 88 | .filter(entry -> entry.getKey().isInstance(object)) |
88 | 89 | .findAny() |
89 | | - .map(entry -> (TagSerializer<Object>) entry.getValue()) |
| 90 | + .map(entry -> (TagSerializer<@NonNull Object>) entry.getValue()) |
90 | 91 | .map(value -> value.serialize(object, this)) |
91 | 92 | .orElseGet(() -> serialize(object, (Type) object.getClass())); |
92 | 93 | } |
93 | 94 |
|
94 | 95 | @Override |
95 | 96 | @SuppressWarnings("unchecked") |
96 | 97 | public Tag serialize(Object object, Type type) throws ParserException { |
97 | | - var serializer = (TagSerializer<Object>) serializers.get(type); |
| 98 | + var serializer = (TagSerializer<@NonNull Object>) serializers.get(type); |
98 | 99 | if (serializer != null) return serializer.serialize(object, this); |
99 | 100 | throw new ParserException("No tag serializer registered for type: " + type); |
100 | 101 | } |
|
0 commit comments