Class ResettableSupplier<T>
- Type Parameters:
T- The type of value supplied.
- All Implemented Interfaces:
Supplier<T>,OptionalSupplier<T>
- Direct Known Subclasses:
Setting
This class extends OptionalSupplier to provide both standard Supplier.get() functionality
and a reset() method to clear the cache, forcing recomputation on the next call to get().
It also inherits all Optional-like convenience methods from OptionalSupplier.
Usage:
Thread Safety:
This class is thread-safe for both get() and reset() operations.
If multiple threads call get() simultaneously after a reset, the supplier may be invoked
multiple times, but only one result will be cached.
Notes:
- The supplier may be called multiple times if threads race, but only one result is cached.
- The cached value can be
null if the supplier returnsnull . - After reset, the next get() will recompute the value.
- This is particularly useful for testing when configuration changes and cached values need to be recalculated.
- Inherits all Optional-like methods from
OptionalSupplier(isPresent(), isEmpty(), map(), orElse(), etc.).
See Also:
Utils.memr(Supplier)OptionalSupplier- Base interface providing Optional-like methods
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncopy()Creates a copy of this supplier.If a value is present, and the value matches the given predicate, returns a ResettableSupplier describing the value, otherwise returns an empty ResettableSupplier.get()Returns the cached value if present, otherwise computes it using the underlying supplier.booleanReturnstrue if the supplier has not yet been called (cache is empty).<U> ResettableSupplier<U>If a value is present, applies the provided mapping function to it and returns a ResettableSupplier describing the result.voidreset()Clears the cached value, forcing the next call toget()to recompute the value.voidSets the cached value directly without invoking the underlying supplier.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.apache.juneau.commons.function.OptionalSupplier
flatMap, ifPresent, ifPresentOrElse, isEmpty, isPresent, orElse, orElseGet, orElseThrow, toOptional
-
Constructor Details
-
ResettableSupplier
Constructor.- Parameters:
supplier- The underlying supplier to call when computing values. Must not benull .
-
-
Method Details
-
get
Returns the cached value if present, otherwise computes it using the underlying supplier. -
reset
Clears the cached value, forcing the next call toget()to recompute the value.This method is thread-safe and can be called from multiple threads. After reset, the next get() call will invoke the underlying supplier to compute a fresh value.
-
set
Sets the cached value directly without invoking the underlying supplier.This method allows you to override the cached value, bypassing the supplier. Subsequent calls to
get()will return the set value untilreset()is called or the value is set again.This method is thread-safe and is particularly useful for testing when you need to inject a specific value without invoking the supplier.
Example:
// Create a supplier ResettableSupplier<String>supplier =new ResettableSupplier<>(() ->"computed" );// Set a value directly without invoking the supplier supplier .set ("injected" );// get() returns the injected value assertEquals("injected" ,supplier .get ());// Reset clears the cache, next get() will invoke the supplier supplier .reset (); assertEquals("computed" ,supplier .get ());- Parameters:
value- The value to cache. Can benull .
-
isSupplied
Returnstrue if the supplier has not yet been called (cache is empty).This method checks whether the cached value is
null , which indicates thatget()has not yet been called, or the cache was reset.- Returns:
true if the supplier has not been called yet,false if a value has been cached.
-
copy
Creates a copy of this supplier.If a value has been cached, the copy will use a supplier that returns that cached value. If no value has been cached yet, the copy will use the original supplier.
- Returns:
- A new
ResettableSupplierinstance with the same state as this supplier.
-
map
If a value is present, applies the provided mapping function to it and returns a ResettableSupplier describing the result.The returned ResettableSupplier maintains its own cache, independent of this supplier. Resetting the mapped supplier does not affect this supplier, and vice versa.
- Specified by:
mapin interfaceOptionalSupplier<T>- Type Parameters:
U- The type of the result of the mapping function.- Parameters:
mapper- A mapping function to apply to the value, if present. Must not benull .- Returns:
- A ResettableSupplier describing the result of applying a mapping function to the value of this ResettableSupplier, if a value is present, otherwise an empty ResettableSupplier.
-
filter
If a value is present, and the value matches the given predicate, returns a ResettableSupplier describing the value, otherwise returns an empty ResettableSupplier.The returned ResettableSupplier maintains its own cache, independent of this supplier. Resetting the filtered supplier does not affect this supplier, and vice versa.
- Specified by:
filterin interfaceOptionalSupplier<T>- Parameters:
predicate- A predicate to apply to the value, if present. Must not benull .- Returns:
- A ResettableSupplier describing the value of this ResettableSupplier if a value is present and the value matches the given predicate, otherwise an empty ResettableSupplier.
-