Skip to content

Commit bbf0bcb

Browse files
committed
Refactored NBT to use SimpleNBT implementation
Replaced `Serializer` with a cleaner `SimpleNBT` structure for improved clarity, flexibility, and separation of concerns. Updated `NBT.Builder` to reflect the new implementation.
1 parent 3ce184d commit bbf0bcb

File tree

4 files changed

+211
-172
lines changed

4 files changed

+211
-172
lines changed

nbt/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ repositories {
2323

2424
dependencies {
2525
api(project(":files"))
26+
compileOnly("org.jetbrains:annotations:26.0.2-1")
2627

2728
testImplementation(project(":files"))
2829

nbt/src/main/java/core/nbt/serialization/NBT.java

Lines changed: 19 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package core.nbt.serialization;
22

33
import core.nbt.tag.Tag;
4+
import org.jetbrains.annotations.Contract;
45
import org.jspecify.annotations.NullMarked;
56

67
import java.lang.reflect.Type;
@@ -10,25 +11,14 @@
1011
* as well as to register custom serializers and deserializers for different types.
1112
*/
1213
@NullMarked
13-
public final class NBT extends Serializer {
14-
private final Serializer serializer;
15-
16-
/**
17-
* Constructs an instance of NBT using the provided Serializer.
18-
*
19-
* @param serializer the serializer to be used for serialization and deserialization operations
20-
*/
21-
private NBT(Serializer serializer) {
22-
this.serializer = serializer;
23-
}
24-
14+
public sealed interface NBT extends TagSerializationContext, TagDeserializationContext permits SimpleNBT {
2515
/**
2616
* Creates a new instance of the Builder class for constructing NBT objects.
2717
*
2818
* @return a new Builder instance for constructing NBT objects
2919
*/
30-
public static Builder builder() {
31-
return new Builder();
20+
static Builder builder() {
21+
return new SimpleNBT.Builder();
3222
}
3323

3424
/**
@@ -100,9 +90,7 @@ default Tag toTag(Object object, Type type) {
10090
/**
10191
* Builder class for constructing instances of NBT with custom serializers and deserializers.
10292
*/
103-
public static class Builder {
104-
private final Serializer serializer = new Serializer();
105-
93+
sealed interface Builder permits SimpleNBT.Builder {
10694
/**
10795
* Registers a custom adapter for both serialization and deserialization of the specified type
10896
* and its subtypes.
@@ -113,10 +101,8 @@ public static class Builder {
113101
* of the specified type and its subtypes
114102
* @return the current builder instance for chaining
115103
*/
116-
public <T> Builder registerTypeHierarchyAdapter(Class<?> type, TagAdapter<T> adapter) {
117-
this.serializer.registerTypeHierarchyAdapter(type, adapter);
118-
return this;
119-
}
104+
@Contract(value = "_, _ -> this", mutates = "this")
105+
<T> Builder registerTypeHierarchyAdapter(Class<?> type, TagAdapter<T> adapter);
120106

121107
/**
122108
* Registers a custom deserializer for the specified type or any of its subtypes.
@@ -126,10 +112,8 @@ public <T> Builder registerTypeHierarchyAdapter(Class<?> type, TagAdapter<T> ada
126112
* @param deserializer the instance of TagDeserializer to handle deserializing the specified type
127113
* @return the current builder instance for chaining
128114
*/
129-
public <T> Builder registerTypeHierarchyAdapter(Class<?> type, TagDeserializer<T> deserializer) {
130-
this.serializer.registerTypeHierarchyAdapter(type, deserializer);
131-
return this;
132-
}
115+
@Contract(value = "_, _ -> this", mutates = "this")
116+
<T> Builder registerTypeHierarchyAdapter(Class<?> type, TagDeserializer<T> deserializer);
133117

134118
/**
135119
* Registers a custom serializer for the specified type or any of its subtypes.
@@ -139,10 +123,8 @@ public <T> Builder registerTypeHierarchyAdapter(Class<?> type, TagDeserializer<T
139123
* @param serializer the instance of TagSerializer to handle serializing the specified type
140124
* @return the current builder instance for chaining
141125
*/
142-
public <T> Builder registerTypeHierarchyAdapter(Class<?> type, TagSerializer<T> serializer) {
143-
this.serializer.registerTypeHierarchyAdapter(type, serializer);
144-
return this;
145-
}
126+
@Contract(value = "_, _ -> this", mutates = "this")
127+
<T> Builder registerTypeHierarchyAdapter(Class<?> type, TagSerializer<T> serializer);
146128

147129
/**
148130
* Registers a custom adapter for both serialization and deserialization of the specified type.
@@ -152,10 +134,8 @@ public <T> Builder registerTypeHierarchyAdapter(Class<?> type, TagSerializer<T>
152134
* @param adapter the instance of TagAdapter to handle both serialization and deserialization of the specified type
153135
* @return the current builder instance for chaining
154136
*/
155-
public <T> Builder registerTypeAdapter(Type type, TagAdapter<T> adapter) {
156-
this.serializer.registerTypeAdapter(type, adapter);
157-
return this;
158-
}
137+
@Contract(value = "_, _ -> this", mutates = "this")
138+
<T> Builder registerTypeAdapter(Type type, TagAdapter<T> adapter);
159139

160140
/**
161141
* Registers a custom deserializer for the specified type.
@@ -165,10 +145,8 @@ public <T> Builder registerTypeAdapter(Type type, TagAdapter<T> adapter) {
165145
* @param deserializer the instance of TagDeserializer to handle deserializing the specified type
166146
* @return the current builder instance for chaining
167147
*/
168-
public <T> Builder registerTypeAdapter(Type type, TagDeserializer<T> deserializer) {
169-
this.serializer.registerTypeAdapter(type, deserializer);
170-
return this;
171-
}
148+
@Contract(value = "_, _ -> this", mutates = "this")
149+
<T> Builder registerTypeAdapter(Type type, TagDeserializer<T> deserializer);
172150

173151
/**
174152
* Registers a custom serializer for the specified type.
@@ -178,18 +156,15 @@ public <T> Builder registerTypeAdapter(Type type, TagDeserializer<T> deserialize
178156
* @param serializer the instance of TagSerializer to handle serializing the specified type
179157
* @return the current builder instance for chaining
180158
*/
181-
public <T> Builder registerTypeAdapter(Type type, TagSerializer<T> serializer) {
182-
this.serializer.registerTypeAdapter(type, serializer);
183-
return this;
184-
}
159+
@Contract(value = "_, _ -> this", mutates = "this")
160+
<T> Builder registerTypeAdapter(Type type, TagSerializer<T> serializer);
185161

186162
/**
187163
* Constructs and returns an instance of NBT using the configured serializers and deserializers.
188164
*
189165
* @return a new instance of NBT
190166
*/
191-
public NBT build() {
192-
return new NBT(serializer);
193-
}
167+
@Contract(value = " -> new", pure = true)
168+
NBT build();
194169
}
195170
}

nbt/src/main/java/core/nbt/serialization/Serializer.java

Lines changed: 0 additions & 128 deletions
This file was deleted.

0 commit comments

Comments
 (0)