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
ConstructorDescriptionCreate a new, emptyIOResources
object.IOResources
(Lifecycle.ThrowingRunnable<? extends IOException>... releases) Create a newIOResources
object, initialized with the given resource release methods.IOResources
(Collection<? extends Lifecycle.ThrowingRunnable<? extends IOException>> releases) Create a newIOResources
object, initialized with the given resource release methods. -
Method Summary
Methods inherited from class io.jenetics.internal.util.Lifecycle.Resources
add, add, 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
-
IOResources
public IOResources(Collection<? extends Lifecycle.ThrowingRunnable<? extends IOException>> releases) Create a newIOResources
object, initialized with the given resource release methods.- Parameters:
releases
- the release methods
-
IOResources
Create a newIOResources
object, initialized with the given resource release methods.- Parameters:
releases
- the release methods
-
IOResources
public IOResources()Create a new, emptyIOResources
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 register- Returns:
- the registered resource
- Throws:
NullPointerException
- if one of the given arguments isnull
-