I'm playing around with uses of a FunctionalInterface. I've seen multiple variations of the following code everywhere:
int i = str != null ? Integer.parseInt() : null;
I'm looking for the following behaviour:
int i = Optional.of(str).ifPresent(Integer::parseInt);
But ifPresent only accepts a Supplier and Optional cannot be extended.
I have created the following FunctionalInterface:
@FunctionalInterface
interface Do<A, B> {
default B ifNotNull(A a) {
return Optional.of(a).isPresent() ? perform(a) : null;
}
B perform(A a);
}
This allows me to do this:
Integer i = ((Do<String, Integer>) Integer::parseInt).ifNotNull(str);
One can add more default methods to do things like
LocalDateTime date = (Do<String, LocalDateTime> MyDateUtils::toDate).ifValidDate(dateStr);
そして、それはうまく読みますDo [my function] and return [function return value] if [my condition] holds true for [my input], otherwise null。
A次の操作を行ったときに、コンパイラが(Stringに渡されたifNotNull) とB(Integerによって返された)の型を推測できないのはなぜですかparseInt。
Integer i = ((Do) Integer::parseInt).ifNotNull(str);
これにより、次の結果が得られます。
互換性のない型: 無効なメソッド参照