java.lang.Object
io.jenetics.internal.util.Lifecycle.Resources<IOException>
io.jenetics.internal.util.Lifecycle.IOResources
- All Implemented Interfaces:
Lifecycle.ExtendedCloseable<IOException>,AutoCloseable
- Enclosing class:
- Lifecycle
This class allows collecting one or more
AutoCloseable objects
into one. The registered closeable objects are closed in reverse order.
Using the IOResources class can simplify the creation of
dependent input streams, where it might be otherwise necessary to create
nested try-with-resources blocks.
try (var resources = new IOResources()) {
final var fin = resources.add(new FileInputStream(file));
if (fin.read() != -1) {
return;
}
final var oin = resources.add(new ObjectInputStream(fin));
// ...
}-
Constructor Summary
ConstructorsConstructorDescriptionCreate a new, emptyIOResourcesobject.IOResources(Lifecycle.ThrowingRunnable<? extends IOException>... releases) Create a newIOResourcesobject, initialized with the given resource release methods.IOResources(Collection<? extends Lifecycle.ThrowingRunnable<? extends IOException>> releases) Create a newIOResourcesobject, initialized with the given resource release methods. -
Method Summary
Methods inherited from class io.jenetics.internal.util.Lifecycle.Resources
add, add, closeMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.jenetics.internal.util.Lifecycle.ExtendedCloseable
silentClose, silentClose, uncheckedClose
-
Constructor Details
-
IOResources
public IOResources(Collection<? extends Lifecycle.ThrowingRunnable<? extends IOException>> releases) Create a newIOResourcesobject, initialized with the given resource release methods.- Parameters:
releases- the release methods
-
IOResources
Create a newIOResourcesobject, initialized with the given resource release methods.- Parameters:
releases- the release methods
-
IOResources
public IOResources()Create a new, emptyIOResourcesobject.
-
-
Method Details
-
add
Registers the givenresourceto the list of managed resources.- Type Parameters:
C- the resource type- Parameters:
resource- the new resource to register- Returns:
- the registered resource
- Throws:
NullPointerException- if one of the given arguments isnull
-