Class Suppliers
- java.lang.Object
- 
- com.google.common.base.Suppliers
 
- 
 @GwtCompatible(emulated=true) public final class Suppliers extends java.lang.Object Useful suppliers.All methods return serializable suppliers as long as they're given serializable parameters. - Since:
- 2.0
- Author:
- Laurence Gonsalves, Harry Heymann
 
- 
- 
Method SummaryAll Methods Static Methods Concrete Methods Modifier and Type Method Description static <F extends @Nullable java.lang.Object,T extends @Nullable java.lang.Object>
 Supplier<T>compose(Function<? super F,T> function, Supplier<F> supplier)Returns a new supplier which is the composition of the provided function and supplier.static <T extends @Nullable java.lang.Object>
 Supplier<T>memoize(Supplier<T> delegate)Returns a supplier which caches the instance retrieved during the first call toget()and returns that value on subsequent calls toget().static <T extends @Nullable java.lang.Object>
 Supplier<T>memoizeWithExpiration(Supplier<T> delegate, long duration, java.util.concurrent.TimeUnit unit)Returns a supplier that caches the instance supplied by the delegate and removes the cached value after the specified time has passed.static <T extends @Nullable java.lang.Object>
 Supplier<T>memoizeWithExpiration(Supplier<T> delegate, java.time.Duration duration)Returns a supplier that caches the instance supplied by the delegate and removes the cached value after the specified time has passed.static <T extends @Nullable java.lang.Object>
 Supplier<T>ofInstance(T instance)Returns a supplier that always suppliesinstance.static <T extends @Nullable java.lang.Object>
 Function<Supplier<T>,T>supplierFunction()Returns a function that accepts a supplier and returns the result of invokingSupplier.get()on that supplier.static <T extends @Nullable java.lang.Object>
 Supplier<T>synchronizedSupplier(Supplier<T> delegate)Returns a supplier whoseget()method synchronizes ondelegatebefore calling it, making it thread-safe.
 
- 
- 
- 
Method Detail- 
composepublic static <F extends @Nullable java.lang.Object,T extends @Nullable java.lang.Object> Supplier<T> compose(Function<? super F,T> function, Supplier<F> supplier) 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 fromsupplier, and then applyingfunctionto that value. Note that the resulting supplier will not callsupplieror invokefunctionuntil it is called.
 - 
memoizepublic static <T extends @Nullable java.lang.Object> Supplier<T> memoize(Supplier<T> delegate) Returns a supplier which caches the instance retrieved during the first call toget()and returns that value on subsequent calls toget(). See: memoizationThe returned supplier is thread-safe. The delegate's get()method will be invoked at most once unless the underlyingget()throws an exception. The supplier's serialized form does not contain the cached value, which will be recalculated whenget()is called on the deserialized instance.When the underlying delegate throws an exception then this memoizing supplier will keep delegating calls until it returns valid data. If delegateis an instance created by an earlier call tomemoize, it is returned directly.
 - 
memoizeWithExpirationpublic static <T extends @Nullable java.lang.Object> Supplier<T> memoizeWithExpiration(Supplier<T> delegate, long duration, java.util.concurrent.TimeUnit unit) Returns a supplier that caches the instance supplied by the delegate and removes the cached value after the specified time has passed. Subsequent calls toget()return the cached value if the expiration time has not passed. After the expiration time, a new value is retrieved, cached, and returned. See: memoizationThe returned supplier is thread-safe. The supplier's serialized form does not contain the cached value, which will be recalculated when get()is called on the reserialized instance. The actual memoization does not happen when the underlying delegate throws an exception.When the underlying delegate throws an exception then this memoizing supplier will keep delegating calls until it returns valid data. - Parameters:
- duration- the length of time after a value is created that it should stop being returned by subsequent- get()calls
- unit- the unit that- durationis expressed in
- Throws:
- java.lang.IllegalArgumentException- if- durationis not positive
- Since:
- 2.0
 
 - 
memoizeWithExpiration@GwtIncompatible public static <T extends @Nullable java.lang.Object> Supplier<T> memoizeWithExpiration(Supplier<T> delegate, java.time.Duration duration) Returns a supplier that caches the instance supplied by the delegate and removes the cached value after the specified time has passed. Subsequent calls toget()return the cached value if the expiration time has not passed. After the expiration time, a new value is retrieved, cached, and returned. See: memoizationThe returned supplier is thread-safe. The supplier's serialized form does not contain the cached value, which will be recalculated when get()is called on the reserialized instance. The actual memoization does not happen when the underlying delegate throws an exception.When the underlying delegate throws an exception then this memoizing supplier will keep delegating calls until it returns valid data. - Parameters:
- duration- the length of time after a value is created that it should stop being returned by subsequent- get()calls
- Throws:
- java.lang.IllegalArgumentException- if- durationis not positive
- Since:
- 33.1.0
 
 - 
ofInstancepublic static <T extends @Nullable java.lang.Object> Supplier<T> ofInstance(T instance) Returns a supplier that always suppliesinstance.
 - 
synchronizedSupplierpublic static <T extends @Nullable java.lang.Object> Supplier<T> synchronizedSupplier(Supplier<T> delegate) Returns a supplier whoseget()method synchronizes ondelegatebefore calling it, making it thread-safe.
 - 
supplierFunctionpublic static <T extends @Nullable java.lang.Object> Function<Supplier<T>,T> supplierFunction() Returns a function that accepts a supplier and returns the result of invokingSupplier.get()on that supplier.Java 8+ users: use the method reference Supplier::getinstead.- Since:
- 8.0
 
 
- 
 
-