Is there any Java 8 API static method that runs a Function
on non-null
input, but returns nullValue
on a null
input?
I can easily write this method myself, but I'd rather use a standard one if it exists.
public static <T, R> R transform(final T t, final Function<T, R> rFromT, final R nullValue) {
return
t == null
? nullValue
: rFromT.apply(t)
;
}
// which can be called like:
final Number x = getNumberThatCouldBeNull();
final long y = transform(x, Number::longValue, 0L);