问题
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".
This is currently working:
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()));
I understand that this doesn't work because I can have only one terminate clausure and ifPresent appears twice.
From dummy viewpoint, my question could be also: is it possible to fork/bifurcate/divide map in two different direction in same lambda expression?
*** 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());
});
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).
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?
回答1:
You can catch your result like that:
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()));
Take a notice, that multiline lambdas are bad and hard to read, so you can extract it into private method.
EDIT
You 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.
回答2:
I've a bit simplified your example here if u don't mind and here is what I came up with:
static class User {
private final Integer age;
private final String name;
public User(Integer age, String name) {
this.age = age;
this.name = name;
}
public Optional<Integer> getAge() {
return Optional.ofNullable(age);
}
public String getName() {
return name;
}
}
And then :
Optional<User> user = Optional.of(new User(null, "test"));
user.map(u -> Map.entry(u, u.getAge())) // this is java-9 Map.entry; but you can use a Pair or a List/array etc
.ifPresent(x -> {
System.out.println("name = " + x.getKey().getName());
x.getValue().ifPresent(age -> {
System.out.println("age = " + age);
});
});
You could apply the same thing to your input
来源:https://stackoverflow.com/questions/47283660/is-it-possible-to-use-twice-ifpresent-each-one-checking-different-return-or-bifu