From a73b4a5682614989976438af0698b8052cdd9d06 Mon Sep 17 00:00:00 2001 From: David Greven Date: Mon, 13 Dec 2021 11:33:32 +0100 Subject: [PATCH] Add Result.mapFailure (#34) Signed-off-by: David Greven Signed-off-by: Sebastian Becker --- .../common/result/FailureTest.java | 17 ++++++++++++++++ .../common/result/SuccessTest.java | 17 ++++++++++++++++ .../carbynestack/common/result/Failure.java | 20 +++++++++++++++++++ .../io/carbynestack/common/result/Result.java | 17 ++++++++++++++++ .../carbynestack/common/result/Success.java | 20 +++++++++++++++++++ .../carbynestack/common/result/Failure.java | 20 +++++++++++++++++++ .../io/carbynestack/common/result/Result.java | 17 ++++++++++++++++ .../carbynestack/common/result/Success.java | 20 +++++++++++++++++++ .../common/result/ResultTest.java | 9 +++++++++ 9 files changed, 157 insertions(+) diff --git a/common-testing/src/test/java/io/carbynestack/common/result/FailureTest.java b/common-testing/src/test/java/io/carbynestack/common/result/FailureTest.java index 4192d62..9a14c6f 100644 --- a/common-testing/src/test/java/io/carbynestack/common/result/FailureTest.java +++ b/common-testing/src/test/java/io/carbynestack/common/result/FailureTest.java @@ -58,6 +58,17 @@ void mapAndTransformType() { .hasReason(reason); } + @Test + void mapFailure() { + assertThat(result.mapFailure(r -> r * 2)).hasReason(reason * 2); + } + + @Test + void mapFailureNullPointerException() { + assertThatThrownBy(() -> result.mapFailure(null)) + .isExactlyInstanceOf(NullPointerException.class); + } + @Test void tryMap() { assertThat(result.tryMap(v -> v * 2, reason * 2)).hasReason(reason); @@ -69,6 +80,12 @@ void tryMapNullPointerException() { .isExactlyInstanceOf(NullPointerException.class); } + @Test + void mapFailureAndTransformType() { + assertThat(result.mapFailure(r -> String.format("%s * 2 -> %s", r, r * 2))) + .hasReason("21 * 2 -> 42"); + } + @Test void tryMapAndTransformType() { assertThat(result.tryMap(v -> String.format("%s * 2 -> %s", v, v * 2), diff --git a/common-testing/src/test/java/io/carbynestack/common/result/SuccessTest.java b/common-testing/src/test/java/io/carbynestack/common/result/SuccessTest.java index ff1c3d1..b2cb25f 100644 --- a/common-testing/src/test/java/io/carbynestack/common/result/SuccessTest.java +++ b/common-testing/src/test/java/io/carbynestack/common/result/SuccessTest.java @@ -59,6 +59,17 @@ void mapAndTransformType() { .hasValue("12 * 2 -> 24"); } + @Test + void mapFailure() { + assertThat(result.mapFailure(r -> r * 2)).hasValue(value); + } + + @Test + void mapFailureNullPointerException() { + assertThatThrownBy(() -> result.mapFailure(null)) + .isExactlyInstanceOf(NullPointerException.class); + } + @Test void tryMap() { assertThat(result.tryMap(v -> v * 2, -11)).hasValue(24); @@ -70,6 +81,12 @@ void tryMapNullPointerException() { .isExactlyInstanceOf(NullPointerException.class); } + @Test + void mapFailureAndTransformType() { + assertThat(result.mapFailure(r -> String.format("%s * 2 -> %s", r, r * 2))) + .hasValue(value); + } + @Test void tryMapAndTransformType() { assertThat(result.tryMap(v -> String.format("%s * 2 -> %s", v, v * 2), diff --git a/common-types/src/main/java/io/carbynestack/common/result/Failure.java b/common-types/src/main/java/io/carbynestack/common/result/Failure.java index 8e02714..9eee258 100644 --- a/common-types/src/main/java/io/carbynestack/common/result/Failure.java +++ b/common-types/src/main/java/io/carbynestack/common/result/Failure.java @@ -90,6 +90,26 @@ public Result map(Function function) { return new Failure<>(this.reason()); } + /** + * {@inheritDoc} + * + * @param function the mapping function to apply to a {@link Failure#reason()} + * @param the failure type of the value returned from the mapping + * function + * @return the {@code Result} of mapping the given function to the value + * from this {@link Failure} or this {@link Success} + * @throws NullPointerException if the mapping function is {@code null} + * @version JDK 8 + * @see #map(Function) + * @see #recover(Function) + * @since 0.2.0 + */ + @Override + @SuppressWarnings("unchecked") + public Result mapFailure(Function function) { + return new Failure<>((R) function.apply(this.reason())); + } + /** * {@inheritDoc} * diff --git a/common-types/src/main/java/io/carbynestack/common/result/Result.java b/common-types/src/main/java/io/carbynestack/common/result/Result.java index 336974d..d9587f2 100644 --- a/common-types/src/main/java/io/carbynestack/common/result/Result.java +++ b/common-types/src/main/java/io/carbynestack/common/result/Result.java @@ -160,6 +160,23 @@ default boolean isFailure() { */ Result map(Function function); + /** + * If the {@code Result} is a {@link Failure}, returns the result of + * applying the given mapping function to the {@link Failure#reason()}. + * Otherwise, a cast version of the {@link Success} is returned. + * + * @param function the mapping function to apply to a {@link Failure#reason()} + * @param the failure type of the value returned from the mapping + * function + * @return the {@code Result} of mapping the given function to the value + * from this {@link Failure} or this {@link Success} + * @throws NullPointerException if the mapping function is {@code null} + * @see #map(Function) + * @see #recover(Function) + * @since 0.2.0 + */ + Result mapFailure(Function function); + /** * If the {@code Result} is a {@link Success}, returns the result of * applying the given mapping function to the {@link Success#value()}. diff --git a/common-types/src/main/java/io/carbynestack/common/result/Success.java b/common-types/src/main/java/io/carbynestack/common/result/Success.java index 7991b45..45b18d0 100644 --- a/common-types/src/main/java/io/carbynestack/common/result/Success.java +++ b/common-types/src/main/java/io/carbynestack/common/result/Success.java @@ -91,6 +91,26 @@ public Result map(Function function) { return new Success<>((N) function.apply(this.value())); } + /** + * {@inheritDoc} + * + * @param function the mapping function to apply to a {@link Failure#reason()} + * @param the failure type of the value returned from the mapping + * function + * @return the {@code Result} of mapping the given function to the value + * from this {@link Failure} or this {@link Success} + * @throws NullPointerException if the mapping function is {@code null} + * @version JDK 8 + * @see #map(Function) + * @see #recover(Function) + * @since 0.2.0 + */ + @Override + public Result mapFailure(Function function) { + requireNonNull(function); + return new Success<>(this.value()); + } + /** * {@inheritDoc} * diff --git a/common-types/src/main/java17/io/carbynestack/common/result/Failure.java b/common-types/src/main/java17/io/carbynestack/common/result/Failure.java index 8d0e406..4fec180 100644 --- a/common-types/src/main/java17/io/carbynestack/common/result/Failure.java +++ b/common-types/src/main/java17/io/carbynestack/common/result/Failure.java @@ -61,6 +61,26 @@ public Result map(Function function) { return new Failure<>(this.reason()); } + /** + * {@inheritDoc} + * + * @param function the mapping function to apply to a {@link Failure#reason()} + * @param the failure type of the value returned from the mapping + * function + * @return the {@code Result} of mapping the given function to the value + * from this {@link Failure} or this {@link Success} + * @throws NullPointerException if the mapping function is {@code null} + * @version JDK 17 + * @see #map(Function) + * @see #recover(Function) + * @since 0.2.0 + */ + @Override + @SuppressWarnings("unchecked") + public Result mapFailure(Function function) { + return new Failure<>((R) function.apply(this.reason())); + } + /** * {@inheritDoc} * diff --git a/common-types/src/main/java17/io/carbynestack/common/result/Result.java b/common-types/src/main/java17/io/carbynestack/common/result/Result.java index eadf726..505c793 100644 --- a/common-types/src/main/java17/io/carbynestack/common/result/Result.java +++ b/common-types/src/main/java17/io/carbynestack/common/result/Result.java @@ -160,6 +160,23 @@ default boolean isFailure() { */ Result map(Function function); + /** + * If the {@code Result} is a {@link Failure}, returns the result of + * applying the given mapping function to the {@link Failure#reason()}. + * Otherwise, a cast version of the {@link Success} is returned. + * + * @param function the mapping function to apply to a {@link Failure#reason()} + * @param the failure type of the value returned from the mapping + * function + * @return the {@code Result} of mapping the given function to the value + * from this {@link Failure} or this {@link Success} + * @throws NullPointerException if the mapping function is {@code null} + * @see #map(Function) + * @see #recover(Function) + * @since 0.2.0 + */ + Result mapFailure(Function function); + /** * If the {@code Result} is a {@link Success}, returns the result of * applying the given mapping function to the {@link Success#value()}. diff --git a/common-types/src/main/java17/io/carbynestack/common/result/Success.java b/common-types/src/main/java17/io/carbynestack/common/result/Success.java index 654b7f6..e7fc4a3 100644 --- a/common-types/src/main/java17/io/carbynestack/common/result/Success.java +++ b/common-types/src/main/java17/io/carbynestack/common/result/Success.java @@ -62,6 +62,26 @@ public Result map(Function function) { return new Success<>((N) function.apply(this.value())); } + /** + * {@inheritDoc} + * + * @param function the mapping function to apply to a {@link Failure#reason()} + * @param the failure type of the value returned from the mapping + * function + * @return the {@code Result} of mapping the given function to the value + * from this {@link Failure} or this {@link Success} + * @throws NullPointerException if the mapping function is {@code null} + * @version JDK 17 + * @see #map(Function) + * @see #recover(Function) + * @since 0.2.0 + */ + @Override + public Result mapFailure(Function function) { + requireNonNull(function); + return new Success<>(this.value()); + } + /** * {@inheritDoc} * diff --git a/common-types/src/test/java/io/carbynestack/common/result/ResultTest.java b/common-types/src/test/java/io/carbynestack/common/result/ResultTest.java index 468e58a..f916bd3 100644 --- a/common-types/src/test/java/io/carbynestack/common/result/ResultTest.java +++ b/common-types/src/test/java/io/carbynestack/common/result/ResultTest.java @@ -77,6 +77,15 @@ void result() { assertThat(res.stream().toList()).isEmpty(); } + @Test + void mapFailure() { + assertThat(new Success<>(12) + .mapFailure(Long.TYPE::cast) + .flatMap(v -> new Failure((long) v * 2)) + .mapFailure(Long::intValue) + .isFailure()).isTrue(); + } + @Test void tryPeek() { assertThat(new Success<>(12)