Class Lifecycle

java.lang.Object
io.jenetics.internal.util.Lifecycle

public class Lifecycle extends Object
Interfaces and classes for handling resource (AutoCloseable) objects. The common use cases are shown as follows:

Wrapping non-closeable values

final Value<Path, IOException> file = new Value<>(
    Files.createFile(Path.of("some_file")),
    Files::deleteIfExists
);

// Automatically delete the file after the test.
try (file) {
    Files.write(file.get(), "foo".getBytes());
    final var writtenText = Files.readString(file.get());
    assert "foo".equals(writtenText);
}

Building complex closeable values

final IOValue<Stream<Object>> result = new IOValue<>(resources -> {
    final var fin = resources.add(new FileInputStream(file.toFile()));
    final var bin = resources.add(new BufferedInputStream(fin));
    final var oin = resources.add(new ObjectInputStream(bin));

    return Stream.generate(() -> readNextObject(oin))
        .takeWhile(Objects::nonNull);
});

try (result) {
    result.get().forEach(System.out::println);
}

Wrapping several closeables into one

try (var __ = ExtendedCloseable.of(c1, c2, c3)) {
    // ...
}
Since:
6.2
Version:
7.2