From 44df94e05e51cf32bb34ff65b2002c114043edc6 Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Thu, 30 Dec 2021 18:43:33 +0100 Subject: Add Decoder#ofPredicate, Decoder#contramap. Change-Id: Ifd4e372a6a3c3028d1cd74e6d9a0145c3f571ff5 --- .../main/java/eu/mulk/jgvariant/core/Decoder.java | 97 +++++++++++++++++++++- 1 file changed, 95 insertions(+), 2 deletions(-) (limited to 'jgvariant-core/src/main/java') diff --git a/jgvariant-core/src/main/java/eu/mulk/jgvariant/core/Decoder.java b/jgvariant-core/src/main/java/eu/mulk/jgvariant/core/Decoder.java index 33b0480..3beb247 100644 --- a/jgvariant-core/src/main/java/eu/mulk/jgvariant/core/Decoder.java +++ b/jgvariant-core/src/main/java/eu/mulk/jgvariant/core/Decoder.java @@ -15,8 +15,11 @@ import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Objects; import java.util.Optional; import java.util.function.Function; +import java.util.function.Predicate; +import java.util.function.UnaryOperator; import org.apiguardian.api.API; import org.apiguardian.api.API.Status; import org.jetbrains.annotations.Nullable; @@ -100,6 +103,29 @@ public abstract class Decoder { return new MappingDecoder<>(function); } + /** + * Creates a new {@link Decoder} from an existing one by applying a function to the input. + * + * @param function the function to apply. + * @return a new, decorated {@link Decoder}. + * @see java.util.stream.Stream#map + */ + public final Decoder contramap(UnaryOperator function) { + return new ContramappingDecoder(function); + } + + /** + * Creates a new {@link Decoder} that delegates to one of two other {@link Decoder}s based on a + * condition on the input {@link ByteBuffer}. + * + * @param selector the predicate to use to determine the decoder to use. + * @return a new {@link Decoder}. + */ + public static Decoder ofPredicate( + Predicate selector, Decoder thenDecoder, Decoder elseDecoder) { + return new PredicateDecoder<>(selector, thenDecoder, elseDecoder); + } + /** * Creates a {@link Decoder} for an {@code Array} type. * @@ -773,7 +799,7 @@ public abstract class Decoder { private final Function function; - public MappingDecoder(Function function) { + MappingDecoder(Function function) { this.function = function; } @@ -793,11 +819,36 @@ public abstract class Decoder { } } + private class ContramappingDecoder extends Decoder { + + private final UnaryOperator function; + + ContramappingDecoder(UnaryOperator function) { + this.function = function; + } + + @Override + public byte alignment() { + return Decoder.this.alignment(); + } + + @Override + public @Nullable Integer fixedSize() { + return Decoder.this.fixedSize(); + } + + @Override + public T decode(ByteBuffer byteSlice) { + var transformedBuffer = function.apply(byteSlice.asReadOnlyBuffer().order(byteSlice.order())); + return Decoder.this.decode(transformedBuffer); + } + } + private class ByteOrderFixingDecoder extends Decoder { private final ByteOrder byteOrder; - public ByteOrderFixingDecoder(ByteOrder byteOrder) { + ByteOrderFixingDecoder(ByteOrder byteOrder) { this.byteOrder = byteOrder; } @@ -822,4 +873,46 @@ public abstract class Decoder { private static ByteBuffer slicePreservingOrder(ByteBuffer byteSlice, int index, int length) { return byteSlice.slice(index, length).order(byteSlice.order()); } + + private static class PredicateDecoder extends Decoder { + + private final Predicate selector; + private final Decoder thenDecoder; + private final Decoder elseDecoder; + + PredicateDecoder( + Predicate selector, Decoder thenDecoder, Decoder elseDecoder) { + this.selector = selector; + this.thenDecoder = thenDecoder; + this.elseDecoder = elseDecoder; + if (thenDecoder.alignment() != elseDecoder.alignment()) { + throw new IllegalArgumentException( + "incompatible alignments in predicate branches: then=%d, else=%d" + .formatted(thenDecoder.alignment(), elseDecoder.alignment())); + } + + if (!Objects.equals(thenDecoder.fixedSize(), elseDecoder.fixedSize())) { + throw new IllegalArgumentException( + "incompatible sizes in predicate branches: then=%s, else=%s" + .formatted(thenDecoder.fixedSize(), elseDecoder.fixedSize())); + } + } + + @Override + public byte alignment() { + return thenDecoder.alignment(); + } + + @Override + public @Nullable Integer fixedSize() { + return thenDecoder.fixedSize(); + } + + @Override + public U decode(ByteBuffer byteSlice) { + var b = selector.test(byteSlice); + byteSlice.rewind(); + return b ? thenDecoder.decode(byteSlice) : elseDecoder.decode(byteSlice); + } + } } -- cgit v1.2.3