
com.pippsford.common.UncheckedCloseable Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of common-utils Show documentation
Show all versions of common-utils Show documentation
Code that seems to me to have utility across multiple projects
The newest version!
package com.pippsford.common;
/**
* A functional interface that facilitates the chaining of "close" calls.
*
* @author Simon Greatrix on 07/07/2021.
*/
@FunctionalInterface
public interface UncheckedCloseable extends Runnable, AutoCloseable {
/**
* Wrap an {@link AutoCloseable} as an {@link UncheckedCloseable}.
*
* @param c the closeable to wrap
*
* @return the new wrapper
*/
static UncheckedCloseable wrap(AutoCloseable c) {
if (c instanceof UncheckedCloseable) {
return (UncheckedCloseable) c;
}
return c::close;
}
/**
* Nest another closeable within this. The {@code close()} method on the nested instance will be invoked prior to the {@code close()} method on this instance.
*
* @param c the closeable to nest within this
*
* @return a new closeable that implements the nesting
*/
default UncheckedCloseable nest(AutoCloseable c) {
return () -> {
try (UncheckedCloseable c1 = this) {
c.close();
}
};
}
/**
* Invoke the close operation. If the wrapped {@code close()} methods fails with a checked exception, then it will be wrapped in a {@link
* UncheckedCheckedException}.
*/
default void run() {
try {
close();
} catch (RuntimeException runtimeException) {
throw runtimeException;
} catch (Exception ex) {
throw new UncheckedCheckedException(ex);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy