/** * A wrapper around a Consumer that throws a checked exception. * * @param unsafeConsumer - something that acts like a consumer but throws an exception * @return a consumer that is wrapped in a try-catch converting the checked exception into a runtime exception */ public static <I> Consumer<I> safeConsumer(UnsafeConsumer<I> unsafeConsumer) { return (input) -> { try { unsafeConsumer.accept(input); } catch (Exception e) { throw asRuntimeException(e); } }; }
/** * A wrapper around a Runnable that throws a checked exception. * * @param unsafeRunnable Something that acts like a Runnable but throws an exception * @return A Runnable that is wrapped in a try-catch converting the checked exception into a runtime exception */ public static Runnable safeRunnable(UnsafeRunnable unsafeRunnable) { return () -> { try { unsafeRunnable.run(); } catch (Exception e) { throw asRuntimeException(e); } }; }
/** * A wrapper around a Consumer that throws a checked exception. * * @param unsafeConsumer - something that acts like a consumer but throws an exception * @return a consumer that is wrapped in a try-catch converting the checked exception into a runtime exception */ public static <I> Consumer<I> safeConsumer(UnsafeConsumer<I> unsafeConsumer) { return (input) -> { try { unsafeConsumer.accept(input); } catch (Exception e) { throw asRuntimeException(e); } }; }
/** * A wrapper around a BiConsumer that throws a checked exception. * * @param unsafeSupplier - something that acts like a BiConsumer but throws an exception * @return a consumer that is wrapped in a try-catch converting the checked exception into a runtime exception */ public static <T> Supplier<T> safeSupplier(UnsafeSupplier<T> unsafeSupplier) { return () -> { try { return unsafeSupplier.get(); } catch (Exception e) { throw asRuntimeException(e); } }; }
/** * A wrapper around a BiConsumer that throws a checked exception. * * @param unsafeSupplier - something that acts like a BiConsumer but throws an exception * @return a consumer that is wrapped in a try-catch converting the checked exception into a runtime exception */ public static <T> Supplier<T> safeSupplier(UnsafeSupplier<T> unsafeSupplier) { return () -> { try { return unsafeSupplier.get(); } catch (Exception e) { throw asRuntimeException(e); } }; }
/** * A wrapper around a Runnable that throws a checked exception. * * @param unsafeRunnable Something that acts like a Runnable but throws an exception * @return A Runnable that is wrapped in a try-catch converting the checked exception into a runtime exception */ public static Runnable safeRunnable(UnsafeRunnable unsafeRunnable) { return () -> { try { unsafeRunnable.run(); } catch (Exception e) { throw asRuntimeException(e); } }; }
/** * Takes a functional interface that throws an exception and returns a {@link Function} that deals with that exception by * wrapping in a runtime exception. Useful for APIs that use the standard Java functional interfaces that don't throw checked * exceptions. * * @param unsafeFunction Functional interface that throws checked exception. * @param <T> Input * @param <R> Output * @return New {@link Function} that handles checked exception. */ public static <T, R> Function<T, R> safeFunction(UnsafeFunction<T, R> unsafeFunction) { return t -> { try { return unsafeFunction.apply(t); } catch (Exception e) { throw asRuntimeException(e); } }; }
/** * Takes a functional interface that throws an exception and returns a {@link Function} that deals with that exception by * wrapping in a runtime exception. Useful for APIs that use the standard Java functional interfaces that don't throw checked * exceptions. * * @param unsafeFunction Functional interface that throws checked exception. * @param <T> Input * @param <R> Output * @return New {@link Function} that handles checked exception. */ public static <T, R> Function<T, R> safeFunction(UnsafeFunction<T, R> unsafeFunction) { return t -> { try { return unsafeFunction.apply(t); } catch (Exception e) { throw asRuntimeException(e); } }; }