I've have a method that takes String
as an input and should also return a String
.
The following ASCII art presents the logical flow:
Option<A> optA = finder.findA(input);
optA
/\
isEmpty() / \ isDefined()
/ \
"ERR_1" Option<B> optB = finder.findB(optA.get().bid);
/ \
isEmpty() / \ isDefined()
/ \
"ERR_2" opt2.get().id
Basically for given input
I'm looking for A
object which is returned wrapped in an Option
. Then is A
is present I'm looking for B
- wrapped in an Option
too, otherwise return ERR_1
. Then if B
is present return it's id, otherwise return ERR_2
.
I'm wondering how it could be implemented using optionals (or pattern matching maybe?) in a nice and concise way (without any ifology) - possibly in one-liner.
Could anyone please suggest something?
Source code to try out can be found here.
Optional<A>
andOptional<B>
don't relate to one another. Are you saying that the existence ofOptional<A>
depends on the existence ofOptional<B>
? – CavallaroOptional<A>
may exist even ifOptional<B>
does not exist. – Sturges