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

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

There is a newer version: 3.1.0
Show newest version
/*
 * 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 com.shapesecurity.functional.ThrowingSupplier;

import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;

@CheckReturnValue
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;
    }

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

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

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

    @Nonnull
    public static  Either _try(@Nonnull ThrowingSupplier s) {
        try {
            return Either.right(s.get());
        } catch (Exception e) {
            return Either.left(e);
        }
    }

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

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

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

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

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

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

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

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

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

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

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

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