-
Notifications
You must be signed in to change notification settings - Fork 4
/
Unthrow.java
148 lines (123 loc) · 4.01 KB
/
Unthrow.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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, E extends Exception> R rethrow(Exception ex) throws E {
throw (E) ex;
}
public static<R, E extends Exception> Stream<R> of(Stream<R> stream) throws E {
return stream;
}
public static<R, E1 extends Exception, E2 extends Exception> Stream<R> of2(Stream<R> stream) throws E1, E2 {
return stream;
}
public static<R, E1 extends Exception, E2 extends Exception, E3 extends Exception> Stream<R> of3(Stream<R> stream) throws E1, E2, E3 {
return stream;
}
public static<R, E1 extends Exception, E2 extends Exception, E3 extends Exception, E4 extends Exception> Stream<R> of4(Stream<R> 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<T> {
void accept(T t) throws Exception;
}
/** like as {@link java.util.function.BiConsumer} */
@FunctionalInterface
public interface IProc2<T1, T2> {
void accept(T1 t1, T2 t2) throws Exception;
}
@FunctionalInterface
public interface IProc3<T1, T2, T3> {
void accept(T1 t1, T2 t2, T3 t3) throws Exception;
}
////////////////////////////////// interfaces FunctionN //////////////////////////////////
/** like as {@link java.util.concurrent.Callable} */
@FunctionalInterface
public interface IFunc0<R> {
R apply() throws Exception;
}
/** like as {@link java.util.function.Function} */
@FunctionalInterface
public interface IFunc1<R, T> {
R apply(T t) throws Exception;
}
/** like as {@link java.util.function.BiFunction} */
@FunctionalInterface
public interface IFunc2<R, T1, T2> {
R apply(T1 t1, T2 t2) throws Exception;
}
@FunctionalInterface
public interface IFunc3<R, T1, T2, T3> {
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 <T> void wrapProc(IProc1<T> proc, T t) {
try {
proc.accept(t);
} catch (Exception ex) {
rethrow(ex);
}
}
public static <T1, T2> void wrapProc(IProc2<T1, T2> proc, T1 t1, T2 t2) {
try {
proc.accept(t1, t2);
} catch (Exception ex) {
rethrow(ex);
}
}
public static <T1, T2, T3> void wrapProc(IProc3<T1, T2, T3> proc, T1 t1, T2 t2, T3 t3) {
try {
proc.accept(t1, t2, t3);
} catch (Exception ex) {
rethrow(ex);
}
}
////////////////////////////////// wrap Functions //////////////////////////////////
public static <R> R wrap(IFunc0<R> func) {
try {
return func.apply();
} catch (Exception ex) {
return rethrow(ex);
}
}
public static <R, T> R wrap(IFunc1<R, T> func, T t) {
try {
return func.apply(t);
} catch (Exception ex) {
return rethrow(ex);
}
}
public static <R, T1, T2> R wrap(IFunc2<R, T1, T2> func, T1 t1, T2 t2) {
try {
return func.apply(t1, t2);
} catch (Exception ex) {
return rethrow(ex);
}
}
public static <R, T1, T2, T3> R wrap(IFunc3<R, T1, T2, T3> func, T1 t1, T2 t2, T3 t3) {
try {
return func.apply(t1, t2, t3);
} catch (Exception ex) {
return rethrow(ex);
}
}
}