forked from functionaljava/functionaljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTry.java
More file actions
178 lines (162 loc) · 6.11 KB
/
Try.java
File metadata and controls
178 lines (162 loc) · 6.11 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package fj;
import fj.data.IO;
import fj.data.IOFunctions;
import fj.data.Validation;
import fj.function.*;
import java.io.IOException;
import static fj.data.Validation.fail;
import static fj.data.Validation.success;
/**
* Created by mperry on 24/07/2014.
*/
public final class Try {
private Try() {
}
/**
* Promotes the Try0 to a Validation that returns an Exception on the failure side and its result on the success side.
*
* @param t A Try0 to promote
* @return A Validation with an Exception on the failure side and its result on the success side.
*/
@SuppressWarnings("unchecked")
public static <A, E extends Exception> P1<Validation<E, A>> f(final Try0<A, E> t) {
return P.lazy(() -> {
try {
return success(t.f());
} catch (Exception e) {
return fail((E) e);
}
});
}
/**
* Promotes the Try1 to a Validation that returns an Exception on the failure side and its result on the success side.
*
* @param t A Try1 to promote
* @return A Validation with an Exception on the failure side and its result on the success side.
*/
@SuppressWarnings("unchecked")
public static <A, B, E extends Exception> F<A, Validation<E, B>> f(final Try1<A, B, E> t) {
return a -> {
try {
return success(t.f(a));
} catch (Exception e) {
return fail((E) e);
}
};
}
/**
* Promotes the Try2 to a Validation that returns an Exception on the failure side and its result on the success side.
*
* @param t A Try2 to promote
* @return A Validation with an Exception on the failure side and its result on the success side.
*/
@SuppressWarnings("unchecked")
public static <A, B, C, E extends Exception> F2<A, B, Validation<E, C>> f(final Try2<A, B, C, E> t) {
return (a, b) -> {
try {
return success(t.f(a, b));
} catch (Exception e) {
return fail((E) e);
}
};
}
/**
* Promotes the Try3 to a Validation that returns an Exception on the failure side and its result on the success side.
*
* @param t A Try3 to promote
* @return A Validation with an Exception on the failure side and its result on the success side.
*/
@SuppressWarnings("unchecked")
public static <A, B, C, D, E extends Exception> F3<A, B, C, Validation<E, D>> f(final Try3<A, B, C, D, E> t) {
return (a, b, c) -> {
try {
return success(t.f(a, b, c));
} catch (Exception e) {
return fail((E) e);
}
};
}
/**
* Promotes the Try4 to a Validation that returns an Exception on the failure side and its result on the success side.
*
* @param t A Try4 to promote
* @return A Validation with an Exception on the failure side and its result on the success side.
*/
@SuppressWarnings("unchecked")
public static <A, B, C, D, E, Z extends Exception> F4<A, B, C, D, Validation<Z, E>> f(final Try4<A, B, C, D, E, Z> t) {
return (a, b, c, d) -> {
try {
return success(t.f(a, b, c, d));
} catch (Exception ex) {
return fail((Z) ex);
}
};
}
/**
* Promotes the Try5 to a Validation that returns an Exception on the failure side and its result on the success side.
*
* @param t A Try5 to promote
* @return A Validation with an Exception on the failure side and its result on the success side.
*/
@SuppressWarnings("unchecked")
public static <A, B, C, D, E, F, Z extends Exception> F5<A, B, C, D, E, Validation<Z, F>> f(final Try5<A, B, C, D, E, F, Z> t) {
return (a, b, c, d, e) -> {
try {
return success(t.f(a, b, c, d, e));
} catch (Exception ex) {
return fail((Z) ex);
}
};
}
/**
* Promotes the Try6 to a Validation that returns an Exception on the failure side and its result on the success side.
*
* @param t A Try6 to promote
* @return A Validation with an Exception on the failure side and its result on the success side.
*/
@SuppressWarnings("unchecked")
public static <A, B, C, D, E, F, G, Z extends Exception> F6<A, B, C, D, E, F, Validation<Z, G>> f(final Try6<A, B, C, D, E, F, G, Z> t) {
return (a, b, c, d, e, f) -> {
try {
return success(t.f(a, b, c, d, e, f));
} catch (Exception ex) {
return fail((Z) ex);
}
};
}
/**
* Promotes the Try7 to a Validation that returns an Exception on the failure side and its result on the success side.
*
* @param t A Try7 to promote
* @return A Validation with an Exception on the failure side and its result on the success side.
*/
@SuppressWarnings("unchecked")
public static <A, B, C, D, E, F, G, H, Z extends Exception> F7<A, B, C, D, E, F, G, Validation<Z, H>> f(final Try7<A, B, C, D, E, F, G, H, Z> t) {
return (a, b, c, d, e, f, g) -> {
try {
return success(t.f(a, b, c, d, e, f, g));
} catch (Exception ex) {
return fail((Z) ex);
}
};
}
/**
* Promotes the Try8 to a Validation that returns an Exception on the failure side and its result on the success side.
*
* @param t A Try8 to promote
* @return A Validation with an Exception on the failure side and its result on the success side.
*/
@SuppressWarnings("unchecked")
public static <A, B, C, D, E, F, G, H, I, Z extends Exception> F8<A, B, C, D, E, F, G, H, Validation<Z, I>> f(final Try8<A, B, C, D, E, F, G, H, I, Z> t) {
return (a, b, c, d, e, f, g, h) -> {
try {
return success(t.f(a, b, c, d, e, f, g, h));
} catch (Exception ex) {
return fail((Z) ex);
}
};
}
public static <A> IO<A> io(Try0<A, ? extends IOException> t) {
return IOFunctions.fromTry(t);
}
}