Is there a concise pipeline-like pattern for Java's optional when having something like if - else if - else
-like structures?
For example, we have two functions returning OptionalInt
s, and we want to use the result of f
, if present, otherwise the result of g
, if present, and otherwise 0
:
OptionalInt f(...) {
return ...
}
OptionalInt g(...) {
return ...
}
int res = f(...).orElse(g(...).orElse(0));
Is there a more pipline-like structure of this pattern, e.g. (pseudocode)
int res = f(...)
.orElseTry(g(...))
.otherwise(0);