Class SingletonSupplier<T>

java.lang.Object
org.springframework.util.function.SingletonSupplier<T>
Type Parameters:
T - the type of results supplied by this supplier
All Implemented Interfaces:
Supplier<T>

public class SingletonSupplier<T> extends Object implements Supplier<T>
A Supplier decorator that caches a singleton result and makes it available from get() (nullable) and obtain() (null-safe).

A SingletonSupplier can be constructed via of factory methods or via constructors that provide a default supplier as a fallback. This is particularly useful for method reference suppliers, falling back to a default supplier for a method that returned null and caching the result.

Since:
5.1
Author:
Juergen Hoeller, Yanming Zhou
  • Constructor Summary

    Constructors
    Constructor
    Description
    SingletonSupplier(Supplier<? extends T> instanceSupplier, Supplier<? extends T> defaultSupplier)
    Build a SingletonSupplier with the given instance supplier and a default supplier for the case when the instance is null.
    SingletonSupplier(T instance, Supplier<? extends T> defaultSupplier)
    Build a SingletonSupplier with the given singleton instance and a default supplier for the case when the instance is null.
  • Method Summary

    Modifier and Type
    Method
    Description
    get()
    Get the shared singleton instance for this supplier.
    Obtain the shared singleton instance for this supplier.
    static <T> SingletonSupplier<T>
    of(Supplier<T> supplier)
    Build a SingletonSupplier with the given supplier.
    static <T> SingletonSupplier<T>
    of(T instance)
    Build a SingletonSupplier with the given singleton instance.
    static <T> SingletonSupplier<T>
    ofNullable(Supplier<T> supplier)
    Build a SingletonSupplier with the given supplier.
    static <T> SingletonSupplier<T>
    ofNullable(T instance)
    Build a SingletonSupplier with the given singleton instance.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • SingletonSupplier

      public SingletonSupplier(@Nullable T instance, Supplier<? extends T> defaultSupplier)
      Build a SingletonSupplier with the given singleton instance and a default supplier for the case when the instance is null.
      Parameters:
      instance - the singleton instance (potentially null)
      defaultSupplier - the default supplier as a fallback
    • SingletonSupplier

      public SingletonSupplier(@Nullable Supplier<? extends T> instanceSupplier, Supplier<? extends T> defaultSupplier)
      Build a SingletonSupplier with the given instance supplier and a default supplier for the case when the instance is null.
      Parameters:
      instanceSupplier - the immediate instance supplier
      defaultSupplier - the default supplier as a fallback
  • Method Details

    • get

      @Nullable public T get()
      Get the shared singleton instance for this supplier.
      Specified by:
      get in interface Supplier<T>
      Returns:
      the singleton instance (or null if none)
    • obtain

      public T obtain()
      Obtain the shared singleton instance for this supplier.
      Returns:
      the singleton instance (never null)
      Throws:
      IllegalStateException - in case of no instance
    • of

      public static <T> SingletonSupplier<T> of(T instance)
      Build a SingletonSupplier with the given singleton instance.
      Parameters:
      instance - the singleton instance (never null)
      Returns:
      the singleton supplier (never null)
    • ofNullable

      @Nullable public static <T> SingletonSupplier<T> ofNullable(@Nullable T instance)
      Build a SingletonSupplier with the given singleton instance.
      Parameters:
      instance - the singleton instance (potentially null)
      Returns:
      the singleton supplier, or null if the instance was null
    • of

      public static <T> SingletonSupplier<T> of(Supplier<T> supplier)
      Build a SingletonSupplier with the given supplier.
      Parameters:
      supplier - the instance supplier (never null)
      Returns:
      the singleton supplier (never null)
    • ofNullable

      @Nullable public static <T> SingletonSupplier<T> ofNullable(@Nullable Supplier<T> supplier)
      Build a SingletonSupplier with the given supplier.
      Parameters:
      supplier - the instance supplier (potentially null)
      Returns:
      the singleton supplier, or null if the instance supplier was null