/** * Returns a new supplier which is the composition of the provided function and supplier. In other * words, the new supplier's value will be computed by retrieving the value from {@code first}, * and then applying {@code function} to that value. Note that the resulting supplier will not * call {@code first} or invoke {@code function} until it is called. */ public static <F, T> Supplier<T> compose(Function<? super F, T> function, Supplier<F> first) { Preconditions.checkNotNull(function); Preconditions.checkNotNull(first); return new SupplierComposition<F, T>(function, first); }
/** * Returns a supplier which caches the instance retrieved during the first call to {@code get()} * and returns that value on subsequent calls to {@code get()}. See: <a * href="http://en.wikipedia.org/wiki/Memoization">memoization</a> * * <p> * * <p>The returned supplier is thread-safe. The supplier's serialized form does not contain the * cached value, which will be recalculated when {@code get()} is called on the reserialized * instance. */ public static <T> Supplier<T> memoize(Supplier<T> delegate) { return new MemoizingSupplier<T>(Preconditions.checkNotNull(delegate)); }
/** * Returns a supplier whose {@code get()} method synchronizes on {@code delegate} before calling * it, making it thread-safe. */ public static <T> Supplier<T> synchronizedSupplier(Supplier<T> delegate) { return new ThreadSafeSupplier<T>(Preconditions.checkNotNull(delegate)); }