From efa8ed01b25b92654ea964eb073b45b7f66860b2 Mon Sep 17 00:00:00 2001 From: Dragon-Seeker Date: Mon, 1 Jan 2024 15:49:51 -0600 Subject: [PATCH 1/3] [endec] Implement EdmMap allowing for easy map operations within EdmEndec --- .../serialization/format/edm/EdmElement.java | 17 ++++++---- .../owo/serialization/format/edm/EdmMap.java | 34 +++++++++++++++++++ 2 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 src/main/java/io/wispforest/owo/serialization/format/edm/EdmMap.java diff --git a/src/main/java/io/wispforest/owo/serialization/format/edm/EdmElement.java b/src/main/java/io/wispforest/owo/serialization/format/edm/EdmElement.java index 8f26712e..eca58adf 100644 --- a/src/main/java/io/wispforest/owo/serialization/format/edm/EdmElement.java +++ b/src/main/java/io/wispforest/owo/serialization/format/edm/EdmElement.java @@ -5,12 +5,12 @@ import java.util.Optional; import java.util.stream.Collectors; -public final class EdmElement { +public sealed class EdmElement permits EdmMap { private final T value; private final Type type; - private EdmElement(T value, Type type) { + EdmElement(T value, Type type) { this.value = value; this.type = type; } @@ -40,6 +40,14 @@ public Object unwrap() { } } + public EdmMap asMap() { + if(this.type != Type.MAP) { + throw new IllegalStateException("Given Element is not a Map Type: " + this); + } + + return new EdmMap(this.cast()); + } + @Override public String toString() { return "E(" + this.type.name() + ", " + this.value + ")"; @@ -48,10 +56,7 @@ public String toString() { @Override public boolean equals(Object o) { if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - EdmElement that = (EdmElement) o; - + if (!(o instanceof EdmElement that)) return false; if (!this.value.equals(that.value)) return false; return this.type == that.type; } diff --git a/src/main/java/io/wispforest/owo/serialization/format/edm/EdmMap.java b/src/main/java/io/wispforest/owo/serialization/format/edm/EdmMap.java new file mode 100644 index 00000000..c931ae12 --- /dev/null +++ b/src/main/java/io/wispforest/owo/serialization/format/edm/EdmMap.java @@ -0,0 +1,34 @@ +package io.wispforest.owo.serialization.format.edm; + +import io.wispforest.owo.serialization.endec.KeyedEndec; +import io.wispforest.owo.serialization.util.MapCarrier; +import org.jetbrains.annotations.NotNull; + +import java.util.Map; + +public final class EdmMap extends EdmElement>> implements MapCarrier { + + EdmMap(EdmElement>> mapElement) { + super(mapElement.value(), mapElement.type()); + } + + @Override + public T getWithErrors(@NotNull KeyedEndec key) { + return key.endec().decodeFully(EdmDeserializer::of, this.value().get(key.key())); + } + + @Override + public void put(@NotNull KeyedEndec key, @NotNull T value) { + this.value().put(key.key(), key.endec().encodeFully(EdmSerializer::of, value)); + } + + @Override + public void delete(@NotNull KeyedEndec key) { + this.value().remove(key.key()); + } + + @Override + public boolean has(@NotNull KeyedEndec key) { + return this.value().containsKey(key.key()); + } +} From 521a11deb7bc17d92a29ad894264e96ac3d43125 Mon Sep 17 00:00:00 2001 From: Dragon-Seeker Date: Wed, 3 Jan 2024 15:42:26 -0600 Subject: [PATCH 2/3] Add EdmMap Endec similar to NbtEndec --- .../io/wispforest/owo/serialization/format/edm/EdmEndec.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/io/wispforest/owo/serialization/format/edm/EdmEndec.java b/src/main/java/io/wispforest/owo/serialization/format/edm/EdmEndec.java index 4d7d5010..ab4f7fc1 100644 --- a/src/main/java/io/wispforest/owo/serialization/format/edm/EdmEndec.java +++ b/src/main/java/io/wispforest/owo/serialization/format/edm/EdmEndec.java @@ -9,6 +9,8 @@ public class EdmEndec implements Endec> { public static final EdmEndec INSTANCE = new EdmEndec(); + public static final Endec MAP = INSTANCE.xmap(EdmElement::asMap, edmMap -> edmMap); + private EdmEndec() {} @Override From f2eb997e3c2024133143dddc54cc31b5654be55b Mon Sep 17 00:00:00 2001 From: glisco Date: Sat, 17 Feb 2024 00:14:13 +0100 Subject: [PATCH 3/3] [endec] clearer error message on EdmElement#asMap, return default value in EdmMap#getWithErrors if key is not present --- .../io/wispforest/owo/serialization/format/edm/EdmElement.java | 2 +- .../java/io/wispforest/owo/serialization/format/edm/EdmMap.java | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/wispforest/owo/serialization/format/edm/EdmElement.java b/src/main/java/io/wispforest/owo/serialization/format/edm/EdmElement.java index eca58adf..b43d797c 100644 --- a/src/main/java/io/wispforest/owo/serialization/format/edm/EdmElement.java +++ b/src/main/java/io/wispforest/owo/serialization/format/edm/EdmElement.java @@ -42,7 +42,7 @@ public Object unwrap() { public EdmMap asMap() { if(this.type != Type.MAP) { - throw new IllegalStateException("Given Element is not a Map Type: " + this); + throw new IllegalStateException("Cannot cast EDM element of type " + this.type + " to MAP"); } return new EdmMap(this.cast()); diff --git a/src/main/java/io/wispforest/owo/serialization/format/edm/EdmMap.java b/src/main/java/io/wispforest/owo/serialization/format/edm/EdmMap.java index c931ae12..67af06ce 100644 --- a/src/main/java/io/wispforest/owo/serialization/format/edm/EdmMap.java +++ b/src/main/java/io/wispforest/owo/serialization/format/edm/EdmMap.java @@ -14,6 +14,7 @@ public final class EdmMap extends EdmElement>> impleme @Override public T getWithErrors(@NotNull KeyedEndec key) { + if (!this.has(key)) return key.defaultValue(); return key.endec().decodeFully(EdmDeserializer::of, this.value().get(key.key())); }