Module io.jenetics.base
Package io.jenetics.internal.util
Class Lifecycle.Resources<E extends Exception>
java.lang.Object
io.jenetics.internal.util.Lifecycle.Resources<E>
- All Implemented Interfaces:
Lifecycle.ExtendedCloseable<E>,AutoCloseable
- Enclosing class:
- Lifecycle
public static final class Lifecycle.Resources<E extends Exception>
extends Object
implements Lifecycle.ExtendedCloseable<E>
This class allows to collect one or more
AutoCloseable objects
into one. The registered closeable objects are closed in reverse order.
Using the Resources class can simplify the the creation of
dependent input streams, where it might be otherwise necessary to create
nested try-with-resources blocks.
try (var resources = new Resources<IOException>()) {
final var fin = resources.add(new FileInputStream(file), Closeable::close);
if (fin.read() != -1) {
return;
}
final var oin = resources.add(new ObjectInputStream(fin), Closeable::close);
// ...
}- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreate a new, emptyResourcesobject.Resources(Lifecycle.ThrowingRunnable<? extends E>... releases) Create a newResourcesobject, initialized with the given resource release methods.Resources(Collection<? extends Lifecycle.ThrowingRunnable<? extends E>> releases) Create a newResourcesobject, initialized with the given resource release methods. -
Method Summary
Modifier and TypeMethodDescription<C> Cadd(C resource, Lifecycle.ThrowingConsumer<? super C, ? extends E> release) Registers the givenresourceto the list of managed resources.voidclose()Methods 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
-
Resources
Create a newResourcesobject, initialized with the given resource release methods.- Parameters:
releases- the release methods
-
Resources
Create a newResourcesobject, initialized with the given resource release methods.- Parameters:
releases- the release methods
-
Resources
public Resources()Create a new, emptyResourcesobject.
-
-
Method Details
-
add
Registers the givenresourceto the list of managed resources.- Type Parameters:
C- the resource type- Parameters:
resource- the new resource to registerrelease- the method, which releases the the acquired resource- Returns:
- the registered resource
- Throws:
NullPointerException- if one of the given arguments isnull
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceLifecycle.ExtendedCloseable<E extends Exception>- Throws:
E extends Exception
-