Interface OptionalSupplier<T>

Type Parameters:
T - The type of value supplied by this supplier.
All Superinterfaces:
Supplier<T>
All Known Implementing Classes:
ResettableSupplier, Setting, StringSetting
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface OptionalSupplier<T> extends Supplier<T>
Combines the features of Supplier and Optional into a single class.

This interface extends Supplier and provides convenience methods for working with potentially null values, similar to Optional. The key difference is that this interface is lazy - the value is only computed when Supplier.get() is called, and the Optional-like methods operate on that computed value.

Features:
  • Extends Supplier - can be used anywhere a Supplier is expected
  • Optional-like API - provides isPresent(), isEmpty(), map(), orElse(), etc.
  • Lazy evaluation - value is only computed when get() is called
  • Null-safe - handles null values gracefully
Usage:

// Create from a supplier OptionalSupplier<String> supplier = OptionalSupplier.of(() -> "value"); // Check if value is present if (supplier.isPresent()) { String value = supplier.get(); } // Map the value OptionalSupplier<Integer> length = supplier.map(String::length); // Get value or default String result = supplier.orElse("default");

See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> OptionalSupplier<T>
    Creates an empty OptionalSupplier that always returns null.
    filter(Predicate<? super T> predicate)
    If a value is present, and the value matches the given predicate, returns an OptionalSupplier describing the value, otherwise returns an empty OptionalSupplier.
    default <U> OptionalSupplier<U>
    flatMap(Function<? super T,? extends OptionalSupplier<? extends U>> mapper)
    If a value is present, returns the result of applying the given OptionalSupplier-bearing mapping function to the value, otherwise returns an empty OptionalSupplier.
    default void
    ifPresent(Consumer<? super T> action)
    If a value is present, performs the given action with the value, otherwise does nothing.
    default void
    ifPresentOrElse(Consumer<? super T> action, Runnable emptyAction)
    If a value is present, performs the given action with the value, otherwise performs the given empty-based action.
    default boolean
    Returns true if the supplied value is null.
    default boolean
    Returns true if the supplied value is not null.
    default <U> OptionalSupplier<U>
    map(Function<? super T,? extends U> mapper)
    If a value is present, applies the provided mapping function to it and returns an OptionalSupplier describing the result.
    static <T> OptionalSupplier<T>
    of(Supplier<T> supplier)
    Creates an OptionalSupplier from a Supplier.
    static <T> OptionalSupplier<T>
    ofNullable(T value)
    Creates an OptionalSupplier that always returns the specified value.
    default T
    orElse(T other)
    If a value is present, returns the value, otherwise returns other.
    default T
    orElseGet(Supplier<? extends T> other)
    If a value is present, returns the value, otherwise returns the result produced by the supplying function.
    default <X extends Throwable>
    T
    orElseThrow(Supplier<? extends X> exceptionSupplier)
    If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function.
    default Optional<T>
    Converts this OptionalSupplier to an Optional.

    Methods inherited from interface java.util.function.Supplier

    get
  • Method Details

    • of

      static <T> OptionalSupplier<T> of(Supplier<T> supplier)
      Creates an OptionalSupplier from a Supplier.
      Type Parameters:
      T - The value type.
      Parameters:
      supplier - The supplier. Must not be null.
      Returns:
      A new OptionalSupplier instance.
    • ofNullable

      static <T> OptionalSupplier<T> ofNullable(T value)
      Creates an OptionalSupplier that always returns the specified value.
      Type Parameters:
      T - The value type.
      Parameters:
      value - The value to return. Can be null.
      Returns:
      A new OptionalSupplier instance.
    • empty

      static <T> OptionalSupplier<T> empty()
      Creates an empty OptionalSupplier that always returns null.
      Type Parameters:
      T - The value type.
      Returns:
      A new OptionalSupplier instance that always returns null.
    • isPresent

      default boolean isPresent()
      Returns true if the supplied value is not null.
      Returns:
      true if the supplied value is not null.
    • isEmpty

      default boolean isEmpty()
      Returns true if the supplied value is null.
      Returns:
      true if the supplied value is null.
    • map

      default <U> OptionalSupplier<U> map(Function<? super T,? extends U> mapper)
      If a value is present, applies the provided mapping function to it and returns an OptionalSupplier describing the result.
      Type Parameters:
      U - The type of the result of the mapping function.
      Parameters:
      mapper - A mapping function to apply to the value, if present. Must not be null.
      Returns:
      An OptionalSupplier describing the result of applying a mapping function to the value of this OptionalSupplier, if a value is present, otherwise an empty OptionalSupplier.
    • flatMap

      default <U> OptionalSupplier<U> flatMap(Function<? super T,? extends OptionalSupplier<? extends U>> mapper)
      If a value is present, returns the result of applying the given OptionalSupplier-bearing mapping function to the value, otherwise returns an empty OptionalSupplier.
      Type Parameters:
      U - The type parameter to the OptionalSupplier returned by the mapping function.
      Parameters:
      mapper - A mapping function to apply to the value, if present. Must not be null.
      Returns:
      The result of applying an OptionalSupplier-bearing mapping function to the value of this OptionalSupplier, if a value is present, otherwise an empty OptionalSupplier.
    • filter

      default OptionalSupplier<T> filter(Predicate<? super T> predicate)
      If a value is present, and the value matches the given predicate, returns an OptionalSupplier describing the value, otherwise returns an empty OptionalSupplier.
      Parameters:
      predicate - A predicate to apply to the value, if present. Must not be null.
      Returns:
      An OptionalSupplier describing the value of this OptionalSupplier if a value is present and the value matches the given predicate, otherwise an empty OptionalSupplier.
    • orElse

      default T orElse(T other)
      If a value is present, returns the value, otherwise returns other.
      Parameters:
      other - The value to be returned if there is no value present. Can be null.
      Returns:
      The value, if present, otherwise other.
    • orElseGet

      default T orElseGet(Supplier<? extends T> other)
      If a value is present, returns the value, otherwise returns the result produced by the supplying function.
      Parameters:
      other - A Supplier whose result is returned if no value is present. Must not be null.
      Returns:
      The value, if present, otherwise the result of other.get().
    • orElseThrow

      default <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X
      If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function.
      Type Parameters:
      X - Type of the exception to be thrown.
      Parameters:
      exceptionSupplier - The supplying function that produces an exception to be thrown. Must not be null.
      Returns:
      The value, if present.
      Throws:
      X - If no value is present.
    • ifPresent

      default void ifPresent(Consumer<? super T> action)
      If a value is present, performs the given action with the value, otherwise does nothing.
      Parameters:
      action - The action to be performed, if a value is present. Must not be null.
    • ifPresentOrElse

      default void ifPresentOrElse(Consumer<? super T> action, Runnable emptyAction)
      If a value is present, performs the given action with the value, otherwise performs the given empty-based action.
      Parameters:
      action - The action to be performed, if a value is present. Must not be null.
      emptyAction - The empty-based action to be performed, if no value is present. Must not be null.
    • toOptional

      default Optional<T> toOptional()
      Converts this OptionalSupplier to an Optional.
      Returns:
      An Optional containing the value if present, otherwise an empty Optional.