How can I convert the below legacy java code to use Optional :
if (outer != null
&& outer.getInnerMethod() != null
&& outer.getInnerMethod().isAllowed()) {
Obj value = outer.getInnerMethod().getSomethingElse();
}
Using something like Optional.ofNullable took me till here -
Optional.ofNullable(Outer.getOuter)
.map(Outer::getInnerMethod)
.map(SomethingElse::isAllowed)
.ifPresent(.. );
But ifPresent will not have access to the outer object. What would be the cleanest way to transform something like this ?
isAllowed(), which yields anOptional<Boolean>. You want to usefilterinstead.