Class CachedSupplier<T>
java.lang.Object
net.darkhax.pricklemc.common.api.util.CachedSupplier<T>
- Type Parameters:
T- The type cached by the supplier.
- All Implemented Interfaces:
Supplier<T>
An implementation of Supplier that will cache the result.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidInvokes the consumer with the cached value.static <T> CachedSupplier<T> Creates a cached supplier that will cache a value from the supplied delegate when queried.<X> CachedSupplier<X> cast()Performs an unsafe cast to the expected type.get()voidSafely attempts to invoke a consumer with the cached value.voidSafely attempts to invoke a consumer with the cached value.voidInvalidates the cached value.booleanisCached()Checks if this supplier has a cached value.static <T> CachedSupplier<T> singleton(T singleton) Creates a cached supplier that can only produce a single value.
-
Constructor Details
-
CachedSupplier
-
-
Method Details
-
get
-
invalidate
public void invalidate()Invalidates the cached value. This will result in a new value being cached the next typeget()is used. -
isCached
public boolean isCached()Checks if this supplier has a cached value. This is not a substitute for null checking.- Returns:
- Has the supplier cached a value.
-
ifCached
Safely attempts to invoke a consumer with the cached value. If a value has not been cached the consumer will not be invoked and a new value will not be cached. The consumer will still be invoked if the cached value is null.- Parameters:
consumer- The consumer to invoke if a value has been cached.
-
ifPresent
Safely attempts to invoke a consumer with the cached value. If a value has not been cached, or the cached value is null the consumer will not be invoked and a new value will not be cached.- Parameters:
consumer- The consumer to invoke if a cached value is present.
-
apply
Invokes the consumer with the cached value. This will cause a value to be cached if one has not been cached already.- Parameters:
consumer- The consumer to invoke.
-
cast
Performs an unsafe cast to the expected type. -
singleton
Creates a cached supplier that can only produce a single value.- Type Parameters:
T- The type of value held by the cache.- Parameters:
singleton- The only value for the cache to use.- Returns:
- A cached supplier that will only produce a single cached value.
-
cache
Creates a cached supplier that will cache a value from the supplied delegate when queried.- Type Parameters:
T- The type of value held by the cache.- Parameters:
delegate- The delegate supplier responsible for producing the cached value. This will only be accessed when the cached supplier is being accessed and has not been cached already.- Returns:
- A supplier that will cache a value from the supplied delegate supplier.
-