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

org.opendcs.utils.FailableResult Maven / Gradle / Ivy

Go to download

A collection of software for aggregatting and processing environmental data such as from NOAA GOES satellites.

The newest version!
package org.opendcs.utils;

import java.util.function.Consumer;

/**
 * Pair objects used for returning Successful and failed results for processing in a stream.
 * @param  Desired Object
 * @param  Object containing error details. Most commonly an Exception, but can be anything.
 */
public class FailableResult
{
    private final SuccessType successResult;
    private final FailType failResult;

    private FailableResult(SuccessType successResult, FailType failResult)
    {
        this.successResult = successResult;
        this.failResult = failResult;
    }

    public boolean isSuccess()
    {
        return failResult == null;
    }

    public boolean isFailure()
    {
        return failResult != null;
    }

    public SuccessType getSuccess()
    {
        if (isFailure())
        {
            throw new IllegalStateException("Attempt to retrieve 'success' result of a failure result.");
        }
        return successResult;
    }

    public FailType getFailure()
    {
        if (!isFailure())
        {
            throw new IllegalStateException("Attempt to retrieve 'failure' result of a succesfull result.");
        }
        return failResult;
    }

    public void handleError(Consumer consumer)
    {
        if (isFailure())
        {
            consumer.accept(failResult);
        }
    }

    public static  FailableResult success(SuccessType successResult)
    {
        return new FailableResult<>(successResult, null);
    }

    public static  FailableResult failure(FailType failResult)
    {
        return new FailableResult<>(null, failResult);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy