From 138fe493873be204e719e09ade1e7d05ba760e39 Mon Sep 17 00:00:00 2001 From: Hileb <107909747+Ecdcaeb@users.noreply.github.com> Date: Sun, 29 Dec 2024 13:16:15 +0800 Subject: [PATCH] new decoder struction new decode struction --- gradle.properties | 2 +- .../deserializer/AbstractDeserializer.java | 6 +- src/main/java/rml/deserializer/Argument.java | 28 +-- .../rml/deserializer/DeserializerBuilder.java | 22 +-- .../rml/deserializer/DeserializerManager.java | 50 +++--- .../JsonDeserializeException.java | 10 +- .../deserializer/struct/gson/JsonStruct.java | 28 +++ .../deserializer/struct/std/StructArray.java | 160 ++++++++++++++++++ .../struct/std/StructBigDecimal.java | 72 ++++++++ .../struct/std/StructBigInteger.java | 74 ++++++++ .../struct/std/StructBoolean.java | 90 ++++++++++ .../deserializer/struct/std/StructByte.java | 55 ++++++ .../struct/std/StructCharacter.java | 55 ++++++ .../deserializer/struct/std/StructDouble.java | 50 ++++++ .../struct/std/StructElement.java | 135 +++++++++++++++ .../deserializer/struct/std/StructFloat.java | 55 ++++++ .../struct/std/StructInteger.java | 55 ++++++ .../deserializer/struct/std/StructLong.java | 50 ++++++ .../deserializer/struct/std/StructNull.java | 21 +++ .../deserializer/struct/std/StructNumber.java | 38 +++++ .../deserializer/struct/std/StructObject.java | 116 +++++++++++++ .../struct/std/StructPrimitive.java | 22 +++ .../deserializer/struct/std/StructShort.java | 55 ++++++ .../deserializer/struct/std/StructString.java | 32 ++++ 24 files changed, 1225 insertions(+), 56 deletions(-) create mode 100644 src/main/java/rml/deserializer/struct/gson/JsonStruct.java create mode 100644 src/main/java/rml/deserializer/struct/std/StructArray.java create mode 100644 src/main/java/rml/deserializer/struct/std/StructBigDecimal.java create mode 100644 src/main/java/rml/deserializer/struct/std/StructBigInteger.java create mode 100644 src/main/java/rml/deserializer/struct/std/StructBoolean.java create mode 100644 src/main/java/rml/deserializer/struct/std/StructByte.java create mode 100644 src/main/java/rml/deserializer/struct/std/StructCharacter.java create mode 100644 src/main/java/rml/deserializer/struct/std/StructDouble.java create mode 100644 src/main/java/rml/deserializer/struct/std/StructElement.java create mode 100644 src/main/java/rml/deserializer/struct/std/StructFloat.java create mode 100644 src/main/java/rml/deserializer/struct/std/StructInteger.java create mode 100644 src/main/java/rml/deserializer/struct/std/StructLong.java create mode 100644 src/main/java/rml/deserializer/struct/std/StructNull.java create mode 100644 src/main/java/rml/deserializer/struct/std/StructNumber.java create mode 100644 src/main/java/rml/deserializer/struct/std/StructObject.java create mode 100644 src/main/java/rml/deserializer/struct/std/StructPrimitive.java create mode 100644 src/main/java/rml/deserializer/struct/std/StructShort.java create mode 100644 src/main/java/rml/deserializer/struct/std/StructString.java diff --git a/gradle.properties b/gradle.properties index aff2764..cd32204 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,7 +5,7 @@ org.gradle.jvmargs = -Xmx3G java_version = 21 -cleanroom_version=0.2.3-alpha +cleanroom_version=0.2.4-alpha # Mapping Properties mapping_channel= stable diff --git a/src/main/java/rml/deserializer/AbstractDeserializer.java b/src/main/java/rml/deserializer/AbstractDeserializer.java index d63b2c3..4e1c8c6 100644 --- a/src/main/java/rml/deserializer/AbstractDeserializer.java +++ b/src/main/java/rml/deserializer/AbstractDeserializer.java @@ -1,7 +1,7 @@ package rml.deserializer; -import com.google.gson.JsonElement; import net.minecraft.util.ResourceLocation; +import rml.deserializer.struct.std.StructElement; import rml.jrx.announces.EarlyClass; import rml.jrx.announces.PublicAPI; @@ -24,7 +24,7 @@ public AbstractDeserializer(ResourceLocation registerName, Class target, IDes public Class getResultTarget(){ return this.target; } - public T deserialize(JsonElement json) throws JsonDeserializeException { + public T deserialize(StructElement json) throws JsonDeserializeException { return this.function.apply(json); } @@ -34,7 +34,7 @@ public ResourceLocation getRegisterName(){ @FunctionalInterface public interface IDeserializer{ - V apply(JsonElement jsonElement) throws JsonDeserializeException; + V apply(StructElement jsonElement) throws JsonDeserializeException; } public static IDeserializer safeRun(IDeserializer deserializer){ diff --git a/src/main/java/rml/deserializer/Argument.java b/src/main/java/rml/deserializer/Argument.java index 7d68a7e..b2d91b3 100644 --- a/src/main/java/rml/deserializer/Argument.java +++ b/src/main/java/rml/deserializer/Argument.java @@ -3,6 +3,8 @@ import com.google.common.collect.Lists; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import rml.deserializer.struct.std.StructElement; +import rml.deserializer.struct.std.StructObject; import rml.jrx.announces.EarlyClass; import rml.jrx.announces.PublicAPI; @@ -35,8 +37,8 @@ public T cast(DeserializerBuilder.Context context) { public static Argument type(String name, Class clazz) { return new Argument(name) { @Override - public void execute(DeserializerManager manager, JsonObject jsonObject, DeserializerBuilder.Context context) throws JsonDeserializeException { - JsonElement element = DeserializerManager.getFromPath(jsonObject, name); + public void execute(DeserializerManager manager, StructObject jsonObject, DeserializerBuilder.Context context) throws JsonDeserializeException { + StructElement element = DeserializerManager.getFromPath(jsonObject, name); if (element != null){ try { final Class clazz2 = DeserializerBuilder.avoidPrimitive(clazz); @@ -56,7 +58,7 @@ public static Argument integer(String name, Function(name) { @Override public void execute(DeserializerManager manager, JsonObject jsonObject, DeserializerBuilder.Context context) throws JsonDeserializeException { - JsonElement element = DeserializerManager.getFromPath(jsonObject, name); + StructElement element = DeserializerManager.getFromPath(jsonObject, name); if (element != null){ Integer obj = null; try { @@ -92,8 +94,8 @@ public static Argument positiveInteger(String name) { public static Argument floatArgument(String name, Function predicate){ return new Argument(name) { @Override - public void execute(DeserializerManager manager, JsonObject jsonObject, DeserializerBuilder.Context context) throws JsonDeserializeException { - JsonElement element = DeserializerManager.getFromPath(jsonObject, name); + public void execute(DeserializerManager manager, StructObject jsonObject, DeserializerBuilder.Context context) throws JsonDeserializeException { + StructElement element = DeserializerManager.getFromPath(jsonObject, name); if (element != null){ Float obj = null; try{ @@ -129,8 +131,8 @@ public static Argument positiveFloat(String name){ public static Argument bool(String name, Function predicate){ return new Argument(name) { @Override - public void execute(DeserializerManager manager, JsonObject jsonObject, DeserializerBuilder.Context context) throws JsonDeserializeException { - JsonElement element = DeserializerManager.getFromPath(jsonObject, name); + public void execute(DeserializerManager manager, StructObject jsonObject, DeserializerBuilder.Context context) throws JsonDeserializeException { + StructElement element = DeserializerManager.getFromPath(jsonObject, name); if (element != null){ Boolean obj = null; try{ @@ -154,12 +156,12 @@ public void execute(DeserializerManager manager, JsonObject jsonObject, Deserial public static Argument> map(String name, final Class clazz){ return new Argument>(name) { @Override - public void execute(DeserializerManager manager, JsonObject jsonObject, DeserializerBuilder.Context context) throws JsonDeserializeException { - JsonElement element = DeserializerManager.getFromPath(jsonObject, name); + public void execute(DeserializerManager manager, StructObject jsonObject, DeserializerBuilder.Context context) throws JsonDeserializeException { + StructElement element = DeserializerManager.getFromPath(jsonObject, name); if (element != null){ - if (!element.isJsonObject()) throw new JsonDeserializeException(jsonObject, "field" + name + "is required be json object."); + if (!element.isObject()) throw new JsonDeserializeException(jsonObject, "field" + name + "is required be json object."); try { - Map map = element.getAsJsonObject().entrySet().stream().collect(Collectors.toMap((entry) -> entry.getKey(), (entry) -> manager.decodeSilently(clazz, entry.getValue()))); + Map map = element.getAsObject().entrySet().stream().collect(Collectors.toMap((entry) -> entry.getKey(), (entry) -> manager.decodeSilently(clazz, entry.getValue()))); context.put(name, map); }catch (Exception e){ throw new JsonDeserializeException(element, e); @@ -174,8 +176,8 @@ public void execute(DeserializerManager manager, JsonObject jsonObject, Deserial public static Argument> list(String name, final Class clazz){ return new Argument>(name) { @Override - public void execute(DeserializerManager manager, JsonObject jsonObject, DeserializerBuilder.Context context) throws JsonDeserializeException { - JsonElement element = DeserializerManager.getFromPath(jsonObject, name); + public void execute(DeserializerManager manager, StructObject jsonObject, DeserializerBuilder.Context context) throws JsonDeserializeException { + StructElement element = DeserializerManager.getFromPath(jsonObject, name); if (element != null){ try { Class arrayCls = Array.newInstance(clazz, 0).getClass(); diff --git a/src/main/java/rml/deserializer/DeserializerBuilder.java b/src/main/java/rml/deserializer/DeserializerBuilder.java index 7a5328d..8e0b655 100644 --- a/src/main/java/rml/deserializer/DeserializerBuilder.java +++ b/src/main/java/rml/deserializer/DeserializerBuilder.java @@ -4,6 +4,8 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import net.minecraft.util.ResourceLocation; +import rml.deserializer.struct.std.StructElement; +import rml.deserializer.struct.std.StructObject; import rml.internal.net.minecraftforge.common.util.LazyOptional; import rml.jrx.announces.EarlyClass; import rml.jrx.announces.PublicAPI; @@ -23,10 +25,10 @@ @EarlyClass @PublicAPI public class DeserializerBuilder { - private DeserializerManager manager; - private Class clazz; - private ResourceLocation resourceLocation; - private HashSet actions = new HashSet<>(); + private final DeserializerManager manager; + private final Class clazz; + private final ResourceLocation resourceLocation; + private final HashSet actions = new HashSet<>(); private IJsonObjectFunction function = (context) -> null; private boolean isDefault = false; public DeserializerBuilder(DeserializerManager manager, Class clazz, ResourceLocation resourceLocation){ @@ -58,7 +60,7 @@ public DeserializerBuilder check(final Function optionalWhen(final Class clazz,final String name,final Context.ToBooleanFunction isNotRequired){ final Class type = DeserializerBuilder.avoidPrimitive(clazz); return this.action(((manager, jsonObject, context) -> { - JsonElement element = DeserializerManager.getFromPath(jsonObject, name); + StructElement element = DeserializerManager.getFromPath(jsonObject, name); if (element != null){ try{ Object obj = manager.decode(type, element); @@ -93,7 +95,7 @@ public DeserializerBuilder optional(Class clazz, String name){ public DeserializerBuilder optionalDefaultWhen(final Class clazz, final String name, final Context.ToBooleanFunction isNotRequired, final V defaultValue){ final Class type = DeserializerBuilder.avoidPrimitive(clazz); return this.action(((manager, jsonObject, context) -> { - JsonElement element = DeserializerManager.getFromPath(jsonObject, name); + StructElement element = DeserializerManager.getFromPath(jsonObject, name); if (element != null){ try{ Object obj = manager.decode(type, element); @@ -116,7 +118,7 @@ public DeserializerBuilder optionalDefault(Class clazz, String name, V public DeserializerBuilder optionalDefaultLazyWhen(final Class clazz, final String name, final Context.ToBooleanFunction isNotRequired, final LazyOptional defaultValue){ final Class type = DeserializerBuilder.avoidPrimitive(clazz); return this.action(((manager, jsonObject, context) -> { - JsonElement element = DeserializerManager.getFromPath(jsonObject, name); + StructElement element = DeserializerManager.getFromPath(jsonObject, name); if (element != null){ try{ Object obj = manager.decode(type, element); @@ -239,8 +241,8 @@ public void put(String s, Object obj){ @FunctionalInterface public interface ToBooleanFunction{ - ToBooleanFunction TRUE = (o)->true; - ToBooleanFunction FALSE = (o)->false; + ToBooleanFunction TRUE = (o) -> true; + ToBooleanFunction FALSE = (o) -> false; boolean apply(Context context); } @@ -249,7 +251,7 @@ public interface ToBooleanFunction{ @FunctionalInterface public interface IAction{ - void execute(DeserializerManager manager, JsonObject jsonObject, Context context) throws JsonDeserializeException; + void execute(DeserializerManager manager, StructObject jsonObject, Context context) throws JsonDeserializeException; } @FunctionalInterface diff --git a/src/main/java/rml/deserializer/DeserializerManager.java b/src/main/java/rml/deserializer/DeserializerManager.java index e304ce5..fcd6eb3 100644 --- a/src/main/java/rml/deserializer/DeserializerManager.java +++ b/src/main/java/rml/deserializer/DeserializerManager.java @@ -4,6 +4,9 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import net.minecraft.util.ResourceLocation; +import rml.deserializer.struct.std.StructArray; +import rml.deserializer.struct.std.StructElement; +import rml.deserializer.struct.std.StructObject; import rml.jrx.announces.EarlyClass; import rml.jrx.announces.PublicAPI; import rml.jrx.utils.ClassHelper; @@ -81,15 +84,15 @@ public AbstractDeserializer addDefaultEntry(AbstractDeserializer abst * * @throws JsonDeserializeException the exception. Error format? Unexpected context? */ - public T decode(Class clazz, JsonElement jsonElement) throws JsonDeserializeException { + public T decode(Class clazz, StructElement jsonElement) throws JsonDeserializeException { ClassHelper.forceInitAll(clazz); clazz = DeserializerBuilder.avoidPrimitive(clazz); try { if (clazz.isArray()){ Class clazzComponentType = clazz.getComponentType(); - if (jsonElement.isJsonArray()){ - JsonArray array = jsonElement.getAsJsonArray(); + if (jsonElement.isArray()){ + StructArray array = jsonElement.getAsArray(); Object arrayToReturn = Array.newInstance(clazzComponentType, array.size()); for(int index = 0 ; index < array.size() ; index++){ Array.set(arrayToReturn, index, decode(clazzComponentType, array.get(index))); @@ -101,9 +104,9 @@ public T decode(Class clazz, JsonElement jsonElement) throws JsonDeserial return clazz.cast(arrayToReturn); } } else { - if (jsonElement.isJsonObject()){ - JsonObject jsonObject = jsonElement.getAsJsonObject(); - if (jsonObject.has("type")){ + if (jsonElement.isObject()){ + StructObject jsonObject = jsonElement.getAsObject(); + if (jsonObject.containsKey("type")){ ResourceLocation decoderName = parseLocation(jsonObject.get("type").getAsString()); if (!registry.containsKey(clazz)) throw new JsonDeserializeException(jsonObject, "Deserializer for " + clazz +" , is not registered."); HashMap> typedRegistry = registry.get(clazz); @@ -131,7 +134,7 @@ public T decode(Class clazz, JsonElement jsonElement) throws JsonDeserial } - public T decodeSilently(Class clazz, JsonElement jsonElement){ + public T decodeSilently(Class clazz, StructElement jsonElement){ JsonDeserializeException exception = null; try { return decode(clazz, jsonElement); @@ -160,20 +163,19 @@ public DeserializerManager(String defaultDomain){ public DeserializerManager(String defaultDomain, Consumer consumer){ this.defaultDomain = defaultDomain; ResourceLocation GSON = new ResourceLocation("google", "primitive"); - this.addDefaultEntry(new AbstractDeserializer<>(GSON, Integer.class, AbstractDeserializer.safeRun(JsonElement::getAsInt))); - this.addDefaultEntry(new AbstractDeserializer<>(GSON, Float.class, AbstractDeserializer.safeRun(JsonElement::getAsFloat))); - this.addDefaultEntry(new AbstractDeserializer<>(GSON, Double.class, AbstractDeserializer.safeRun(JsonElement::getAsDouble))); - this.addDefaultEntry(new AbstractDeserializer<>(GSON, Long.class, AbstractDeserializer.safeRun(JsonElement::getAsLong))); - this.addDefaultEntry(new AbstractDeserializer<>(GSON, Character.class, AbstractDeserializer.safeRun(JsonElement::getAsCharacter))); - this.addDefaultEntry(new AbstractDeserializer<>(GSON, Byte.class, AbstractDeserializer.safeRun(JsonElement::getAsByte))); - this.addDefaultEntry(new AbstractDeserializer<>(GSON, String.class, AbstractDeserializer.safeRun(JsonElement::getAsString))); - this.addDefaultEntry(new AbstractDeserializer<>(GSON, Boolean.class, AbstractDeserializer.safeRun(JsonElement::getAsBoolean))); - this.addDefaultEntry(new AbstractDeserializer<>(GSON, Short.class, AbstractDeserializer.safeRun(JsonElement::getAsShort))); - this.addDefaultEntry(new AbstractDeserializer<>(GSON, BigInteger.class, AbstractDeserializer.safeRun(JsonElement::getAsBigInteger))); - this.addDefaultEntry(new AbstractDeserializer<>(GSON, BigDecimal.class, AbstractDeserializer.safeRun(JsonElement::getAsBigDecimal))); - this.addDefaultEntry(new AbstractDeserializer<>(GSON, Number.class, AbstractDeserializer.safeRun(JsonElement::getAsNumber))); + this.addDefaultEntry(new AbstractDeserializer<>(GSON, Integer.class, AbstractDeserializer.safeRun(StructElement::getAsInteger))); + this.addDefaultEntry(new AbstractDeserializer<>(GSON, Float.class, AbstractDeserializer.safeRun(StructElement::getAsFloat))); + this.addDefaultEntry(new AbstractDeserializer<>(GSON, Double.class, AbstractDeserializer.safeRun(StructElement::getAsDouble))); + this.addDefaultEntry(new AbstractDeserializer<>(GSON, Long.class, AbstractDeserializer.safeRun(StructElement::getAsLong))); + this.addDefaultEntry(new AbstractDeserializer<>(GSON, Character.class, AbstractDeserializer.safeRun(StructElement::getAsCharacter))); + this.addDefaultEntry(new AbstractDeserializer<>(GSON, Byte.class, AbstractDeserializer.safeRun(StructElement::getAsByte))); + this.addDefaultEntry(new AbstractDeserializer<>(GSON, String.class, AbstractDeserializer.safeRun(StructElement::getAsString))); + this.addDefaultEntry(new AbstractDeserializer<>(GSON, Boolean.class, AbstractDeserializer.safeRun(StructElement::getAsBoolean))); + this.addDefaultEntry(new AbstractDeserializer<>(GSON, Short.class, AbstractDeserializer.safeRun(StructElement::getAsShort))); + this.addDefaultEntry(new AbstractDeserializer<>(GSON, BigInteger.class, AbstractDeserializer.safeRun(StructElement::getAsBigInteger))); + this.addDefaultEntry(new AbstractDeserializer<>(GSON, BigDecimal.class, AbstractDeserializer.safeRun(StructElement::getAsBigDecimal))); + this.addDefaultEntry(new AbstractDeserializer<>(GSON, Number.class, AbstractDeserializer.safeRun((number)->number.getAsNumber().castToNumber()))); this.addDefaultEntry(new AbstractDeserializer<>(GSON, Void.class, AbstractDeserializer.safeRun((jsonElement)->null))); - this.addDefaultEntry(new AbstractDeserializer<>(GSON, JsonObject.class, AbstractDeserializer.safeRun(JsonElement::getAsJsonObject))); consumer.accept(this); } @@ -203,15 +205,15 @@ public AbstractDeserializer map(final Class tClass, final Class return new AbstractDeserializer(resourceLocation, tClass, jsonElement -> function.apply(DeserializerManager.this.decode(fClass, jsonElement))); } - public static JsonElement getFromPath(JsonObject jsonObject, String path){ + public static StructElement getFromPath(StructObject jsonObject, String path){ int i = path.indexOf('.'); if (i == -1){ return jsonObject.get(path); }else { String first = path.substring(0, i-1); - JsonElement element1 = jsonObject.get(first); - if (element1 instanceof JsonObject){ - return getFromPath((JsonObject)element1, path.substring(i+1)); + StructElement element1 = jsonObject.get(first); + if (element1.isObject()){ + return getFromPath(element1.getAsObject(), path.substring(i+1)); }else return null; } } diff --git a/src/main/java/rml/deserializer/JsonDeserializeException.java b/src/main/java/rml/deserializer/JsonDeserializeException.java index 54089f3..8d0d5c6 100644 --- a/src/main/java/rml/deserializer/JsonDeserializeException.java +++ b/src/main/java/rml/deserializer/JsonDeserializeException.java @@ -1,6 +1,6 @@ package rml.deserializer; -import com.google.gson.JsonElement; +import rml.deserializer.struct.std.StructElement; import rml.jrx.announces.EarlyClass; import rml.jrx.announces.PublicAPI; @@ -13,17 +13,17 @@ @EarlyClass @PublicAPI public class JsonDeserializeException extends Exception{ - public JsonDeserializeException(JsonElement element, Throwable throwable){ + public JsonDeserializeException(StructElement element, Throwable throwable){ super(String.valueOf(element), throwable); } - public JsonDeserializeException(JsonElement element, String s){ + public JsonDeserializeException(StructElement element, String s){ super(s + ", " + element); } - public JsonDeserializeException(JsonElement element){ + public JsonDeserializeException(StructElement element){ super(String.valueOf(element)); } - public JsonDeserializeException(JsonElement element, String s, Throwable throwable){ + public JsonDeserializeException(StructElement element, String s, Throwable throwable){ super(s + ", " + element, throwable); } } diff --git a/src/main/java/rml/deserializer/struct/gson/JsonStruct.java b/src/main/java/rml/deserializer/struct/gson/JsonStruct.java new file mode 100644 index 0000000..d221e5e --- /dev/null +++ b/src/main/java/rml/deserializer/struct/gson/JsonStruct.java @@ -0,0 +1,28 @@ +package rml.deserializer.struct.gson; + +import com.google.gson.JsonElement; +import com.google.gson.JsonPrimitive; +import dev.latvian.kubejs.documentation.O; +import javassist.expr.FieldAccess; +import rml.deserializer.struct.std.StructBoolean; +import rml.deserializer.struct.std.StructElement; +import rml.deserializer.struct.std.StructNull; +import rml.deserializer.struct.std.StructString; +import rml.jrx.reflection.jvm.FieldAccessor; +import rml.jrx.reflection.jvm.ReflectionHelper; + +public class JsonStruct { +// public static final FieldAccessor jsonPrimitive$value = ReflectionHelper.getFieldAccessor(JsonPrimitive.class, "value"); +// +// public static StructElement toStruct(JsonElement element){ +// if (element == null || element.isJsonNull()) return StructNull.NULL; +// else if (element instanceof JsonPrimitive jsonPrimitive){ +// Object obj = jsonPrimitive$value.get(jsonPrimitive); +// if (obj instanceof Boolean z) return StructBoolean.of(z); +// else if (obj instanceof String s) return new StructString(s); +// else if (obj instanceof Float f) +// if (jsonPrimitive.isBoolean()) return StructBoolean.of(jsonPrimitive.getAsBoolean()); +// else if (jsonPrimitive.isString()) +// } +// } +} diff --git a/src/main/java/rml/deserializer/struct/std/StructArray.java b/src/main/java/rml/deserializer/struct/std/StructArray.java new file mode 100644 index 0000000..9730f2d --- /dev/null +++ b/src/main/java/rml/deserializer/struct/std/StructArray.java @@ -0,0 +1,160 @@ +package rml.deserializer.struct.std; + +import org.jetbrains.annotations.NotNull; + +import java.util.*; + +public class StructArray extends StructElement implements List { + protected final List list; + + public StructArray(){ + this.list = new ArrayList<>(); + } + + + @Override + public int size() { + return this.list.size(); + } + + @Override + public boolean isEmpty() { + return this.list.isEmpty(); + } + + @Override + public boolean contains(Object o) { + return this.list.contains(o); + } + + @Override + public @NotNull Iterator iterator() { + return this.list.iterator(); + } + + @Override + @SuppressWarnings("all") + public @NotNull Object[] toArray() { + return this.list.toArray(); + } + + @Override + @SuppressWarnings("all") + public @NotNull T[] toArray(@NotNull T[] a) { + return this.list.toArray(a); + } + + @Override + public boolean add(StructElement structElement) { + return this.list.add(structElement); + } + + @Override + public boolean remove(Object o) { + return this.list.remove(o); + } + + @Override + @SuppressWarnings("all") + public boolean containsAll(@NotNull Collection c) { + return this.list.containsAll(c); + } + + @Override + public boolean addAll(@NotNull Collection c) { + return this.list.addAll(c); + } + + @Override + public boolean addAll(int index, @NotNull Collection c) { + return this.list.addAll(index, c); + } + + @Override + public boolean removeAll(@NotNull Collection c) { + return this.list.removeAll(c); + } + + @Override + public boolean retainAll(@NotNull Collection c) { + return this.list.retainAll(c); + } + + @Override + public void clear() { + this.list.clear(); + } + + @Override + public StructElement get(int index) { + return this.list.get(index); + } + + @Override + public StructElement set(int index, StructElement element) { + return this.list.set(index, element); + } + + @Override + public void add(int index, StructElement element) { + this.list.add(index, element); + } + + @Override + public StructElement remove(int index) { + return this.list.remove(index); + } + + @Override + public int indexOf(Object o) { + return this.list.indexOf(o); + } + + @Override + public int lastIndexOf(Object o) { + return this.list.lastIndexOf(o); + } + + @Override + public @NotNull ListIterator listIterator() { + return this.list.listIterator(); + } + + @Override + public @NotNull ListIterator listIterator(int index) { + return this.list.listIterator(index); + } + + @Override + public @NotNull List subList(int fromIndex, int toIndex) { + return this.list.subList(fromIndex, toIndex); + } + + @Override + public boolean equals(Object obj) { + if (obj == this) return true; + else if (obj instanceof StructArray array) { + return array.list.equals(this.list); + } else return false; + } + + @Override + public int hashCode() { + return Objects.hashCode(list); + } + + @Override + public String toString() { + return Arrays.toString(list.toArray()); + } + + @Override + public boolean isArray() { + return true; + } + + @Override + public StructArray getAsArray() { + return this; + } +} diff --git a/src/main/java/rml/deserializer/struct/std/StructBigDecimal.java b/src/main/java/rml/deserializer/struct/std/StructBigDecimal.java new file mode 100644 index 0000000..3e5496f --- /dev/null +++ b/src/main/java/rml/deserializer/struct/std/StructBigDecimal.java @@ -0,0 +1,72 @@ +package rml.deserializer.struct.std; + +import java.math.BigDecimal; +import java.util.Objects; + +public class StructBigDecimal extends StructNumber{ + BigDecimal bigDecimal; + public StructBigDecimal(BigDecimal bigDecimal){ + this.bigDecimal = bigDecimal; + } + + @Override + public boolean isBigDecimal() { + return true; + } + + @Override + public BigDecimal getAsBigDecimal() { + return bigDecimal; + } + + @Override + public String toString() { + return bigDecimal.toString(); + } + + @Override + public final boolean equals(Object o) { + if (!(o instanceof StructBigDecimal that)) return false; + return Objects.equals(bigDecimal, that.bigDecimal); + } + + @Override + public int hashCode() { + return Objects.hashCode(bigDecimal); + } + + @Override + public double castToDouble() { + return bigDecimal.doubleValue(); + } + + @Override + public long castToLong() { + return bigDecimal.longValue(); + } + + @Override + public float castToFloat() { + return bigDecimal.floatValue(); + } + + @Override + public int castToInteger() { + return bigDecimal.intValue(); + } + + @Override + public byte castToByte() { + return bigDecimal.byteValue(); + } + + @Override + public short castToShort() { + return bigDecimal.shortValue(); + } + + @Override + public Number castToNumber() { + return bigDecimal; + } +} diff --git a/src/main/java/rml/deserializer/struct/std/StructBigInteger.java b/src/main/java/rml/deserializer/struct/std/StructBigInteger.java new file mode 100644 index 0000000..be133c4 --- /dev/null +++ b/src/main/java/rml/deserializer/struct/std/StructBigInteger.java @@ -0,0 +1,74 @@ +package rml.deserializer.struct.std; + +import java.math.BigInteger; +import java.util.Objects; + +public class StructBigInteger extends StructNumber{ + BigInteger bigInteger; + public StructBigInteger(BigInteger bigInteger){ + this.bigInteger = bigInteger; + } + + @Override + public boolean isBigInteger() { + return true; + } + + @Override + public BigInteger getAsBigInteger() { + return bigInteger; + } + + + @Override + public String toString() { + return bigInteger.toString(); + } + + @Override + public final boolean equals(Object o) { + if (!(o instanceof StructBigInteger that)) return false; + + return Objects.equals(bigInteger, that.bigInteger); + } + + @Override + public int hashCode() { + return Objects.hashCode(bigInteger); + } + + @Override + public double castToDouble() { + return bigInteger.doubleValue(); + } + + @Override + public long castToLong() { + return bigInteger.longValue(); + } + + @Override + public float castToFloat() { + return bigInteger.floatValue(); + } + + @Override + public int castToInteger() { + return bigInteger.intValue(); + } + + @Override + public byte castToByte() { + return bigInteger.byteValue(); + } + + @Override + public short castToShort() { + return bigInteger.shortValue(); + } + + @Override + public Number castToNumber() { + return bigInteger; + } +} diff --git a/src/main/java/rml/deserializer/struct/std/StructBoolean.java b/src/main/java/rml/deserializer/struct/std/StructBoolean.java new file mode 100644 index 0000000..9ea2188 --- /dev/null +++ b/src/main/java/rml/deserializer/struct/std/StructBoolean.java @@ -0,0 +1,90 @@ +package rml.deserializer.struct.std; + +public abstract class StructBoolean extends StructNumber{ + public static StructBoolean of(boolean b){ + return b ? TRUE : FALSE; + } + + public static final StructBoolean TRUE = new StructBoolean(){ + @Override + public boolean getAsBoolean() { + return true; + } + + @Override + public String toString() { + return "true"; + } + + @Override + public boolean castToBoolean() { + return true; + } + + @Override + public long castToLong() { + return 1; + } + + @Override + public double castToDouble() { + return 1; + } + + @Override + public Number castToNumber() { + return 1; + } + }; + + public static final StructBoolean FALSE = new StructBoolean(){ + @Override + public boolean getAsBoolean() { + return false; + } + + @Override + public String toString() { + return "false"; + } + + @Override + public boolean castToBoolean() { + return false; + } + + @Override + public long castToLong() { + return 0; + } + + @Override + public double castToDouble() { + return 0; + } + + @Override + public Number castToNumber() { + return 0; + } + }; + + private StructBoolean(){ + super(); + } + + @Override + public boolean isBoolean() { + return true; + } + + @Override + public boolean equals(Object obj) { + return obj == this; + } + + @Override + public int hashCode() { + return toString().hashCode(); + } +} diff --git a/src/main/java/rml/deserializer/struct/std/StructByte.java b/src/main/java/rml/deserializer/struct/std/StructByte.java new file mode 100644 index 0000000..918cce1 --- /dev/null +++ b/src/main/java/rml/deserializer/struct/std/StructByte.java @@ -0,0 +1,55 @@ +package rml.deserializer.struct.std; + +public class StructByte extends StructNumber{ + byte byte_; + public StructByte(byte byte_){ + this.byte_ = byte_; + } + + @Override + public boolean isByte() { + return true; + } + + @Override + public byte getAsByte() { + return byte_; + } + + @Override + public final boolean equals(Object o) { + if (!(o instanceof StructByte that)) return false; + + return byte_ == that.byte_; + } + + @Override + public int hashCode() { + return byte_; + } + + @Override + public String toString() { + return Byte.toString(byte_); + } + + @Override + public double castToDouble() { + return byte_; + } + + @Override + public long castToLong() { + return byte_; + } + + @Override + public byte castToByte() { + return byte_; + } + + @Override + public Byte castToNumber() { + return byte_; + } +} diff --git a/src/main/java/rml/deserializer/struct/std/StructCharacter.java b/src/main/java/rml/deserializer/struct/std/StructCharacter.java new file mode 100644 index 0000000..ab57cb4 --- /dev/null +++ b/src/main/java/rml/deserializer/struct/std/StructCharacter.java @@ -0,0 +1,55 @@ +package rml.deserializer.struct.std; + +public class StructCharacter extends StructNumber{ + char char_; + public StructCharacter(char char_){ + this.char_ = char_; + } + + @Override + public boolean isCharacter() { + return true; + } + + @Override + public char getAsCharacter() { + return char_; + } + + @Override + public final boolean equals(Object o) { + if (!(o instanceof StructCharacter that)) return false; + + return char_ == that.char_; + } + + @Override + public int hashCode() { + return char_; + } + + @Override + public String toString() { + return Character.toString(char_); + } + + @Override + public double castToDouble() { + return char_; + } + + @Override + public long castToLong() { + return char_; + } + + @Override + public char castToCharacter() { + return char_; + } + + @Override + public Character castToNumber() { + return char_; + } +} diff --git a/src/main/java/rml/deserializer/struct/std/StructDouble.java b/src/main/java/rml/deserializer/struct/std/StructDouble.java new file mode 100644 index 0000000..df23ff2 --- /dev/null +++ b/src/main/java/rml/deserializer/struct/std/StructDouble.java @@ -0,0 +1,50 @@ +package rml.deserializer.struct.std; + +public class StructDouble extends StructNumber{ + double double_; + public StructDouble(double double_){ + this.double_ = double_; + } + + @Override + public boolean isDouble() { + return true; + } + + @Override + public double getAsDouble() { + return double_; + } + + @Override + public final boolean equals(Object o) { + if (!(o instanceof StructDouble that)) return false; + + return Double.compare(double_, that.double_) == 0; + } + + @Override + public int hashCode() { + return Double.hashCode(double_); + } + + @Override + public String toString() { + return Double.toString(double_); + } + + @Override + public double castToDouble() { + return double_; + } + + @Override + public long castToLong() { + return (long) double_; + } + + @Override + public Character castToNumber() { + return double_; + } +} diff --git a/src/main/java/rml/deserializer/struct/std/StructElement.java b/src/main/java/rml/deserializer/struct/std/StructElement.java new file mode 100644 index 0000000..e186e33 --- /dev/null +++ b/src/main/java/rml/deserializer/struct/std/StructElement.java @@ -0,0 +1,135 @@ +package rml.deserializer.struct.std; + +import java.math.BigDecimal; +import java.math.BigInteger; + +public abstract class StructElement { + + public boolean isObject(){ + return false; + } + + public StructObject getAsObject(){ + throw new UnsupportedOperationException(); + } + + public boolean isArray(){ + return false; + } + + public StructArray getAsArray(){ + throw new UnsupportedOperationException(); + } + + public boolean isInteger(){ + return false; + } + + public int getAsInteger(){ + throw new UnsupportedOperationException(); + } + + public boolean isString(){ + return false; + } + + public String getAsString(){ + throw new UnsupportedOperationException(); + } + + public boolean isFloat(){ + return false; + } + + public float getAsFloat(){ + throw new UnsupportedOperationException(); + } + + public boolean isDouble(){ + return false; + } + + public double getAsDouble(){ + throw new UnsupportedOperationException(); + } + + public boolean isLong(){ + return false; + } + + public long getAsLong(){ + throw new UnsupportedOperationException(); + } + + public boolean isCharacter(){ + return false; + } + + public char getAsCharacter(){ + throw new UnsupportedOperationException(); + } + + public boolean isByte(){ + return false; + } + + public byte getAsByte(){ + throw new UnsupportedOperationException(); + } + + public boolean isBoolean(){ + return false; + } + + public boolean getAsBoolean(){ + throw new UnsupportedOperationException(); + } + + public boolean isShort(){ + return false; + } + + public short getAsShort(){ + throw new UnsupportedOperationException(); + } + + public boolean isBigInteger(){ + return false; + } + + public BigInteger getAsBigInteger(){ + throw new UnsupportedOperationException(); + } + + public boolean isBigDecimal(){ + return false; + } + + public BigDecimal getAsBigDecimal(){ + throw new UnsupportedOperationException(); + } + + public boolean isNumber(){ + return false; + } + + public StructNumber getAsNumber(){ + throw new UnsupportedOperationException(); + } + + public boolean isPrimitive(){ + return false; + } + + public String getAsPrimitiveString(){ + throw new UnsupportedOperationException(); + } + + public boolean isNull(){ + return false; + } + + public Void getAsNull(){ + throw new UnsupportedOperationException(); + } +} diff --git a/src/main/java/rml/deserializer/struct/std/StructFloat.java b/src/main/java/rml/deserializer/struct/std/StructFloat.java new file mode 100644 index 0000000..da3fa01 --- /dev/null +++ b/src/main/java/rml/deserializer/struct/std/StructFloat.java @@ -0,0 +1,55 @@ +package rml.deserializer.struct.std; + +public class StructFloat extends StructNumber{ + float float_; + public StructFloat(float float_){ + this.float_ = float_; + } + + @Override + public boolean isFloat() { + return true; + } + + @Override + public float getAsFloat() { + return float_; + } + + @Override + public final boolean equals(Object o) { + if (!(o instanceof StructFloat that)) return false; + + return Float.compare(float_, that.float_) == 0; + } + + @Override + public int hashCode() { + return Float.hashCode(float_); + } + + @Override + public String toString() { + return Float.toString(float_); + } + + @Override + public double castToDouble() { + return float_; + } + + @Override + public long castToLong() { + return (long) float_; + } + + @Override + public float castToFloat() { + return float_; + } + + @Override + public Character castToNumber() { + return float_; + } +} diff --git a/src/main/java/rml/deserializer/struct/std/StructInteger.java b/src/main/java/rml/deserializer/struct/std/StructInteger.java new file mode 100644 index 0000000..54afc73 --- /dev/null +++ b/src/main/java/rml/deserializer/struct/std/StructInteger.java @@ -0,0 +1,55 @@ +package rml.deserializer.struct.std; + +public class StructInteger extends StructNumber{ + int int_; + public StructInteger(int int_){ + this.int_ = int_; + } + + @Override + public boolean isInteger() { + return true; + } + + @Override + public int getAsInteger() { + return int_; + } + + @Override + public final boolean equals(Object o) { + if (!(o instanceof StructInteger that)) return false; + + return int_ == that.int_; + } + + @Override + public int hashCode() { + return int_; + } + + @Override + public String toString() { + return Integer.toString(int_); + } + + @Override + public double castToDouble() { + return int_; + } + + @Override + public long castToLong() { + return int_; + } + + @Override + public int castToInteger() { + return int_; + } + + @Override + public Character castToNumber() { + return int_; + } +} diff --git a/src/main/java/rml/deserializer/struct/std/StructLong.java b/src/main/java/rml/deserializer/struct/std/StructLong.java new file mode 100644 index 0000000..eca85c0 --- /dev/null +++ b/src/main/java/rml/deserializer/struct/std/StructLong.java @@ -0,0 +1,50 @@ +package rml.deserializer.struct.std; + +public class StructLong extends StructNumber{ + long long_; + public StructLong(long long_){ + this.long_ = long_; + } + + @Override + public boolean isLong() { + return true; + } + + @Override + public long getAsLong() { + return long_; + } + + @Override + public final boolean equals(Object o) { + if (!(o instanceof StructLong that)) return false; + + return long_ == that.long_; + } + + @Override + public int hashCode() { + return Long.hashCode(long_); + } + + @Override + public String toString() { + return Long.toString(long_); + } + + @Override + public double castToDouble() { + return long_; + } + + @Override + public long castToLong() { + return long_; + } + + @Override + public Character castToNumber() { + return long_; + } +} diff --git a/src/main/java/rml/deserializer/struct/std/StructNull.java b/src/main/java/rml/deserializer/struct/std/StructNull.java new file mode 100644 index 0000000..c8e7aa8 --- /dev/null +++ b/src/main/java/rml/deserializer/struct/std/StructNull.java @@ -0,0 +1,21 @@ +package rml.deserializer.struct.std; + +public class StructNull extends StructElement{ + public static final StructNull NULL = new StructNull(); + private StructNull(){} + + @Override + public boolean isNull() { + return true; + } + + @Override + public Void getAsNull() { + return null; + } + + @Override + public String toString() { + return "null"; + } +} diff --git a/src/main/java/rml/deserializer/struct/std/StructNumber.java b/src/main/java/rml/deserializer/struct/std/StructNumber.java new file mode 100644 index 0000000..855b883 --- /dev/null +++ b/src/main/java/rml/deserializer/struct/std/StructNumber.java @@ -0,0 +1,38 @@ +package rml.deserializer.struct.std; + +public abstract class StructNumber extends StructPrimitive{ + @Override + public boolean isNumber() { + return true; + } + + @Override + public StructNumber getAsNumber() { + return this; + } + + public int castToInteger(){ + return (int) castToLong(); + } + public abstract double castToDouble(); + public abstract long castToLong(); + public float castToFloat(){ + return (float) castToDouble(); + } + public short castToShort(){ + return (short) castToLong(); + } + public byte castToByte(){ + return (byte) castToLong(); + } + public char castToCharacter(){ + return (char) castToInteger(); + } + public boolean castToBoolean(){ + return castToInteger() != 0; + } + public String castToString(){ + return toString(); + } + public abstract Number castToNumber(); +} diff --git a/src/main/java/rml/deserializer/struct/std/StructObject.java b/src/main/java/rml/deserializer/struct/std/StructObject.java new file mode 100644 index 0000000..0f68581 --- /dev/null +++ b/src/main/java/rml/deserializer/struct/std/StructObject.java @@ -0,0 +1,116 @@ +package rml.deserializer.struct.std; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.*; + +public class StructObject extends StructElement implements Map{ + protected final Map map; + + public StructObject(){ + this.map = new HashMap<>(); + } + + public StructObject(Map map) { + this.map = map; + } + + @Override + public int size() { + return map.size(); + } + + @Override + public boolean isEmpty() { + return map.isEmpty(); + } + + @Override + public boolean containsKey(Object key) { + return map.containsKey(key); + } + + @Override + public boolean containsValue(Object value) { + return map.containsValue(value); + } + + @Override + public StructElement get(Object key) { + return map.get(key); + } + + @Override + public @Nullable StructElement put(String key, StructElement value) { + return map.put(key, value); + } + + @Override + public StructElement remove(Object key) { + return map.remove(key); + } + + @Override + public void putAll(@NotNull Map m) { + map.putAll(m); + } + + @Override + public void clear() { + map.clear(); + } + + @Override + public @NotNull Set keySet() { + return map.keySet(); + } + + @Override + public @NotNull Collection values() { + return map.values(); + } + + @Override + public @NotNull Set> entrySet() { + return map.entrySet(); + } + + @Override + public boolean equals(Object o) { + if (o == null || getClass() != o.getClass()) return false; + StructObject that = (StructObject) o; + return Objects.equals(map, that.map); + } + + @Override + public int hashCode() { + return Objects.hashCode(map); + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append('{'); + int i = 1; + int size = this.size(); + for (Map.Entry entry : this.entrySet()) { + builder.append(" \"").append(entry.getKey()).append("\": ").append(entry.getValue().toString()); + if (i < size) builder.append(',').append('\n'); + else builder.append('\n'); + } + builder.append('}'); + + return builder.toString(); + } + + @Override + public boolean isObject() { + return true; + } + + @Override + public StructObject getAsObject() { + return this; + } +} diff --git a/src/main/java/rml/deserializer/struct/std/StructPrimitive.java b/src/main/java/rml/deserializer/struct/std/StructPrimitive.java new file mode 100644 index 0000000..e0754e6 --- /dev/null +++ b/src/main/java/rml/deserializer/struct/std/StructPrimitive.java @@ -0,0 +1,22 @@ +package rml.deserializer.struct.std; + +public abstract class StructPrimitive extends StructElement{ + @Override + public boolean isPrimitive() { + return true; + } + + @Override + public abstract String toString(); + + @Override + public abstract boolean equals(Object obj); + + @Override + public abstract int hashCode(); + + @Override + public String getAsPrimitiveString() { + return toString(); + } +} diff --git a/src/main/java/rml/deserializer/struct/std/StructShort.java b/src/main/java/rml/deserializer/struct/std/StructShort.java new file mode 100644 index 0000000..483f48a --- /dev/null +++ b/src/main/java/rml/deserializer/struct/std/StructShort.java @@ -0,0 +1,55 @@ +package rml.deserializer.struct.std; + +public class StructShort extends StructNumber{ + short s; + public StructShort(short s){ + this.s = s; + } + + @Override + public boolean isShort() { + return true; + } + + @Override + public short getAsShort() { + return s; + } + + @Override + public final boolean equals(Object o) { + if (!(o instanceof StructShort that)) return false; + + return s == that.s; + } + + @Override + public int hashCode() { + return s; + } + + @Override + public String toString() { + return Short.toString(s); + } + + @Override + public double castToDouble() { + return s; + } + + @Override + public long castToLong() { + return s; + } + + @Override + public short castToShort() { + return s; + } + + @Override + public Character castToNumber() { + return s; + } +} diff --git a/src/main/java/rml/deserializer/struct/std/StructString.java b/src/main/java/rml/deserializer/struct/std/StructString.java new file mode 100644 index 0000000..93d17a1 --- /dev/null +++ b/src/main/java/rml/deserializer/struct/std/StructString.java @@ -0,0 +1,32 @@ +package rml.deserializer.struct.std; + +import java.util.Objects; + +public class StructString extends StructElement{ + String string; + public StructString(String s){ + this.string = s; + } + + @Override + public boolean isString() { + return true; + } + + @Override + public String getAsString() { + return string; + } + + @Override + public final boolean equals(Object o) { + if (!(o instanceof StructString that)) return false; + + return Objects.equals(string, that.string); + } + + @Override + public int hashCode() { + return Objects.hashCode(string); + } +}