
net.amygdalum.testrecorder.util.Exceptional Maven / Gradle / Ivy
The newest version!
package net.amygdalum.testrecorder.util;
import java.util.function.Function;
public class Exceptional {
private Throwable throwable;
private T value;
public Exceptional(T value) {
this.value = value;
}
public Exceptional(Throwable throwable) {
this.throwable = throwable;
}
public static Exceptional throwing(Throwable throwable) {
return new Exceptional<>(throwable);
}
public static Exceptional success(T value) {
return new Exceptional<>(value);
}
public T andRecover(T defaultValue) {
if (throwable == null) {
return value;
} else {
return defaultValue;
}
}
public T andRecover(Function defaultValue) {
if (throwable == null) {
return value;
} else {
return defaultValue.apply(throwable);
}
}
public T orFail() throws Throwable {
if (throwable == null) {
return value;
}
throw throwable;
}
public T orFail(Function exceptionMapper) throws E {
if (throwable == null) {
return value;
}
throw exceptionMapper.apply(throwable);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy