Package de.xima.fc.mdl
Class Closable<T>
java.lang.Object
de.xima.fc.mdl.Closable<T>
- Type Parameters:
T
- Type of the contained item.
- All Implemented Interfaces:
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 Summary
Modifier and TypeMethodDescriptionvoid
close()
static <T extends AutoCloseable>
Closable<T>ofAutoCloseable
(T closable) Wraps an auto closable in a closable instance.toString()
value()
Gets the wrapped value.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.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.withDeleteFileOrFolder
(File fileOrFolder) Creates a new closable that wraps the given file or folder and delete that file or folder when closed.withDeleteFileOrFolder
(Path fileOrFolder) Creates a new closable that wraps the given file or folder and delete that file or folder when closed.
-
Method Details
-
close
- Specified by:
close
in interfaceAutoCloseable
- Throws:
Exception
-
toString
-
value
Gets the wrapped value. Once this closable asclosed
, this method always returns null.- Returns:
- The wrapped value, or
null
when already closed.
-
ofAutoCloseable
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
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
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
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
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.
-