Skip to content

Commit cc0858a

Browse files
committed
#22 Added Either Monad
1 parent 4245fc1 commit cc0858a

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.redomar.game.lib;
2+
3+
import org.jetbrains.annotations.Contract;
4+
import org.jetbrains.annotations.NotNull;
5+
6+
import java.util.Optional;
7+
8+
public class Either<L, R> {
9+
private final Optional<L> left;
10+
private final Optional<R> right;
11+
12+
private Either(final Optional<L> left, final Optional<R> right) {
13+
this.left = left;
14+
this.right = right;
15+
}
16+
17+
@Contract("_ -> new")
18+
static <L, R> @NotNull Either<L, R> left(final L value) {
19+
return new Either<>(Optional.of(value), Optional.empty());
20+
}
21+
22+
@Contract("_ -> new")
23+
static <L, R> @NotNull Either<L, R> right(final R value) {
24+
return new Either<>(Optional.empty(), Optional.of(value));
25+
}
26+
27+
boolean isLeft() {
28+
return left.isPresent();
29+
}
30+
31+
L getLeft() {
32+
return left.get();
33+
}
34+
35+
R getRight() {
36+
return right.get();
37+
}
38+
}

0 commit comments

Comments
 (0)