java.lang.Object
io.jenetics.internal.util.Lifecycle.Value<T,IOException>
io.jenetics.internal.util.Lifecycle.IOValue<T>
- Type Parameters:
T- the value type
- All Implemented Interfaces:
Lifecycle.ExtendedCloseable<IOException>,Closeable,AutoCloseable,Supplier<T>
- Enclosing class:
- Lifecycle
public static final class Lifecycle.IOValue<T>
extends Lifecycle.Value<T,IOException>
implements Closeable
This class represents a closeable value. It is useful in cases
where the object value doesn't implement the
AutoCloseable
interface but needs some cleanup work to do after usage. In the following
example the created file is automatically deleted when leaving the
try block.
// Create the closeable file.
final IOValue<Path> file = new IOValue<>(
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);
}-
Constructor Summary
ConstructorsConstructorDescriptionIOValue(Lifecycle.ThrowingFunction<? super Lifecycle.IOResources, ? extends T, ? extends BE> builder) Opens a kind oftry-catchwith resources block.IOValue(T value, Lifecycle.ThrowingConsumer<? super T, ? extends IOException> release) Create a new closeable value with the given resourcevalueand itsreleasemethod. -
Method Summary
Methods inherited from class io.jenetics.internal.util.Lifecycle.Value
close, get, toString, tryingMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface io.jenetics.internal.util.Lifecycle.ExtendedCloseable
silentClose, silentClose, uncheckedClose
-
Constructor Details
-
IOValue
Create a new closeable value with the given resourcevalueand itsreleasemethod.- Parameters:
value- the actual resource valuerelease- thereleasemethod for the givenvalue- Throws:
NullPointerException- if thereleasefunction isnull
-
IOValue
public IOValue(Lifecycle.ThrowingFunction<? super Lifecycle.IOResources, ? extends T, throws BE? extends BE> builder) Opens a kind oftry-catchwith resources block. The difference is, that the resources, registered with theLifecycle.Resources.add(Object, ThrowingConsumer)method, are only closed in the case of an error. If the value could be created, the caller is responsible for closing the opened resources by calling theLifecycle.Value.close()method.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); }- Type Parameters:
BE- the exception type which might be thrown while building the value- Parameters:
builder- the builder method- Throws:
BE- in the case of an error. If this exception is thrown, all already registered resources are closed.NullPointerException- if the givenbuilderisnull- See Also:
-