map(Function super S, ? super N> function);
+ /**
+ * If the {@code Result} is a {@link Success}, returns the result of
+ * applying the given mapping function to the {@link Success#value()}.
+ * Otherwise, a cast version of the {@link Failure} is returned.
+ *
+ * In case the function throws a {@link Throwable} the failure reason
+ * is returned as a {@code Failure}.
+ *
+ * @param function the mapping function to apply to a {@link Success#value()}
+ * @param reason the failure reason in case of the function throwing
+ * a {@code Throwable}
+ * @param the success type of the value returned from the mapping
+ * function
+ * @return the {@code Result} of mapping the given function to the value
+ * from this {@link Success} or this {@link Failure}
+ * @throws NullPointerException if the mapping function is {@code null}
+ * @see #recover(Function)
+ * @see #peek(Consumer)
+ * @since 0.2.0
+ */
+ Result tryMap(AnyThrowingFunction super S, ? super N> function, F reason);
+
/**
* If the {@code Result} is a {@link Success}, invokes the provided
* consumer with the {@link Success#value()}. Otherwise, the
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 4959edd..7991b45 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
@@ -8,6 +8,7 @@
import io.carbynestack.common.Generated;
import io.carbynestack.common.function.AnyThrowingConsumer;
+import io.carbynestack.common.function.AnyThrowingFunction;
import java.util.Objects;
import java.util.Optional;
@@ -90,6 +91,33 @@ public Result map(Function super S, ? super N> function) {
return new Success<>((N) function.apply(this.value()));
}
+ /**
+ * {@inheritDoc}
+ *
+ * @param function the mapping function to apply to a {@link Success#value()}
+ * @param reason the failure reason in case of the function throwing
+ * a {@code Throwable}
+ * @param the success type of the value returned from the mapping
+ * function
+ * @return the {@code Result} of mapping the given function to the value
+ * from this {@link Success} or this {@link Failure}
+ * @throws NullPointerException if the mapping function is {@code null}
+ * @version JDK 8
+ * @see #recover(Function)
+ * @see #peek(Consumer)
+ * @since 0.2.0
+ */
+ @Override
+ @SuppressWarnings("unchecked")
+ public Result tryMap(AnyThrowingFunction super S, ? super N> function, F reason) {
+ requireNonNull(function);
+ try {
+ return new Success<>((N) function.apply(this.value()));
+ } catch (Throwable throwable) {
+ return new Failure<>(reason);
+ }
+ }
+
/**
* {@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 c008ca7..8d0e406 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
@@ -7,6 +7,7 @@
package io.carbynestack.common.result;
import io.carbynestack.common.function.AnyThrowingConsumer;
+import io.carbynestack.common.function.AnyThrowingFunction;
import java.util.Optional;
import java.util.function.Consumer;
@@ -60,6 +61,28 @@ public Result map(Function super S, ? super N> function) {
return new Failure<>(this.reason());
}
+ /**
+ * {@inheritDoc}
+ *
+ * @param function the mapping function to apply to a {@link Success#value()}
+ * @param reason the failure reason in case of the function throwing
+ * a {@code Throwable}
+ * @param the success type of the value returned from the mapping
+ * function
+ * @return the {@code Result} of mapping the given function to the value
+ * from this {@link Success} or this {@link Failure}
+ * @throws NullPointerException if the mapping function is {@code null}
+ * @version JDK 17
+ * @see #recover(Function)
+ * @see #peek(Consumer)
+ * @since 0.2.0
+ */
+ @Override
+ public Result tryMap(AnyThrowingFunction super S, ? super N> function, F reason) {
+ requireNonNull(function);
+ return new Failure<>(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 6110478..eadf726 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
@@ -9,6 +9,7 @@
import io.carbynestack.common.CsFailureReason;
import io.carbynestack.common.function.AnyThrowingConsumer;
import io.carbynestack.common.function.AnyThrowingSupplier;
+import io.carbynestack.common.function.AnyThrowingFunction;
import io.carbynestack.common.function.ThrowingSupplier;
import java.util.Map;
@@ -159,6 +160,28 @@ default boolean isFailure() {
*/
Result map(Function super S, ? super N> function);
+ /**
+ * If the {@code Result} is a {@link Success}, returns the result of
+ * applying the given mapping function to the {@link Success#value()}.
+ * Otherwise, a cast version of the {@link Failure} is returned.
+ *
+ * In case the function throws a {@link Throwable} the failure reason
+ * is returned as a {@code Failure}.
+ *
+ * @param function the mapping function to apply to a {@link Success#value()}
+ * @param reason the failure reason in case of the function throwing
+ * a {@code Throwable}
+ * @param the success type of the value returned from the mapping
+ * function
+ * @return the {@code Result} of mapping the given function to the value
+ * from this {@link Success} or this {@link Failure}
+ * @throws NullPointerException if the mapping function is {@code null}
+ * @see #recover(Function)
+ * @see #peek(Consumer)
+ * @since 0.2.0
+ */
+ Result tryMap(AnyThrowingFunction super S, ? super N> function, F reason);
+
/**
* If the {@code Result} is a {@link Success}, invokes the provided
* consumer with the {@link Success#value()}. Otherwise, the
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 3b0c397..654b7f6 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
@@ -7,6 +7,7 @@
package io.carbynestack.common.result;
import io.carbynestack.common.function.AnyThrowingConsumer;
+import io.carbynestack.common.function.AnyThrowingFunction;
import java.util.Optional;
import java.util.function.Consumer;
@@ -61,6 +62,33 @@ public Result map(Function super S, ? super N> function) {
return new Success<>((N) function.apply(this.value()));
}
+ /**
+ * {@inheritDoc}
+ *
+ * @param function the mapping function to apply to a {@link Success#value()}
+ * @param reason the failure reason in case of the function throwing
+ * a {@code Throwable}
+ * @param the success type of the value returned from the mapping
+ * function
+ * @return the {@code Result} of mapping the given function to the value
+ * from this {@link Success} or this {@link Failure}
+ * @throws NullPointerException if the mapping function is {@code null}
+ * @version JDK 17
+ * @see #recover(Function)
+ * @see #peek(Consumer)
+ * @since 0.2.0
+ */
+ @Override
+ @SuppressWarnings("unchecked")
+ public Result tryMap(AnyThrowingFunction super S, ? super N> function, F reason) {
+ requireNonNull(function);
+ try {
+ return new Success<>((N) function.apply(this.value()));
+ } catch (Throwable throwable) {
+ return new Failure<>(reason);
+ }
+ }
+
/**
* {@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 e11644b..468e58a 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
@@ -88,6 +88,17 @@ void tryPeek() {
.isFailure()).isTrue();
}
+ @Test
+ void tryMap() {
+ assertThat(new Success<>(12)
+ .tryMap(v -> v * 2, -11)
+ .tryMap(v -> {
+ throw new RuntimeException();
+ }, -21)
+ .tryMap(v -> v * 2, -11)
+ .isFailure()).isTrue();
+ }
+
@Test
void of() {
var value = 12;