Package com.linkedin.venice.utils.lazy
Interface Lazy<T>
- All Known Subinterfaces:
LazyResettable<C>
- All Known Implementing Classes:
LazyImpl,LazyResettableImpl,LazyResettableWithTearDown
public interface Lazy<T>
This utility provides lazy initialization for a wrapped object.
API-wise, it can be considered similar to
Optional with the main difference that
since it will never be Optional.empty(), the get() function will never
fail. Rather, get() will result in initializing the object if initialization has
not happened yet. All other functions mimic the behavior of their counterpart in
Optional and do NOT trigger initialization if it has not happened yet.
Note that Lazy does allow wrapping a null if that is what the lazy initialization
returns. For the use case of lazy initializing to something which may or may not exist,
the implementer should consider combining this class with Optional, thus
resulting in a Lazy<Optional<T>>.
It is recommended to use instances of Lazy exclusively in conjunction with the
final keyword, such that it is not allowed to swap the reference to a new Lazy
instance. Following the recommended usage allows making the assumption that there will
be no more than one initialization, and that the transition from uninitialized to
initialized is one-way and irreversible.
This interface promises to be threadsafe if multiple concurrent calls happen before the
first initialization finishes, and furthermore guarantees that the initialization
routine will happen at most once.-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionIf a value is initialized, and the value matches the given predicate, return anOptionaldescribing the value, otherwise return an emptyOptional.<U> Optional<U>If a value is initialized, apply the providedOptional-bearing mapping function to it, return that result, otherwise return an emptyOptional.get()voidboolean<U> Optional<U>If a value is initialized, apply the provided mapping function to it, and if the result is non-null, return anOptionaldescribing the result.static <C> Lazy<C>Return the value if initialized, otherwise returnother.Return the value if initialized, otherwise invokeotherand return the result of that invocation.orElseThrow(Supplier<? extends X> exceptionSupplier) Return the contained value, if initialized, otherwise throw an exception to be created by the provided supplier.
-
Field Details
-
FALSE
-
TRUE
-
-
Method Details
-
of
- Parameters:
supplier- to initialize the wrapped value- Returns:
- an instance of
Lazywhich will execute the if needed
-
get
T get()- Returns:
- the wrapped value, with the side-effect of initializing it has not happened yet
-
ifPresent
- Parameters:
consumer- to pass the wrapped value to, only if it has already been initialized
-
isPresent
boolean isPresent()- Returns:
trueif there is a value present (i.e. initialized), otherwisefalse
-
filter
If a value is initialized, and the value matches the given predicate, return anOptionaldescribing the value, otherwise return an emptyOptional.- Parameters:
predicate- a predicate to apply to the value, if initialized- Returns:
- an
Optionaldescribing the value of thisOptionalif a value is present and the value matches the given predicate, otherwise an emptyOptional - Throws:
NullPointerException- if the predicate is null
-
map
If a value is initialized, apply the provided mapping function to it, and if the result is non-null, return anOptionaldescribing the result. Otherwise return an emptyOptional.- Type Parameters:
U- The type of the result of the mapping function- Parameters:
mapper- a mapping function to apply to the value, if initialized- Returns:
- an
Optionaldescribing the result of applying a mapping function to the value of thisOptional, if a value is initialized, otherwise an emptyOptional - Throws:
NullPointerException- if the mapping function is null
-
flatMap
If a value is initialized, apply the providedOptional-bearing mapping function to it, return that result, otherwise return an emptyOptional. This method is similar tomap(Function), but the provided mapper is one whose result is already anOptional, and if invoked,flatMapdoes not wrap it with an additionalOptional.- Type Parameters:
U- The type parameter to theOptionalreturned by the mapping function- Parameters:
mapper- a mapping function to apply to the value, if initialized- Returns:
- the result of applying an
Optional-bearing mapping function to the value of thisOptional, if a value is initialized, otherwise an emptyOptional - Throws:
NullPointerException- if the mapping function is null or returns a null result
-
orElse
Return the value if initialized, otherwise returnother.- Parameters:
other- the value to be returned if there is no value initialized, may be null- Returns:
- the value, if initialized, otherwise
other
-
orElseGet
Return the value if initialized, otherwise invokeotherand return the result of that invocation.- Parameters:
other- aSupplierwhose result is returned if no value is initialized- Returns:
- the value if initialized otherwise the result of
other.get() - Throws:
NullPointerException- if value is not initialized andotheris null
-
orElseThrow
Return the contained value, if initialized, otherwise throw an exception to be created by the provided supplier.- Type Parameters:
X- Type of the exception to be thrown- Parameters:
exceptionSupplier- The supplier which will return the exception to be thrown- Returns:
- the initialized value
- Throws:
X- if there is no value initializedNullPointerException- if no value is initialized andexceptionSupplieris null
-