Class Lifecycle.Value<T,E extends Exception>

Type Parameters:
T - the value type
All Implemented Interfaces:
Lifecycle.ExtendedCloseable<E>, AutoCloseable, Supplier<T>
Direct Known Subclasses:
Enclosing class:

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.
      value - the actual resource value
      release - the release method for the given value
      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
      builder - the builder method
      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>
      E extends Exception
    • toString

      public String toString()
      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
      block - the codec block which is applied to the value
      releases - additional release methods, which are called in the case of an error
      E - if applying the block throws an exception