diff --git a/src/main/java/com/mojang/datafixers/optics/Affine.java b/src/main/java/com/mojang/datafixers/optics/Affine.java index 9f7e4fee..4b64505a 100644 --- a/src/main/java/com/mojang/datafixers/optics/Affine.java +++ b/src/main/java/com/mojang/datafixers/optics/Affine.java @@ -32,9 +32,7 @@ default

FunctionType, App2> eval(final App return input -> cartesian.dimap( cocartesian.left(cartesian.rmap(cartesian.first(input), p -> set(p.getFirst(), p.getSecond()))), (S s) -> preview(s).map(Either::right, a -> Either.left(Pair.of(a, s))), - (Either e) -> { - return e.map(Function.identity(), Function.identity()); - } + Either::unwrap ); } diff --git a/src/main/java/com/mojang/serialization/Codec.java b/src/main/java/com/mojang/serialization/Codec.java index 3a570a89..47a4a1ad 100644 --- a/src/main/java/com/mojang/serialization/Codec.java +++ b/src/main/java/com/mojang/serialization/Codec.java @@ -132,7 +132,7 @@ static Codec withAlternative(final Codec primary, final Codec either.map(v -> v, v -> v), + Either::unwrap, Either::left ); } diff --git a/src/main/java/com/mojang/serialization/DataResult.java b/src/main/java/com/mojang/serialization/DataResult.java index e2504df1..b2531817 100644 --- a/src/main/java/com/mojang/serialization/DataResult.java +++ b/src/main/java/com/mojang/serialization/DataResult.java @@ -126,6 +126,12 @@ default DataResult addLifecycle(final Lifecycle lifecycle) { return setLifecycle(lifecycle().add(lifecycle)); } + boolean isSuccess(); + + default boolean isError() { + return !isSuccess(); + } + record Success(R value, Lifecycle lifecycle) implements DataResult { @Override public Optional result() { @@ -223,6 +229,11 @@ public DataResult setLifecycle(final Lifecycle lifecycle) { return new Success<>(value, lifecycle); } + @Override + public boolean isSuccess() { + return true; + } + @Override public String toString() { return "DataResult.Success[" + value + "]"; @@ -361,6 +372,11 @@ public Error setLifecycle(final Lifecycle lifecycle) { return new Error<>(messageSupplier, partialValue, lifecycle); } + @Override + public boolean isSuccess() { + return false; + } + @Override public String toString() { return "DataResult.Error['" + message() + "'" + partialValue.map(value -> ": " + value).orElse("") + "]"; diff --git a/src/main/java/com/mojang/serialization/DynamicOps.java b/src/main/java/com/mojang/serialization/DynamicOps.java index 951ab9b8..a7bd844a 100644 --- a/src/main/java/com/mojang/serialization/DynamicOps.java +++ b/src/main/java/com/mojang/serialization/DynamicOps.java @@ -151,7 +151,7 @@ default DataResult>> getList(final T input) { default DataResult getByteBuffer(final T input) { return getStream(input).flatMap(stream -> { final List list = stream.collect(Collectors.toList()); - if (list.stream().allMatch(element -> getNumberValue(element).result().isPresent())) { + if (list.stream().allMatch(element -> getNumberValue(element).isSuccess())) { final ByteBuffer buffer = ByteBuffer.wrap(new byte[list.size()]); for (int i = 0; i < list.size(); i++) { buffer.put(i, getNumberValue(list.get(i)).result().get().byteValue()); @@ -168,9 +168,9 @@ default T createByteList(final ByteBuffer input) { default DataResult getIntStream(final T input) { return getStream(input).flatMap(stream -> { - final List list = stream.collect(Collectors.toList()); - if (list.stream().allMatch(element -> getNumberValue(element).result().isPresent())) { - return DataResult.success(list.stream().mapToInt(element -> getNumberValue(element).result().get().intValue())); + final List list = stream.toList(); + if (list.stream().allMatch(element -> getNumberValue(element).isSuccess())) { + return DataResult.success(list.stream().mapToInt(element -> getNumberValue(element).getOrThrow().intValue())); } return DataResult.error(() -> "Some elements are not ints: " + input); }); @@ -182,9 +182,9 @@ default T createIntList(final IntStream input) { default DataResult getLongStream(final T input) { return getStream(input).flatMap(stream -> { - final List list = stream.collect(Collectors.toList()); - if (list.stream().allMatch(element -> getNumberValue(element).result().isPresent())) { - return DataResult.success(list.stream().mapToLong(element -> getNumberValue(element).result().get().longValue())); + final List list = stream.toList(); + if (list.stream().allMatch(element -> getNumberValue(element).isSuccess())) { + return DataResult.success(list.stream().mapToLong(element -> getNumberValue(element).getOrThrow().longValue())); } return DataResult.error(() -> "Some elements are not longs: " + input); }); diff --git a/src/main/java/com/mojang/serialization/codecs/BaseMapCodec.java b/src/main/java/com/mojang/serialization/codecs/BaseMapCodec.java index 3050ee94..89835b64 100644 --- a/src/main/java/com/mojang/serialization/codecs/BaseMapCodec.java +++ b/src/main/java/com/mojang/serialization/codecs/BaseMapCodec.java @@ -42,7 +42,7 @@ default DataResult> decode(final DynamicOps ops, final MapLike< return r.apply2stable((u, p) -> u, DataResult.error(() -> "Duplicate entry for key: '" + entry.get().getFirst() + "'")); } } - if (entryResult.error().isPresent()) { + if (entryResult.isError()) { failed.add(pair); } diff --git a/src/main/java/com/mojang/serialization/codecs/DispatchedMapCodec.java b/src/main/java/com/mojang/serialization/codecs/DispatchedMapCodec.java index 4102e7a6..5ea28c74 100644 --- a/src/main/java/com/mojang/serialization/codecs/DispatchedMapCodec.java +++ b/src/main/java/com/mojang/serialization/codecs/DispatchedMapCodec.java @@ -68,7 +68,7 @@ private DataResult parseEntry(final DataResult result, final Dyn return result.apply2stable((u, p) -> u, DataResult.error(() -> "Duplicate entry for key: '" + key + "'")); } } - if (entryResult.error().isPresent()) { + if (entryResult.isError()) { failed.add(input); } diff --git a/src/main/java/com/mojang/serialization/codecs/EitherCodec.java b/src/main/java/com/mojang/serialization/codecs/EitherCodec.java index da6f7e88..1ad0b6b2 100644 --- a/src/main/java/com/mojang/serialization/codecs/EitherCodec.java +++ b/src/main/java/com/mojang/serialization/codecs/EitherCodec.java @@ -12,11 +12,11 @@ public record EitherCodec(Codec first, Codec second) implements Code @Override public DataResult, T>> decode(final DynamicOps ops, final T input) { final DataResult, T>> firstRead = first.decode(ops, input).map(vo -> vo.mapFirst(Either::left)); - if (firstRead.error().isEmpty()) { + if (firstRead.isSuccess()) { return firstRead; } final DataResult, T>> secondRead = second.decode(ops, input).map(vo -> vo.mapFirst(Either::right)); - if (secondRead.error().isEmpty()) { + if (secondRead.isSuccess()) { return secondRead; } return firstRead.apply2((f, s) -> s, secondRead); diff --git a/src/main/java/com/mojang/serialization/codecs/EitherMapCodec.java b/src/main/java/com/mojang/serialization/codecs/EitherMapCodec.java index d3c9067d..b7ff61cb 100644 --- a/src/main/java/com/mojang/serialization/codecs/EitherMapCodec.java +++ b/src/main/java/com/mojang/serialization/codecs/EitherMapCodec.java @@ -25,11 +25,11 @@ public EitherMapCodec(final MapCodec first, final MapCodec second) { @Override public DataResult> decode(final DynamicOps ops, final MapLike input) { final DataResult> firstRead = first.decode(ops, input).map(Either::left); - if (firstRead.error().isEmpty()) { + if (firstRead.isSuccess()) { return firstRead; } final DataResult> secondRead = second.decode(ops, input).map(Either::right); - if (secondRead.error().isEmpty()) { + if (secondRead.isSuccess()) { return secondRead; } return firstRead.apply2((f, s) -> s, secondRead); diff --git a/src/main/java/com/mojang/serialization/codecs/KeyDispatchCodec.java b/src/main/java/com/mojang/serialization/codecs/KeyDispatchCodec.java index ba98e7c8..5da4edc7 100644 --- a/src/main/java/com/mojang/serialization/codecs/KeyDispatchCodec.java +++ b/src/main/java/com/mojang/serialization/codecs/KeyDispatchCodec.java @@ -60,7 +60,7 @@ public DataResult decode(final DynamicOps ops, final MapLike input) public RecordBuilder encode(final V input, final DynamicOps ops, final RecordBuilder prefix) { final DataResult> encoderResult = encoder.apply(input); final RecordBuilder builder = prefix.withErrorsFrom(encoderResult); - if (encoderResult.result().isEmpty()) { + if (encoderResult.isError()) { return builder; } diff --git a/src/main/java/com/mojang/serialization/codecs/OptionalFieldCodec.java b/src/main/java/com/mojang/serialization/codecs/OptionalFieldCodec.java index d18ec781..27451152 100644 --- a/src/main/java/com/mojang/serialization/codecs/OptionalFieldCodec.java +++ b/src/main/java/com/mojang/serialization/codecs/OptionalFieldCodec.java @@ -32,7 +32,7 @@ public DataResult> decode(final DynamicOps ops, final MapLike return DataResult.success(Optional.empty()); } final DataResult parsed = elementCodec.parse(ops, value); - if (parsed.result().isEmpty() && lenient) { + if (parsed.isError() && lenient) { return DataResult.success(Optional.empty()); } return parsed.map(Optional::of).setPartial(parsed.resultOrPartial()); diff --git a/src/test/java/com/mojang/serialization/CodecTests.java b/src/test/java/com/mojang/serialization/CodecTests.java index 200b42f1..15ac630d 100644 --- a/src/test/java/com/mojang/serialization/CodecTests.java +++ b/src/test/java/com/mojang/serialization/CodecTests.java @@ -34,7 +34,7 @@ private static T fromJavaOrPartial(final Codec codec, final Object value) private static void assertFromJavaFails(final Codec codec, final Object value) { final DataResult result = codec.parse(JavaOps.INSTANCE, value); - assertTrue("Expected data result error, but got: " + result.result(), result.result().isEmpty()); + assertTrue("Expected data result error, but got: " + result.result(), result.isError()); } private static void assertFromJavaFailsPartial(final Codec codec, final Object value) { @@ -44,7 +44,7 @@ private static void assertFromJavaFailsPartial(final Codec codec, final Objec private static void assertToJavaFails(final Codec codec, final T value) { final DataResult result = codec.encodeStart(JavaOps.INSTANCE, value); - assertTrue("Expected data result error, but got: " + result.result(), result.result().isEmpty()); + assertTrue("Expected data result error, but got: " + result.result(), result.isError()); } private static void assertRoundTrip(final Codec codec, final T value, final Object java) {