Class LazyAtomicReference<T>

java.lang.Object
org.apache.hadoop.util.functional.LazyAtomicReference<T>
Type Parameters:
T - type of reference
All Implemented Interfaces:
Supplier<T>, CallableRaisingIOE<T>
Direct Known Subclasses:
LazyAutoCloseableReference

public class LazyAtomicReference<T> extends Object implements CallableRaisingIOE<T>, Supplier<T>
A lazily constructed reference, whose reference constructor is a CallableRaisingIOE so may raise IOExceptions.

This constructor is only invoked on demand when the reference is first needed, after which the same value is returned. This value MUST NOT be null.

Implements CallableRaisingIOE and java.util.function.Supplier. An instance of this can therefore be used in a functional IO chain. As such, it can act as a delayed and caching invocator of a function: the supplier passed in is only ever invoked once, and only when requested.

  • Constructor Details

    • LazyAtomicReference

      public LazyAtomicReference(CallableRaisingIOE<? extends T> constructor)
      Constructor for this instance.
      Parameters:
      constructor - method to invoke to actually construct the inner object.
  • Method Details

    • getConstructor

      protected CallableRaisingIOE<? extends T> getConstructor()
      Getter for the constructor.
      Returns:
      the constructor class
    • getReference

      protected AtomicReference<T> getReference()
      Get the reference. Subclasses working with this need to be careful working with this.
      Returns:
      the reference.
    • eval

      public T eval() throws IOException
      Get the value, constructing it if needed.
      Returns:
      the value
      Throws:
      IOException - on any evaluation failure
      NullPointerException - if the evaluated function returned null.
    • apply

      public final T apply() throws IOException
      Implementation of CallableRaisingIOE.apply(). Invoke eval().
      Specified by:
      apply in interface CallableRaisingIOE<T>
      Returns:
      the value
      Throws:
      IOException - on any evaluation failure
    • get

      public final T get() throws UncheckedIOException
      Implementation of Supplier.get().

      Invoke eval() and convert IOEs to UncheckedIOException.

      This is the Supplier.get() implementation, which allows this class to passed into anything taking a supplier.

      Specified by:
      get in interface Supplier<T>
      Returns:
      the value
      Throws:
      UncheckedIOException - if the constructor raised an IOException.
    • isSet

      public final boolean isSet()
      Is the reference set?
      Returns:
      true if the reference has been set.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • lazyAtomicReferenceFromSupplier

      public static <T> LazyAtomicReference<T> lazyAtomicReferenceFromSupplier(Supplier<T> supplier)
      Create from a supplier. This is not a constructor to avoid ambiguity when a lambda-expression is passed in.
      Type Parameters:
      T - type of reference
      Parameters:
      supplier - supplier implementation.
      Returns:
      a lazy reference.