All Downloads are FREE. Search and download functionalities are using the official Maven repository.

emu.grasscutter.utils.Either Maven / Gradle / Ivy

There is a newer version: 1.7.3
Show newest version
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 f1, Function f2) {
            return new Left<>(f1.apply(this.value));
        }

        @Override
        public  T map(Function l, Function r) {
            return l.apply(this.value);
        }

        @Override
        public Either ifLeft(Consumer consumer) {
            consumer.accept(this.value);
            return this;
        }

        @Override
        public Either ifRight(Consumer 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 f1, Function f2) {
            return new Right<>(f2.apply(this.value));
        }

        @Override
        public  T map(Function l, Function r) {
            return r.apply(this.value);
        }

        @Override
        public Either ifLeft(Consumer consumer) {
            return this;
        }

        @Override
        public Either ifRight(Consumer 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 f1, Function f2);

    public abstract  T map(Function l, Function r);

    public abstract Either ifLeft(Consumer consumer);

    public abstract Either ifRight(Consumer consumer);

    public abstract Optional left();

    public abstract Optional right();

    public  Either mapLeft(Function l) {
        return map(t -> left(l.apply(t)), Either::right);
    }

    public  Either mapRight(Function 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);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy