LazyAtomicReference (Apache Hadoop Common 3.5.0 API)
Description: declaration: package: org.apache.hadoop.util.functional, class: LazyAtomicReference

Package org.apache.hadoop.util.functional

# 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 Summary

  Constructors
  Constructor
  Description
  `LazyAtomicReference(CallableRaisingIOE<? extends T> constructor)`
  Constructor for this instance.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `final T`
  `apply()`
  Implementation of `CallableRaisingIOE.apply()`.
  `T`
  `eval()`
  Get the value, constructing it if needed.
  `final T`
  `get()`
  Implementation of `Supplier.get()`.
  `protected CallableRaisingIOE<? extends T>`
  `getConstructor()`
  Getter for the constructor.
  `protected AtomicReference<T>`
  `getReference()`
  Get the reference.
  `final boolean`
  `isSet()`
  Is the reference set?
  `static <T> LazyAtomicReference<T>`
  `lazyAtomicReferenceFromSupplier(Supplier<T> supplier)`
  Create from a supplier.
  `String`
  `toString()`

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

  ### Methods inherited from interface org.apache.hadoop.util.functional.CallableRaisingIOE

  `unchecked`

- ## 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.