So currently I have
String uri = website.getUri();
Optional pageDetail = webClient.getDetailOfUri(uri);
String displayName;
String description;
You could write:
String uri = website.getUri();
Optional pageDetail = webClient.getDetailOfUri(uri);
String displayName = pageDetail.map(PageDetail::getName).orElse(uri);
String description = pageDetail.map(PageDetail::getDescription).orElse("");
If the Optional is not set, map will return the same unset Optional
. Otherwise, it will map it to an Optional
containing the result of getName()
. Then we can use orElse to return a default value when the Optional
is unset.