diff --git a/common/api/src/main/java/com/dfsek/terra/api/util/generic/data/BiFunctor.java b/common/api/src/main/java/com/dfsek/terra/api/util/generic/data/BiFunctor.java index 05daf80a2..537b9aca2 100644 --- a/common/api/src/main/java/com/dfsek/terra/api/util/generic/data/BiFunctor.java +++ b/common/api/src/main/java/com/dfsek/terra/api/util/generic/data/BiFunctor.java @@ -6,4 +6,8 @@ import java.util.function.Function; public interface BiFunctor> { BiFunctor mapLeft(Function map); BiFunctor mapRight(Function map); + + default BiFunctor bimap(Function left, Function right) { + return mapLeft(left).mapRight(right); + } } diff --git a/common/api/src/main/java/com/dfsek/terra/api/util/generic/data/types/Either.java b/common/api/src/main/java/com/dfsek/terra/api/util/generic/data/types/Either.java index 7c47f3516..4e01be3b8 100644 --- a/common/api/src/main/java/com/dfsek/terra/api/util/generic/data/types/Either.java +++ b/common/api/src/main/java/com/dfsek/terra/api/util/generic/data/types/Either.java @@ -59,36 +59,40 @@ public interface Either extends Monad>, BiFunctor flip(); + + U collect(Function left, Function right); + @SuppressWarnings({ "unchecked" }) @NotNull @Contract("_ -> new") static Either left(L1 left) { - record Left(T value) implements Either { + record Left(L value) implements Either { @Override @SuppressWarnings("unchecked") - public Either bind(Function>> map) { - return (Either) this; + public Either bind(Function>> map) { + return (Either) this; } @Override - public Either mapLeft(Function f) { + public Either mapLeft(Function f) { return new Left<>(f.apply(value)); } @SuppressWarnings({ "unchecked" }) @Override - public Either mapRight(Function f) { - return (Either) this; + public Either mapRight(Function f) { + return (Either) this; } @Override - public Optional getLeft() { + public Optional getLeft() { return Optional.of(value); } @Override - public Optional getRight() { + public Optional getRight() { return Optional.empty(); } @@ -101,39 +105,49 @@ public interface Either extends Monad>, BiFunctor flip() { + return right(value); + } + + @Override + public U collect(Function left, Function right) { + return left.apply(value); + } } - return new Left(Objects.requireNonNull(left)); + return new Left<>(Objects.requireNonNull(left)); } - @SuppressWarnings({ "rawtypes", "unchecked" }) + @SuppressWarnings({"unchecked" }) @NotNull @Contract("_ -> new") static Either right(R1 right) { - record Right(T value) implements Either { + record Right(R value) implements Either { @Override - public Either bind(Function>> map) { - return (Either) map.apply(value); + public Either bind(Function>> map) { + return (Either) map.apply(value); } @SuppressWarnings({ "unchecked" }) @Override - public Either mapLeft(Function f) { - return (Either) this; + public Either mapLeft(Function f) { + return (Either) this; } @Override - public Either mapRight(Function f) { + public Either mapRight(Function f) { return new Right<>(f.apply(value)); } @Override - public Optional getLeft() { + public Optional getLeft() { return Optional.empty(); } @Override - public Optional getRight() { + public Optional getRight() { return Optional.of(value); } @@ -146,8 +160,18 @@ public interface Either extends Monad>, BiFunctor flip() { + return left(value); + } + + @Override + public U collect(Function left, Function right) { + return right.apply(value); + } } - return new Right(Objects.requireNonNull(right)); + return new Right<>(Objects.requireNonNull(right)); } } \ No newline at end of file