org.opendcs.utils.FailableResult Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of opendcs Show documentation
Show all versions of opendcs Show documentation
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);
}
}