From a6db1e130c4823d3293d3748e9948379c6a183b6 Mon Sep 17 00:00:00 2001 From: Eric Slep Date: Sat, 29 Oct 2022 00:48:50 +0200 Subject: [PATCH 1/2] Try::toEither with throwable mapper --- src/main/java/io/vavr/control/Try.java | 15 +++++++++++++++ src/test/java/io/vavr/control/TryTest.java | 9 +++++++++ 2 files changed, 24 insertions(+) diff --git a/src/main/java/io/vavr/control/Try.java b/src/main/java/io/vavr/control/Try.java index 88fb8f246..18ccea889 100644 --- a/src/main/java/io/vavr/control/Try.java +++ b/src/main/java/io/vavr/control/Try.java @@ -1132,6 +1132,21 @@ public final Either toEither() { } } + /** + * Converts this {@code Try} to an {@link Either}, converting the Throwable (if present) + * to a left value using the passed {@link Function} + * + * @param throwableMapper A transformation from throwable to the left type of the new {@code Either} + * @return A new {@code Either} + */ + public final Either toEither(Function throwableMapper) { + if (isFailure()) { + return Either.left(throwableMapper.apply(getCause())); + } else { + return Either.right(get()); + } + } + /** * Converts this {@code Try} to a {@link Validation}. * diff --git a/src/test/java/io/vavr/control/TryTest.java b/src/test/java/io/vavr/control/TryTest.java index ce7a36fd3..3cfdcbd4c 100644 --- a/src/test/java/io/vavr/control/TryTest.java +++ b/src/test/java/io/vavr/control/TryTest.java @@ -969,6 +969,15 @@ public void shouldConvertFailureToEitherLeftSupplier() { assertThat(failure().toEither(() -> "test").isLeft()).isTrue(); } + @Test + public void shouldConvertFailureToEitherLeftMapper() { + final Try failure = Try.failure(new RuntimeException("a certain value")); + final Either either = failure.toEither(t -> + t.getMessage().equals("a certain value") + ); + assertThat(either).isEqualTo(Either.left(true)); + } + // -- toValidation From 839cc8adbf89afb05a58008958b0d4db24307944 Mon Sep 17 00:00:00 2001 From: Eric Slep Date: Mon, 31 Oct 2022 18:17:52 +0100 Subject: [PATCH 2/2] Code review improvements --- src/main/java/io/vavr/control/Try.java | 9 +++++---- src/test/java/io/vavr/control/TryTest.java | 6 ++---- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/main/java/io/vavr/control/Try.java b/src/main/java/io/vavr/control/Try.java index 18ccea889..a5121a5a9 100644 --- a/src/main/java/io/vavr/control/Try.java +++ b/src/main/java/io/vavr/control/Try.java @@ -1133,9 +1133,10 @@ public final Either toEither() { } /** - * Converts this {@code Try} to an {@link Either}, converting the Throwable (if present) - * to a left value using the passed {@link Function} + * Converts this {@code Try} to an {@link Either}, converting the Throwable (in the failure case) + * to a left value using the passed {@link Function}. * + * @param left type of the resulting Either * @param throwableMapper A transformation from throwable to the left type of the new {@code Either} * @return A new {@code Either} */ @@ -1157,8 +1158,8 @@ public final Validation toValidation() { } /** - * Converts this {@code Try} to a {@link Validation}, converting the Throwable (if present) - * to another object using passed {@link Function}. + * Converts this {@code Try} to a {@link Validation}, converting the Throwable (in the failure case) + * to another object using the passed {@link Function}. * *
{@code
      * Validation = Try.of(() -> 1/0).toValidation(Throwable::getMessage));
diff --git a/src/test/java/io/vavr/control/TryTest.java b/src/test/java/io/vavr/control/TryTest.java
index 3cfdcbd4c..b4bb9da36 100644
--- a/src/test/java/io/vavr/control/TryTest.java
+++ b/src/test/java/io/vavr/control/TryTest.java
@@ -972,10 +972,8 @@ public void shouldConvertFailureToEitherLeftSupplier() {
     @Test
     public void shouldConvertFailureToEitherLeftMapper() {
         final Try failure = Try.failure(new RuntimeException("a certain value"));
-        final Either either = failure.toEither(t ->
-                t.getMessage().equals("a certain value")
-        );
-        assertThat(either).isEqualTo(Either.left(true));
+        final Either either = failure.toEither(Throwable::getMessage);
+        assertThat(either).isEqualTo(Either.left("a certain value"));
     }