可以使用Java 8 API来替换所有Guava API吗?

huangapple go评论65阅读模式
英文:

Is it possible to change all Guava API with the Java 8 API?

问题

我的项目刚刚运行了Sonar扫描,我发现了1个报告的情况,如下所示:
> Sonar建议使用Java 8的API,而不是Guava的API。

在某些情况下,我可以更改API,但我发现一些情况,我无法找到相应的Java 8替代Guava的方法。

例如:

import java.util.Optional;
import static com.google.common.base.Preconditions.checkNotNull;

Optional<String> proxyHost;
this.proxyHost = checkNotNull(proxyHost, "proxyHost is required")
                .orNull(); // 这一行会出现编译错误,因为没有定义orNull方法。

我试图在Java 8的API中找到替代Guava的checkNotNull的方法,但看起来Java 8没有它。这只是其中的一个例子。

我也无法找到以下代码段的替代方法:

import com.google.common.collect.Iterables.tryFind;
import com.google.common.base.Suppliers;

等等。

我能否在其他地方找到这些情况的Java 8替代API?还是我应该将这个情况标记为Sonar扫描的误报?

英文:

My project just ran sonar scan, and I found 1 reported case, which is:
> Sonar suggest use Java 8 API instead of Guava API.

In some cases, I can change the API, but I found some of the cases, where I cannot find the corresponding Java 8 substitution for Guava.

For example:

import java.util.Optional;
import static com.google.common.base.Preconditions.checkNotNull;

Optional&lt;String&gt; proxyHost;
this.proxyHost = checkNotNull(proxyHost, &quot;proxyHost is required&quot;)
                .orNull(); // this line will having compilation error orNull method undefined.

I am trying to find a replacement in Java 8 API to replace the Guava checkNotNull, but it looks like Java 8 does not have it. And this is just one of the examples.

I cannot find any replacement for the following snippet as well:

import com.google.common.collect.Iterables.tryFind;
import com.google.common.base.Suppliers;

and so on.

Can I find the Java 8 replacement API for this somewhere? or I should mask this case as false positive in the Sonar scan?

答案1

得分: 2

Java 8 API中可以专门用于替代Preconditions.checkNotNull(T)的是Objects.requireNonNull(T)。但是,在您的情况下,您有一个Optional实例,所以可能您不想检查变量是否为null,而是要检查它是否非空。您可以使用Optional.orElseThrow来实现:

this.proxyHost = proxyHost.orElseThrow(() -> new SomeExceptionClass("proxyHost is required"));

关于Iterables类的Guava文档指出,在许多使用Iterables的情况下,您可以改用Java 8的Stream

至于Suppliers类,我不知道Java 8中是否有直接等价的Supplier实例。

英文:

The Java 8 API which specifically can be used instead of Preconditions.checkNotNull(T) is Objects.requireNonNull(T). However, in your case you have an Optional instance, so probably you don't want to check that your variable is null, but that it is non-empty. This can be done with Optional.orElseThrow as follows:

this.proxyHost = proxyHost.orElseThrow(() -&gt; new SomeExceptionClass(&quot;proxyHost is required&quot;));

The Guava documentation for the Iterables class specifies that in many cases where Iterables is used, you can use Java 8 Streams instead.

For the Suppliers class I don't known direct equivalents for Java 8 Supplier instances.

答案2

得分: -1

你可以这样写:
要抛出错误,你可以按照以下方式使用。

proxyHost.orElseThrow(() -> new Exception("proxyHost is required"));

或者在设置默认值时,你可以尝试以下方法:

String result = proxyHost.isPresent() ? proxyHost.get() : "proxyHost is required";

英文:

You can write like this
For throwing an error, you can use as below.

proxyHost.orElseThrow(() -&gt; new Exception(&quot;proxyHost is required&quot;));

Or setting default you can try with

String result=proxyHost.isPresent()?proxyHost.get():&quot;proxyHost is required&quot;;

huangapple
  • 本文由 发表于 2020年8月21日 18:24:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63521056.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定