本文介绍了是否有可能使用两次ifPresent每一个检查不同的返回或分叉图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文:我链接了由wsdl2java生成的对象。有一个已经创建的方法调用soap web服务,接收xml并在级联对象中解组它。

Context: I have chained objects generated by wsdl2java. There is an already created method that call the soap web services, receives the xml and unmarshalling it in cascaded objects.

欲望:我想得到两个不同的对象,它们位于不同层次的节点。从映射的角度来看,它们彼此远,但从商业角度看,它们是情侣。

Desire: I want to get two different objects which lies in different hierarchy nodes. From mapping perspective they are hold "far" from one another but from business perspective they are a "couple".

目前正在运作:

Optional.ofNullable(onderneming.getOndernemingOfVestiging()).map(OndernemingOfVestigingType::getCode) //same return
        .map(CodeOndernemingOfVestigingType::getValue) // different object return
        .ifPresent(o -> myMethod("onderneming_type", o.getValue()));

Optional.ofNullable(onderneming.getOndernemingOfVestiging()).map(OndernemingOfVestigingType::getCode) //same return
        .map(CodeOndernemingOfVestigingType::getBeschrijving) // different object return
        .ifPresent(o -> myMethod("onderneming_type_omschrijving", o.getValue()));

我正在尝试的是(伪解释):

What I am trying is something like (pseudo-explanation):

Optional.ofNullable(onderneming.getOndernemingOfVestiging()).map(OndernemingOfVestigingType::getCode) //same return
        .map(CodeOndernemingOfVestigingType::getValue) // different object return
        .ifPresent(o -> myMethod("onderneming_type", o.getValue()))
        .map(CodeOndernemingOfVestigingType::getBeschrijving) // different object return
        .ifPresent(o -> myMethod("onderneming_type_omschrijving", o.getValue()));

我知道这不起作用,因为我只有一个终止clausure而ifPresent出现两次。

I understand that this doesn't work because I can have only one terminate clausure and ifPresent appears twice.

从虚拟角度来看,我的问题也可能是:是否可以在同一个lambda表达式中以两个不同的方向分叉/分叉/划分地图?

From dummy viewpoint, my question could be also: is it possible to fork/bifurcate/divide map in two different direction in same lambda expression?

***基于Eugene的建议我成功了

*** Based on Eugene suggestion I did successfully

Optional.ofNullable(onderneming.getOndernemingOfVestiging()).map(OndernemingOfVestigingType::getCode)
        .ifPresent(o -> {
            System.out.println(o.getValue().getValue());
            System.out.println(o.getBeschrijving().getValue());
        }); 

我从myMethod更改为System.out.println所以没有造成混淆(我不在乎)关于myMethod在这个上下文中。它可以是任何实例化对象的任何其他方法。)

I changed from myMethod to System.out.println so no confusion is caused (I don't care about myMethod in this context. It could be anyother method of any instanced object).

虽然它起作用,但与原始的相比,它听起来是一个很好的进步(而不是两个lambda)句子只有一个),我看到一些问题:o.getValue()。getValue()和o.getBeschrijving()。getValue()是空的不安全,不是吗?

Althought it works and it sounds a good progress compared to original one (instead of two lambda sentences there are only one), I see some problem: "o.getValue().getValue()" and "o.getBeschrijving().getValue()" are null unsafety, aren't they?

推荐答案

你可以抓住你的结果:

Optional.ofNullable(onderneming.getOndernemingOfVestiging()).map(OndernemingOfVestigingType::getCode)
    .map(CodeOndernemingOfVestigingType::getValue) 
    .map(o -> {
        myMethod("onderneming_type", o.getValue());
        return o.getBeschrijving();
    })
    .ifPresent(o -> myMethod("onderneming_type_omschrijving", o.getValue()));

请注意,多行lambda是坏的,难以阅读,所以你可以把它提取到私人方法。

Take a notice, that multiline lambdas are bad and hard to read, so you can extract it into private method.

编辑
您想要分支您的可选。是不可能的,您应该这样做:

EDITYou want to branch your Optional. Is not possible and you should do:

Optional<Code> op = Optional.ofNullable(onderneming.getOndernemingOfVestiging())
    .map(OndernemingOfVestigingType::getCode) 

op.map(CodeOndernemingOfVestigingType::getValue)
    .ifPresent(o -> myMethod("onderneming_type", o.getValue()));

op.map(CodeOndernemingOfVestigingType::getBeschrijving) 
    .ifPresent(o -> myMethod("onderneming_type_omschrijving", o.getValue()));

它应该比一些黑客更具可读性。

It should be more readable than do some hacks.

这篇关于是否有可能使用两次ifPresent每一个检查不同的返回或分叉图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 15:46