com.kenshoo.pl.simulation.internal.ValueOrException Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of persistence-layer Show documentation
Show all versions of persistence-layer Show documentation
A Java persistence layer based on JOOQ for high performance and business flow support.
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);
}
}
}