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..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 @@ -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("Cannot cast EDM element of type " + this.type + " to MAP"); + } + + 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/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 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..67af06ce --- /dev/null +++ b/src/main/java/io/wispforest/owo/serialization/format/edm/EdmMap.java @@ -0,0 +1,35 @@ +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) { + if (!this.has(key)) return key.defaultValue(); + 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()); + } +}