We have a method which receives an object of the class on the top of the class hierarchy. It uses a condition based on a field somewhere deeper in the hierarchy, and if that is fulfilled then it uses for a builder another field also somewhere deeper in the hierarchy but on a different route starting from the top class.
public Optional<SomeType> create(final TopClassInHierarchy topClass) {
Optional<SomeType> someObject = Optional.empty();
if (someCondition.evaluate(getFieldOne(topClass))) {
someObject = Optional.of(new SomeType.Builder()
.withFieldTwo(getFieldTwo(topClass))
.build());
}
return someObject;
private FieldOne getFieldOne(TopClassInHierarchy topClass) { return topClass.getSomething()...getFieldOne();
private FieldTwo getFieldTwo(TopClassInHierarchy topClass) { return topClass.getSomethingElse()...getFieldTwo();
We would like to preferably condense this into one statement, something like this
SomeType.Builder builder = new SomeType.Builder();
Optional.of(topClass)
.map(this::getFieldOne)
.filter(someCondition::evaluate)
.map(this::getFieldTwo) //???
.ifPresent(builder::withFieldTwo);
However, once we map topClass down to fieldOne for the condition evaluation, afterwards we don't seem to be able to "step back" to topClass to map it down to fieldTwo for the builder. Is this feasible with one statement?