gpt4 book ai didi

java - 在任何一种情况下都可以选择执行操作

转载 作者:行者123 更新时间:2023-11-29 06:48:59 24 4
gpt4 key购买 nike

我有一个方法

   public Single<SignatureOrder> confirmPayment(PaymentEntity entity, Identity identity, ConfirmationRequest confirmationRequest) {
return chooseClient(entity)
.confirmPayment(identity, entity,confirmationRequest);
}

我需要做一点修改,如果 confirmationRequestnull 然后调用 confirmPayment(identity, entity);

我怎么能说类似的话Optional.ofNullable(confirmationRequest) 然后 chooseClient(entity) 然后 ifPresent 调用 confirmPayment(identity, entity,confirmationRequest); ifNotPresent 调用 confirmPayment(identity, entity);

confirmPayment 的签名

 public Single<SignatureOrder> confirmPayment(Identity identity, PaymentEntity entity)
public Single<SignatureOrder> confirmPayment(Identity identity, PaymentEntity entity, ConfirmationRequest confirmationRequest)

最佳答案

public Single<SignatureOrder> confirmPayment(PaymentEntity entity, Identity identity, ConfirmationRequest confirmationRequest) {
return Optional.ofNullable(confirmationRequest)
.map(cr -> chooseClient(entity).confirmPayment(identity, entity, cr)
.orElseGet(() -> chooseClient(entity).confirmPayment(identity, entity);
}

映射到 null 返回空,通过使用 orElseGet 如果它映射到空它会调用这个 lambda

关于java - 在任何一种情况下都可以选择执行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55833041/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com