如果使用来自Guava的com.google.common,为什么不使用Objects.requireNonNull()?

Mag*_*lex 6 java guava

来自Google的Guava库的Javadoc for Preconditions指出:

使用的项目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");
  }
}
Run Code Online (Sandbox Code Playgroud)

shm*_*sel 5

Guava 的版本提供了许多接受格式字符串和参数的重载。引用PreconditionsExplained wiki:

出于几个原因,我们更喜欢滚动我们自己的先决条件检查,而不是来自 Apache Commons 的类似实用程序。简要地:

...