Package it.tidalwave.util
Class FunctionalCheckedExceptionWrappers
java.lang.Object
it.tidalwave.util.FunctionalCheckedExceptionWrappers
A collections of utility methods for simplifying the syntax of lambda expressions with APIs that don't accept
checked exceptions (such as
Stream): they provide wrapped functions that have no checked
exception in the signature and whose implementation delegates to the original function wrapping an eventual checked
exception into a RuntimeException. For instance, given the following method that could not be used as a
Stream.filter(Predicate) argument:
private boolean matchEven (final int number)
throws Exception
{
if (number == 13)
{
throw new Exception("13!");
}
return number % 2 == 0;
}
working code can be written as:
try
{
List<Integer> numbers = IntStream.rangeClosed(1, 20)
.mapToObj(Integer::valueOf)
.filter(_p(this::matchEven)) // note the wrapper here
.collect(Collectors.toList());
...
}
catch (RuntimeException e)
{
...
}
Any checked exception is wrapped by a RuntimeException, but IOException is wrapped by a
UncheckedIOException.- Since:
- 3.2-ALPHA-1
- Author:
- Fabrizio Giudici
- Status: draft API
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Consumer<T> _c(it.tidalwave.util.FunctionalCheckedExceptionWrappers.ConsumerWithException<? super T> consumer) A wrapper for aConsumerthat catches exceptions and wraps them intoRuntimeExceptions.static <T,R> Function <T, R> _f(it.tidalwave.util.FunctionalCheckedExceptionWrappers.FunctionWithException<? super T, ? extends R> function) A wrapper for aFunctionthat catches exceptions and wraps them intoRuntimeExceptions.static <T> Predicate<T> _p(it.tidalwave.util.FunctionalCheckedExceptionWrappers.PredicateWithException<? super T> predicate) A wrapper for aPredicatethat catches exceptions and wraps them intoRuntimeExceptions.static Runnable_r(it.tidalwave.util.FunctionalCheckedExceptionWrappers.RunnableWithException runnable) A wrapper for an equivalent ofRunnablethat catches exceptions and wraps them intoRuntimeExceptions.static <T> Supplier<T> _s(it.tidalwave.util.FunctionalCheckedExceptionWrappers.SupplierWithException<? extends T> supplier) A wrapper for aSupplierthat catches exceptions and wraps them intoRuntimeExceptions.static RuntimeExceptionWraps a throwable with aRuntimeException.
-
Constructor Details
-
FunctionalCheckedExceptionWrappers
public FunctionalCheckedExceptionWrappers()
-
-
Method Details
-
_f
@Nonnull public static <T,R> Function<T,R> _f(@Nonnull it.tidalwave.util.FunctionalCheckedExceptionWrappers.FunctionWithException<? super T, ? extends R> function) A wrapper for aFunctionthat catches exceptions and wraps them intoRuntimeExceptions.- Type Parameters:
T- the type of the function argumentR- the type of the function return value- Parameters:
function- theFunctionto wrap.- Returns:
- the wrapped
Function
-
_c
@Nonnull public static <T> Consumer<T> _c(@Nonnull it.tidalwave.util.FunctionalCheckedExceptionWrappers.ConsumerWithException<? super T> consumer) A wrapper for aConsumerthat catches exceptions and wraps them intoRuntimeExceptions.- Type Parameters:
T- the type of theConsumerargument- Parameters:
consumer- theConsumerto wrap.- Returns:
- the wrapped
Consumer
-
_s
@Nonnull public static <T> Supplier<T> _s(@Nonnull it.tidalwave.util.FunctionalCheckedExceptionWrappers.SupplierWithException<? extends T> supplier) A wrapper for aSupplierthat catches exceptions and wraps them intoRuntimeExceptions.- Type Parameters:
T- the type of theSupplierargument- Parameters:
supplier- theSupplierto wrap.- Returns:
- the wrapped
Supplier
-
_p
@Nonnull public static <T> Predicate<T> _p(@Nonnull it.tidalwave.util.FunctionalCheckedExceptionWrappers.PredicateWithException<? super T> predicate) A wrapper for aPredicatethat catches exceptions and wraps them intoRuntimeExceptions.- Type Parameters:
T- the type of thePredicateargument- Parameters:
predicate- thePredicateto wrap.- Returns:
- the wrapped
Predicate
-
_r
@Nonnull public static Runnable _r(@Nonnull it.tidalwave.util.FunctionalCheckedExceptionWrappers.RunnableWithException runnable) A wrapper for an equivalent ofRunnablethat catches exceptions and wraps them intoRuntimeExceptions.- Parameters:
runnable- theRunnableto wrap.- Returns:
- the wrapped
Predicate - Since:
- 3.2-ALPHA-12
-
wrappedException
Wraps a throwable with aRuntimeException. Unchecked exceptions are not wrapped;IOExceptionis wrapped withUncheckedIOException.- Parameters:
e- the exception to wrap- Returns:
- the wrapped exception
-