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

com.bluecatcode.common.base.Eithers Maven / Gradle / Ivy

The newest version!
package com.bluecatcode.common.base;

import com.bluecatcode.common.exceptions.WrappedException;
import com.bluecatcode.common.functions.Block;
import com.bluecatcode.common.functions.CheckedBlock;
import com.bluecatcode.common.functions.CheckedFunction;
import com.bluecatcode.common.functions.Function;
import com.google.common.annotations.Beta;

import java.util.concurrent.Callable;

import static com.bluecatcode.common.contract.Preconditions.require;
import static com.bluecatcode.common.exceptions.WrappedException.wrap;

@Beta
public class Eithers {

    public static  Function> either(CheckedFunction function) {
        require(function != null, "Expected non-null function");
        return input -> {
            try {
                R reference = function.apply(input);
                require(reference != null, "Expected function to return non-null reference");
                return Either.valueOf(reference);
            } catch (Exception e) {
                return Either.errorOf(wrap(e));
            }
        };
    }

    public static  Function> either(Function function) {
        require(function != null, "Expected non-null function");
        return input -> {
            try {
                R reference = function.apply(input);
                require(reference != null, "Expected function to return non-null reference");
                return Either.valueOf(reference);
            } catch (Exception e) {
                return Either.errorOf(wrap(e));
            }
        };
    }

    public static  Either either(CheckedBlock block) {
        require(block != null, "Expected non-null block");
        try {
            R reference = block.execute();
            require(reference != null, "Expected block to return non-null reference");
            return Either.valueOf(reference);
        } catch (Exception e) {
            return Either.errorOf(wrap(e));
        }
    }

    public static  Either either(Block block) {
        require(block != null, "Expected non-null block");
        try {
            R reference = block.execute();
            require(reference != null, "Expected block to return non-null reference");
            return Either.valueOf(reference);
        } catch (Exception e) {
            return Either.errorOf(wrap(e));
        }
    }

    public static  Either either(Callable callable) {
        require(callable != null, "Expected non-null callable");
        try {
            R reference = callable.call();
            require(reference != null, "Expected callable to return non-null reference");
            return Either.valueOf(reference);
        } catch (Exception e) {
            return Either.errorOf(wrap(e));
        }
    }

    private Eithers() {
        throw new UnsupportedOperationException();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy