主要作用就是一个方法可以返回两个不同类型的参数,源码如下:
package com.lifengdi.common;
import java.io.Serializable;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
/**
*
* @author: 李锋镝
* @date: 2020/06/03 10:29
*/
public abstract class Either implements Serializable {
public static Either either(Supplier leftSupplier, Supplier rightSupplier) {
R rightValue = rightSupplier.get();
if (rightValue != null) {
return Either.right(rightValue);
} else {
return Either.left(leftSupplier.get());
}
}
public static Either left(L left) {
return new Left<>(left);
}
public static Either right(R right) {
Objects.requireNonNull(right);
return new Right<>(right);
}
public abstract L getLeft();
public abstract R getRight();
public abstract boolean isLeft();
public abstract boolean isRight();
public abstract T fold(Function transformLeft, Function transformRight);
public Either swap() {
return either(this::getRight, this::getLeft);
}
public abstract Either map(Function transformLeft, Function transformRight);
public abstract Either mapRight(Function super R, ? extends T> rFunc);
public abstract Either peekRight(Consumer super R> action);
public abstract void run(Consumer runLeft, Consumer runRight);
public abstract Optional toOptional();
public static class Left extends Either {
L leftValue;
private Left(L left) {
this.leftValue = left;
}
@Override
public L getLeft() {
return this.leftValue;
}
@Override
public R getRight() {
throw new NoSuchElementException("Tried to getRight from a Left");
}
@Override
public boolean isLeft() {
return true;
}
@Override
public boolean isRight() {
return false;
}
@Override
public T fold(Function transformLeft, Function transformRight) {
return transformLeft.apply(this.leftValue);
}
@Override
public Either map(Function transformLeft, Function transformRight) {
return Either.left(transformLeft.apply(this.leftValue));
}
@Override
public Either mapRight(Function super R, ? extends T> rFunc) {
return Either.left(this.getLeft());
}
@Override
public Either peekRight(Consumer super R> action) {
return Either.left(this.getLeft());
}
@Override
public void run(Consumer runLeft, Consumer runRight) {
runLeft.accept(this.leftValue);
}
@Override
public Optional toOptional() {
return Optional.empty();
}
@Override
public int hashCode() {
return this.leftValue.hashCode();
}
@Override
public boolean equals(Object other) {
if (other instanceof Left, ?>) {
final Left, ?> otherAsLeft = (Left, ?>) other;
return this.leftValue.equals(otherAsLeft.leftValue);
} else {
return false;
}
}
}
public static class Right extends Either {
R rightValue;
private Right(R right) {
this.rightValue = right;
}
@Override
public L getLeft() {
throw new NoSuchElementException("Tried to getLeft from a Right");
}
@Override
public R getRight() {
return rightValue;
}
@Override
public boolean isLeft() {
return false;
}
@Override
public boolean isRight() {
return true;
}
@Override
public T fold(Function transformLeft, Function transformRight) {
Objects.requireNonNull(transformRight);
return transformRight.apply(this.rightValue);
}
@Override
public Either map(Function transformLeft, Function transformRight) {
Objects.requireNonNull(transformRight);
return Either.right(transformRight.apply(this.rightValue));
}
@Override
public Either mapRight(Function super R, ? extends T> rFunc) {
return Either.right(rFunc.apply(this.getRight()));
}
@Override
public Either peekRight(Consumer super R> action) {
action.accept(getRight());
return this;
}
@Override
public void run(Consumer runLeft, Consumer runRight) {
runRight.accept(this.rightValue);
}
@Override
public Optional toOptional() {
return Optional.ofNullable(rightValue);
}
@Override
public int hashCode() {
return this.rightValue.hashCode();
}
@Override
public boolean equals(Object other) {
if (other instanceof Right, ?>) {
final Right, ?> otherAsRight = (Right, ?>) other;
return this.rightValue.equals(otherAsRight.rightValue);
} else {
return false;
}
}
}
}