Class Optionals


  • public class Optionals
    extends Object
    Utility methods for instances of Optional, and which are not already present in Optional.
    • Constructor Detail

      • Optionals

        public Optionals()
    • Method Detail

      • ifPresentOrElseThrowChecked

        public static <T,​E extends Exception> void ifPresentOrElseThrowChecked​(Optional<T> optional,
                                                                                     Consumer<? super T> action,
                                                                                     Supplier<E> checkedExceptionSupplier)
                                                                              throws E extends Exception
        Takes an action if the optional is present, otherwise throws a checked exception that is created by the given Supplier.
        Type Parameters:
        T - the type parameter
        E - the type of checked exception
        Parameters:
        optional - the Optional to act upon
        action - action to be performed if a value is present in optional
        checkedExceptionSupplier - supplier for the checked exception that will be thrown if optional is empty
        Throws:
        E - if the optional is empty
        E extends Exception