public final class MoreSuppliers extends Object
Modifier and Type | Method and Description |
---|---|
static <T> java.util.function.Supplier<T> |
memoize(java.util.function.Supplier<T> delegate)
Returns a supplier which caches the instance retrieved during the first call to
get()
and returns that value on subsequent calls to get() . |
static <T,E extends Exception> |
memoize(ThrowingSupplier<T,E> delegate,
Class<E> exceptionClass)
Returns a
ThrowingSupplier that caches the result or the exception during the first
call to get() , and returns or throws on subsequent calls to get() |
static <T> java.util.function.Supplier<T> |
weakMemoize(java.util.function.Supplier<T> delegate) |
public static <T> java.util.function.Supplier<T> memoize(java.util.function.Supplier<T> delegate)
get()
and returns that value on subsequent calls to get()
.
Unlike Guava's Suppliers.memoize(com.google.common.base.Supplier)
, this version
removes the reference to the underlying Supplier once the value is computed. This frees up
memory used in lambda captures, at the cost of causing the supplier to be not Serializable.
public static <T,E extends Exception> ThrowingSupplier<T,E> memoize(ThrowingSupplier<T,E> delegate, Class<E> exceptionClass)
ThrowingSupplier
that caches the result or the exception during the first
call to get()
, and returns or throws on subsequent calls to get()
public static <T> java.util.function.Supplier<T> weakMemoize(java.util.function.Supplier<T> delegate)