Class LazyAutoCloseableReference<T extends AutoCloseable>

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

public class LazyAutoCloseableReference<T extends AutoCloseable> extends LazyAtomicReference<T> implements AutoCloseable
A subclass of LazyAtomicReference which holds an AutoCloseable reference and calls close() when it itself is closed.
  • Constructor Details

    • LazyAutoCloseableReference

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

    • eval

      public T eval() throws IOException
      Get the value, constructing it if needed.
      Overrides:
      eval in class LazyAtomicReference<T extends AutoCloseable>
      Returns:
      the value
      Throws:
      IllegalStateException - if the reference is closed.
      IOException - on any evaluation failure
    • isClosed

      public boolean isClosed()
      Is the reference closed?
      Returns:
      true if the reference is closed.
    • close

      public void close() throws Exception
      Close the reference value if it is non-null. Sets the reference to null afterwards, even on a failure.
      Specified by:
      close in interface AutoCloseable
      Throws:
      Exception - failure to close.
    • lazyAutoCloseablefromSupplier

      public static <T extends AutoCloseable> LazyAutoCloseableReference<T> lazyAutoCloseablefromSupplier(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.