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 = Value.of( 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 Value<Stream<Object>, IOException> result = Value.build(resources -> { final var fin = resources.add(new FileInputStream(file.toFile()), Closeable::close); final var bin = resources.add(new BufferedInputStream(fin), Closeable::close); final var oin = resources.add(new ObjectInputStream(bin), Closeable::close); 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:
6.3