Class Closer

java.lang.Object
com.franz.util.Closer
All Implemented Interfaces:
AutoCloseable

public class Closer extends Object implements AutoCloseable
Use this class to collect objects that should be closed at a later time.

Subclasses can override handleCloseException() to decide what should happen with exceptions thrown during close. The default behavior is to log a warning and ignore the exception.

Since:
AG v4.3.3
  • Constructor Details

    • Closer

      public Closer()
  • Method Details

    • closeLater

      public <Obj extends AutoCloseable> Obj closeLater(Obj o)
      Add a resource to be closed with close().

      Resources will be closed in reverse registration order.

      Type Parameters:
      Obj - The type of objects to be closed by this Closer
      Parameters:
      o - Object to note for later closing
      Returns:
      Obj always returns o
    • remove

      public boolean remove(AutoCloseable o)
      Remove object from collection so close will not be called later.
      Parameters:
      o - Object to be removed from this Closer
      Returns:
      boolean Returns true if o was removed, else false
      See Also:
    • close

      public void close()
      Closes all resources registered with closeLater(AutoCloseable).
      Specified by:
      close in interface AutoCloseable
    • close

      public void close(Collection<? extends AutoCloseable> objects)
      Close given objects immediately, will not be closed "later".
      Parameters:
      objects - The collection of objects to close
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • close

      public <CloseableType extends AutoCloseable> CloseableType close(CloseableType o)
      Close an object immediately, will not be closed "later".

      If the object is registered on the list of resources to be closed later by this Closer, it will be removed from that list.

      Type Parameters:
      CloseableType - The type of objects that can be closed by this Closer
      Parameters:
      o - The object being closed
      Returns:
      Obj o is always returned.