Skip to content

Commit de2ca4c

Browse files
author
Julien Debon
authored
Add Validation.getOrElseThrow(Function<E, X>) (#2695)
1 parent c3e4db0 commit de2ca4c

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,33 @@ public final <U> U fold(Function<? super E, ? extends U> ifInvalid, Function<? s
657657
return isValid() ? ifValid.apply(get()) : ifInvalid.apply(getError());
658658
}
659659

660+
/**
661+
* Gets the Valid value or throws, if the projected Validation is an Invalid.
662+
*
663+
* <pre>{@code
664+
* Function<String, RuntimeException> exceptionFunction = RuntimeException::new;
665+
* // prints "42"
666+
* System.out.println(Validation.<String, Integer>valid(42).getOrElseThrow(exceptionFunction));
667+
*
668+
* // throws RuntimeException("no value found")
669+
* Validation.invalid("no value found").getOrElseThrow(exceptionFunction);
670+
* }</pre>
671+
*
672+
* @param <X> a throwable type
673+
* @param exceptionFunction a function which creates an exception based on an Invalid value
674+
* @return the valid value, if the underlying Valdition is a Valid or else throws the exception provided by
675+
* {@code exceptionFunction} by applying the Invalid value.
676+
* @throws X if the projected Validation is an Invalid
677+
*/
678+
public final <X extends Throwable> T getOrElseThrow(Function<? super E, X> exceptionFunction) throws X {
679+
Objects.requireNonNull(exceptionFunction, "exceptionFunction is null");
680+
if (isValid()) {
681+
return get();
682+
} else {
683+
throw exceptionFunction.apply(getError());
684+
}
685+
}
686+
660687
/**
661688
* Flip the valid/invalid values for this Validation. If this is a Valid&lt;E,T&gt;, returns Invalid&lt;T,E&gt;.
662689
* Or if this is an Invalid&lt;E,T&gt;, return a Valid&lt;T,E&gt;.

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,30 @@ public void shouldConvertFailureToU() {
300300
assertThat(result).isEqualTo(3);
301301
}
302302

303+
304+
// getOrElseThrow(Function)
305+
306+
@Test
307+
public void shouldReturnValidWhenGetOrElseThrowWithFunctionOnValid() {
308+
final Integer actual = Validation.<String, Integer> valid(1).getOrElseThrow(s -> new RuntimeException(s));
309+
assertThat(actual).isEqualTo(1);
310+
}
311+
312+
@Test(expected = RuntimeException.class)
313+
public void shouldThrowWhenGetOrElseThrowWithFunctionOnInvalid() {
314+
Validation.<String, Integer> invalid("some error").getOrElseThrow(s -> new RuntimeException(s));
315+
}
316+
317+
@Test(expected = NullPointerException.class)
318+
public void shouldThrowNullPointerExceptionWhenGetOrElseThrowNullWithFunctionOnValid() {
319+
Validation.<String, Integer> valid(1).getOrElseThrow((Function<? super String, RuntimeException>) null);
320+
}
321+
322+
@Test(expected = NullPointerException.class)
323+
public void shouldThrowNullPointerExceptionWhenGetOrElseThrowNullWithFunctionOnInvalid() {
324+
Validation.<String, Integer> invalid("some error").getOrElseThrow((Function<? super String, RuntimeException>) null);
325+
}
326+
303327
// -- swap
304328

305329
@Test

0 commit comments

Comments
 (0)