Package org.elasticsearch.common.util
Class LazyInitializable<T,E extends java.lang.Exception>
- java.lang.Object
-
- org.elasticsearch.common.util.LazyInitializable<T,E>
-
public final class LazyInitializable<T,E extends java.lang.Exception> extends java.lang.Object
Encapsulates aCheckedSupplier
which is lazily invoked once on the first call to#getOrCompute()
. The value which thesupplier
returns is memorized and will be served until#reset()
is called. Each value returned by#getOrCompute()
, newly minted or cached, will be passed to theonGet
Consumer
. On#reset()
the value will be passed to theonReset
Consumer
and the next#getOrCompute()
will regenerate the value.
-
-
Constructor Summary
Constructors Constructor Description LazyInitializable(CheckedSupplier<T,E> supplier)
Creates the simple LazyInitializable instance.LazyInitializable(CheckedSupplier<T,E> supplier, java.util.function.Consumer<T> onGet, java.util.function.Consumer<T> onReset)
Creates the complete LazyInitializable instance.
-
Method Summary
Modifier and Type Method Description T
getOrCompute()
Returns a value that was created bysupplier
.void
reset()
Clears the value, if it has been previously created by calling#getOrCompute()
.
-
-
-
Constructor Detail
-
LazyInitializable
public LazyInitializable(CheckedSupplier<T,E> supplier)
Creates the simple LazyInitializable instance.- Parameters:
supplier
- TheCheckedSupplier
to generate values which will be served on#getOrCompute()
invocations.
-
LazyInitializable
public LazyInitializable(CheckedSupplier<T,E> supplier, java.util.function.Consumer<T> onGet, java.util.function.Consumer<T> onReset)
Creates the complete LazyInitializable instance.- Parameters:
supplier
- TheCheckedSupplier
to generate values which will be served on#getOrCompute()
invocations.onGet
- AConsumer
which is called on each value, newly forged or stale, that is returned by#getOrCompute()
onReset
- AConsumer
which is invoked on the value that will be erased when calling#reset()
-
-
Method Detail
-
getOrCompute
public T getOrCompute() throws E extends java.lang.Exception
Returns a value that was created bysupplier
. The value might have been previously created, if not it will be created now, thread safe of course.- Throws:
E extends java.lang.Exception
-
reset
public void reset()
Clears the value, if it has been previously created by calling#getOrCompute()
. TheonReset
will be called on this value. The next call to#getOrCompute()
will recreate the value.
-
-