package edu.rice.comp322; import java.util.function.Supplier; public class Lazy { private T contents; private Supplier supplier; private Lazy(Supplier supplier) { contents = null; this.supplier = supplier; } public static Lazy of(Supplier supplier) { return new Lazy(supplier); } /** * Get the value out of the memo. If it's already computed, then just return it. * If it's not, then compute it, then return it. */ public T get() { if (contents != null) { return contents; } if (supplier != null) { contents = supplier.get(); supplier = null; } return contents; } }