Package org.kiwiproject.base
Class Optionals
java.lang.Object
org.kiwiproject.base.Optionals
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> voidifPresentOrElseThrow(Optional<T> optional, Consumer<? super T> action, Supplier<? extends RuntimeException> exceptionSupplier) Takes an action if theoptionalis present, otherwise throws aRuntimeExceptionthat is created by the givenSupplier.static <T,E extends Exception>
voidifPresentOrElseThrowChecked(Optional<T> optional, Consumer<? super T> action, Supplier<E> checkedExceptionSupplier) Takes an action if theoptionalis present, otherwise throws a checked exception that is created by the givenSupplier.
-
Method Details
-
ifPresentOrElseThrow
public static <T> void ifPresentOrElseThrow(Optional<T> optional, Consumer<? super T> action, Supplier<? extends RuntimeException> exceptionSupplier) Takes an action if theoptionalis present, otherwise throws aRuntimeExceptionthat is created by the givenSupplier.Note that only
RuntimeExceptions can be thrown. UseifPresentOrElseThrowChecked(Optional, Consumer, Supplier)if you need to throw a checked exception.- Type Parameters:
T- the type parameter- Parameters:
optional- theOptionalto act uponaction- action to be performed if a value is present inoptionalexceptionSupplier- supplier for aRuntimeExceptionthat will be thrown ifoptionalis empty
-
ifPresentOrElseThrowChecked
public static <T,E extends Exception> void ifPresentOrElseThrowChecked(Optional<T> optional, Consumer<? super T> action, Supplier<E> checkedExceptionSupplier) throws E Takes an action if theoptionalis present, otherwise throws a checked exception that is created by the givenSupplier.- Type Parameters:
T- the type parameterE- the type of checked exception- Parameters:
optional- theOptionalto act uponaction- action to be performed if a value is present inoptionalcheckedExceptionSupplier- supplier for the checked exception that will be thrown ifoptionalis empty- Throws:
E- if theoptionalis empty
-