public final class Exceptional extends Object implements Serializable
Static utility for easy conversion of checked exceptions, and invocation of methods declaring checked exceptions.
There are many cases where you know, within a given context that a checked exception will not be raised, but if it is, then it's certainly game over. An example being URLs in server configuration.
The functionality centres around java.util.function given their beautifully broad application.
In addition, common standard library annoyances are included; e.g. URL, URI, and highly
concise (re-read as terse one-liner - arguably laconic over expressive) for common IO streams usage.
When dealing with Stream from an IO source (e.g. the file system), it may be preferable (to pay a
performance cost due to wrapping) and use EmeticStream which mirrors Stream functionality
but with checked equivalent types.
adjective:
1. unusual; not typical.
| Modifier and Type | Field and Description |
|---|---|
static Thread.UncaughtExceptionHandler |
RETHROWING
An
Thread.UncaughtExceptionHandler that simply rethrows,
wrapping in an appropriate unchecked if necessary |
static Thread.UncaughtExceptionHandler |
SWALLOWING
An
Thread.UncaughtExceptionHandler that simply swallows all exceptions,
except subclasses of Error, which are rethrown. |
| Modifier and Type | Method and Description |
|---|---|
static <T,U> void |
accept(CheckedBiConsumer<T,U,?> consumer,
T t,
U u)
Invokes
CheckedConsumer.accept(Object) catching any checked
Exceptions rethrowing as unchecked. |
static <T> void |
accept(CheckedConsumer<T,?> consumer,
T value)
Invokes
CheckedConsumer.accept(Object) catching any checked
Exceptions rethrowing as unchecked. |
static void |
accept(CheckedIntConsumer<?> consumer,
int value)
Invokes
CheckedIntConsumer.accept(int) catching any checked
Exceptions rethrowing as unchecked. |
static <T,U,R> R |
apply(CheckedBiFunction<T,U,R,?> function,
T t,
U u)
Invokes
CheckedFunction.apply(Object) catching any checked
Exceptions rethrowing as unchecked. |
static <T,R> R |
apply(CheckedFunction<T,R,?> function,
T argument)
Invokes
CheckedFunction.apply(Object) catching any checked
Exceptions rethrowing as unchecked. |
static <T> double |
applyAsDouble(CheckedToDoubleFunction<T,?> function,
T t)
Invokes
CheckedToDoubleFunction.applyAsDouble(Object) catching any checked
Exceptions rethrowing as unchecked. |
static <T,U> int |
applyAsInt(CheckedToIntBiFunction<T,U,?> function,
T t,
U u)
Invokes
CheckedFunction.apply(Object) catching any checked
Exceptions rethrowing as unchecked. |
static <T> int |
applyAsInt(CheckedToIntFunction<T,?> function,
T t)
Invokes
CheckedToIntFunction.applyAsInt(Object) catching any checked
Exceptions rethrowing as unchecked. |
static <T> long |
applyAsLong(CheckedToLongFunction<T,?> function,
T t)
Invokes
CheckedToLongFunction.applyAsLong(Object) catching any checked
Exceptions rethrowing as unchecked. |
static <T> T |
call(Callable<T> callable)
Invokes
Callable.call() catching any checked
Exceptions rethrowing as unchecked. |
static <T> void |
forEach(Iterable<T> iterable,
CheckedConsumer<T,?> consumer)
Invokes the
Iterable.forEach(Consumer) method having unchecked the consumer |
static <K,V> void |
forEach(Map<K,V> map,
CheckedBiConsumer<K,V,?> consumer)
Invokes the
Map.forEach(BiConsumer) method having unchecked the bi-consumer |
static <T> T |
get(CheckedSupplier<T,?> supplier)
Invokes
CheckedSupplier.get() catching any checked
Exceptions rethrowing as unchecked. |
static void |
resetIfInterrupt(Throwable thrown)
Catching an
InterruptedException clears the interrupt flag,
this merely resets the flag IFF the thrown parameter is
an instance of InterruptedException. |
static RuntimeException |
rethrow(Throwable thrown)
Directly rethrows
Errors or RuntimeExceptions, wraps
checked exceptions appropriately |
static void |
run(CheckedRunnable<?> runnable)
Invokes
CheckedRunnable.run() catching any checked
Exceptions rethrowing them as unchecked. |
static void |
swallow(Throwable caught)
Will regurgitate any
Error - otherwise will dutifully and silently swallow whole. |
static <T> boolean |
test(CheckedPredicate<T,?> predicate,
T value)
Invokes
CheckedPredicate.test(Object) catching any checked
Exceptions rethrowing as unchecked. |
static RuntimeException |
throwAsUnchecked(Throwable throwable)
This fugly method relies on erasure to trick the compiler, allowing you to throw any checked
exception without declaring so on the surrounding method.
|
static RuntimeException |
uncheck(Throwable caught)
Converts
Throwables to RuntimeExceptions. |
static <T,U> BiConsumer<T,U> |
uncheckBiConsumer(CheckedBiConsumer<T,U,?> consumer)
Convert a
CheckedBiConsumer into a BiConsumer. |
static <T,U,R> BiFunction<T,U,R> |
uncheckBiFunction(CheckedBiFunction<T,U,R,?> function)
Convert a
CheckedBiFunction into a BiFunction. |
static <T> BinaryOperator<T> |
uncheckBinaryOperator(CheckedBinaryOperator<T,?> operator)
Converts a
CheckedBinaryOperator into a BinaryOperator. |
static <T> Comparator<T> |
uncheckComparator(CheckedComparator<T,?> comparator)
Converts a
CheckedComparator into an unchecked Comparator. |
static <T> Consumer<T> |
uncheckConsumer(CheckedConsumer<T,?> consumer)
Converts
CheckedConsumer to Consumer |
static <T,R> Function<T,R> |
uncheckFunction(CheckedFunction<T,R,?> function)
Converts a
CheckedFunction into a Function. |
static IntConsumer |
uncheckIntConsumer(CheckedIntConsumer<?> consumer)
Converts
CheckedIntConsumer to IntConsumer |
static <T> Predicate<T> |
uncheckPredicate(CheckedPredicate<T,?> predicate)
Converts a
CheckedPredicate into a Predicate. |
static Runnable |
uncheckRunnable(CheckedRunnable<?> runnable)
Convert a
CheckedRunnable into a Runnable |
static <T> Supplier<T> |
uncheckSupplier(CheckedSupplier<T,?> supplier)
Convert a
CheckedSupplier into an unchecked Supplier. |
static <T> ToDoubleFunction<T> |
uncheckToDoubleFunction(CheckedToDoubleFunction<T,?> function)
Converts a
CheckedToDoubleFunction into a ToDoubleFunction. |
static <T,U> ToIntBiFunction<T,U> |
uncheckToIntBiFunction(CheckedToIntBiFunction<T,U,?> function)
Converts a
CheckedToIntBiFunction into an unchecked ToIntBiFunction. |
static <T> ToIntFunction<T> |
uncheckToIntFunction(CheckedToIntFunction<T,?> function)
Converts a
CheckedToIntFunction into a ToIntFunction. |
static <T> ToLongFunction<T> |
uncheckToLongFunction(CheckedToLongFunction<T,?> function)
Converts a
CheckedToLongFunction into a ToLongFunction. |
static Throwable |
unwrap(InvocationTargetException throwable)
Recursively find the wrapped throwable
|
static Throwable |
unwrap(Throwable throwable)
Attempts to unwrap invocation and unchecked exceptions to their underlying cause.
|
static Throwable |
unwrap(UndeclaredThrowableException throwable)
Recursively find the wrapped throwable
|
static URI |
uri(CharSequence ʊri) |
static URI |
uri(URL earl)
Invokes
URL.toURI(), catching any URISyntaxException and rethrowing
wrapped as UncheckedUriSyntaxException |
static URL |
url(CharSequence earl) |
static URL |
url(String protocol,
String host,
int port,
String path,
URLStreamHandler handler) |
public static final Thread.UncaughtExceptionHandler RETHROWING
Thread.UncaughtExceptionHandler that simply rethrows,
wrapping in an appropriate unchecked if necessarypublic static final Thread.UncaughtExceptionHandler SWALLOWING
Thread.UncaughtExceptionHandler that simply swallows all exceptions,
except subclasses of Error, which are rethrown.public static URL url(CharSequence earl)
earl - the text URLURL representationpublic static URL url(String protocol, String host, int port, String path, URLStreamHandler handler)
protocol - e.g. https, ftp, sctphost - hostname or IPport - 0 to 65536path - the "file" portion of the URLhandler - optional URLStreamHandlerURL representationpublic static URI uri(CharSequence ʊri)
ʊri - the text URI (as Earl is to URL, so ʊri (as in Uri Geller) is to URI)URI representationpublic static URI uri(URL earl)
URL.toURI(), catching any URISyntaxException and rethrowing
wrapped as UncheckedUriSyntaxExceptionpublic static void swallow(Throwable caught)
Error - otherwise will dutifully and silently swallow whole.
Gulping of InterruptedExceptions will result in the current Thread's interrupt
flag being reset.
Caution: used carelessly/incorrectly this foul method will inevitably lead to a frustrating
debugging session resulting in plenty of "doh"/"wtf" but never a "eureka" moment. Shooting of
messenger is in breach of license terms.caught - the caught unmentionablepublic static void resetIfInterrupt(Throwable thrown)
InterruptedException clears the interrupt flag,
this merely resets the flag IFF the thrown parameter is
an instance of InterruptedException.thrown - possible InterruptedExceptionpublic static RuntimeException rethrow(Throwable thrown)
Errors or RuntimeExceptions, wraps
checked exceptions appropriatelythrown - the caught throwable to be rethrown as uncheckedthrow Exceptional.rethrow(e) for methods than
have return values.throwAsUnchecked(Throwable)public static void run(CheckedRunnable<?> runnable)
CheckedRunnable.run() catching any checked
Exceptions rethrowing them as unchecked.runnable - the checked runnable to runpublic static Runnable uncheckRunnable(CheckedRunnable<?> runnable)
CheckedRunnable into a Runnablerunnable - the checked runnable to wraprunnable argumentpublic static <T> T call(Callable<T> callable)
Callable.call() catching any checked
Exceptions rethrowing as unchecked.public static RuntimeException uncheck(Throwable caught)
Converts Throwables to RuntimeExceptions.
If the supplied Throwable is already a RuntimeException, then it's simply cast and returned.
Error subclasses will be wrapped in an UncheckedException.
The interrupt flag will be reset IFF caught instanceof InterruptedException
caught - any ThrowableRuntimeException, typically a subclass of UncheckedException or an
UncheckedIOExceptionrethrow(Throwable)public static <T> Consumer<T> uncheckConsumer(CheckedConsumer<T,?> consumer)
CheckedConsumer to ConsumerT - the consumed typeconsumer - a consumer that declares checked exception(s)Consumerpublic static <T> void accept(CheckedConsumer<T,?> consumer, T value)
CheckedConsumer.accept(Object) catching any checked
Exceptions rethrowing as unchecked.T - the consumed typeconsumer - the consumer of the valuevalue - the value to be consumedpublic static IntConsumer uncheckIntConsumer(CheckedIntConsumer<?> consumer)
CheckedIntConsumer to IntConsumerconsumer - a consumer of primitive integer that declares checked exception(s)IntConsumerpublic static void accept(CheckedIntConsumer<?> consumer, int value)
CheckedIntConsumer.accept(int) catching any checked
Exceptions rethrowing as unchecked.consumer - the consumer of the int valuevalue - the value to be consumedpublic static <T,U> BiConsumer<T,U> uncheckBiConsumer(CheckedBiConsumer<T,U,?> consumer)
CheckedBiConsumer into a BiConsumer.T - first argument typeU - last argument typeconsumer - the checked consumerconsumer argumentpublic static <T,U> void accept(CheckedBiConsumer<T,U,?> consumer, T t, U u)
CheckedConsumer.accept(Object) catching any checked
Exceptions rethrowing as unchecked.T - first argument typeU - last argument typeconsumer - the consumer of the valuet - first argument to be consumedu - last argument to be consumedpublic static <T,R> Function<T,R> uncheckFunction(CheckedFunction<T,R,?> function)
CheckedFunction into a Function.T - argument typeR - return typefunction - the checked functionfunction argument.public static <T,R> R apply(CheckedFunction<T,R,?> function, T argument)
CheckedFunction.apply(Object) catching any checked
Exceptions rethrowing as unchecked.T - argument typeR - return typefunction - the checked function to invoke with the argumentargument - the argument to apply to the functionargument to functionpublic static <T,U,R> BiFunction<T,U,R> uncheckBiFunction(CheckedBiFunction<T,U,R,?> function)
CheckedBiFunction into a BiFunction.T - first argument typeU - last argument typeR - return typefunction - the checked bi-functionfunction argument.public static <T,U,R> R apply(CheckedBiFunction<T,U,R,?> function, T t, U u)
CheckedFunction.apply(Object) catching any checked
Exceptions rethrowing as unchecked.T - first argument typeU - last argument typeR - return typefunction - the checked function to invoke with the argumentt - the first argument to apply to the functionu - the second argument to apply to the functionfunction to the arguments t and upublic static <T> BinaryOperator<T> uncheckBinaryOperator(CheckedBinaryOperator<T,?> operator)
CheckedBinaryOperator into a BinaryOperator.T - operator typeoperator - the checked binary operatoroperator argumentpublic static <T> ToDoubleFunction<T> uncheckToDoubleFunction(CheckedToDoubleFunction<T,?> function)
CheckedToDoubleFunction into a ToDoubleFunction.T - argument typefunction - the checked to-double-functionfunction argument.public static <T> double applyAsDouble(CheckedToDoubleFunction<T,?> function, T t)
CheckedToDoubleFunction.applyAsDouble(Object) catching any checked
Exceptions rethrowing as unchecked.T - argument typefunction - the checked to-double functiont - the function argumentfunction to argument tpublic static <T> ToIntFunction<T> uncheckToIntFunction(CheckedToIntFunction<T,?> function)
CheckedToIntFunction into a ToIntFunction.T - argument typefunction - the checked to-int-functionfunction argument.public static <T> int applyAsInt(CheckedToIntFunction<T,?> function, T t)
CheckedToIntFunction.applyAsInt(Object) catching any checked
Exceptions rethrowing as unchecked.T - argument typefunction - the checked to-int functiont - the function argumentfunction to argument tpublic static <T> ToLongFunction<T> uncheckToLongFunction(CheckedToLongFunction<T,?> function)
CheckedToLongFunction into a ToLongFunction.T - argument typefunction - the checked to-long-functionfunction argument.public static <T> long applyAsLong(CheckedToLongFunction<T,?> function, T t)
CheckedToLongFunction.applyAsLong(Object) catching any checked
Exceptions rethrowing as unchecked.T - argument typefunction - the checked to-long functiont - the function argumentfunction to argument tpublic static <T> Supplier<T> uncheckSupplier(CheckedSupplier<T,?> supplier)
CheckedSupplier into an unchecked Supplier.T - supplied typesupplier - the checked supplier to wrapsupplier argumentpublic static <T> T get(CheckedSupplier<T,?> supplier)
CheckedSupplier.get() catching any checked
Exceptions rethrowing as unchecked.T - supplied typesupplier - the checked suppliersupplier argumentpublic static <T> Predicate<T> uncheckPredicate(CheckedPredicate<T,?> predicate)
CheckedPredicate into a Predicate.T - tested typepredicate - the checked predicate to wrappredicate argumentpublic static <T> boolean test(CheckedPredicate<T,?> predicate, T value)
CheckedPredicate.test(Object) catching any checked
Exceptions rethrowing as unchecked.T - tested typepredicate - the checked predicate to test on the valuevalue - the value to be testedpublic static <T> Comparator<T> uncheckComparator(CheckedComparator<T,?> comparator)
CheckedComparator into an unchecked Comparator.T - compared typecomparator - the checked comparator to wrapcomparator argumentpublic static <T,U> ToIntBiFunction<T,U> uncheckToIntBiFunction(CheckedToIntBiFunction<T,U,?> function)
CheckedToIntBiFunction into an unchecked ToIntBiFunction.T - first argument typeU - last argument typefunction - the checked bi-functionpublic static <T,U> int applyAsInt(CheckedToIntBiFunction<T,U,?> function, T t, U u)
CheckedFunction.apply(Object) catching any checked
Exceptions rethrowing as unchecked.T - first argument typeU - last argument typefunction - the checked function to invoke with the argumentt - the first argument to apply to the functionu - the second argument to apply to the functionfunction to the arguments t and upublic static RuntimeException throwAsUnchecked(Throwable throwable)
This fugly method relies on erasure to trick the compiler, allowing you to throw any checked
exception without declaring so on the surrounding method. You are almost certainly better off
using rethrow(Throwable).
Note: this may well become an obsolete hack in future versions of Java if generics change.
throwable - the Throwable to be thrownthrows throwAsUnchecked(Throwable);
to placate the compiler WRT non-void method returns, and ensure Static Code
Analysis doesn't accuse you of swallowing the exception.rethrow(Throwable)public static Throwable unwrap(Throwable throwable)
Attempts to unwrap invocation and unchecked exceptions to their underlying cause. Unwrapping is recursively applied. The original wrapper exception is added to the suppressed throwables of the returned unwrapped throwable.
Unwraps: InvocationTargetException, UndeclaredThrowableException, UncheckedIOException
and UncheckedException
throwable - to be unwrappedpublic static Throwable unwrap(UndeclaredThrowableException throwable)
Recursively find the wrapped throwable
NOTE: this method now just delegates tounwrap(Throwable) and is therefore superfluous,
as such it will be removed in the next major release without impact to client code; given
this, it is not marked Deprecated to avoid unnecessary upcasting to Throwable.throwable - to be unwrappedUndeclaredThrowableException.getUndeclaredThrowable()unwrap(Throwable)public static Throwable unwrap(InvocationTargetException throwable)
Recursively find the wrapped throwable
NOTE: this method now just delegates tounwrap(Throwable) and is therefore superfluous,
as such it will be removed in the next major release without impact to client code; given
this, it is not marked Deprecated to avoid unnecessary upcasting to Throwable.throwable - to be unwrappedInvocationTargetException.getTargetException()public static <T> void forEach(Iterable<T> iterable, CheckedConsumer<T,?> consumer)
Iterable.forEach(Consumer) method having unchecked the consumerT - the consumed Iterable's element typeiterable - the Iterable to consume each element ofconsumer - the checked consumer of the Iterable's elementsuncheckConsumer(CheckedConsumer)public static <K,V> void forEach(Map<K,V> map, CheckedBiConsumer<K,V,?> consumer)
Map.forEach(BiConsumer) method having unchecked the bi-consumerK - the consumed Map's key typeV - the consumed Map's value typemap - the entries to consumeconsumer - the consumer of the map's key value pairsuncheckBiConsumer(CheckedBiConsumer)Copyright © 2016–2018 earcam. All rights reserved.