public final class Closing extends Object
Succinctly handle AutoCloseables that throw checked Exceptions.
There are two families of static methods; closeAfterApplying(...) and closeAfterAccepting(...), the
former applies checked functions (the use-case being read) while the later accepts checked
consumers (the use-case being write)
Example: this zero-branch, single-instruction, one-liner will find a free port number:
int port = Closing.closeAfterApplying(ServerSocket::new, 0, ServerSocket::getLocalPort);
Motivation: I/O stream instances typically (and necessarily) requires handling a lot of possible
IOExceptions - by invoking:
AutoCloseable.close()AutoCloseable.close() - via
try-with-resourcesThat's a lot of branches to cover - often it is irrelevant to the application which branch actually throws, but regardless test coverage suffers without some needless stub/mocked throw-on-create/read/write/close tests.
| Modifier and Type | Class and Description |
|---|---|
static class |
Closing.AutoClosed<T,E extends Exception> |
| Modifier and Type | Method and Description |
|---|---|
static <T,E extends Exception> |
autoClosing(T instance,
CheckedConsumer<T,E> closeMethod)
Reshape instances to fit try-catch autoclose.
|
static <C extends AutoCloseable> |
closeAfterAccepting(C closeable,
CheckedConsumer<C,?> consume)
Consumes the
closeable before closing. |
static <C extends AutoCloseable,T> |
closeAfterAccepting(CheckedFunction<T,C,?> create,
T t,
CheckedConsumer<C,?> consume)
Applies the
create function to t, resulting in a AutoCloseable which is closed after
being consumed. |
static <C extends AutoCloseable,T,U> |
closeAfterAccepting(CheckedFunction<T,C,?> create,
T t,
U instance,
CheckedBiConsumer<C,U,?> consume) |
static <C extends AutoCloseable,U> |
closeAfterAccepting(C closeable,
U instance,
CheckedBiConsumer<C,U,?> consume)
Consumes both the
closeable and instance before closing. |
static <C extends AutoCloseable,R> |
closeAfterApplying(C closeable,
CheckedFunction<C,R,?> convert)
Applies the function to the closeable, returning the result and closing the closable - checked exceptions are
rethrown as unchecked.
|
static <C extends AutoCloseable,T,R> |
closeAfterApplying(CheckedFunction<T,C,?> create,
T t,
CheckedFunction<C,R,?> convert)
|
static <C extends AutoCloseable,U,R,E extends Throwable> |
closeAfterApplying(C closeable,
U instance,
CheckedBiFunction<C,U,R,E> convert)
Applies the bi-function to the closeable, returning the result and closing the closable - checked exceptions are
rethrown as unchecked.
|
public static <C extends AutoCloseable,T,R> R closeAfterApplying(CheckedFunction<T,C,?> create, T t, CheckedFunction<C,R,?> convert)
Ultra-shorthand for AutoCloseable/Closeable, obvious use for InputStream
C - AutoCloseable typeT - create function argument typeR - the result typecreate - a function applying t to produce an AutoCloseable of type <C>t - the argument to apply to the create functionconvert - a function applied to the AutoCloseable to produce the resultconvert functionpublic static <C extends AutoCloseable,R> R closeAfterApplying(C closeable, CheckedFunction<C,R,?> convert)
C - the auto-closeable type, to apply and closeR - the result typecloseable - the closeable subject of the convert functionconvert - the function consuming the closeable and supplying the resultconvert function to the closeable argumentpublic static <C extends AutoCloseable,U,R,E extends Throwable> R closeAfterApplying(C closeable, U instance, CheckedBiFunction<C,U,R,E> convert)
C - the auto-closeable type, will be applied and closedU - the type of second argument to bi-function applyR - the result typecloseable - the closeable subject of the convert bi-functioninstance - the second argument for the bi-functionconvert - the function consuming the closeable and supplying the resultconvert function to the closeable argumentpublic static <C extends AutoCloseable,T> void closeAfterAccepting(CheckedFunction<T,C,?> create, T t, CheckedConsumer<C,?> consume)
create function to t, resulting in a AutoCloseable which is closed after
being consumed.
Checked exceptions are rethrown as unchecked.C - the auto-closeable type, to be created, consumed and closedT - the function's argument type, used to create the auto-closeablecreate - the function creating the AutoCloseablet - the argument that the create function is applied toconsume - the consumer of the AutoCloseablepublic static <C extends AutoCloseable> void closeAfterAccepting(C closeable, CheckedConsumer<C,?> consume)
closeable before closing. Checked exceptions are rethrown as unchecked.C - the auto-closeable typecloseable - the closeable to be consumed and closedconsume - the consumer of the AutoCloseablepublic static <C extends AutoCloseable,T,U> void closeAfterAccepting(CheckedFunction<T,C,?> create, T t, U instance, CheckedBiConsumer<C,U,?> consume)
public static <C extends AutoCloseable,U> void closeAfterAccepting(C closeable, U instance, CheckedBiConsumer<C,U,?> consume)
closeable and instance before closing. Checked exceptions are rethrown as
unchecked.C - the auto-closeable type to consumeU - the type of consumer's second argumentcloseable - the closeable to be consumed and closedinstance - the instance to consumeconsume - the consumer of the AutoCloseablepublic static <T,E extends Exception> Closing.AutoClosed<T,E> autoClosing(T instance, CheckedConsumer<T,E> closeMethod)
try(AutoClosed vm = Closing.autoClosing(getCurrentVm(), VirtualMachine::detach)) {
vm.get().loadAgent(x, y);
}
instance - closeMethod - Copyright © 2016–2018 earcam. All rights reserved.