Class Unchecked
- java.lang.Object
-
- com.opengamma.strata.collect.Unchecked
-
public final class Unchecked extends Object
Static utility methods that convert checked exceptions to unchecked.Two
wrap()methods are provided that can wrap an arbitrary piece of logic and convert checked exceptions to unchecked.A number of other methods are provided that allow a lambda block to be decorated to avoid handling checked exceptions. For example, the method
File.getCanonicalFile()throws anIOExceptionwhich can be handled as follows:stream.map(Unchecked.function(file -> file.getCanonicalFile())
Each method accepts a functional interface that is defined to throw
Throwable. CatchingThrowablemeans that any method can be wrapped. AnyInvocationTargetExceptionis extracted and processed recursively. AnyIOExceptionis converted to anUncheckedIOException. AnyReflectiveOperationExceptionis converted to anUncheckedReflectiveOperationException. AnyErrororRuntimeExceptionis re-thrown without alteration. Any other exception is wrapped in aRuntimeException.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T,U>
BiConsumer<T,U>biConsumer(CheckedBiConsumer<T,U> consumer)Converts checked exceptions to unchecked based on theBiConsumerinterface.static <T,U,R>
BiFunction<T,U,R>biFunction(CheckedBiFunction<T,U,R> function)Converts checked exceptions to unchecked based on theBiFunctioninterface.static <T> BinaryOperator<T>binaryOperator(CheckedBinaryOperator<T> function)Converts checked exceptions to unchecked based on theBinaryOperatorinterface.static <T,U>
BiPredicate<T,U>biPredicate(CheckedBiPredicate<T,U> predicate)Converts checked exceptions to unchecked based on theBiPredicateinterface.static <T> Consumer<T>consumer(CheckedConsumer<T> consumer)Converts checked exceptions to unchecked based on theConsumerinterface.static <T,R>
Function<T,R>function(CheckedFunction<T,R> function)Converts checked exceptions to unchecked based on theFunctioninterface.static <T> Predicate<T>predicate(CheckedPredicate<T> predicate)Converts checked exceptions to unchecked based on thePredicateinterface.static RuntimeExceptionpropagate(Throwable throwable)Propagatesthrowableas-is if possible, or by wrapping in aRuntimeExceptionif not.static Runnablerunnable(CheckedRunnable runnable)Converts checked exceptions to unchecked based on theRunnableinterface.static <R> Supplier<R>supplier(CheckedSupplier<R> supplier)Converts checked exceptions to unchecked based on theSupplierinterface.static <T> UnaryOperator<T>unaryOperator(CheckedUnaryOperator<T> function)Converts checked exceptions to unchecked based on theUnaryOperatorinterface.static voidwrap(CheckedRunnable block)Wraps a block of code, converting checked exceptions to unchecked.static <T> Twrap(CheckedSupplier<T> block)Wraps a block of code, converting checked exceptions to unchecked.
-
-
-
Method Detail
-
wrap
public static void wrap(CheckedRunnable block)
Wraps a block of code, converting checked exceptions to unchecked.Unchecked.wrap(() -> { // any code that throws a checked exception }If a checked exception is thrown it is converted to an
UncheckedIOExceptionorRuntimeExceptionas appropriate.- Parameters:
block- the code block to wrap- Throws:
UncheckedIOException- if an IO exception occursRuntimeException- if an exception occurs
-
wrap
public static <T> T wrap(CheckedSupplier<T> block)
Wraps a block of code, converting checked exceptions to unchecked.Unchecked.wrap(() -> { // any code that throws a checked exception }If a checked exception is thrown it is converted to an
UncheckedIOExceptionorRuntimeExceptionas appropriate.- Type Parameters:
T- the type of the result- Parameters:
block- the code block to wrap- Returns:
- the result of invoking the block
- Throws:
UncheckedIOException- if an IO exception occursRuntimeException- if an exception occurs
-
runnable
public static Runnable runnable(CheckedRunnable runnable)
Converts checked exceptions to unchecked based on theRunnableinterface.This wraps the specified runnable returning an instance that handles checked exceptions. If a checked exception is thrown it is converted to an
UncheckedIOExceptionorRuntimeExceptionas appropriate.- Parameters:
runnable- the runnable to be decorated- Returns:
- the runnable instance that handles checked exceptions
-
function
public static <T,R> Function<T,R> function(CheckedFunction<T,R> function)
Converts checked exceptions to unchecked based on theFunctioninterface.This wraps the specified function returning an instance that handles checked exceptions. If a checked exception is thrown it is converted to an
UncheckedIOExceptionorRuntimeExceptionas appropriate.- Type Parameters:
T- the input type of the functionR- the return type of the function- Parameters:
function- the function to be decorated- Returns:
- the function instance that handles checked exceptions
-
biFunction
public static <T,U,R> BiFunction<T,U,R> biFunction(CheckedBiFunction<T,U,R> function)
Converts checked exceptions to unchecked based on theBiFunctioninterface.This wraps the specified function returning an instance that handles checked exceptions. If a checked exception is thrown it is converted to an
UncheckedIOExceptionorRuntimeExceptionas appropriate.- Type Parameters:
T- the first input type of the functionU- the second input type of the functionR- the return type of the function- Parameters:
function- the function to be decorated- Returns:
- the function instance that handles checked exceptions
-
unaryOperator
public static <T> UnaryOperator<T> unaryOperator(CheckedUnaryOperator<T> function)
Converts checked exceptions to unchecked based on theUnaryOperatorinterface.This wraps the specified operator returning an instance that handles checked exceptions. If a checked exception is thrown it is converted to an
UncheckedIOExceptionorRuntimeExceptionas appropriate.- Type Parameters:
T- the type of the operator- Parameters:
function- the function to be decorated- Returns:
- the function instance that handles checked exceptions
-
binaryOperator
public static <T> BinaryOperator<T> binaryOperator(CheckedBinaryOperator<T> function)
Converts checked exceptions to unchecked based on theBinaryOperatorinterface.This wraps the specified operator returning an instance that handles checked exceptions. If a checked exception is thrown it is converted to an
UncheckedIOExceptionorRuntimeExceptionas appropriate.- Type Parameters:
T- the type of the operator- Parameters:
function- the function to be decorated- Returns:
- the function instance that handles checked exceptions
-
predicate
public static <T> Predicate<T> predicate(CheckedPredicate<T> predicate)
Converts checked exceptions to unchecked based on thePredicateinterface.This wraps the specified predicate returning an instance that handles checked exceptions. If a checked exception is thrown it is converted to an
UncheckedIOExceptionorRuntimeExceptionas appropriate.- Type Parameters:
T- the type of the predicate- Parameters:
predicate- the predicate to be decorated- Returns:
- the predicate instance that handles checked exceptions
-
biPredicate
public static <T,U> BiPredicate<T,U> biPredicate(CheckedBiPredicate<T,U> predicate)
Converts checked exceptions to unchecked based on theBiPredicateinterface.This wraps the specified predicate returning an instance that handles checked exceptions. If a checked exception is thrown it is converted to an
UncheckedIOExceptionorRuntimeExceptionas appropriate.- Type Parameters:
T- the first type of the predicateU- the second type of the predicate- Parameters:
predicate- the predicate to be decorated- Returns:
- the predicate instance that handles checked exceptions
-
consumer
public static <T> Consumer<T> consumer(CheckedConsumer<T> consumer)
Converts checked exceptions to unchecked based on theConsumerinterface.This wraps the specified consumer returning an instance that handles checked exceptions. If a checked exception is thrown it is converted to an
UncheckedIOExceptionorRuntimeExceptionas appropriate.- Type Parameters:
T- the type of the consumer- Parameters:
consumer- the consumer to be decorated- Returns:
- the consumer instance that handles checked exceptions
-
biConsumer
public static <T,U> BiConsumer<T,U> biConsumer(CheckedBiConsumer<T,U> consumer)
Converts checked exceptions to unchecked based on theBiConsumerinterface.This wraps the specified consumer returning an instance that handles checked exceptions. If a checked exception is thrown it is converted to an
UncheckedIOExceptionorRuntimeExceptionas appropriate.- Type Parameters:
T- the first type of the consumerU- the second type of the consumer- Parameters:
consumer- the consumer to be decorated- Returns:
- the consumer instance that handles checked exceptions
-
supplier
public static <R> Supplier<R> supplier(CheckedSupplier<R> supplier)
Converts checked exceptions to unchecked based on theSupplierinterface.This wraps the specified supplier returning an instance that handles checked exceptions. If a checked exception is thrown it is converted to an
UncheckedIOExceptionorRuntimeExceptionas appropriate.- Type Parameters:
R- the result type of the supplier- Parameters:
supplier- the supplier to be decorated- Returns:
- the supplier instance that handles checked exceptions
-
propagate
public static RuntimeException propagate(Throwable throwable)
Propagatesthrowableas-is if possible, or by wrapping in aRuntimeExceptionif not.- If
throwableis anInvocationTargetExceptionthe cause is extracted and processed recursively. - If
throwableis anErrororRuntimeExceptionit is propagated as-is. - If
throwableis anIOExceptionit is wrapped inUncheckedIOExceptionand thrown. - If
throwableis anReflectiveOperationExceptionit is wrapped inUncheckedReflectiveOperationExceptionand thrown. - Otherwise
throwableis wrapped in aRuntimeExceptionand thrown.
T foo() { try { return methodWithCheckedException(); } catch (Exception e) { throw Unchecked.propagate(e); } }- Parameters:
throwable- theThrowableto propagate- Returns:
- nothing; this method always throws an exception
- If
-
-