org.infinispan.commons.util.Either Maven / Gradle / Ivy
package org.infinispan.commons.util;
import java.util.NoSuchElementException;
public abstract class Either {
public enum Type {
LEFT, RIGHT
}
public static Either newLeft(A a) {
return new Left<>(a);
}
public static Either newRight(B b) {
return new Right<>(b);
}
public abstract Type type();
public abstract A left();
public abstract B right();
private static class Left extends Either {
private A leftValue;
Left(A a) { leftValue = a; }
@Override public Type type() { return Type.LEFT; }
@Override public A left() { return leftValue; }
@Override public B right() { throw new NoSuchElementException("Either.right() called on Left"); }
@Override
public String
toString() {
return "Left(" + leftValue + ')';
}
}
private static class Right extends Either {
private B rightValue;
Right(B b) { rightValue = b; }
@Override public Type type() { return Type.RIGHT; }
@Override public A left() { throw new NoSuchElementException("Either.left() called on Right"); }
@Override public B right() { return rightValue; }
@Override
public String
toString() {
return "Right(" + rightValue + ')';
}
}
}