Optional orElse Optional in Java
This is part of JDK 9 in the form of or
, which takes a Supplier<Optional<T>>
. Your example would then be:
return serviceA(args)
.or(() -> serviceB(args))
.or(() -> serviceC(args));
For details see the Javadoc or this post I wrote.
The cleanest “try services” approach given the current API would be:
Optional<Result> o = Stream.<Supplier<Optional<Result>>>of(
()->serviceA(args),
()->serviceB(args),
()->serviceC(args),
()->serviceD(args))
.map(Supplier::get)
.filter(Optional::isPresent)
.map(Optional::get)
.findFirst();
The important aspect is not the (constant) chain of operations you have to write once but how easy it is to add another service (or modify the list of services in general). Here, adding or removing a single ()->serviceX(args)
is enough.
Due to the lazy evaluation of streams, no service will be invoked if a preceding service returned a non-empty Optional
.
Starting with Java 9, you can simplify the code to
Optional<Result> o = Stream.<Supplier<Optional<Result>>>of(
()->serviceA(args),
()->serviceB(args),
()->serviceC(args),
()->serviceD(args))
.flatMap(s -> s.get().stream())
.findFirst();
though this answer already contains an even simpler approach for JDK 9.
JDK 16 offers the alternative
Optional<Result> o = Stream.<Supplier<Optional<Result>>>of(
()->serviceA(args),
()->serviceB(args),
()->serviceC(args),
()->serviceD(args))
.<Result>mapMulti((s,c) -> s.get().ifPresent(c))
.findFirst();
though this approach might be more convenient with service methods accepting a Consumer
rather than returning a Supplier
.