Interface Lazy<T>
- Type Parameters:
T- type of wrapped value
- All Superinterfaces:
Supplier<T>
- All Known Implementing Classes:
Lazy.DoubleCheckedLazy,Lazy.LockingWeakLazy,Lazy.SimpleLazy,Lazy.SimpleWeakLazy,Lazy.ThreadLocalLazy
A wrapper for a value initialized once needed.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final classThread-safe (using double-checked locking) lazy getting its value from the specified value supplier.static final classThread-safe (using read-write-lock) weak lazy getting its value from the specified value supplier.static final classNon-thread-safe lazy getting its value from the specified value supplier.static final classNon-thread-safe (using double-checked locking) weak lazy getting its value from the specified value supplier.static classLazy which stores its values thread-locally. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Lazy<T>Creates a new lazy creating its value using the given supplier.static <T> Lazy<T>createThreadLocal(@NonNull Supplier<? extends T> valueSupplier) Creates a new thread-local lazy creating its value using the given supplier.static <T> Lazy<T>createThreadSafe(@NonNull Supplier<? extends T> valueSupplier) Creates a new thread-safe lazy creating its value using the given supplier.static <T> Lazy<@NotNull T>createWeak(@NonNull Supplier<@NotNull ? extends T> valueSupplier) Creates a new weak lazy creating its value using the given supplier.static <T> Lazy<T>createWeakThreadSafe(@NonNull Supplier<? extends T> valueSupplier) Creates a new weak thread-safe lazy creating its value using the given supplier.get()Gets the wrapped value initializing it once requested.Gets the wrapped value wrapped inOptionalif it is already initialized or anempty optionalotherwise.Gets the wrapped value wrapped inResultif it is already initialized or aResult.nullError()otherwise.Gets the wrapped value if it is already initialized ornullotherwise.booleanTests if the value of this lazy was initialized.
-
Method Details
-
get
T get()Gets the wrapped value initializing it once requested. -
isInitialized
boolean isInitialized()Tests if the value of this lazy was initialized.- Returns:
trueif this lazy's value was initialize andfalseotherwise
-
getInitializedOrNull
Gets the wrapped value if it is already initialized ornullotherwise.- Returns:
- wrapped value if it is already initialized or
nullotherwise - See Also:
- API note
- both
nullvalue and uninitialized state will be represented bynull
-
getAsOptional
Gets the wrapped value wrapped inOptionalif it is already initialized or anempty optionalotherwise.- Returns:
- wrapped value wrapped in
Optionalif it is already initialized or anempty optionalotherwise - See Also:
- API note
- both
nullvalue and uninitialized state will be represented byOptional.empty()
-
getAsResult
Gets the wrapped value wrapped inResultif it is already initialized or aResult.nullError()otherwise.- Returns:
- wrapped value wrapped in
Resultif it is already initialized orResult.nullError()otherwise
-
create
Creates a new lazy creating its value using the given supplier.- Type Parameters:
T- type of value wrapped- Parameters:
valueSupplier- supplier of the value to be called once needed- Returns:
- created lazy
- API note
- might be thread-unsafe
-
createThreadSafe
Creates a new thread-safe lazy creating its value using the given supplier.- Type Parameters:
T- type of value wrapped- Parameters:
valueSupplier- supplier of the value to be called once needed- Returns:
- created lazy
-
createWeak
static <T> Lazy<@NotNull T> createWeak(@NonNull @NonNull Supplier<@NotNull ? extends T> valueSupplier) Creates a new weak lazy creating its value using the given supplier.- Type Parameters:
T- type of value wrapped- Parameters:
valueSupplier- supplier of the value to be called once needed- Returns:
- created lazy
- API note
- might be thread-unsafe, weak lazy stores the value wrapped in weak reference and so it may be GCed and so the new one might be recomputed using the value supplier
-
createWeakThreadSafe
Creates a new weak thread-safe lazy creating its value using the given supplier.- Type Parameters:
T- type of value wrapped- Parameters:
valueSupplier- supplier of the value to be called once needed- Returns:
- created lazy
- API note
- weak lazy stores the value wrapped in weak reference and so it may be GCed and so the new one might be recomputed using the value supplier
-
createThreadLocal
Creates a new thread-local lazy creating its value using the given supplier.- Type Parameters:
T- type of value wrapped- Parameters:
valueSupplier- supplier of the value to be called once needed- Returns:
- created lazy
-