Interface Lazy<T>

Type Parameters:
T - type of value
All Superinterfaces:
Supplier<T>

public interface Lazy<T> extends Supplier<T>
Provides a lazily-initialized value from a Supplier<T>.
  • Method Summary

    Modifier and Type
    Method
    Description
    default T
    get()
    Returns the value held by this lazy.
    boolean
    Indicates whether this lazy value has been initialized.
    static <T> Lazy<T>
    lazy(Supplier<T> supplier)
    Creates a strict lazy value using the provided Supplier.
    default <R> Lazy<R>
    map(Function<? super T,? extends R> function)
    Creates a new lazy value derived from this lazy value using the provided value mapping function.
    static <T> Lazy<T>
    pure(Supplier<T> supplier)
    Creates a pure lazy value using the provided Supplier to initialize the value.
    void
    set(T newValue)
    Sets this lazy value to the provided value.
    Returns the value held by this lazy.
    static <T> Lazy<T>
    value(T value)
    Creates a lazy value using the provided constant value.
    static <T> Lazy<T>
    weak(T value)
    Creates a lazy value using a weak reference to the provided value.
  • Method Details

    • value

      T value()
      Returns the value held by this lazy. This may cause the value to initialize if it hasn't been already.
    • get

      default T get()
      Returns the value held by this lazy. This may cause the value to initialize if it hasn't been already.
      Specified by:
      get in interface Supplier<T>
    • map

      default <R> Lazy<R> map(Function<? super T,? extends R> function)
      Creates a new lazy value derived from this lazy value using the provided value mapping function.
    • isInitialized

      boolean isInitialized()
      Indicates whether this lazy value has been initialized.
    • set

      void set(T newValue)
      Sets this lazy value to the provided value.
      Throws:
      UnsupportedOperationException - if this type of lazy value cannot be updated
    • lazy

      static <T> Lazy<T> lazy(Supplier<T> supplier)
      Creates a strict lazy value using the provided Supplier. The supplier is guaranteed to only be invoked by at most one thread, and all threads will see the same published value when this returns.
    • value

      static <T> Lazy<T> value(T value)
      Creates a lazy value using the provided constant value.
    • weak

      static <T> Lazy<T> weak(T value)
      Creates a lazy value using a weak reference to the provided value.
    • pure

      static <T> Lazy<T> pure(Supplier<T> supplier)
      Creates a pure lazy value using the provided Supplier to initialize the value. The supplier may be invoked more than once, and the return value should be a purely computed value as the result may be a different instance each time. This is useful for building cache tables and other pure computations.