There's a method called validate which as input accepts an instance of Option
and a Predicate
- two arguments (yeah, I know Option
should be passed as an argument but this is simplified real-world scenario here. Now, if Option
is empty I need to throw NotFoundException
. When it has a value that doesn't match the Predicate
passed it should fail with ForbiddenException
. If it has value and it matches the predicate nothing happens.
So this will be:
Option, Predicate
/\
isEmpty() / \ isDefined()
/ \
throw NFE matches(Predicate)
/ \
no / \ yes
/ \
throw FE end
I have some solutions but would like potential answerers to come to this question with clear mind ;) My problem is getting the first exception if both tests fail. I expect an elegant solution if I may expect anything ;) All vavr beings allowed (Either
, Validation
, Try
..)
One of the ideas is to use double Option
:
Option
.of(o.getOrElseThrow(NotFoundException::new))
.filter(p)
.getOrElseThrow(ForbiddenException::new);
But this seems a bit awkward.
Here sample project may be found.