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 Details

    • AutoCloseablePointer

      public AutoCloseablePointer()
      Constructor for a null-valued pointer.
    • AutoCloseablePointer

      public AutoCloseablePointer(T pointer)
      Constructor for a pointer value.
      Parameters:
      pointer - the initial pointer value
  • Method Details

    • close

      public void close() throws Exception
      Specified by:
      close in interface AutoCloseable
      Throws:
      Exception
    • get

      public T get()
      Get the raw pointer out for use.
      Returns:
      the raw pointer
    • adopt

      public T 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

      public void assign(T newP) throws Exception
      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

      public void assignNoChecked(T newP)
      Like assign(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