Class OptionalUtilities

Object
OptionalUtilities

public class OptionalUtilities extends Object
Additional utility functions for Optional and exceptions.

See OptionalFactory for utility functions to create Optionals.

Author:
Owen Feehan
  • Method Details

    • ifPresent

      public static <S, E extends Exception> void ifPresent(Optional<S> optional, CheckedConsumer<S,E> consumerFunction) throws E
      Like Optional.map(java.util.function.Function<? super T, ? extends U>) but tolerates an exception in the mapping function, which is immediately thrown.
      Type Parameters:
      S - optional-type
      E - exception that may be thrown during mapping.
      Parameters:
      optional - incoming optional.
      consumerFunction - the function that is called if an optional contains a value.
      Throws:
      E - an exception if consumerFunction throws it.
    • map

      public static <S, T, E extends Exception> Optional<T> map(Optional<S> optional, CheckedFunction<S,T,E> mapFunction) throws E
      Like Optional.map(java.util.function.Function<? super T, ? extends U>) but tolerates an exception in the mapping function, which is immediately thrown.
      Type Parameters:
      S - incoming optional-type for map
      T - outgoing optional-type for map
      E - exception that may be thrown during mapping
      Parameters:
      optional - incoming optional.
      mapFunction - the function that does the mapping from incoming to outgoing.
      Returns:
      the outgoing "mapped" optional.
      Throws:
      E - an exception if the mapping function throws it.
    • orElseGet

      public static <T, E extends Exception> T orElseGet(Optional<T> optional, CheckedSupplier<T,E> supplier) throws E
      Like Optional.orElseGet(java.util.function.Supplier<? extends T>) but tolerates an exception in the supplier function, which is immediately thrown.
      Type Parameters:
      T - optional-type
      E - exception that may be thrown during mapping
      Parameters:
      optional - incoming optional.
      supplier - supplies a value if the optional is empty.
      Returns:
      the outgoing optional.
      Throws:
      E - an exception if the supplier throws it.
    • orElseGetFlat

      @SafeVarargs public static <T, E extends Exception> Optional<T> orElseGetFlat(Optional<T> optional, CheckedSupplier<Optional<T>,E>... suppliers) throws E
      Type Parameters:
      T - optional-type
      E - exception that may be thrown during mapping
      Parameters:
      optional - incoming optional
      suppliers - tries each alternative Optional value successively (if optional is empty) until one is not-empty
      Returns:
      the outgoing optional
      Throws:
      E - an exception if the supplier throws it
    • flatMap

      public static <S, T, E extends Exception> Optional<T> flatMap(Optional<S> optional, CheckedFunction<S,Optional<T>,E> mapFunction) throws E
      Like Optional.flatMap(java.util.function.Function<? super T, ? extends java.util.Optional<? extends U>>) but tolerates an exception in the mapping function, which is immediately thrown.
      Type Parameters:
      S - incoming optional-type for map
      T - outgoing optional-type for map
      E - exception that may be thrown during mapping
      Parameters:
      optional - incoming optional.
      mapFunction - the function that does the mapping from incoming to outgoing.
      Returns:
      the outgoing "mapped" optional.
      Throws:
      E - an exception if the mapping function throws it.
    • mapBoth

      public static <T, U, V, E extends Exception> Optional<T> mapBoth(Optional<U> optional1, Optional<V> optional2, CheckedBiFunction<U,V,T,E> mapFunction) throws E
      Mapping only occurs if both Optionals are non-empty (equivalent to a logical and on the optionals)
      Type Parameters:
      T - outgoing optional-type for map
      U - first incoming optional-type for map
      V - second incoming optional-type for map
      E - an exception that may be thrown by an mapFunction.
      Parameters:
      optional1 - first incoming optional.
      optional2 - second incoming optional.
      mapFunction - the function that does the mapping from both incoming objects to outgoing.
      Returns:
      the outgoing "mapped" optional (empty() if either incoming optional is empty).
      Throws:
      E - if mapFunction throws it.
    • orFlat

      public static <T> Optional<T> orFlat(Iterable<Optional<T>> optionals)
      The first optional if it's present, or the second, or the third etc. using an Iterable.
      Type Parameters:
      T - type of optionals.
      Parameters:
      optionals - one or more optionals to combine together using a logical or operation.
      Returns:
      a new optional that is optionals[0] OR optionals[1] OR optionals[2] etc.
    • orFlat

      public static <T> Optional<T> orFlat(Stream<Optional<T>> optionals)
      The first optional if it's present, or the second, or the third etc. using an Stream.
      Type Parameters:
      T - type of optionals.
      Parameters:
      optionals - one or more optionals to combine together using a logical or operation.
      Returns:
      a new optional that is optionals[0] OR optionals[1] OR optionals[2] etc.
    • orFlatSupplier

      public static <T, E extends Exception> Optional<T> orFlatSupplier(Iterable<CheckedSupplier<Optional<T>,E>> optionals) throws E
      The first optional if it's present, or the second, or the third etc. using an iterable and a CheckedSupplier.
      Type Parameters:
      T - type of optionals.
      E - an exception that may be thrown by an optional.
      Parameters:
      optionals - one or more optionals to combine together using a logical or operation. * @return a new optional that is optionals[0] OR optionals[1] OR optionals[2] etc.
      Returns:
      a new optional that is optionals[0] OR optionals[1] OR optionals[2] etc.
      Throws:
      E - if any optional throws it.
    • orFlat

      @SafeVarargs public static <T> Optional<T> orFlat(Optional<T>... optional)
      Like orFlat(Iterable) but allows Optionals to be specified as variable-arguments.
      Type Parameters:
      T - type of optionals.
      Parameters:
      optional - one or more optionals to combine together using a logical or operation.
      Returns:
      a new optional that is optionals[0] OR optionals[1] OR optionals[2] etc.
    • orFlatSupplier

      @SafeVarargs public static <T, E extends Exception> Optional<T> orFlatSupplier(CheckedSupplier<Optional<T>,E>... optional) throws E
      Like orFlatSupplier(Iterable) but allows Optionals to be specified as variable-arguments.
      Type Parameters:
      T - type of optionals.
      E - an exception that may be thrown by an optional.
      Parameters:
      optional - one or more optionals to combine together using a logical or operation.
      Returns:
      a new optional that is optionals[0] OR optionals[1] OR optionals[2] etc.
      Throws:
      E - if any optional throws it.