Class Lifecycle.Value<T,E extends Exception>

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

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); }
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
    • 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 resource value and its release method.
      Type Parameters:
      T - the value type
      value - the actual resource value
      release - the release method for the given value
      a new closeable value
      NullPointerException - if one of the arguments is null
    • build

      public static <T, BE extends Exception, VE extends Exception> Lifecycle.Value<T,VE> build(Lifecycle.ThrowingFunction<? super Lifecycle.Resources<VE>,? 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 = -> { 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 value
      BE - the exception type which might be thrown while building the value
      VE - the exception type which might be thrown when releasing the returned closeable value
      builder - the builder method
      the built closeable value
      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: