Package com.gengoai

Class Lazy<T>

  • Type Parameters:
    T - the type parameter
    All Implemented Interfaces:
    SerializableSupplier<T>, Serializable, Supplier<T>

    public final class Lazy<T>
    extends Object
    implements SerializableSupplier<T>

    Lazily create a value in a thread safe manner. Common usage is as follows:

     
        //Declare a lazy initialized object.
        Lazy<ExpensiveObject> lazy = new Lazy(() -> createExpensiveObject());
    
        //Now we will actually create the object.
        lazy.get().operation();
    
        //Successive calls will use the already created object.
        lazy.get().operation_2();
     
     
    Author:
    David B. Bracewell
    See Also:
    Serialized Form
    • Constructor Detail

      • Lazy

        public Lazy​(SerializableSupplier<? extends T> supplier)
        Instantiates a new Lazy created object.
        Parameters:
        supplier - the supplier used to create the object
    • Method Detail

      • get

        public T get()
        Specified by:
        get in interface Supplier<T>