I know how to create a reference to a method that has a String
parameter and returns an int
, it\'s:
Function
What I'm doing is to allow the user to give the value he actually want in case of exception . So I've something looking like this
public static Function super T, ? extends R> defaultIfThrows(FunctionThatThrows super T, ? extends R> delegate, R defaultValue) {
return x -> {
try {
return delegate.apply(x);
} catch (Throwable throwable) {
return defaultValue;
}
};
}
@FunctionalInterface
public interface FunctionThatThrows {
R apply(T t) throws Throwable;
}
And this can then be call like :
defaultIfThrows(child -> child.getID(), null)