package utils.stream; import java.util.stream.Stream; /** * Throw unchecker * @author SeregaLBN * how to use - see unit test */ public class Unthrow { @SuppressWarnings("unchecked") static R rethrow(Exception ex) throws E { throw (E) ex; } public static Stream of(Stream stream) throws E { return stream; } public static Stream of2(Stream stream) throws E1, E2 { return stream; } public static Stream of3(Stream stream) throws E1, E2, E3 { return stream; } public static Stream of4(Stream stream) throws E1, E2, E3, E4 { return stream; } ////////////////////////////////// interfaces ProcedureN ////////////////////////////////// /** like as {@link java.lang.Runnable} */ @FunctionalInterface public interface IProc0 { void accept() throws Exception; } /** like as {@link java.util.function.Consumer} */ @FunctionalInterface public interface IProc1 { void accept(T t) throws Exception; } /** like as {@link java.util.function.BiConsumer} */ @FunctionalInterface public interface IProc2 { void accept(T1 t1, T2 t2) throws Exception; } @FunctionalInterface public interface IProc3 { void accept(T1 t1, T2 t2, T3 t3) throws Exception; } ////////////////////////////////// interfaces FunctionN ////////////////////////////////// /** like as {@link java.util.concurrent.Callable} */ @FunctionalInterface public interface IFunc0 { R apply() throws Exception; } /** like as {@link java.util.function.Function} */ @FunctionalInterface public interface IFunc1 { R apply(T t) throws Exception; } /** like as {@link java.util.function.BiFunction} */ @FunctionalInterface public interface IFunc2 { R apply(T1 t1, T2 t2) throws Exception; } @FunctionalInterface public interface IFunc3 { R apply(T1 t1, T2 t2, T3 t3) throws Exception; } ////////////////////////////////// wrap Procedures ////////////////////////////////// public static void wrapProc(IProc0 proc) { try { proc.accept(); } catch (Exception ex) { rethrow(ex); } } public static void wrapProc(IProc1 proc, T t) { try { proc.accept(t); } catch (Exception ex) { rethrow(ex); } } public static void wrapProc(IProc2 proc, T1 t1, T2 t2) { try { proc.accept(t1, t2); } catch (Exception ex) { rethrow(ex); } } public static void wrapProc(IProc3 proc, T1 t1, T2 t2, T3 t3) { try { proc.accept(t1, t2, t3); } catch (Exception ex) { rethrow(ex); } } ////////////////////////////////// wrap Functions ////////////////////////////////// public static R wrap(IFunc0 func) { try { return func.apply(); } catch (Exception ex) { return rethrow(ex); } } public static R wrap(IFunc1 func, T t) { try { return func.apply(t); } catch (Exception ex) { return rethrow(ex); } } public static R wrap(IFunc2 func, T1 t1, T2 t2) { try { return func.apply(t1, t2); } catch (Exception ex) { return rethrow(ex); } } public static R wrap(IFunc3 func, T1 t1, T2 t2, T3 t3) { try { return func.apply(t1, t2, t3); } catch (Exception ex) { return rethrow(ex); } } }