Interface NullableSupplier<T>

Type Parameters:
T - The type of value supplied by this supplier.
All Superinterfaces:
Supplier<T>
All Known Implementing Classes:
Memoizer, 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 NullableSupplier<T> extends Supplier<T>
A Supplier whose value may be null, with Optional-like null-safe query methods.

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 null-safe methods operate on that computed value.

Features:
  • Extends Supplier - can be used anywhere a Supplier is expected
  • Null-safe 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 NullableSupplier<String> supplier = NullableSupplier.of(() -> "value"); // Check if value is present if (supplier.isPresent()) { String value = supplier.get(); } // Map the value NullableSupplier<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> NullableSupplier<T>
    Creates an empty NullableSupplier that always returns null.
    filter(Predicate<? super T> predicate)
    If a value is present, and the value matches the given predicate, returns a NullableSupplier describing the value, otherwise returns an empty NullableSupplier.
    default <U> NullableSupplier<U>
    flatMap(Function<? super T,? extends NullableSupplier<? extends U>> mapper)
    If a value is present, returns the result of applying the given NullableSupplier-bearing mapping function to the value, otherwise returns an empty NullableSupplier.
    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> NullableSupplier<U>
    map(Function<? super T,? extends U> mapper)
    If a value is present, applies the provided mapping function to it and returns a NullableSupplier describing the result.
    static <T> NullableSupplier<T>
    of(Supplier<T> supplier)
    Creates a NullableSupplier from a Supplier.
    static <T> NullableSupplier<T>
    ofNullable(T value)
    Creates a NullableSupplier 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 NullableSupplier to an Optional.

    Methods inherited from interface java.util.function.Supplier

    get
  • Method Details

    • of

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

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

      static <T> NullableSupplier<T> empty()
      Creates an empty NullableSupplier that always returns null.
      Type Parameters:
      T - The value type.
      Returns:
      A new NullableSupplier 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> NullableSupplier<U> map(Function<? super T,? extends U> mapper)
      If a value is present, applies the provided mapping function to it and returns a NullableSupplier 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:
      A NullableSupplier describing the result of applying a mapping function to the value of this NullableSupplier, if a value is present, otherwise an empty NullableSupplier.
    • flatMap

      default <U> NullableSupplier<U> flatMap(Function<? super T,? extends NullableSupplier<? extends U>> mapper)
      If a value is present, returns the result of applying the given NullableSupplier-bearing mapping function to the value, otherwise returns an empty NullableSupplier.
      Type Parameters:
      U - The type parameter to the NullableSupplier 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 a NullableSupplier-bearing mapping function to the value of this NullableSupplier, if a value is present, otherwise an empty NullableSupplier.
    • filter

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