Class Lifecycle.IOResources

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

public static final class Lifecycle.IOResources extends Lifecycle.Resources<IOException>
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 Details

  • Method Details

    • add

      public <C extends Closeable> C add(C resource)
      Registers the given resource 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 is null