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

com.shapesecurity.functional.data.Either Maven / Gradle / Ivy

/*
 * Copyright 2014 Shape Security, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.shapesecurity.functional.data;

import com.shapesecurity.functional.Effect;
import com.shapesecurity.functional.F;

import org.jetbrains.annotations.NotNull;

public final class Either {
    private final Object data;

    private enum Tag {
        LEFT, RIGHT;
    }

    private final Tag tag;

    // class local
    private Either(Object data, Tag tag) {
        super();
        this.data = data;
        this.tag = tag;
    }

    @NotNull
    public static  Either left(@NotNull A a) {
        return new Either<>(a, Tag.LEFT);
    }

    @NotNull
    public static  Either right(@NotNull B b) {
        return new Either<>(b, Tag.RIGHT);
    }

    @NotNull
    public static  A extract(Either e) {
        return e.either(x -> x, x -> x);
    }

    public final boolean isLeft() {
        return tag == Tag.LEFT;
    }

    public final boolean isRight() {
        return tag == Tag.RIGHT;
    }

    @SuppressWarnings("unchecked")
    public  X either(F f1, F f2) {
        if (tag == Tag.LEFT) {
            return f1.apply((A) data);
        } else {
            return f2.apply((B) data);
        }
    }

    @SuppressWarnings("unchecked")
    public void foreach(@NotNull Effect f1, @NotNull Effect f2) {
        if (tag == Tag.LEFT) {
            f1.apply((A) data);
        } else {
            f2.apply((B) data);
        }
    }

    @NotNull
    public  Either map(F f1, F f2) {
        return either(a -> Either.left(f1.apply(a)), b -> Either.right(f2.apply(b)));
    }

    @NotNull
    public  Either mapLeft(@NotNull F f) {
        return map(f, b -> b);
    }

    @NotNull
    public  Either mapRight(@NotNull F f) {
        return map(a -> a, f);
    }

    @SuppressWarnings("unchecked")
    @NotNull
    public Maybe left() {
        return tag == Tag.LEFT ? Maybe.of((A) data) : Maybe.empty();
    }

    @SuppressWarnings("unchecked")
    @NotNull
    public Maybe right() {
        return tag == Tag.RIGHT ? Maybe.of((B) data) : Maybe.empty();
    }

    public boolean eq(Either either) {
        return either.tag == this.tag && either.data.equals(this.data);
    }

    @Override
    public int hashCode() {
        return (0b10101010 << tag.ordinal()) ^ this.data.hashCode();
    }

    @SuppressWarnings("unchecked")
    @Override
    public final boolean equals(Object object) {
        return this == object || object instanceof Either && this.eq((Either) object);
    }
}