org.unlaxer.util.Either Maven / Gradle / Ivy
package org.unlaxer.util;
import java.util.Optional;
import java.util.function.Function;
public class Either {
public final Optional left;
public final Optional right;
protected Either(L left, R right) {
super();
this.left = Optional.ofNullable(left);
this.right = Optional.ofNullable(right);
if(this.left.isPresent() ^ this.right.isPresent()) {
}else {
throw new IllegalArgumentException(
"left is " + this.left.isPresent() + " / right is " + this.right.isPresent()
);
}
}
public static Either rightOf(R right){
if(right == null){
throw new IllegalArgumentException("must be not null");
}
return new Either(null, right);
}
public static Either rightOfNullable(R right){
return new Either(null, right);
}
public static Either leftOf(L left){
if(left == null){
throw new IllegalArgumentException("must be not null");
}
return new Either(left,null);
}
public static Either leftOfNullable(L left){
return new Either(left,null);
}
public V apply(Function leftFunction, Function rightFunction){
return left.map(leftFunction).orElseGet(()->rightFunction.apply(right.get()));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy