Module io.jenetics.base
Package io.jenetics.internal.util
Class Lifecycle.Value<T,E extends Exception>
java.lang.Object
io.jenetics.internal.util.Lifecycle.Value<T,E>
- Type Parameters:
T- the value type
- All Implemented Interfaces:
Lifecycle.ExtendedCloseable<E>,AutoCloseable,Supplier<T>
- Direct Known Subclasses:
Lifecycle.IOValue
- Enclosing class:
- Lifecycle
public static sealed class Lifecycle.Value<T,E extends Exception>
extends Object
implements Supplier<T>, Lifecycle.ExtendedCloseable<E>
permits Lifecycle.IOValue<T>
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 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);
}-
Constructor Summary
ConstructorsConstructorDescriptionValue(Lifecycle.ThrowingFunction<? super Lifecycle.Resources<E>, ? extends T, ? extends BE> builder) Opens a kind oftry-catchwith resources block.Value(T value, Lifecycle.ThrowingConsumer<? super T, ? extends E> release) Create a new closeable value with the given resourcevalueand itsreleasemethod. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()get()toString()final <E extends Exception>
voidtrying(Lifecycle.ThrowingConsumer<? super T, ? extends E> block, Lifecycle.ThrowingRunnable<? extends E>... releases) Applies the giveblockto the already created closeable value.Methods 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
-
Value
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
-
Value
public Value(Lifecycle.ThrowingFunction<? super Lifecycle.Resources<E>, ? 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 theclose()method.final Value<Stream<Object>, IOException> result = new Value<>(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); }- 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:
-
-
Method Details
-
get
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceLifecycle.ExtendedCloseable<T>- Throws:
E extends Exception
-
toString
-
trying
@SafeVarargs public final <E extends Exception> void trying(Lifecycle.ThrowingConsumer<? super T, ? extends E> block, Lifecycle.ThrowingRunnable<? extends E>... releases) throws EApplies the giveblockto the already created closeable value. If theblockthrows an exception, the resource value is released, by calling the defined release method. The typical use case for this method is when additional initialization of the value is needed.final var file = CloseableValue.of( Files.createFile(Path.of("some_file")), Files::deleteIfExists ); // Trying to do additional setup, e.g. setting the 'delete-on-exit' // flag. file.trying(f -> f.toFile().deleteOnExit()); try (file) { // Do something with temp file. }- Type Parameters:
E- the thrown exception type- Parameters:
block- the codec block which is applied to the valuereleases- additional release methods, which are called in the case of an error- Throws:
E- if applying theblockthrows an exception
-