Class OptionalFactory

Object
OptionalFactory

public class OptionalFactory extends Object
Utility functions to create Optional from flags.

See OptionalUtilities for utility functions for already-instantiated Optionals.

Author:
Owen Feehan
  • Method Details

    • create

      public static <T> Optional<T> create(boolean flag, T value)
      Creates only if a boolean flag is true, otherwise returns Optional.empty().
      Type Parameters:
      T - type of optional
      Parameters:
      flag - boolean flag.
      value - a value used only if flag is true.
      Returns:
      an optional that is defined or empty depending on the flag.
    • create

      public static <T> Optional<T> create(boolean flag, Supplier<T> supplier)
      Creates only if a boolean flag is true, otherwise returns Optional.empty().
      Type Parameters:
      T - type of optional
      Parameters:
      flag - boolean flag.
      supplier - a function to create a value T only called if flag is true.
      Returns:
      an optional that is defined or empty depending on the flag.
    • createChecked

      public static <T, E extends Exception> Optional<T> createChecked(boolean flag, CheckedSupplier<T,E> supplier) throws E
      Like create(boolean, T) but accepts a supplier that throws a checked/exception.
      Type Parameters:
      T - type of optional
      E - the checked exception
      Parameters:
      flag - boolean flag
      supplier - a function to create a value T only called if flag is true
      Returns:
      an optional that is defined or empty depending on the flag
      Throws:
      E - if supplier throws it
    • create

      public static Optional<String> create(String string)
      Creates Optional.empty() for an empty string, or otherwise Optional.of().
      Parameters:
      string - the string (possibly empty or null).
      Returns:
      the optional.
    • createFlat

      public static <T> Optional<T> createFlat(boolean flag, Supplier<Optional<T>> valueIfFlagTrue)
      Creates an Optional from a boolean flag and a supplier that returns an Optional.
      Type Parameters:
      T - type in optional.
      Parameters:
      flag - iff true an populated optional is returned, otherwise Optional.empty().
      valueIfFlagTrue - used to generate a positive value.
      Returns:
      a filled or empty optional depending on flag.