Optional API update. You can use or() method if you want to return return an Optional describing the value, otherwise returns an Optional produced by the supplying function.
For instance
private <T> Optional<T> getSetting(Integer Id, String country) {
return repo.findByIdAndCountry(id, country,)
.or(() -> repo.findDefaultByCountry(country))
.or(() -> repo.findGlobalDefault());
}
The or() method description
/**
* If a value is present, returns an {@code Optional} describing the value,
* otherwise returns an {@code Optional} produced by the supplying function.
*
* @param supplier the supplying function that produces an {@code Optional}
* to be returned
* @return returns an {@code Optional} describing the value of this
* {@code Optional}, if a value is present, otherwise an
* {@code Optional} produced by the supplying function.
* @throws NullPointerException if the supplying function is {@code null} or
* produces a {@code null} result
* @since 9
*/
public Optional<T> or(Supplier<? extends Optional<? extends T>> supplier) {
Objects.requireNonNull(supplier);
if (isPresent()) {
return this;
} else {
@SuppressWarnings("unchecked")
Optional<T> r = (Optional<T>) supplier.get();
return Objects.requireNonNull(r);
}
}
OptionalXXXlikeOptionalDouble?