java.lang.Object
org.litebridge.commons.type.ConcurrentLazy<T>
- Type Parameters:
T- the type of the lazily initialized value
A thread-safe, lazily initialized value holder that allows resetting the value.
This class ensures that the value is initialized only once, using the provided Supplier,
and that subsequent calls to get() or getOrNull() return the cached value.
The value can be reset using reset(), which clears the cached value and allows it to be recomputed
on the next access.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionConcurrentLazy(Supplier<T> initializer) Constructs a newResettableLazywith the given initializer. -
Method Summary
Modifier and TypeMethodDescriptionget()The primary accessor.@Nullable TReturns the lazily initialized value.Returns the value, or throws aNoSuchElementExceptionif the value is null.getOrThrow(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 Tpeek()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
-
ConcurrentLazy
Constructs a newResettableLazywith the given initializer.- Parameters:
initializer- aSupplierthat provides the value when it is first needed- Throws:
NullPointerException- if the initializer isnull
-
-
Method Details
-
get
The primary accessor. Returns the value wrapped in anOptional.- Returns:
- the value wrapped in an
Optional
-
getOrThrow
Returns the value, or throws aNoSuchElementExceptionif the value is null.- 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:
exceptionSupplier- the supplier of the exception to be thrown- Returns:
- the value
- Throws:
X- if the value is null
-
getOrNull
Returns the lazily initialized value. If the value has not yet been initialized, it will be computed using the provided initializer in a thread-safe manner.- Returns:
- the initialized 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
-