如何修复Java 9中Optional的“Cannot return a void result”错误消息?

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

How to fix Java 9 Optional "Cannot return a void result" error message?

问题

我有一个类,其中有一个类似这样的方法:

public class Client {
    
private project.enums.ClientType clientType;

private ClientType clientTypeV2;


    @JsonIgnore
    public Optional<Integer> getCodeClientTypeV2() {
        
        return Optional.ofNullable(this.clientTypeV2).map(ClientType::getCode);
    }

}

但我想要改变这个方法的逻辑。我希望如果 clientTypeV2 被填充了,它会返回该对象的 code。否则,我希望它返回 clientType 枚举中的 code。如何在使用 Java 8 的情况下实现这一点?我尝试了以下代码,但是出现了一个错误消息 “Cannot return a void result”

@JsonIgnore
public Optional<Integer> getCodeClientTypeV2() {

 return Optional.ofNullable(this.clientTypeV2).ifPresentOrElse(ClientType::getCode, () -> this.clientType.getCode());
}

#编辑 1

我尝试了这个:

@JsonIgnore
public Integer getCodeClientTypeV2() {

return Optional.ofNullable(this.clientTypeV2)
.map(ClientType::getCode)
.orElse(this.clientType.getCode()) ;

}

在调试过程中,尽管 clientTypeV2 被填充了,执行流程仍然进入了 orElse 部分,并且因为 clientType 为空而抛出了空指针异常。我漏掉了什么?

英文:

I had a class with a method like this:

public class Client {
    
private project.enums.ClientType clientType;

private ClientType clientTypeV2;


    @JsonIgnore
    public Optional&lt;Integer&gt; getCodeClientTypeV2() {
        
        return Optional.ofNullable(this.clientTypeV2).map(ClientType::getCode);
    }

}

But I would like to change the logic of this method. I want that if clientTypeV2 is filled, it returns the code for that object. Otherwise, I want it to return the code that is in the enum of the clientType. How to do this using java 8? I tried the following code but an error message appears &quot;Cannot return a void result&quot;

@JsonIgnore
public Optional&lt;Integer&gt; getCodeClientTypeV2() {

 return Optional.ofNullable(this.clientTypeV2).ifPresentOrElse(ClientType::getCode, () -&gt; this.clientType.getCode());
}

#Edit 1

I tried this:

@JsonIgnore
public Integer getCodeClientTypeV2() {

return Optional.ofNullable(this.clientTypeV2)
.map(ClientType::getCode)
.orElse(this.clientType.getCode()) ;

}

In debug, although clientTypeV2 is filled, the execution flow is entering inside orElse and giving NullPointerException because the clientType is null. What am I missing?

答案1

得分: 3

有不同的解决方案,取决于 getCode 是否会返回 null

当你不希望先评估替代表达式时,必须使用 orElseGet(Supplier<? extends T> other) 替代 orElse(T other)

return Optional.ofNullable(clientTypeV2).map(ClientType::getCode)
    .orElseGet(() -> clientType.getCode());

如果 getCode 不会返回 null,并且你只想处理 clientTypeV2clientType 可能为 null 的情况,你也可以使用

return Optional.ofNullable(clientTypeV2).orElse(clientType).getCode();

甚至更简单的方式是

return (clientTypeV2 != null ? clientTypeV2 : clientType).getCode();

所有解决方案都基于这样的假设,即至少其中之一的 clientTypeV2clientType 不为 null

英文:

There are different solutions, depending on whether getCode can return null.

When you don’t want the alternative expression to be evaluated beforehand, you have to use orElseGet(Supplier&lt;? extends T&gt; other) instead of orElse(T other).

return Optional.ofNullable(clientTypeV2).map(ClientType::getCode)
    .orElseGet(() -&gt; clientType.getCode());

If getCode can not return null and you only want to deal with the possibility that either clientTypeV2 or clientType can be null, you can also use

return Optional.ofNullable(clientTypeV2).orElse(clientType).getCode();

or even simpler

return (clientTypeV2 != null? clientTypeV2: clientType).getCode()

Common to all solution is the assumption that at least on of clientTypeV2 or clientType is not null.

答案2

得分: 0

按照惯例,如果您使用 orElse,您将返回一个常量,确保存在的 orElse 要么展开 Optional 中包含的值,要么使用您提供的默认值。

如果您不想更改方法签名,请改用 Optional#or。您将需要在处理 clientType 对象的空值检查时保持灵活,因为如果它不存在,您不能指望它是一个具体可返回的对象。

return Optional.ofNullable(this.clientTypeV2)
               .map(ClientType::getCode)
               .or(this.clientType.getCode());
英文:

By convention, if you are using orElse, you are returning a constant, guaranteed present value. orElse either unwraps the value contained in the Optional, or uses the default value you provide instead.

If you don't want to change the method signature, use Optional#or instead. You will have to be intelligent about null checks for your clientType object, since if it doesn't exist, you can't rely on it being a concrete returnable thing.

return Optional.ofNullable(this.clientTypeV2)
               .map(ClientType::getCode)
               .or(this.clientType.getCode());

huangapple
  • 本文由 发表于 2020年10月16日 01:31:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64376887.html
匿名

发表评论

匿名网友

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

确定