The Google Guava库的Preconditions Javadoc声明:
使用
com.google.common
的项目通常应避免使用Objects.requireNonNull(Object)。相反,使用适合情况的checkNotNull(Object)或Verify.verifyNotNull(Object)。(相同的规则适用于接受消息重载。)
这个建议背后的动机是什么?在Javadoc中找不到任何信息。
我的意思是,它们基本上执行相同的操作,在这些情况下通常最好使用标准API(例如,Joda-Time的人现在建议人们使用java.time
,基本上废弃了他们自己的框架)。
例如,这两个输入验证行都执行相同的操作:
class PreconditionsExample {
private String string;
public PreconditionsExample(String string) {
this.string = Objects.requireNonNull(string, "string must not be null");
this.string = Preconditions.checkNotNull(string, "string must not be null");
}
}