--Null support avec parse
String args = "1";
Integer a = Optional.ofNullable(args).map(o -> Integer.parseInt(o)).orElse(999);
Integer b = Optional.ofNullable(args).map(o -> Optional.ofNullable(Integer.parseInt(o))).orElse(999);
--Exceptions asynchrones
List<Future<Void>> futures = new ArrayList<>();
for (ExecutorService service : ExecutorServices) {
futures.add(service.async());
}
Exception exceptions = null;
for (Future<Void> future : futures) {
try {
future.get();
} catch (InterruptedException | ExecutionException e) {
exceptions = Optional.ofNullable(exceptions).map(exception -> {
exception.addSuppressed(e);
return exception;
}).orElse(e);
}
}
Il était facultatif de revenir avec Optional.map Facultatif.flatMap requiertNonNull
public<U> Optional<U> map(Function<? super T, ? extends U> mapper) {
Objects.requireNonNull(mapper);
if (!isPresent())
return empty();
else {
return Optional.ofNullable(mapper.apply(value));
}
}
public<U> Optional<U> flatMap(Function<? super T, Optional<U>> mapper) {
Objects.requireNonNull(mapper);
if (!isPresent())
return empty();
else {
return Objects.requireNonNull(mapper.apply(value));
}
}
Recommended Posts