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 Summary
ConstructorsConstructorDescriptionLazyAutoCloseableReference(CallableRaisingIOE<? extends T> constructor) Constructor for this instance. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Close the reference value if it is non-null.eval()Get the value, constructing it if needed.booleanisClosed()Is the reference closed?static <T extends AutoCloseable>
LazyAutoCloseableReference<T>lazyAutoCloseablefromSupplier(Supplier<T> supplier) Create from a supplier.Methods inherited from class org.apache.hadoop.util.functional.LazyAtomicReference
apply, get, getConstructor, getReference, isSet, lazyAtomicReferenceFromSupplier, toStringMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.apache.hadoop.util.functional.CallableRaisingIOE
unchecked
-
Constructor Details
-
LazyAutoCloseableReference
Constructor for this instance.- Parameters:
constructor- method to invoke to actually construct the inner object.
-
-
Method Details
-
eval
Get the value, constructing it if needed.- Overrides:
evalin classLazyAtomicReference<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
Close the reference value if it is non-null. Sets the reference to null afterwards, even on a failure.- Specified by:
closein interfaceAutoCloseable- 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.
-