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>
- Enclosing class:
- Lifecycle
public static final class Lifecycle.Value<T,E extends Exception>
extends Object
implements Supplier<T>, Lifecycle.ExtendedCloseable<E>
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 = 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);
}
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T,
BE extends Exception, VE extends Exception>
Lifecycle.Value<T,VE> build
(Lifecycle.ThrowingFunction<? super Lifecycle.Resources<VE>, ? extends T, ? extends BE> builder) Opens a kind oftry-catch
with resources block.void
close()
get()
static <T,
E extends Exception>
Lifecycle.Value<T,E> of
(T value, Lifecycle.ThrowingConsumer<? super T, ? extends E> release) Create a new closeable value with the given resourcevalue
and itsrelease
method.toString()
final <E extends Exception>
voidtrying
(Lifecycle.ThrowingConsumer<? super T, ? extends E> block, Lifecycle.ThrowingRunnable<? extends E>... releases) Applies the giveblock
to the already created closeable value.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface io.jenetics.internal.util.Lifecycle.ExtendedCloseable
silentClose, silentClose, uncheckedClose
-
Method Details
-
get
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in 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 giveblock
to the already created closeable value. If theblock
throws 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 theblock
throws an exception
-
of
public static <T,E extends Exception> Lifecycle.Value<T,E> of(T value, Lifecycle.ThrowingConsumer<? super T, ? extends E> release) Create a new closeable value with the given resourcevalue
and itsrelease
method.- Type Parameters:
T
- the value type- Parameters:
value
- the actual resource valuerelease
- therelease
method for the givenvalue
- Returns:
- a new closeable value
- Throws:
NullPointerException
- if one of the arguments isnull
-
build
public static <T,BE extends Exception, Lifecycle.Value<T,VE extends Exception> VE> build(Lifecycle.ThrowingFunction<? super Lifecycle.Resources<VE>, ? extends T, throws BE? extends BE> builder) Opens a kind oftry-catch
with 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 = 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); }
- Type Parameters:
T
- the value type of the created closeable valueBE
- the exception type which might be thrown while building the valueVE
- the exception type which might be thrown when releasing the returned closeable value- Parameters:
builder
- the builder method- Returns:
- the built closeable value
- Throws:
BE
- in the case of an error. If this exception is thrown, all already registered resources are closed.NullPointerException
- if the givenbuilder
isnull
- See Also:
-