Skip to content

io7m-com/jmulticlose

Repository files navigation

jmulticlose

Maven Central Maven Central (snapshot) Codecov Java Version

com.io7m.jmulticlose

JVM Platform Status
OpenJDK (Temurin) Current Linux Build (OpenJDK (Temurin) Current, Linux)
OpenJDK (Temurin) LTS Linux Build (OpenJDK (Temurin) LTS, Linux)
OpenJDK (Temurin) Current Windows Build (OpenJDK (Temurin) Current, Windows)
OpenJDK (Temurin) LTS Windows Build (OpenJDK (Temurin) LTS, Windows)

Motivation

The jmulticlose package implements a simple extension to the Java try-with-resources statement that allows for closing many resources at once - without nested or compound statements - in a type-safe manner.

Usage

Create a CloseableCollection to track resources. Add resources to it. The resources will be closed when the collection is closed. All resources will be closed even if any of the individual resources raises an exception.

final Resource r0;
final Resource r1;
final Resource r2;

try (CloseableCollectionType<ClosingResourceFailedException> c = CloseableCollection.create()) {
  r0 = c.add(new Resource(0));
  r1 = c.add(new Resource(1));
  r2 = c.add(new Resource(2));
}