// GENERATED File for the above: import java.lang.Override; import java.lang.SuppressWarnings; import java.util.Optional; import java.util.function.Function; import java.util.function.Supplier; public final class Eithers { private Eithers() { } public static Either left(A left) { return new Left<>(left); } public static Either right(B right) { return new Right<>(right); } public static Either lazy(Supplier> either) { return new Lazy<>(either); } @SuppressWarnings("unchecked") public static CasesMatchers.TotalMatcher_Left cases() { return (CasesMatchers.TotalMatcher_Left) CasesMatchers.totalMatcher_Left; } public static CaseOfMatchers.TotalMatcher_Left caseOf(Either either) { return new CaseOfMatchers.TotalMatcher_Left(either); } public static Optional getLeft(Either either) { return either.match((left) -> Optional.of(left), (right) -> Optional.empty());} public static Optional getRight(Either either) { return either.match((left) -> Optional.empty(), (right) -> Optional.of(right));} public static Function, Either> setLeft(XA newLeft) { return modLeft(__ -> newLeft); } public static Function, Either> modLeft(Function leftMod) { return either -> either.match((left) -> left(leftMod.apply(left)), Eithers::right); } public static Function, Either> setRight(XB newRight) { return modRight(__ -> newRight); } public static Function, Either> modRight(Function rightMod) { return either -> either.match(Eithers::left, (right) -> right(rightMod.apply(right))); } private static final class Left extends Either { private final A left; Left(A left) { this.left = left; } @Override public X match(Function left, Function right) { return left.apply(this.left); } } private static final class Right extends Either { private final B right; Right(B right) { this.right = right; } @Override public X match(Function left, Function right) { return right.apply(this.right); } } private static final class Lazy extends Either { private volatile Supplier> expression; private Either evaluation; Lazy(Supplier> either) { this.expression = either; } private synchronized Either _evaluate() { Supplier> e = expression; if (e != null) { evaluation = e.get(); expression = null; } return evaluation; } @Override public X match(Function left, Function right) { return (this.expression == null ? this.evaluation : _evaluate()).match(left, right); } } public static class CasesMatchers { private static final TotalMatcher_Left totalMatcher_Left = new TotalMatcher_Left<>(); private CasesMatchers() { } public static final class TotalMatcher_Left { TotalMatcher_Left() { } public final TotalMatcher_Right left(Function left) { return new TotalMatcher_Right<>(left); } public final TotalMatcher_Right left_(X x) { return this.left((left) -> x); } public final PartialMatcher right(Function right) { return new PartialMatcher<>(null, right); } public final PartialMatcher right_(X x) { return this.right((right) -> x); } } public static final class TotalMatcher_Right extends PartialMatcher { TotalMatcher_Right(Function left) { super(left, null); } public final Function, X> right(Function right) { Function left = super.left; return either -> either.match(left, right); } public final Function, X> right_(X x) { return this.right((right) -> x); } } public static class PartialMatcher { private final Function left; private final Function right; PartialMatcher(Function left, Function right) { this.left = left; this.right = right; } public final Function, X> otherwise(Supplier otherwise) { Function left = (this.left != null) ? this.left : (left_) -> otherwise.get(); Function right = (this.right != null) ? this.right : (right_) -> otherwise.get(); return either -> either.match(left, right); } public final Function, X> otherwise_(X x) { return this.otherwise(() -> x); } public final Function, Optional> otherwiseEmpty() { Function> left = (this.left != null) ? (left_) -> Optional.of(this.left.apply(left_)) : (left_) -> Optional.empty(); Function> right = (this.right != null) ? (right_) -> Optional.of(this.right.apply(right_)) : (right_) -> Optional.empty(); return either -> either.match(left, right); } } } public static class CaseOfMatchers { private CaseOfMatchers() { } public static final class TotalMatcher_Left { private final Either _either; TotalMatcher_Left(Either _either) { this._either = _either; } public final TotalMatcher_Right left(Function left) { return new TotalMatcher_Right<>(this._either, left); } public final TotalMatcher_Right left_(X x) { return this.left((left) -> x); } public final PartialMatcher right(Function right) { return new PartialMatcher<>(this._either, null, right); } public final PartialMatcher right_(X x) { return this.right((right) -> x); } } public static final class TotalMatcher_Right extends PartialMatcher { TotalMatcher_Right(Either _either, Function left) { super(_either, left, null); } public final X right(Function right) { Function left = super.left; return ((PartialMatcher) this)._either.match(left, right); } public final X right_(X x) { return this.right((right) -> x); } } public static class PartialMatcher { private final Either _either; private final Function left; private final Function right; PartialMatcher(Either _either, Function left, Function right) { this._either = _either; this.left = left; this.right = right; } public final X otherwise(Supplier otherwise) { Function left = (this.left != null) ? this.left : (left_) -> otherwise.get(); Function right = (this.right != null) ? this.right : (right_) -> otherwise.get(); return this._either.match(left, right); } public final X otherwise_(X x) { return this.otherwise(() -> x); } public final Optional otherwiseEmpty() { Function> left = (this.left != null) ? (left_) -> Optional.of(this.left.apply(left_)) : (left_) -> Optional.empty(); Function> right = (this.right != null) ? (right_) -> Optional.of(this.right.apply(right_)) : (right_) -> Optional.empty(); return this._either.match(left, right); } } } }