Skip to content

Commit 5a53ab5

Browse files
adamkopecEric Slep
andauthored
New feature: Try.toEither(Throwable -> L) (#3088)
Allows: ``` Either<String, Locale> either = Try.ofSupplier(() -> Locale.forLanguageTag("non-existing")) .toEither(exception -> "This was an unsupported language"); ``` This is a rework of #2724, which was initially suggested and developed by @eslep Co-authored-by: Eric Slep <eric.slep@zenjob.co>
1 parent 8c53086 commit 5a53ab5

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

vavr/src/main/java/io/vavr/control/Try.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,24 @@ default Either<Throwable, T> toEither() {
10611061
}
10621062
}
10631063

1064+
/**
1065+
* Converts this {@code Try} to an {@link Either}, converting the Throwable (in the failure case)
1066+
* to a left value using the passed {@link Function}.
1067+
*
1068+
* @param <L> left type of the resulting Either
1069+
* @param throwableMapper A transformation from throwable to the left type of the new {@code Either}
1070+
* @return A new {@code Either}
1071+
* @throws NullPointerException if the given {@code throwableMapper} is null
1072+
*/
1073+
default <L> Either<L, T> toEither(Function<? super Throwable, ? extends L> throwableMapper) {
1074+
Objects.requireNonNull(throwableMapper, "throwableMapper is null");
1075+
if (isFailure()) {
1076+
return Either.left(throwableMapper.apply(getCause()));
1077+
} else {
1078+
return Either.right(get());
1079+
}
1080+
}
1081+
10641082
/**
10651083
* Converts this {@code Try} to a {@link Validation}.
10661084
*
@@ -1071,8 +1089,8 @@ default Validation<Throwable, T> toValidation() {
10711089
}
10721090

10731091
/**
1074-
* Converts this {@code Try} to a {@link Validation}, converting the Throwable (if present)
1075-
* to another object using passed {@link Function}.
1092+
* Converts this {@code Try} to a {@link Validation}, converting the Throwable (in the failure case)
1093+
* to another object using the passed {@link Function}.
10761094
*
10771095
* <pre>{@code
10781096
* Validation<String, Integer> = Try.of(() -> 1/0).toValidation(Throwable::getMessage));

vavr/src/test/java/io/vavr/control/TryTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,41 @@ public void shouldConvertFailureToEither() {
10651065
assertThat(failure().toEither().isLeft()).isTrue();
10661066
}
10671067

1068+
@Test
1069+
public void shouldConvertFailureToEitherUsingMapper() {
1070+
Either<String, Object> converted = failure().toEither(
1071+
exception -> "error string"
1072+
);
1073+
assertThat(converted.isLeft()).isTrue();
1074+
assertThat(converted.getLeft()).isEqualTo("error string");
1075+
}
1076+
1077+
@Test
1078+
public void shouldConvertSuccessToEitherUsingMapper() {
1079+
Either<String, String> converted = success().toEither(
1080+
exception -> "another error"
1081+
);
1082+
assertThat(converted.isRight()).isTrue();
1083+
assertThat(converted.get()).isEqualTo(success().get());
1084+
}
1085+
1086+
@Test
1087+
public void shouldExecuteToEitherMapperLazilyOnlyWhenFailure() {
1088+
Either<String, String> converted = success().toEither(
1089+
exception -> {
1090+
throw new RuntimeException();
1091+
}
1092+
);
1093+
assertThat(converted.isRight()).isTrue();
1094+
assertThat(converted.get()).isEqualTo(success().get());
1095+
}
1096+
1097+
@Test
1098+
public void shouldNotAcceptNullAsThrowableMapperForToEither() {
1099+
Function<Throwable, String> mapper = null;
1100+
assertThrows(NullPointerException.class, () -> failure().toEither(mapper));
1101+
}
1102+
10681103
@Test
10691104
public void shouldConvertFailureToEitherLeft() {
10701105
assertThat(failure().toEither("test").isLeft()).isTrue();

0 commit comments

Comments
 (0)