Class Lazy<T>

java.lang.Object
atlantafx.base.util.Lazy<T>
All Implemented Interfaces:
Supplier<T>

public class Lazy<T> extends Object implements Supplier<T>
A wrapper around a Supplier that defers and caches a single computation.

The wrapped value is computed on the first call to get() and reused on every subsequent call.

If the supplier produces null, the result is indistinguishable from "not yet computed" — empty() will keep returning true, and get() will invoke the supplier again on every call.

Instances are compared by identity. Do not override hashCode() or equals(), as two instances wrapping equivalent suppliers are not necessarily interchangeable, particularly before initialization.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final Supplier<T>
     
    protected @Nullable T
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Lazy(Supplier<T> supplier)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Checks whether the wrapped value is absent.
    get()
    Returns the wrapped value, computing it via the supplier on first access.
    Returns the string representation of the currently cached value, without triggering initialization.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • supplier

      protected final Supplier<T> supplier
    • value

      protected @Nullable T value
  • Constructor Details

  • Method Details

    • get

      public T get()
      Returns the wrapped value, computing it via the supplier on first access.
      Specified by:
      get in interface Supplier<T>
      Returns:
      the (possibly newly computed) value
    • empty

      public boolean empty()
      Checks whether the wrapped value is absent.

      Returns true both when get() has never been called and when the supplier has returned null — the two cases cannot be distinguished.

      Returns:
      true if there is currently no cached value, false otherwise
    • toString

      public String toString()
      Returns the string representation of the currently cached value, without triggering initialization.
      Overrides:
      toString in class Object
      Returns:
      "null" if the value is currently absent (see empty()), otherwise String.valueOf of the cached value