简体   繁体   English

java.util.Objects.requireNonNull vs Preconditions.checkNotNull

[英]java.util.Objects.requireNonNull vs Preconditions.checkNotNull

The documentation for Guava Preconditions notes: Guava Preconditions的文档说明:

Projects which use com.google.common should generally avoid the use of Objects.requireNonNull(Object) . 使用com.google.common项目通常应避免使用Objects.requireNonNull(Object) Instead, use whichever of checkNotNull(Object) or Verify.verifyNotNull(Object) is appropriate to the situation. 相反,使用checkNotNull(Object)Verify.verifyNotNull(Object)中适合的情况。 (The same goes for the message-accepting overloads.) (对于接收消息的重载也是如此。)

Can someone explain the rationale for this suggestion? 有人可以解释这个建议的理由吗?

Is it for the purpose of consistency or is there something fundamentally wrong with the implementation of Objects.requireNonNull ? 是为了保持一致性还是Objects.requireNonNull的实现存在根本性的错误?

It's just for consistency. 这只是为了保持一致性。 The implementations are the same. 实现是相同的。

Although from the example in the question it seems that OP is asking specifically about the particular form of checkNotNull , there is one more subtle difference in general in favor of using checkNotNull which is reflected in the printf style varargs form. 虽然从问题中的例子来看,OP似乎是专门询问checkNotNull的特定形式,但是通常还有一个细微的区别,即支持使用checkNotNull ,这反映在printf样式的varargs表单中。 For example using Guava Preconditions you can do following: 例如,使用Guava Preconditions您可以执行以下操作:

public void getInput(String companyName) {
   String context = "Google";
   String moreContext = "Facebook";
   checkNotNull(companyName, "Why not try %s or %s", context, moreContext);
}

with Objects.requireNonNull you will have to do something like 使用Objects.requireNonNull你必须做类似的事情

public void getInput(String companyName) {
   String context = "Google";
   String moreContext = "Facebook";
   requireNonNull(companyName, "Why not try " + context + " or " + moreContext);
}

Reference: See bottom section of Preconditions Explained 参考:参见前提条件说明的底部部分

Simple, varargs "printf-style" exception messages. 简单,varargs“printf-style”异常消息。 (This advantage is also why we recommend continuing to use checkNotNull over Objects.requireNonNull introduced in JDK 7.) (这个优点也是我们建议继续在JDK 7中引入的Objects.requireNonNull上使用checkNotNull的原因。)

EDIT: One thing to note though is that all args to the errorMessageTemplate are converted to String using String.valueOf(arg) so you can use only %s and not other type specifiers like %d or %f etc. 编辑:有一点需要注意的是,使用String.valueOf(arg)将errorMessageTemplate的所有参数转换为String,因此您只能使用%s而不能使用%d或%f等其他类型说明符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 java.util.Objects.requireNonNull 的 java.lang.NullPointerException - java.lang.NullPointerException at java.util.Objects.requireNonNull int 上的余数运算符导致 java.util.Objects.requireNonNull? - Remainder operator on int causes java.util.Objects.requireNonNull? 同时使用@Nonnull和Preconditions.checkNotNull(…) - Using both @Nonnull and Preconditions.checkNotNull(…) 使用Preconditions.checkNotNull()时,如何避免“潜在的空指针访问”? - How do I avoid “potential null pointer access” when using Preconditions.checkNotNull()? java.lang.NullPointerException---“com.google.common.base.Preconditions.checkNotNull(Preconditions.java:770)” - java.lang.NullPointerException---“com.google.common.base.Preconditions.checkNotNull(Preconditions.java:770)” 空检查vs findFragmentByTag中的Objects.requireNonNull - Null check vs Objects.requireNonNull in findFragmentByTag 这是什么错误:在 com.google.common.base.Preconditions.checkNotNull - What is this Error: at com.google.common.base.Preconditions.checkNotNull java.util.Objects 与 Optional 哪个更可取? - java.util.Objects vs Optional which is preferable? Objects#requireNonNull 的目的是什么 - What is the purpose of Objects#requireNonNull java.util.Objects.isNull vs object == null - java.util.Objects.isNull vs object == null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM