Class 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 Detail

      • MuleLazyValue

        public MuleLazyValue​(Supplier<T> supplier)
        Creates a new instance which lazily obtains its value from the given supplier. It is guaranteed that Supplier.get() will only be invoked once. Because this class is thread-safe, the supplier is not required to be.
        Parameters:
        supplier - A Supplier through which the value is obtained
      • MuleLazyValue

        public MuleLazyValue​(T value)
        Creates a new instance which is already initialised with the given value.
        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
        Specified by:
        get in interface Supplier<T>
        Returns:
        the lazy value
      • 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 given consumer. 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 - a Consumer
      • flatMap

        public <R> R flatMap​(Function<T,​R> function)
        Applies the given function through the output of get(). 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