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
- Direct Known Subclasses:
Lifecycle.IOResources
- Enclosing class:
- Lifecycle
public static sealed class Lifecycle.Resources<E extends Exception>
extends Object
implements Lifecycle.ExtendedCloseable<E>
permits Lifecycle.IOResources
This class allows collecting one or more
AutoCloseable
objects
into one. The registered closeable objects are closed in reverse order.
Using the Resources
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 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);
// ...
}
-
Constructor Summary
ConstructorDescriptionCreate a new, emptyResources
object.Resources
(Lifecycle.ThrowingRunnable<? extends E>... releases) Create a newResources
object, initialized with the given resource release methods.Resources
(Collection<? extends Lifecycle.ThrowingRunnable<? extends E>> releases) Create a newResources
object, initialized with the given resource release methods. -
Method Summary
Modifier and TypeMethodDescription<C extends Lifecycle.ExtendedCloseable<? extends E>>
Cadd
(C resource) Registers the givenresource
to the list of managed resources.<C> C
add
(C resource, Lifecycle.ThrowingConsumer<? super C, ? extends E> release) Registers the givenresource
to the list of managed resources.void
close()
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.jenetics.internal.util.Lifecycle.ExtendedCloseable
silentClose, silentClose, uncheckedClose
-
Constructor Details
-
Resources
Create a newResources
object, initialized with the given resource release methods.- Parameters:
releases
- the release methods
-
Resources
Create a newResources
object, initialized with the given resource release methods.- Parameters:
releases
- the release methods
-
Resources
public Resources()Create a new, emptyResources
object.
-
-
Method Details
-
add
Registers the givenresource
to the list of managed resources.- Type Parameters:
C
- the resource type- Parameters:
resource
- the new resource to registerrelease
- the method, which releases the acquired resource- Returns:
- the registered resource
- Throws:
NullPointerException
- if one of the given arguments isnull
-
add
Registers the givenresource
to 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
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceLifecycle.ExtendedCloseable<E extends Exception>
- Throws:
E extends Exception
-