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 optional() or orNull() 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.
-
Constructor Summary
ConstructorsConstructorDescriptionConcurrentLazy(Supplier<T> initializer) Constructs a newResettableLazywith the given initializer. -
Method Summary
Modifier and TypeMethodDescriptionbooleanReturnstrueif the value has already been initialised, orfalseotherwise.optional()The primary accessor.@Nullable TorNull()Returns the lazily initialized value.orThrow()Returns the value, or throws aNoSuchElementExceptionif the value is null.Returns the value, or throws an exception provided by the supplier if the value is null.@Nullable Tpeek()Returns the current value without triggering initialization.voidreset()Resets the cached value, allowing it to be recomputed on the next call tooptional()ororNull().
-
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
-
optional
The primary accessor. Returns the value wrapped in anOptional.- Returns:
- the value wrapped in an
Optional
-
orThrow
Returns the value, or throws aNoSuchElementExceptionif the value is null.- Returns:
- the value
- Throws:
NoSuchElementException- if the value is null
-
orThrow
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
-
orNull
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 initialization. Useful for logging or debugging where you don't want to force a side-effect.- Returns:
- the current value, or
nullif not yet initialized
-
reset
public void reset()Resets the cached value, allowing it to be recomputed on the next call tooptional()ororNull(). This method is thread-safe. -
isInitialised
public boolean isInitialised()Returnstrueif the value has already been initialised, orfalseotherwise.- Returns:
trueif the value is initialised;falseif it is still uninitialised
-