Class Closable<T>

java.lang.Object
de.xima.fc.mdl.Closable<T>
Type Parameters:
T - Type of the contained item.
All Implemented Interfaces:
AutoCloseable

@ThreadSafe public final class Closable<T> extends Object implements AutoCloseable
A auto closable wrapper for an item that needs to be cleaned up via a custom close action. Useful especially for instances of classes that do not implement AutoCloseable themselves.

This class guarantees that the close action is performed only once, even if close method is called multiple times. Once closed, the value() method will always return null.

This class is thread-safe.

Since:
8.0.0
Author:
XIMA MEDIA GmbH
  • Method Details

    • close

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

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

      public T value()
      Gets the wrapped value. Once this closable as closed, this method always returns null.
      Returns:
      The wrapped value, or null when already closed.
    • ofAutoCloseable

      public static <T extends AutoCloseable> Closable<T> ofAutoCloseable(T closable)
      Wraps an auto closable in a closable instance. When the returned closable is closed, the auto closable is closed as well.
      Type Parameters:
      T - Type of the auto closable.
      Parameters:
      closable - Auto closable to wrap.
      Returns:
      A closable that wraps the given instance.
    • withCloseAction

      public static <T> Closable<T> withCloseAction(T item, IThrowingConsumer<T> closeAction)
      Creates a new closable that wraps the given item and applies the custom close action when closed.
      Type Parameters:
      T - Type of the item that needs closing.
      Parameters:
      item - An item that needs closing.
      closeAction - Action that closes the item.
      Returns:
      A closable with the given item and close action.
    • withCloseAction

      public static <T> Closable<T> withCloseAction(T item, IThrowingRunnable closeAction)
      Creates a new closable that wraps the given item and applies the custom close action when closed.
      Type Parameters:
      T - Type of the item that needs closing.
      Parameters:
      item - An item that needs closing.
      closeAction - Action that closes the item.
      Returns:
      A closable with the given item and close action.
    • withDeleteFileOrFolder

      public static Closable<File> withDeleteFileOrFolder(File fileOrFolder)
      Creates a new closable that wraps the given file or folder and delete that file or folder when closed.
      Parameters:
      fileOrFolder - File or folder to wrap.
      Returns:
      A closable with the given file or folder that deletes the file or folder.
    • withDeleteFileOrFolder

      public static Closable<Path> withDeleteFileOrFolder(Path fileOrFolder)
      Creates a new closable that wraps the given file or folder and delete that file or folder when closed.
      Parameters:
      fileOrFolder - File or folder to wrap.
      Returns:
      A closable with the given file or folder that deletes the file or folder.