Package org.mule.metadata.ast.internal
Class MuleLazyValue<T>
- java.lang.Object
-
- org.mule.metadata.ast.internal.MuleLazyValue<T>
-
- Type Parameters:
T- the generic type of the provided value
- All Implemented Interfaces:
Supplier<T>
public class MuleLazyValue<T> extends Object implements Supplier<T>
Provides a value which may be lazily computed.The value is only computed on the first invokation of
get(). Subsequent calls to such method will always return the same value.This class is thread-safe. When invoking
get(), it is guaranteed that the value will be computed only once.- Since:
- 1.2.0
-
-
Constructor Summary
Constructors Constructor Description MuleLazyValue(Supplier<T> supplier)Creates a new instance which lazily obtains its value from the givensupplier.MuleLazyValue(T value)Creates a new instance which is already initialised with the givenvalue.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <R> RflatMap(Function<T,R> function)Applies the givenfunctionthrough the output ofget().Tget()Returns the lazy value.voidifComputed(Consumer<T> consumer)If the value has already been computed, if passes it to the givenconsumer.booleanisComputed()
-
-
-
Constructor Detail
-
MuleLazyValue
public MuleLazyValue(Supplier<T> supplier)
Creates a new instance which lazily obtains its value from the givensupplier. It is guaranteed thatSupplier.get()will only be invoked once. Because this class is thread-safe, the supplier is not required to be.- Parameters:
supplier- ASupplierthrough which the value is obtained
-
MuleLazyValue
public MuleLazyValue(T value)
Creates a new instance which is already initialised with the givenvalue.- Parameters:
value- the initialization value
-
-
Method Detail
-
get
public T get()
Returns the lazy value. If the value has not yet been computed, then it does so
-
isComputed
public boolean isComputed()
- Returns:
- Whether the value has already been calculated.
-
ifComputed
public void ifComputed(Consumer<T> consumer)
If the value has already been computed, if passes it to the givenconsumer. This method does not perform any synchronization so keep in mind that dirty reads are possible if this method is being called from one thread while another thread is triggering the value's computation- Parameters:
consumer- aConsumer
-
flatMap
public <R> R flatMap(Function<T,R> function)
Applies the givenfunctionthrough the output ofget(). If the value has not already been computed, this method will trigger computation. This method is thread-safe.- Type Parameters:
R- the generic type of the function's output- Parameters:
function- a transformation function- Returns:
- a transformed value
-
-