add Either class

This commit is contained in:
Albert Shift 2015-03-22 18:41:35 -07:00
parent 905831b5e0
commit 74a30a618d
2 changed files with 64 additions and 0 deletions

View file

@ -0,0 +1,59 @@
package casser.support;
import java.util.function.Function;
public final class Either<L, R> {
private final L left;
private final R right;
private Either(L left, R right) {
this.left = left;
this.right = right;
}
public boolean isLeft() {
return left != null;
}
public L getLeft() {
return left;
}
public boolean isRight() {
return right != null;
}
public R getRight() {
return right;
}
public static <L, R> Either<L, R> left(L left) {
return new Either<L, R>(left, null);
}
public static <L, R> Either<L, R> right(R right) {
return new Either<L, R>(null, right);
}
public EitherCase getCase() {
if (left != null) {
return EitherCase.LEFT;
}
else if (right != null) {
return EitherCase.RIGHT;
}
throw new IllegalStateException("unexpected state");
}
public <T> T fold(Function<L, T> leftFunction, Function<R,T> rightFunction) {
switch(getCase()) {
case LEFT:
return leftFunction.apply(left);
case RIGHT:
return rightFunction.apply(right);
}
throw new IllegalStateException("unexpected state");
}
}

View file

@ -0,0 +1,5 @@
package casser.support;
public enum EitherCase {
LEFT, RIGHT;
}