Repository navigation
Expand file tree
/
Copy pathEither.java
More file actions
362 lines (323 loc) · 13.8 KB
/
Copy pathEither.java
File metadata and controls
362 lines (323 loc) · 13.8 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package com.jnape.palatable.lambda.adt;
import com.jnape.palatable.lambda.adt.coproduct.CoProduct2;
import com.jnape.palatable.lambda.functions.specialized.checked.CheckedFn1;
import com.jnape.palatable.lambda.functions.specialized.checked.CheckedSupplier;
import com.jnape.palatable.lambda.functor.Bifunctor;
import com.jnape.palatable.lambda.functor.Functor;
import java.util.Objects;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import static com.jnape.palatable.lambda.functions.builtin.fn1.Id.id;
import static com.jnape.palatable.lambda.functions.builtin.fn3.FoldLeft.foldLeft;
import static java.util.Arrays.asList;
/**
* The binary tagged union. General semantics tend to connote "success" values via the right value and "failure" values
* via the left values. <code>Either</code>s are both <code>Functor</code>s over their right value and
* <code>Bifunctor</code>s over both values.
*
* @param <L> The left parameter type
* @param <R> The right parameter type
*/
public abstract class Either<L, R> implements CoProduct2<L, R>, Functor<R>, Bifunctor<L, R> {
private Either() {
}
/**
* Return the value wrapped by this <code>Either</code> if it's a right value; otherwise, return defaultValue.
*
* @param defaultValue the value to return if this is a left
* @return the value wrapped by this Either if right; otherwise, defaultValue
*/
public final R or(R defaultValue) {
return recover(l -> defaultValue);
}
/**
* "Recover" from a left value by applying a recoveryFn to the wrapped value and returning it in the case of a left
* value; otherwise, return the wrapped right value.
*
* @param recoveryFn a function from L to R
* @return either the wrapped value (if right) or the result of the left value applied to recoveryFn
*/
public final R recover(Function<? super L, ? extends R> recoveryFn) {
return match(recoveryFn, id());
}
/**
* Inverse of recover. If this is a right value, apply the wrapped value to forfeitFn and return it; otherwise,
* return the wrapped left value;
*
* @param forfeitFn a function from R to L
* @return either the wrapped value (if left) or the result of the right value applied to forfeitFn
*/
public final L forfeit(Function<? super R, ? extends L> forfeitFn) {
return match(id(), forfeitFn);
}
/**
* Return the wrapped value if this is a right; otherwise, map the wrapped left value to a <code>T</code> and throw
* it.
*
* @param throwableFn a function from L to T
* @param <T> the left parameter type (the throwable type)
* @return the wrapped value if this is a right
* @throws T the result of applying the wrapped left value to throwableFn, if this is a left
*/
public final <T extends Throwable> R orThrow(Function<? super L, ? extends T> throwableFn) throws T {
return match((CheckedFn1<T, L, R>) l -> {
throw throwableFn.apply(l);
}, id());
}
/**
* If this is a right value, apply pred to it. If the result is true, return the same value; otherwise, return the
* result of leftSupplier wrapped as a left value.
* <p>
* If this is a left value, return it.
*
* @param pred the predicate to apply to a right value
* @param leftSupplier the supplier of a left value if pred fails
* @return this if a left value or a right value that pred matches; otherwise, the result of leftSupplier wrapped in
* a left
*/
public final Either<L, R> filter(Function<? super R, Boolean> pred, Supplier<L> leftSupplier) {
return flatMap(r -> pred.apply(r) ? right(r) : left(leftSupplier.get()));
}
/**
* If a right value, unwrap it and apply it to <code>rightFn</code>, returning the resulting
* <code>Either<L ,R></code>. Otherwise, return the left value.
* <p>
* Note that because this monadic form of <code>flatMap</code> only supports mapping over a theoretical right value,
* the resulting <code>Either</code> must be invariant on the same left value to flatten properly.
*
* @param rightFn the function to apply to a right value
* @param <R2> the new right parameter type
* @return the Either resulting from applying rightFn to this right value, or this left value if left
*/
public final <R2> Either<L, R2> flatMap(Function<? super R, ? extends Either<L, R2>> rightFn) {
return flatMap(Either::left, rightFn);
}
/**
* If a right value, apply <code>rightFn</code> to the unwrapped right value and return the resulting
* <code>Either</code>; otherwise, apply the unwrapped left value to leftFn and return the resulting
* <code>Either</code>.
*
* @param leftFn the function to apply if a left value
* @param rightFn the function to apply if a right value
* @param <L2> the new left parameter type
* @param <R2> the new right parameter type
* @return the result of either rightFn or leftFn, depending on whether this is a right or a left
*/
public final <L2, R2> Either<L2, R2> flatMap(Function<? super L, ? extends Either<L2, R2>> leftFn,
Function<? super R, ? extends Either<L2, R2>> rightFn) {
return match(leftFn, rightFn);
}
/**
* Transform an <code>Either<L, R></code> into an <code>Either<R, L></code>, preserving the current
* values.
*
* @return The inverted either
*/
public final Either<R, L> invert() {
return flatMap(Either::right, Either::left);
}
/**
* Given two binary operators over L and R, merge multiple <code>Either<L, R></code>s into a single
* <code>Either<L, R></code>. Note that <code>merge</code> biases towards left values; that is, if any left
* value exists, the result will be a left value, such that only unanimous right values result in an ultimate right
* value.
*
* @param leftFn the binary operator for L
* @param rightFn the binary operator for R
* @param others the other Eithers to merge into this one
* @return the merged Either
*/
@SafeVarargs
public final Either<L, R> merge(BiFunction<? super L, ? super L, ? extends L> leftFn,
BiFunction<? super R, ? super R, ? extends R> rightFn,
Either<L, R>... others) {
return foldLeft((x, y) -> x.match(l1 -> y.match(l2 -> left(leftFn.apply(l1, l2)), r -> left(l1)),
r1 -> y.match(Either::left, r2 -> right(rightFn.apply(r1, r2)))),
this,
asList(others));
}
/**
* Perform side-effects against a wrapped right value, returning back the <code>Either</code> unaltered.
*
* @param rightConsumer the effecting consumer
* @return the Either, unaltered
*/
public Either<L, R> peek(Consumer<R> rightConsumer) {
return peek(l -> {
}, rightConsumer);
}
/**
* Perform side-effects against a wrapped right or left value, returning back the <code>Either</code> unaltered.
*
* @param leftConsumer the effecting consumer for left values
* @param rightConsumer the effecting consumer for right values
* @return the Either, unaltered
*/
public Either<L, R> peek(Consumer<L> leftConsumer, Consumer<R> rightConsumer) {
return flatMap(l -> {
leftConsumer.accept(l);
return this;
}, r -> {
rightConsumer.accept(r);
return this;
});
}
/**
* Given two mapping functions (one from an <code>L</code> to a <code>V</code>, one from an <code>R</code> to a
* <code>V</code>), unwrap the value stored in this <code>Either</code>, apply the appropriate mapping function,
* and return the result.
*
* @param leftFn the left value mapping function
* @param rightFn the right value mapping function
* @param <V> the result type
* @return the result of applying the appropriate mapping function to the wrapped value
*/
public abstract <V> V match(Function<? super L, ? extends V> leftFn, Function<? super R, ? extends V> rightFn);
@Override
public final <R2> Either<L, R2> fmap(Function<? super R, ? extends R2> fn) {
return biMapR(fn);
}
@Override
@SuppressWarnings("unchecked")
public final <L2> Either<L2, R> biMapL(Function<? super L, ? extends L2> fn) {
return (Either<L2, R>) Bifunctor.super.biMapL(fn);
}
@Override
@SuppressWarnings("unchecked")
public final <R2> Either<L, R2> biMapR(Function<? super R, ? extends R2> fn) {
return (Either<L, R2>) Bifunctor.super.biMapR(fn);
}
@Override
public final <L2, R2> Either<L2, R2> biMap(Function<? super L, ? extends L2> leftFn,
Function<? super R, ? extends R2> rightFn) {
return match(l -> left(leftFn.apply(l)), r -> right(rightFn.apply(r)));
}
/**
* In the left case, returns an {@link Optional#empty}; otherwise, returns {@link Optional#ofNullable} around the
* right value.
*
* @return an Optional around the right value, or empty if left
*/
public Optional<R> toOptional() {
return match(__ -> Optional.empty(), Optional::ofNullable);
}
/**
* Convert an {@link Optional}<R> into an <code>Either<L, R></code>, supplying the left value from
* <code>leftFn</code> in the case of {@link Optional#empty()}.
*
* @param optional the optional
* @param leftFn the supplier to use for left values
* @param <L> the left parameter type
* @param <R> the right parameter type
* @return a right value of the contained optional value, or a left value of leftFn's result
*/
public static <L, R> Either<L, R> fromOptional(Optional<R> optional, Supplier<L> leftFn) {
return optional.<Either<L, R>>map(Either::right)
.orElse(left(leftFn.get()));
}
/**
* Attempt to execute the {@link CheckedSupplier}, returning its result in a right value. If the supplier throws an
* exception, apply leftFn to it, wrap it in a left value and return it.
*
* @param supplier the supplier of the right value
* @param leftFn a function mapping E to L
* @param <E> the most contravariant exception that the supplier might throw
* @param <L> the left parameter type
* @param <R> the right parameter type
* @return the supplier result as a right value, or leftFn's mapping result as a left value
*/
@SuppressWarnings("unchecked")
public static <E extends Exception, L, R> Either<L, R> trying(CheckedSupplier<E, ? extends R> supplier,
Function<? super E, ? extends L> leftFn) {
try {
return right(supplier.get());
} catch (Exception e) {
return left(leftFn.apply((E) e));
}
}
/**
* Attempt to execute the {@link CheckedSupplier}, returning its result in a right value. If the supplier throws an
* exception, wrap it in a left value and return it.
*
* @param supplier the supplier of the right value
* @param <E> the left parameter type (the most contravariant exception that supplier might throw)
* @param <R> the right parameter type
* @return the supplier result as a right value, or a left value of the thrown exception
*/
public static <E extends Exception, R> Either<E, R> trying(CheckedSupplier<E, R> supplier) {
return trying(supplier, id());
}
/**
* Static factory method for creating a left value.
*
* @param l the wrapped value
* @param <L> the left parameter type
* @param <R> the right parameter type
* @return a left value of l
*/
public static <L, R> Either<L, R> left(L l) {
return new Left<>(l);
}
/**
* Static factory method for creating a right value.
*
* @param r the wrapped value
* @param <L> the left parameter type
* @param <R> the right parameter type
* @return a right value of r
*/
public static <L, R> Either<L, R> right(R r) {
return new Right<>(r);
}
private static final class Left<L, R> extends Either<L, R> {
private final L l;
private Left(L l) {
this.l = l;
}
@Override
public <V> V match(Function<? super L, ? extends V> leftFn, Function<? super R, ? extends V> rightFn) {
return leftFn.apply(l);
}
@Override
public boolean equals(Object other) {
return other instanceof Left && Objects.equals(l, ((Left) other).l);
}
@Override
public int hashCode() {
return Objects.hash(l);
}
@Override
public String toString() {
return "Left{" +
"l=" + l +
'}';
}
}
private static final class Right<L, R> extends Either<L, R> {
private final R r;
private Right(R r) {
this.r = r;
}
@Override
public <V> V match(Function<? super L, ? extends V> leftFn, Function<? super R, ? extends V> rightFn) {
return rightFn.apply(r);
}
@Override
public boolean equals(Object other) {
return other instanceof Right && Objects.equals(r, ((Right) other).r);
}
@Override
public int hashCode() {
return Objects.hash(r);
}
@Override
public String toString() {
return "Right{" +
"r=" + r +
'}';
}
}
}