package editor.util;

import java.util.function.Supplier;

/**
 * This class "lazily" supplies a value by not evaluating it until the first time
 * it is requested.  Then that function's result is cached so it doesn't have to
 * be reevaluated again.
 *
 * @param <T> Type of object that is being lazily evaluated
 * @author Alec Roelke
 */
public class Lazy<T> implements Supplier<T>
{

    /**
     * Cached value.
     */
    private T value;

    /**
     * Supplier for the cached value.
     */
    private Supplier<T> supplier;

    /**
     * Create a new Lazy supplier.
     *
     * @param val Function supplying the value to lazily evaluate.
     */
    public Lazy(Supplier<T> val)
    {
        value = null;
        supplier = val;
    }

    /**
     * If the value has not been computed, compute it and then return it.  Otherwise,
     * just return it.
     *
     * @return The value computed by the function
     */
    @Override
    public T get()
    {
        if (value == null)
            value = supplier.get();
        return value;
    }
}

@@ -994,16 +994,26 @@
 = null)%0A
+        %7B%0A
         
@@ -1040,16 +1040,55 @@
 .get();%0A
+            supplier = null;%0A        %7D%0A
         

Discard supplier after it's used