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

no.gorandalum.fluentresult.Implementations Maven / Gradle / Ivy

Go to download

A result library helping you get rid of your exceptions, handling errors in a more fluent way.

There is a newer version: 1.5.0
Show newest version
package no.gorandalum.fluentresult;

import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

final class Implementations {

    private Implementations() {}

    static > T orElse(T other, R instance) {
        return instance.errorOpt().map(err -> other).orElse(instance.value());
    }

    static > T orElseGet(
            Function function, R instance) {
        Objects.requireNonNull(function);
        return instance.errorOpt().map(err -> (T) function.apply(err)).orElse(instance.value());
    }

    static , X extends Throwable> T orElseThrow(
            Function function, R instance) throws X {
        Objects.requireNonNull(function);
        if (instance.error() == null) {
            return instance.value();
        }
        throw function.apply(instance.error());
    }

    static > N fold(
            Function valueFunction,
            Function errorFunction,
            R instance) {
        Objects.requireNonNull(valueFunction);
        Objects.requireNonNull(errorFunction);
        return instance.isSuccess() ?
                valueFunction.apply(instance.value()) : errorFunction.apply(instance.error());
    }

    static > R consume(Consumer consumer,
                                                        R instance) {
        Objects.requireNonNull(consumer);
        if (instance.isSuccess()) {
            consumer.accept(instance.value());
        }
        return instance;
    }

    static > R consumeError(Consumer consumer,
                                                             R instance) {
        Objects.requireNonNull(consumer);
        if (!instance.isSuccess()) {
            consumer.accept(instance.error());
        }
        return instance;
    }

    static > R consumeEither(Consumer valueConsumer,
                                                              Consumer errorConsumer,
                                                              R instance) {
        Objects.requireNonNull(valueConsumer);
        Objects.requireNonNull(errorConsumer);
        if (instance.isSuccess()) {
            valueConsumer.accept(instance.value());
        } else {
            errorConsumer.accept(instance.error());
        }
        return instance;
    }

    static > R runIfSuccess(Runnable runnable,
                                                             R instance) {
        Objects.requireNonNull(runnable);
        if (instance.isSuccess()) {
            runnable.run();
        }
        return instance;
    }

    static > R runIfError(Runnable runnable,
                                                           R instance) {
        Objects.requireNonNull(runnable);
        instance.errorOpt().ifPresent(err -> runnable.run());
        return instance;
    }

    static > R runEither(Runnable successRunnable,
                                                          Runnable errorRunnable,
                                                          R instance) {
        Objects.requireNonNull(successRunnable);
        Objects.requireNonNull(errorRunnable);
        if (instance.isSuccess()) {
            successRunnable.run();
        } else {
            errorRunnable.run();
        }
        return instance;
    }

    static > R run(Runnable runnable, R instance) {
        Objects.requireNonNull(runnable);
        runnable.run();
        return instance;
    }

    static > R verify(Predicate predicate,
                                                       Supplier errorSupplier,
                                                       Function errorConstructor,
                                                       R instance) {
        Objects.requireNonNull(predicate);
        Objects.requireNonNull(errorSupplier);
        return instance.errorOpt()
                .map(err -> instance)
                .orElseGet(() -> predicate.test(instance.value()) ?
                        instance : errorConstructor.apply(errorSupplier.get()));
    }

    static > R verify(
            Function> function,
            Function errorConstructor,
            R instance) {
        Objects.requireNonNull(function);
        return instance.errorOpt()
                .map(err -> instance)
                .orElseGet(() -> {
                    BaseResult res = function.apply(instance.value());
                    return res.isSuccess() ? instance : errorConstructor.apply(res.error());
                });
    }

    static > NR map(
            Function function,
            Function successConstructor,
            Function errorConstructor,
            R instance) {
        Objects.requireNonNull(function);
        return instance.isSuccess() ?
                successConstructor.apply(function.apply(instance.value())) :
                errorConstructor.apply(instance.error());
    }

    static > NR mapError(
            Function function,
            Function errorConstructor,
            R instance) {
        Objects.requireNonNull(function);
        if (instance.isSuccess()) {
            @SuppressWarnings("unchecked")
            NR res = (NR) instance;
            return res;
        } else {
            return errorConstructor.apply(function.apply(instance.error()));
        }
    }

    static , R extends BaseResult> NR flatMap(
            Function function, R instance) {
        Objects.requireNonNull(function);
        @SuppressWarnings("unchecked")
        NR res = (NR) instance;
        return flatMap(function, instance, err -> res);
    }

    static , R extends BaseResult> NR flatMap(
            Function function, R instance,
            Function errorConstructor) {
        Objects.requireNonNull(function);
        return instance.isSuccess() ?
                Objects.requireNonNull(function.apply(instance.value())) :
                errorConstructor.apply(instance.error());
    }
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy