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 Details

    • Value

      public Value(T value, Lifecycle.ThrowingConsumer<? super T,? extends E> release)
      Create a new closeable value with the given resource value and its release method.
      Parameters:
      value - the actual resource value
      release - the release method for the given value
      Throws:
      NullPointerException - if the release function is null
    • Value

      public Value(Lifecycle.ThrowingFunction<? super Lifecycle.Resources<E>,? extends T,? extends BE> builder) throws BE
      Opens a kind of try-catch with resources block. The difference is, that the resources, registered with the Lifecycle.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 the close() 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 given builder is null
      See Also:
  • Method Details

    • get

      public T get()
      Specified by:
      get in interface Supplier<T>
    • close

      public void close() throws E
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Lifecycle.ExtendedCloseable<T>
      Throws:
      E extends Exception
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • trying

      @SafeVarargs public final <E extends Exception> void trying(Lifecycle.ThrowingConsumer<? super T,? extends E> block, Lifecycle.ThrowingRunnable<? extends E>... releases) throws E
      Applies the give block to the already created closeable value. If the block 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 value
      releases - additional release methods, which are called in the case of an error
      Throws:
      E - if applying the block throws an exception