-
- All Superinterfaces:
-
AutoCloseable
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
@FunctionalInterface public interface CloseableScope extends AutoCloseable
Specialization ofAutoCloseable
that throws only unchecked exceptions. It is intended to be used with try-with-resources construct to implement custom scoping rules.- See Also:
- CloseableScope tutorial
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default CloseableScope
andFinally(Runnable action)
Creates extendedCloseableScope
that additionally runs specifiedaction
regardless of exceptions.default CloseableScope
andThen(Runnable action)
Creates extendedCloseableScope
that additionally runs specifiedaction
unless exception is thrown.void
close()
Closes this scope or other closeable resource.
-
-
-
Method Detail
-
close
void close()
Closes this scope or other closeable resource. This is a specialization ofAutoCloseable.close()
that throws only unchecked exceptions.- Specified by:
-
close
in interfaceAutoCloseable
-
andThen
default CloseableScope andThen(Runnable action)
Creates extendedCloseableScope
that additionally runs specifiedaction
unless exception is thrown. This is useful for quickly creating outer scopes that add extra operations to some inner scope. Ifclose()
throws,action
does not run.- Parameters:
-
action
- the operation to perform afterclose()
is called - Returns:
-
new
CloseableScope
that, when closed, first closes thisCloseableScope
and then executesaction
- Throws:
-
NullPointerException
- ifaction
isnull
- See Also:
-
andFinally(Runnable)
-
andFinally
default CloseableScope andFinally(Runnable action)
Creates extendedCloseableScope
that additionally runs specifiedaction
regardless of exceptions. This is useful for quickly creating outer scopes that add extra operations to some inner scope. Ifclose()
throws,action
runs anyway as if in try-with-resources block. If bothclose()
} andaction
throw, the exception fromaction
is added to suppressed exception list by callingThrowable.addSuppressed(Throwable)
.- Parameters:
-
action
- the operation to perform afterclose()
is called - Returns:
-
new
CloseableScope
that, when closed, first closes thisCloseableScope
and then executesaction
- Throws:
-
NullPointerException
- ifaction
isnull
- See Also:
-
andThen(Runnable)
-
-