java.lang.Object
dev.metaschema.core.util.AutoCloser<T,E>
- Type Parameters:
T- the resource typeE- the exception type that may be thrown if an error occurs when closing the resource
- All Implemented Interfaces:
AutoCloseable
Wraps a resource to make it
AutoCloseable.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceAutoCloser.Closer<T,E extends Exception> A callback interface representing a close operation. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T,E extends Exception>
@Owning AutoCloser<T,E> autoClose(T resource, AutoCloser.Closer<T, E> lambda) voidclose()Get the wrapped resource.static @Owning OutputStreampreventClose(OutputStream out) Wraps the provided output stream to prevent the wrapped stream from being closed.
-
Method Details
-
autoClose
@Owning @NonNull public static <T,E extends Exception> @Owning AutoCloser<T,E> autoClose(@NonNull T resource, @NonNull AutoCloser.Closer<T, E> lambda) Adapt the the providedresourceto beAutoCloseable, using a provided closerlambda.The caller owns the returned wrapper and is responsible for closing it.
- Type Parameters:
T- the resource's typeE- the exception type that can be thrown when closing- Parameters:
resource- the object to adaptlambda- the lambda to use as a callback on close- Returns:
- the resource wrapped in an
AutoCloseable
-
getResource
Get the wrapped resource.- Returns:
- the resource object
-
close
- Specified by:
closein interfaceAutoCloseable- Throws:
E extends Exception
-
preventClose
Wraps the provided output stream to prevent the wrapped stream from being closed.This is useful for protecting standard streams. i.e.
System.out,System.err.The caller owns the returned stream and is responsible for closing it.
- Parameters:
out- the stream to wrap- Returns:
- the new wrapped stream
-