Class LazyInitializable<T,E extends Exception>

java.lang.Object
org.elasticsearch.common.util.LazyInitializable<T,E>

public final class LazyInitializable<T,E extends Exception> extends Object
Encapsulates a CheckedSupplier which is lazily invoked once on the first call to #getOrCompute(). The value which the supplier returns is memorized and will be served until #reset() is called. Each value returned by #getOrCompute(), newly minted or cached, will be passed to the onGet Consumer. On #reset() the value will be passed to the onReset Consumer and the next #getOrCompute() will regenerate the value.
  • Constructor Details

    • LazyInitializable

      public LazyInitializable(CheckedSupplier<T,E> supplier)
      Creates the simple LazyInitializable instance.
      Parameters:
      supplier - The CheckedSupplier to generate values which will be served on #getOrCompute() invocations.
    • LazyInitializable

      public LazyInitializable(CheckedSupplier<T,E> supplier, Consumer<T> onGet, Consumer<T> onReset)
      Creates the complete LazyInitializable instance.
      Parameters:
      supplier - The CheckedSupplier to generate values which will be served on #getOrCompute() invocations.
      onGet - A Consumer which is called on each value, newly forged or stale, that is returned by #getOrCompute()
      onReset - A Consumer which is invoked on the value that will be erased when calling #reset()
  • Method Details

    • getOrCompute

      public T getOrCompute() throws E
      Returns a value that was created by supplier. The value might have been previously created, if not it will be created now, thread safe of course.
      Throws:
      E extends Exception
    • reset

      public void reset()
      Clears the value, if it has been previously created by calling #getOrCompute(). The onReset will be called on this value. The next call to #getOrCompute() will recreate the value.