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 Details

    • Resources

      public Resources(Collection<? extends Lifecycle.ThrowingRunnable<? extends E>> releases)
      Create a new Resources object, initialized with the given resource release methods.
      Parameters:
      releases - the release methods
    • Resources

      @SafeVarargs public Resources(Lifecycle.ThrowingRunnable<? extends E>... releases)
      Create a new Resources object, initialized with the given resource release methods.
      Parameters:
      releases - the release methods
    • Resources

      public Resources()
      Create a new, empty Resources object.
  • Method Details