emu.grasscutter.utils.Either Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of grasscutter Show documentation
Show all versions of grasscutter Show documentation
A server software reimplementation for an anime game.
package emu.grasscutter.utils;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
public abstract class Either {
private static final class Left extends Either {
private final L value;
public Left(L value) {
this.value = value;
}
@Override
public Either mapBoth(
Function super L, ? extends U> f1, Function super R, ? extends V> f2) {
return new Left<>(f1.apply(this.value));
}
@Override
public T map(Function super L, ? extends T> l, Function super R, ? extends T> r) {
return l.apply(this.value);
}
@Override
public Either ifLeft(Consumer super L> consumer) {
consumer.accept(this.value);
return this;
}
@Override
public Either ifRight(Consumer super R> consumer) {
return this;
}
@Override
public Optional left() {
return Optional.of(this.value);
}
@Override
public Optional right() {
return Optional.empty();
}
@Override
public String toString() {
return "Left[" + this.value + "]";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Left, ?> left = (Left, ?>) o;
return Objects.equals(value, left.value);
}
@Override
public int hashCode() {
return Objects.hash(value);
}
}
private static final class Right extends Either {
private final R value;
public Right(R value) {
this.value = value;
}
@Override
public Either mapBoth(
Function super L, ? extends U> f1, Function super R, ? extends V> f2) {
return new Right<>(f2.apply(this.value));
}
@Override
public T map(Function super L, ? extends T> l, Function super R, ? extends T> r) {
return r.apply(this.value);
}
@Override
public Either ifLeft(Consumer super L> consumer) {
return this;
}
@Override
public Either ifRight(Consumer super R> consumer) {
consumer.accept(this.value);
return this;
}
@Override
public Optional left() {
return Optional.empty();
}
@Override
public Optional right() {
return Optional.of(this.value);
}
@Override
public String toString() {
return "Right[" + this.value + "]";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Right, ?> right = (Right, ?>) o;
return Objects.equals(value, right.value);
}
@Override
public int hashCode() {
return Objects.hash(value);
}
}
private Either() {}
public abstract Either mapBoth(
Function super L, ? extends U> f1, Function super R, ? extends V> f2);
public abstract T map(Function super L, ? extends T> l, Function super R, ? extends T> r);
public abstract Either ifLeft(Consumer super L> consumer);
public abstract Either ifRight(Consumer super R> consumer);
public abstract Optional left();
public abstract Optional right();
public Either mapLeft(Function super L, ? extends T> l) {
return map(t -> left(l.apply(t)), Either::right);
}
public Either mapRight(Function super R, ? extends T> l) {
return map(Either::left, t -> right(l.apply(t)));
}
public static Either left(L value) {
return new Left<>(value);
}
public static Either right(R value) {
return new Right<>(value);
}
}