From 2d1ffa210ba92dcf3aa7fd161247ef53448f6dc0 Mon Sep 17 00:00:00 2001 From: Luke Bemish Date: Mon, 22 Jul 2024 22:52:54 -0500 Subject: [PATCH] Simplify ParametricKeyedValue --- .../structured/KeyStoringInterpreter.java | 2 +- .../structured/MapCodecInterpreter.java | 14 +++++++------- .../structured/ParametricKeyedValue.java | 10 ++++------ .../test/structured/TestParametricKeys.java | 4 ++-- 4 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/main/java/dev/lukebemish/codecextras/structured/KeyStoringInterpreter.java b/src/main/java/dev/lukebemish/codecextras/structured/KeyStoringInterpreter.java index 3828ed2..7865e6a 100644 --- a/src/main/java/dev/lukebemish/codecextras/structured/KeyStoringInterpreter.java +++ b/src/main/java/dev/lukebemish/codecextras/structured/KeyStoringInterpreter.java @@ -22,7 +22,7 @@ public DataResult> keyed(Key key) { public DataResult>> parametricallyKeyed(Key2 key, App parameter) { return parametricKeys.get(key) .map(ParametricKeyedValue::unbox) - .map(val -> val.converter().convert(parameter)) + .map(val -> val.convert(parameter)) .map(DataResult::success) .orElse(DataResult.error(() -> "Unknown key "+key.name())); } diff --git a/src/main/java/dev/lukebemish/codecextras/structured/MapCodecInterpreter.java b/src/main/java/dev/lukebemish/codecextras/structured/MapCodecInterpreter.java index 5d5da66..e567d48 100644 --- a/src/main/java/dev/lukebemish/codecextras/structured/MapCodecInterpreter.java +++ b/src/main/java/dev/lukebemish/codecextras/structured/MapCodecInterpreter.java @@ -29,13 +29,13 @@ public App convert(App app) { @Override public App2, A, B> convert(App2, A, B> input) { var unboxed = ParametricKeyedValue.unbox(input); - return new ParametricKeyedValue<>(new ParametricKeyedValue.Converter<>() { - @Override - public App> convert(App parameter) { - var mapCodec = unbox(unboxed.converter().convert(parameter)); - return new CodecInterpreter.Holder<>(mapCodec.codec()); - } - }); + return new ParametricKeyedValue<>() { + @Override + public App> convert(App parameter) { + var mapCodec = unbox(unboxed.convert(parameter)); + return new CodecInterpreter.Holder<>(mapCodec.codec()); + } + }; } }).join(parametricCodecKeys)); } diff --git a/src/main/java/dev/lukebemish/codecextras/structured/ParametricKeyedValue.java b/src/main/java/dev/lukebemish/codecextras/structured/ParametricKeyedValue.java index c20e18c..0bdc345 100644 --- a/src/main/java/dev/lukebemish/codecextras/structured/ParametricKeyedValue.java +++ b/src/main/java/dev/lukebemish/codecextras/structured/ParametricKeyedValue.java @@ -5,14 +5,12 @@ import com.mojang.datafixers.kinds.K1; import com.mojang.datafixers.kinds.K2; -public record ParametricKeyedValue(Converter converter) implements App2, MuP, MuO> { - public static class Mu implements K2 {} +public interface ParametricKeyedValue extends App2, MuP, MuO> { + class Mu implements K2 {} - public interface Converter { - App> convert(App parameter); - } + App> convert(App parameter); - public static ParametricKeyedValue unbox(App2, MuP, MuO> boxed) { + static ParametricKeyedValue unbox(App2, MuP, MuO> boxed) { return (ParametricKeyedValue) boxed; } } diff --git a/src/test/java/dev/lukebemish/codecextras/test/structured/TestParametricKeys.java b/src/test/java/dev/lukebemish/codecextras/test/structured/TestParametricKeys.java index 65d9ad8..8eaa8fa 100644 --- a/src/test/java/dev/lukebemish/codecextras/test/structured/TestParametricKeys.java +++ b/src/test/java/dev/lukebemish/codecextras/test/structured/TestParametricKeys.java @@ -52,12 +52,12 @@ private static Codec> withTypeCodec(Prefix prefix) { private static final Codec> CODEC = new CodecInterpreter( Keys.builder().build(), Keys2., K1, K1>builder() - .add(WITH_TYPE, new ParametricKeyedValue<>(new ParametricKeyedValue.Converter<>() { + .add(WITH_TYPE, new ParametricKeyedValue<>() { @Override public App> convert(App parameter) { return new CodecInterpreter.Holder<>(withTypeCodec(Prefix.unbox(parameter)).xmap(Function.identity(), WithType::unbox)); } - })) + }) .build() ).interpret(STRUCTURE).getOrThrow();