java.lang.Object
org.litebridge.commons.type.ConcurrentLazyFunction<T,R>
- Type Parameters:
T- the type of the input required to compute the valueR- the type of the lazily initialised value
A thread-safe, lazily initialized value holder for values of type
R that require an input of type T
to compute the value.
Allows resetting the cached value and ensures initialisation is performed only once in a concurrent environment.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionConcurrentLazyFunction(Function<T, R> initialiser) Constructs a newConcurrentLazyFunctionwith the given initialiser. -
Method Summary
Modifier and TypeMethodDescriptionThe primary accessor.@Nullable RReturns the lazily initialised value.getOrThrow(T input) Returns the value, or throws aNoSuchElementExceptionif the value is null.getOrThrow(T input, Supplier<X> exceptionSupplier) Returns the value, or throws an exception provided by the supplier if the value is null.final booleanReturnstrueif the value has already been initialised, orfalseotherwise.final Rpeek()Returns the current value without triggering initialisation.final voidreset()Resets the cached value.
-
Field Details
-
UNINITIALISED
Sentinel object indicating that the lazy value has not yet been initialised. -
value
The cached value, orUNINITIALISEDif not yet initialised.
-
-
Constructor Details
-
ConcurrentLazyFunction
Constructs a newConcurrentLazyFunctionwith the given initialiser.- Parameters:
initialiser- aFunctionthat computes the value from the given input when it is first needed
-
-
Method Details
-
get
The primary accessor. Returns the value wrapped in anOptional.- Parameters:
input- the input required to compute the value- Returns:
- the value wrapped in an
Optional
-
getOrThrow
Returns the value, or throws aNoSuchElementExceptionif the value is null.- Parameters:
input- the input required to compute the value- Returns:
- the value
- Throws:
NoSuchElementException- if the value is null
-
getOrThrow
Returns the value, or throws an exception provided by the supplier if the value is null.- Type Parameters:
X- the type of the exception to be thrown- Parameters:
input- the input required to compute the valueexceptionSupplier- the supplier of the exception to be thrown- Returns:
- the value
- Throws:
X- if the value is null
-
getOrNull
Returns the lazily initialised value. If the value has not yet been initialised, it will be computed using the provided initialiser in a thread-safe manner.- Parameters:
input- the input required to compute the value- Returns:
- the initialised value
-
peek
Returns the current value without triggering initialisation.Useful for logging or debugging without forcing a side effect.
- Returns:
- the current value, or
nullif not yet initialised
-
reset
public final void reset()Resets the cached value.This allows the cache to be recomputed on the next call to
get()orgetOrNull(). -
isInitialised
public final boolean isInitialised()Returnstrueif the value has already been initialised, orfalseotherwise.- Returns:
trueif the value is initialised;falseif it is still uninitialised
-