java.lang.Object
io.jenetics.internal.util.Lifecycle
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
Lifecycle.ExtendedCloseable<E extends Exception>
Extends theAutoCloseable
with methods for wrapping the thrown exception into unchecked exceptions or ignoring them.static final class
Lifecycle.Resources<E extends Exception>
This class allows to collect one or moreAutoCloseable
objects into one.static interface
Lifecycle.ThrowingConsumer<A,
E extends Exception> A method which takes an argument and can throw an exception.static interface
Lifecycle.ThrowingFunction<A,
R, E extends Exception> A function which takes an argument and can throw an exception.static interface
Lifecycle.ThrowingRunnable<E extends Exception>
Runnable task/method, which might throw an exceptionE
.static final class
Lifecycle.Value<T,
E extends Exception> This class represents a closeable value. -
Method Summary