public final class {
public interface ConsumerWithExceptions<T, E extends Exception> { void accept(T t) throws E; }
public interface BiConsumerWithExceptions<T, U, E extends Exception> { void accept(T t, U u) throws E; }
public interface FunctionWithExceptions<T, R, E extends Exception> { R apply(T t) throws E; }
public interface SupplierWithExceptions<T, E extends Exception> { T get() throws E; }
public interface RunnableWithExceptions<E extends Exception> { void run() throws E; }
public static <T, E extends Exception> Consumer<T> rethrowConsumer(ConsumerWithExceptions<T, E> consumer) throws E { return t -> { try { consumer.accept(t); } catch (Exception exception) { throwAsUnchecked(exception); } }; }
public static <T, U, E extends Exception> BiConsumer<T, U> rethrowBiConsumer(BiConsumerWithExceptions<T, U, E> biConsumer) throws E { return (t, u) -> { try { biConsumer.accept(t, u); } catch (Exception exception) { throwAsUnchecked(exception); } }; }
public static <T, R, E extends Exception> Function<T, R> rethrowFunction(FunctionWithExceptions<T, R, E> function) throws E { return t -> { try { return function.apply(t); } catch (Exception exception) { throwAsUnchecked(exception); return null; } }; }
public static <T, E extends Exception> Supplier<T> rethrowSupplier(SupplierWithExceptions<T, E> function) throws E { return () -> { try { return function.get(); } catch (Exception exception) { throwAsUnchecked(exception); return null; } }; }
public static void uncheck(RunnableWithExceptions t) { try { t.run(); } catch (Exception exception) { throwAsUnchecked(exception); } }
public static <R, E extends Exception> R uncheck(SupplierWithExceptions<R, E> supplier) { try { return supplier.get(); } catch (Exception exception) { throwAsUnchecked(exception); return null; } }
public static <T, R, E extends Exception> R uncheck(FunctionWithExceptions<T, R, E> function, T t) { try { return function.apply(t); } catch (Exception exception) { throwAsUnchecked(exception); return null; } }
@SuppressWarnings ("unchecked") private static <E extends Throwable> void throwAsUnchecked(Exception exception) throws E { throw (E)exception; }
}
|
近期评论