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 anIOException
which can be handled as follows:stream.map(Unchecked.function(file -> file.getCanonicalFile())
Each method accepts a functional interface that is defined to throw
Throwable
. CatchingThrowable
means that any method can be wrapped. AnyInvocationTargetException
is extracted and processed recursively. AnyIOException
is converted to anUncheckedIOException
. AnyReflectiveOperationException
is converted to anUncheckedReflectiveOperationException
. AnyError
orRuntimeException
is 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 theBiConsumer
interface.static <T,U,R>
BiFunction<T,U,R>biFunction(CheckedBiFunction<T,U,R> function)
Converts checked exceptions to unchecked based on theBiFunction
interface.static <T> BinaryOperator<T>
binaryOperator(CheckedBinaryOperator<T> function)
Converts checked exceptions to unchecked based on theBinaryOperator
interface.static <T,U>
BiPredicate<T,U>biPredicate(CheckedBiPredicate<T,U> predicate)
Converts checked exceptions to unchecked based on theBiPredicate
interface.static <T> Consumer<T>
consumer(CheckedConsumer<T> consumer)
Converts checked exceptions to unchecked based on theConsumer
interface.static <T,R>
Function<T,R>function(CheckedFunction<T,R> function)
Converts checked exceptions to unchecked based on theFunction
interface.static <T> Predicate<T>
predicate(CheckedPredicate<T> predicate)
Converts checked exceptions to unchecked based on thePredicate
interface.static RuntimeException
propagate(Throwable throwable)
Propagatesthrowable
as-is if possible, or by wrapping in aRuntimeException
if not.static Runnable
runnable(CheckedRunnable runnable)
Converts checked exceptions to unchecked based on theRunnable
interface.static <R> Supplier<R>
supplier(CheckedSupplier<R> supplier)
Converts checked exceptions to unchecked based on theSupplier
interface.static <T> UnaryOperator<T>
unaryOperator(CheckedUnaryOperator<T> function)
Converts checked exceptions to unchecked based on theUnaryOperator
interface.static void
wrap(CheckedRunnable block)
Wraps a block of code, converting checked exceptions to unchecked.static <T> T
wrap(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
UncheckedIOException
orRuntimeException
as 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
UncheckedIOException
orRuntimeException
as 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 theRunnable
interface.This wraps the specified runnable returning an instance that handles checked exceptions. If a checked exception is thrown it is converted to an
UncheckedIOException
orRuntimeException
as 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 theFunction
interface.This wraps the specified function returning an instance that handles checked exceptions. If a checked exception is thrown it is converted to an
UncheckedIOException
orRuntimeException
as 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 theBiFunction
interface.This wraps the specified function returning an instance that handles checked exceptions. If a checked exception is thrown it is converted to an
UncheckedIOException
orRuntimeException
as 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 theUnaryOperator
interface.This wraps the specified operator returning an instance that handles checked exceptions. If a checked exception is thrown it is converted to an
UncheckedIOException
orRuntimeException
as 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 theBinaryOperator
interface.This wraps the specified operator returning an instance that handles checked exceptions. If a checked exception is thrown it is converted to an
UncheckedIOException
orRuntimeException
as 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 thePredicate
interface.This wraps the specified predicate returning an instance that handles checked exceptions. If a checked exception is thrown it is converted to an
UncheckedIOException
orRuntimeException
as 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 theBiPredicate
interface.This wraps the specified predicate returning an instance that handles checked exceptions. If a checked exception is thrown it is converted to an
UncheckedIOException
orRuntimeException
as 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 theConsumer
interface.This wraps the specified consumer returning an instance that handles checked exceptions. If a checked exception is thrown it is converted to an
UncheckedIOException
orRuntimeException
as 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 theBiConsumer
interface.This wraps the specified consumer returning an instance that handles checked exceptions. If a checked exception is thrown it is converted to an
UncheckedIOException
orRuntimeException
as 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 theSupplier
interface.This wraps the specified supplier returning an instance that handles checked exceptions. If a checked exception is thrown it is converted to an
UncheckedIOException
orRuntimeException
as 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)
Propagatesthrowable
as-is if possible, or by wrapping in aRuntimeException
if not.- If
throwable
is anInvocationTargetException
the cause is extracted and processed recursively. - If
throwable
is anError
orRuntimeException
it is propagated as-is. - If
throwable
is anIOException
it is wrapped inUncheckedIOException
and thrown. - If
throwable
is anReflectiveOperationException
it is wrapped inUncheckedReflectiveOperationException
and thrown. - Otherwise
throwable
is wrapped in aRuntimeException
and thrown.
T foo() { try { return methodWithCheckedException(); } catch (Exception e) { throw Unchecked.propagate(e); } }
- Parameters:
throwable
- theThrowable
to propagate- Returns:
- nothing; this method always throws an exception
- If
-
-