/** * Converts checked exceptions to unchecked based on the {@code UnaryOperator} interface. * <p> * This wraps the specified operator returning an instance that handles checked exceptions. * If a checked exception is thrown it is converted to an {@link UncheckedIOException} * or {@link RuntimeException} as appropriate. * * @param <T> the type of the operator * @param function the function to be decorated * @return the function instance that handles checked exceptions */ public static <T> UnaryOperator<T> unaryOperator(CheckedUnaryOperator<T> function) { return (t) -> { try { return function.apply(t); } catch (Throwable ex) { throw propagate(ex); } }; }
/** * Converts checked exceptions to unchecked based on the {@code Runnable} interface. * <p> * This wraps the specified runnable returning an instance that handles checked exceptions. * If a checked exception is thrown it is converted to an {@link UncheckedIOException} * or {@link RuntimeException} as appropriate. * * @param runnable the runnable to be decorated * @return the runnable instance that handles checked exceptions */ public static Runnable runnable(CheckedRunnable runnable) { return () -> { try { runnable.run(); } catch (Throwable ex) { throw propagate(ex); } }; }
/** * Converts checked exceptions to unchecked based on the {@code BinaryOperator} interface. * <p> * This wraps the specified operator returning an instance that handles checked exceptions. * If a checked exception is thrown it is converted to an {@link UncheckedIOException} * or {@link RuntimeException} as appropriate. * * @param <T> the type of the operator * @param function the function to be decorated * @return the function instance that handles checked exceptions */ public static <T> BinaryOperator<T> binaryOperator(CheckedBinaryOperator<T> function) { return (t, u) -> { try { return function.apply(t, u); } catch (Throwable ex) { throw propagate(ex); } }; }
/** * Converts checked exceptions to unchecked based on the {@code BiPredicate} interface. * <p> * This wraps the specified predicate returning an instance that handles checked exceptions. * If a checked exception is thrown it is converted to an {@link UncheckedIOException} * or {@link RuntimeException} as appropriate. * * @param <T> the first type of the predicate * @param <U> the second type of the predicate * @param predicate the predicate to be decorated * @return the predicate instance that handles checked exceptions */ public static <T, U> BiPredicate<T, U> biPredicate(CheckedBiPredicate<T, U> predicate) { return (t, u) -> { try { return predicate.test(t, u); } catch (Throwable ex) { throw propagate(ex); } }; }
/** * Converts checked exceptions to unchecked based on the {@code Function} interface. * <p> * This wraps the specified function returning an instance that handles checked exceptions. * If a checked exception is thrown it is converted to an {@link UncheckedIOException} * or {@link RuntimeException} as appropriate. * * @param <T> the input type of the function * @param <R> the return type of the function * @param function the function to be decorated * @return the function instance that handles checked exceptions */ public static <T, R> Function<T, R> function(CheckedFunction<T, R> function) { return (t) -> { try { return function.apply(t); } catch (Throwable ex) { throw propagate(ex); } }; }
/** * Wraps a block of code, converting checked exceptions to unchecked. * <pre> * Unchecked.wrap(() -> { * // any code that throws a checked exception * } * </pre> * <p> * If a checked exception is thrown it is converted to an {@link UncheckedIOException} * or {@link RuntimeException} as appropriate. * * @param <T> the type of the result * @param block the code block to wrap * @return the result of invoking the block * @throws UncheckedIOException if an IO exception occurs * @throws RuntimeException if an exception occurs */ public static <T> T wrap(CheckedSupplier<T> block) { try { return block.get(); } catch (Throwable ex) { throw propagate(ex); } }
/** * Converts checked exceptions to unchecked based on the {@code Predicate} interface. * <p> * This wraps the specified predicate returning an instance that handles checked exceptions. * If a checked exception is thrown it is converted to an {@link UncheckedIOException} * or {@link RuntimeException} as appropriate. * * @param <T> the type of the predicate * @param predicate the predicate to be decorated * @return the predicate instance that handles checked exceptions */ public static <T> Predicate<T> predicate(CheckedPredicate<T> predicate) { return (t) -> { try { return predicate.test(t); } catch (Throwable ex) { throw propagate(ex); } }; }
/** * Converts checked exceptions to unchecked based on the {@code Consumer} interface. * <p> * This wraps the specified consumer returning an instance that handles checked exceptions. * If a checked exception is thrown it is converted to an {@link UncheckedIOException} * or {@link RuntimeException} as appropriate. * * @param <T> the type of the consumer * @param consumer the consumer to be decorated * @return the consumer instance that handles checked exceptions */ public static <T> Consumer<T> consumer(CheckedConsumer<T> consumer) { return (t) -> { try { consumer.accept(t); } catch (Throwable ex) { throw propagate(ex); } }; }
/** * Converts checked exceptions to unchecked based on the {@code Supplier} interface. * <p> * This wraps the specified supplier returning an instance that handles checked exceptions. * If a checked exception is thrown it is converted to an {@link UncheckedIOException} * or {@link RuntimeException} as appropriate. * * @param <R> the result type of the supplier * @param supplier the supplier to be decorated * @return the supplier instance that handles checked exceptions */ public static <R> Supplier<R> supplier(CheckedSupplier<R> supplier) { return () -> { try { return supplier.get(); } catch (Throwable ex) { throw propagate(ex); } }; }
/** * Converts checked exceptions to unchecked based on the {@code BiConsumer} interface. * <p> * This wraps the specified consumer returning an instance that handles checked exceptions. * If a checked exception is thrown it is converted to an {@link UncheckedIOException} * or {@link RuntimeException} as appropriate. * * @param <T> the first type of the consumer * @param <U> the second type of the consumer * @param consumer the consumer to be decorated * @return the consumer instance that handles checked exceptions */ public static <T, U> BiConsumer<T, U> biConsumer(CheckedBiConsumer<T, U> consumer) { return (t, u) -> { try { consumer.accept(t, u); } catch (Throwable ex) { throw propagate(ex); } }; }
/** * Wraps a block of code, converting checked exceptions to unchecked. * <pre> * Unchecked.wrap(() -> { * // any code that throws a checked exception * } * </pre> * <p> * If a checked exception is thrown it is converted to an {@link UncheckedIOException} * or {@link RuntimeException} as appropriate. * * @param block the code block to wrap * @throws UncheckedIOException if an IO exception occurs * @throws RuntimeException if an exception occurs */ public static void wrap(CheckedRunnable block) { try { block.run(); } catch (Throwable ex) { throw propagate(ex); } }
/** * Converts checked exceptions to unchecked based on the {@code BiFunction} interface. * <p> * This wraps the specified function returning an instance that handles checked exceptions. * If a checked exception is thrown it is converted to an {@link UncheckedIOException} * or {@link RuntimeException} as appropriate. * * @param <T> the first input type of the function * @param <U> the second input type of the function * @param <R> the return type of the function * @param function the function to be decorated * @return the function instance that handles checked exceptions */ public static <T, U, R> BiFunction<T, U, R> biFunction(CheckedBiFunction<T, U, R> function) { return (t, u) -> { try { return function.apply(t, u); } catch (Throwable ex) { throw propagate(ex); } }; }
throw propagate(((InvocationTargetException) throwable).getCause()); } else if (throwable instanceof IOException) { throw new UncheckedIOException((IOException) throwable);
Unchecked.propagate(error); fail(); } catch (Error ex) { Unchecked.propagate(argEx); fail(); } catch (IllegalArgumentException ex) { Unchecked.propagate(ioEx); fail(); } catch (UncheckedIOException ex) { Unchecked.propagate(namingEx); fail(); } catch (RuntimeException ex) { Unchecked.propagate(new InvocationTargetException(error)); fail(); } catch (Error ex) { Unchecked.propagate(new InvocationTargetException(argEx)); fail(); } catch (IllegalArgumentException ex) { Unchecked.propagate(new InvocationTargetException(ioEx)); fail(); } catch (UncheckedIOException ex) { Unchecked.propagate(new InvocationTargetException(namingEx));