Class Memoizer<T>
- Type Parameters:
T- The type of value supplied.
- All Implemented Interfaces:
Supplier<T>,NullableSupplier<T>
- Direct Known Subclasses:
Setting
This class extends NullableSupplier 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 NullableSupplier.
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
NullableSupplier(isPresent(), isEmpty(), map(), orElse(), etc.).
See Also:
Suppliers.memoizer(Supplier)NullableSupplier- Base interface providing Optional-like methods
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncopy()Creates a copy of this memoizer.If a value is present, and the value matches the given predicate, returns a Memoizer describing the value, otherwise returns an empty Memoizer.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> Memoizer<U>If a value is present, applies the provided mapping function to it and returns a Memoizer 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.NullableSupplier
flatMap, ifPresent, ifPresentOrElse, isEmpty, isPresent, orElse, orElseGet, orElseThrow, toOptional
-
Constructor Details
-
Memoizer
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 memoizer Memoizer<String>supplier =new Memoizer<>(() ->"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 memoizer.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
Memoizerinstance with the same state as this memoizer.
-
map
If a value is present, applies the provided mapping function to it and returns a Memoizer describing the result.The returned Memoizer maintains its own cache, independent of this memoizer. Resetting the mapped memoizer does not affect this memoizer, and vice versa.
- Specified by:
mapin interfaceNullableSupplier<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 Memoizer describing the result of applying a mapping function to the value of this Memoizer, if a value is present, otherwise an empty Memoizer.
-
filter
If a value is present, and the value matches the given predicate, returns a Memoizer describing the value, otherwise returns an empty Memoizer.The returned Memoizer maintains its own cache, independent of this memoizer. Resetting the filtered memoizer does not affect this memoizer, and vice versa.
- Specified by:
filterin interfaceNullableSupplier<T>- Parameters:
predicate- A predicate to apply to the value, if present. Must not benull .- Returns:
- A Memoizer describing the value of this Memoizer if a value is present and the value matches the given predicate, otherwise an empty Memoizer.
-