我已经写了这个谓词,而声纳对此有所抱怨.我不确定如何解决此违规问题.请帮忙:
import com.google.common.base.Predicate;
import java.util.Map;
public final class FooPredicate {
private FooPredicate(){}
public static Predicate<Map.Entry<Long,Long>> isFirstElement(final Long o) {
return new Predicate<Map.Entry<Long,Long>>() {
@Override
public boolean apply(Map.Entry<Long,Long> foo) {
return foo.getKey().equals(o);
}
};
}
}
它正在抱怨apply方法的Foo参数.
06001
apply方法定义为采用可为null的值,对此我无能为力.声纳和番石榴不是在这里战斗吗?
> boolean apply(@Nullable
> T input)
>
> Returns the result of applying this predicate to input. This method is
> generally expected, but not absolutely required, to have the following
> properties:
>
> Its execution does not cause any observable side effects.
> The computation is consistent with equals; that is, Objects.equal(a, b) implies that predicate.apply(a) ==
> predicate.apply(b)).
>
> Throws:
> NullPointerException - if input is null and this predicate does not accept null arguments
解决方法:
您正在做foo.getKey().如果foo为null,则将抛出NullPointerException,但您并不是在声明foo不能为null.
在使用前请先检查foo.
if (foo != null) {
return foo.getKey().equals(o);
} else {
return null;
}