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 = 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
-
Nested Class Summary
Modifier 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
This class allows collecting one or moreAutoCloseable
objects into one.static final class
This class represents a closeable value.static class
Lifecycle.Resources<E extends Exception>
This class allows collecting 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 class
Lifecycle.Value<T,
E extends Exception> This class represents a closeable value. -
Method Summary