如果有的话,正确使用

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

IfPresent proper use

问题

我正在尝试在这里使用ifPresent,但无法使其正常工作。

这是代码:

final Optional<GenericApiGatewayResponse> apiGatewayResponse = getQueryEventsCallResponse(apiGatewayRequest);
apiGatewayResponse.ifPresent(this::getQueryEvents);

private Optional<QueryEventsResponse> getQueryEvents(final GenericApiGatewayResponse apiGatewayResponse) {
    try {
        return Optional.of(gson.fromJson(apiGatewayResponse.getBody(), QueryEventsResponse.class));
    } catch (final Exception e) {
        log.error("QueryEventsResponseDeserializationFailure : Failure " +
                "while deserialize of QueryEvents from GenericApiGatewayResponse", e);
    }
    return Optional.empty();
}

private Optional<GenericApiGatewayResponse> getQueryEventsCallResponse(final GenericApiGatewayRequest request) {
    try {
        return Optional.of(apiGatewayClient.execute(request));
    } catch(final Exception e) {
        log.error("QueryEventsCallError : Error during invoke of QueryEvents API Gateway", e);
    }
    return Optional.empty();
}

但我想要ifPresent的响应作为Optional<QueryEventsResponse>。但ifPresent不允许你返回任何东西。

英文:

I am trying to use ifPresent here but not able to get it working.

Here is the code.

final Optional&lt;GenericApiGatewayResponse&gt; apiGatewayResponse = getQueryEventsCallResponse(apiGatewayRequest);
apiGatewayResponse.ifPresent(this::getQueryEvents);

private Optional&lt;QueryEventsResponse&gt; getQueryEvents(final GenericApiGatewayResponse apiGatewayResponse) {
    try {
        return Optional.of(gson.fromJson(apiGatewayResponse.getBody(), QueryEventsResponse.class));
    } catch (final Exception e) {
        log.error(&quot;QueryEventsResponseDeserializationFailure : Failure &quot; +
                &quot;while deserialize of QueryEvents from GenericApiGatewayResponse&quot;, e);
    }
    return Optional.empty();
}

private Optional&lt;GenericApiGatewayResponse&gt; getQueryEventsCallResponse(final GenericApiGatewayRequest request) {
    try {
        return Optional.of(apiGatewayClient.execute(request));
    } catch(final Exception e) {
        log.error(&quot;QueryEventsCallError : Error during invoke of QueryEvents API Gateway&quot;, e);
    }
    return Optional.empty();
}

But I want to get response of ifPresent as Optional<QueryEventsResponse>. But ifPresent does not allow you to return anything.

答案1

得分: 2

你需要使用的方法是flatMap。尝试以下代码:

Optional<GenericApiGatewayResponse> apiGatewayResponseOptional = getQueryEventsCallResponse(apiGatewayRequest);
Optional<QueryEventsResponse> queryEventsResponseOptional = apiGatewayResponseOptional.flatMap(this::getQueryEvents);
英文:

The method you need to use is flatMap. Try the following:

Optional&lt;GenericApiGatewayResponse&gt; apiGatewayResponseOptional = getQueryEventsCallResponse(apiGatewayRequest);
Optional&lt;QueryEventsResponse&gt; queryEventsResponseOptional = apiGatewayResponseOptional.flatmap(this::getQueryEvents);

答案2

得分: 0

Optional.ifPresent() 是一种在存在于 optional 中的值上添加效果的方法。由于在 Java 中不能通过返回值重载方法,因此它不能有任何返回值,因此它保持为 void,并且只允许执行诸如打印 Optional 包含值等副作用。

您仍然可以使用以下方式之一:

if (!optional.isEmpty()) {
    this.getQueryEvents(optional.get())
}

或者:

optional.flatMap(containedVal -> this.getQueryEvents(containedVal));
英文:

Optional.ifPresent() is method to add effect when optional is present over value that is stored inside that optional. It cannot have any return value since in Java you can't overload method's by return value -> therefore it stick's with void and only allow's you to do side effect like printing Optional contained value for example.

You can still use:

if (!optional.isEmpty()) {
    this.getQueryEvents(optional.get())
}

or:

optional.flatMap(containedVal -&gt; this.getQueryEvents(containedVal));

huangapple
  • 本文由 发表于 2020年7月29日 07:44:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63144337.html
匿名

发表评论

匿名网友

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

确定