Module xaos.core

Class Lazy<T>

  • Type Parameters:
    T - The type of the returned lazy evaluation.
    All Implemented Interfaces:
    Supplier<T>

    public class Lazy<T>
    extends Object
    implements Supplier<T>
    Lazy evaluation value container. set(Object) can be used to force a new value to the Lazy container. If called before get(), it will inhibit the lazy evaluation.

    This class is thread-safe.

    Usage: instead of creating a (instance) variable of type T, create if of type Lazy<T> passing the supplier for the lazy evaluation. Use the get() method on your (instance) variable the get the actual value.

    Note: don't rely in possible collateral effects from the provided Supplier. If set(Object) is called before get(), then the lazy evaluation will never occur.

    Author:
    claudio.rosati@esss.se
    See Also:
    Lazy Assignment in Java
    • Constructor Detail

      • Lazy

        protected Lazy​(Supplier<T> supplier)
    • Method Detail

      • of

        public static <T> Lazy<T> of​(Supplier<T> supplier)
        Creates and returns a new instance of Lazy to be used for a late evaluation of the given Supplier.
        Type Parameters:
        T - The type of the returned lazy evaluation.
        Parameters:
        supplier - The Supplier of the evaluated value.
        Returns:
        A new instance of Lazy to be used for a late evaluation of the given Supplier.
      • get

        public T get()
        Returns the current value of this lazy container. If set(Object) was not yet called, then the first time get() is executed it will perform the lazy evaluation of the initially provided Supplier.
        Specified by:
        get in interface Supplier<T>
        Returns:
        The current value of this lazy container.
      • set

        public void set​(T newValue)
        Sets a new value for this value container. If this method is called before the get() one, then the lazy evaluation of the initially provided Supplier will be inhibited.
        Parameters:
        newValue - The new value for this value container.