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 toOptional
with the main difference that since it will never beOptional.empty()
, theget()
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 inOptional
and do NOT trigger initialization if it has not happened yet. Note thatLazy
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 withOptional
, thus resulting in aLazy
. It is recommended to use instances ofLazy
exclusively in conjunction with the final keyword, such that it is not allowed to swap the reference to a newLazy
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.
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Modifier and Type Method Description java.util.Optional<T>
filter(java.util.function.Predicate<? super T> predicate)
If a value is initialized, and the value matches the given predicate, return anOptional
describing the value, otherwise return an emptyOptional
.<U> java.util.Optional<U>
flatMap(java.util.function.Function<? super T,java.util.Optional<U>> mapper)
If a value is initialized, apply the providedOptional
-bearing mapping function to it, return that result, otherwise return an emptyOptional
.T
get()
void
ifPresent(java.util.function.Consumer<? super T> consumer)
boolean
isPresent()
<U> java.util.Optional<U>
map(java.util.function.Function<? super T,? extends U> mapper)
If a value is initialized, apply the provided mapping function to it, and if the result is non-null, return anOptional
describing the result.static <C> Lazy<C>
of(java.util.function.Supplier<C> supplier)
T
orElse(T other)
Return the value if initialized, otherwise returnother
.T
orElseGet(java.util.function.Supplier<? extends T> other)
Return the value if initialized, otherwise invokeother
and return the result of that invocation.<X extends java.lang.Throwable>
TorElseThrow(java.util.function.Supplier<? extends X> exceptionSupplier)
Return the contained value, if initialized, otherwise throw an exception to be created by the provided supplier.
-
-
-
Method Detail
-
of
static <C> Lazy<C> of(java.util.function.Supplier<C> supplier)
- Parameters:
supplier
- to initialize the wrapped value- Returns:
- an instance of
Lazy
which will execute the {@param supplier} if needed
-
get
T get()
- Returns:
- the wrapped value, with the side-effect of initializing it has not happened yet
-
ifPresent
void ifPresent(java.util.function.Consumer<? super T> consumer)
- Parameters:
consumer
- to pass the wrapped value to, only if it has already been initialized
-
isPresent
boolean isPresent()
- Returns:
true
if there is a value present (i.e. initialized), otherwisefalse
-
filter
java.util.Optional<T> filter(java.util.function.Predicate<? super T> predicate)
If a value is initialized, and the value matches the given predicate, return anOptional
describing the value, otherwise return an emptyOptional
.- Parameters:
predicate
- a predicate to apply to the value, if initialized- Returns:
- an
Optional
describing the value of thisOptional
if a value is present and the value matches the given predicate, otherwise an emptyOptional
- Throws:
java.lang.NullPointerException
- if the predicate is null
-
map
<U> java.util.Optional<U> map(java.util.function.Function<? super T,? extends U> mapper)
If a value is initialized, apply the provided mapping function to it, and if the result is non-null, return anOptional
describing 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
Optional
describing the result of applying a mapping function to the value of thisOptional
, if a value is initialized, otherwise an emptyOptional
- Throws:
java.lang.NullPointerException
- if the mapping function is null
-
flatMap
<U> java.util.Optional<U> flatMap(java.util.function.Function<? super T,java.util.Optional<U>> mapper)
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,flatMap
does not wrap it with an additionalOptional
.- Type Parameters:
U
- The type parameter to theOptional
returned 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:
java.lang.NullPointerException
- if the mapping function is null or returns a null result
-
orElse
T orElse(T other)
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
T orElseGet(java.util.function.Supplier<? extends T> other)
Return the value if initialized, otherwise invokeother
and return the result of that invocation.- Parameters:
other
- aSupplier
whose result is returned if no value is initialized- Returns:
- the value if initialized otherwise the result of
other.get()
- Throws:
java.lang.NullPointerException
- if value is not initialized andother
is null
-
orElseThrow
<X extends java.lang.Throwable> T orElseThrow(java.util.function.Supplier<? extends X> exceptionSupplier) throws X extends java.lang.Throwable
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 initializedjava.lang.NullPointerException
- if no value is initialized andexceptionSupplier
is nullX extends java.lang.Throwable
-
-