Package org.apache.drill.common
Class AutoCloseablePointer<T extends AutoCloseable>
java.lang.Object
org.apache.drill.common.AutoCloseablePointer<T>
- Type Parameters:
T
- type of the pointer
- All Implemented Interfaces:
AutoCloseable
public final class AutoCloseablePointer<T extends AutoCloseable>
extends Object
implements AutoCloseable
A class similar to Pointer<>, but with features unique to holding
AutoCloseable pointers. The AutoCloseablePointer<> must be closed
when it will no longer be used.
If you're familiar with C++/Boost's shared_ptr<>, you might recognize some of the features here.
-
Constructor Summary
ConstructorDescriptionConstructor for a null-valued pointer.AutoCloseablePointer
(T pointer) Constructor for a pointer value. -
Method Summary
Modifier and TypeMethodDescriptionadopt()
The caller adopts the pointer; the holder is set to null, and will no longer be responsible for close()ing this pointer.void
Assign a new pointer to this holder.void
assignNoChecked
(T newP) Likeassign(AutoCloseable)
, except that any exception thrown by closing the previously held pointer is wrapped with (an unchecked)RuntimeException
.void
close()
get()
Get the raw pointer out for use.
-
Constructor Details
-
AutoCloseablePointer
public AutoCloseablePointer()Constructor for a null-valued pointer. -
AutoCloseablePointer
Constructor for a pointer value.- Parameters:
pointer
- the initial pointer value
-
-
Method Details
-
close
- Specified by:
close
in interfaceAutoCloseable
- Throws:
Exception
-
get
Get the raw pointer out for use.- Returns:
- the raw pointer
-
adopt
The caller adopts the pointer; the holder is set to null, and will no longer be responsible for close()ing this pointer.- Returns:
- the pointer being adopted; may be null
-
assign
Assign a new pointer to this holder. Any currently held pointer will first be closed. If closing the currently held pointer throws an exception, the new pointer is still assigned, and the holder must still be closed to close that.This makes it convenient to assign a new pointer without having to check for a previous value and worry about cleaning it up; this does all that.
- Parameters:
newP
- the new pointer to hold- Throws:
Exception
- any exception thrown by closing the currently held pointer
-
assignNoChecked
Likeassign(AutoCloseable)
, except that any exception thrown by closing the previously held pointer is wrapped with (an unchecked)RuntimeException
.- Parameters:
newP
- the new pointer to hold
-