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
@ThreadSafe public final class Closable<T> extends Object implements AutoCloseable
Aauto 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 implementAutoCloseable
themselves.This class guarantees that the close action is performed only once, even if
close
method is called multiple times. Once closed, thevalue()
method will always returnnull
.This class is thread-safe.
- Since:
- 8.0.0
- Author:
- XIMA MEDIA GmbH
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
static <T extends AutoCloseable>
Closable<T>ofAutoCloseable(T closable)
Wraps an auto closable in a closable instance.String
toString()
T
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.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.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.
-
-
-
Method Detail
-
close
public void close() throws Exception
- Specified by:
close
in interfaceAutoCloseable
- Throws:
Exception
-
value
public T value()
Gets the wrapped value. Once this closable asclosed
, 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.
-
-