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

com.spencerwi.either.Result Maven / Gradle / Ivy

Go to download

A right-biased implementation of "Either a b" for Java, using Java 8 for mapping/folding and type inference.

There is a newer version: 2.9.0
Show newest version
package com.spencerwi.either;

import java.util.NoSuchElementException;
import java.util.function.Function;

public abstract class Result {

    public static  Result attempt(ExceptionThrowingSupplier resultSupplier){
        try {
            R resultValue = resultSupplier.get();
            return Result.ok(resultValue);
        } catch (Exception e){
            return Result.err(e);
        }
    }

    public static  Result err(Exception e){ return new Err<>(e); }
    public static  Result ok(R result){ return new Ok<>(result); }

    public abstract Exception getException();
    public abstract R getResult();

    public abstract boolean isErr();
    public abstract boolean isOk();

    public abstract  T fold(Function transformException, Function transformValue);
    public abstract  Result map(ExceptionThrowingFunction transformValue);
    public abstract  Result flatMap(ExceptionThrowingFunction> transformValue);

    public static class Err extends Result {
        private Exception leftValue;
        private Err(Exception e) {
            this.leftValue = e;
        }

        @Override
        public Exception getException() { return this.leftValue; }
        @Override
        public R getResult() { throw new NoSuchElementException("Tried to getResult from an Err"); }

        @Override
        public boolean isErr() { return true; }
        @Override
        public boolean isOk() { return false; }

        @Override
        public  T fold(Function transformException, Function transformValue) {
            return transformException.apply(this.leftValue);
        }

        @Override
        public  Result map(ExceptionThrowingFunction transformRight) {
            return Result.err(this.leftValue);
        }
        @Override
        public  Result flatMap(ExceptionThrowingFunction> transformValue) {
            return Result.err(this.leftValue);
        }

        @Override
        public int hashCode(){ return this.leftValue.hashCode(); }
        @Override
        public boolean equals(Object other){
            if (other instanceof Err){
                final Err otherAsErr = (Err)other;
                return this.leftValue.equals(otherAsErr.leftValue);
            } else {
                return false;
            }
        }

    }
    public static class Ok extends Result {
        private R rightValue;
        private Ok(R value) {
            this.rightValue = value;
        }

        @Override
        public Exception getException() { throw new NoSuchElementException("Tried to getException from an Ok"); }
        @Override
        public R getResult() { return rightValue; }

        @Override
        public boolean isErr() { return false; }
        @Override
        public boolean isOk() { return true; }

        @Override
        public  T fold(Function transformException, Function transformValue) {
            return transformValue.apply(this.rightValue);
        }
        @Override
        public int hashCode(){ return this.rightValue.hashCode(); }
        @Override
        public boolean equals(Object other){
            if (other instanceof Ok){
                final Ok otherAsOk = (Ok)other;
                return this.rightValue.equals(otherAsOk.rightValue);
            } else {
                return false;
            }
        }

        @Override
        public  Result map(ExceptionThrowingFunction transformValue) {
            return Result.attempt(() -> transformValue.apply(this.rightValue));
        }
        @Override
        public  Result flatMap(ExceptionThrowingFunction> transformValue) {
            try {
                return transformValue.apply(this.rightValue);
            } catch(Exception e) {
                return new Err(e);
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy