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

com.kenshoo.pl.simulation.internal.ValueOrException Maven / Gradle / Ivy

Go to download

A Java persistence layer based on JOOQ for high performance and business flow support.

There is a newer version: 0.1.121-jooq-3.16.3
Show newest version
package com.kenshoo.pl.simulation.internal;

import java.util.concurrent.Callable;
import java.util.function.Function;

public class ValueOrException {

    private final T value;
    private final Exception exception;

    private ValueOrException(T value, Exception exception) {
        this.value = value;
        this.exception = exception;
    }

    public static  ValueOrException of(V value) {
        return new ValueOrException<>(value, null);
    }

    public static  ValueOrException error(Exception exception) {
        return new ValueOrException<>(null, exception);
    }

    public T value() throws Exception {
        if (exception != null) {
            throw exception;
        }
        return value;
    }

    public T orWhenException(Function handler) {
        return exception == null ? value : handler.apply(exception);
    }

    public static  ValueOrException tryGet(Callable callable) {
        try {
            return ValueOrException.of(callable.call());
        } catch (Exception exception) {
            return ValueOrException.error(exception);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy