optional orelse throw code example
Example 1: optional throw if present
Optional.of(usuarios.findByEmail(usuario.getEmail())).ifPresent(param -> {
throw new IllegalArgumentException("Only Strings allowed in path parameters.")
});
Example 2: orElseThrow
private static <T> HttpMessageWriter<T> findWriter(
BodyInserter.Context context, ResolvableType elementType, @Nullable MediaType mediaType) {
return context.messageWriters().stream()
.filter(messageWriter -> messageWriter.canWrite(elementType, mediaType))
.findFirst()
.map(BodyInserters::<T>cast)
.orElseThrow(() -> new IllegalStateException(
"No HttpMessageWriter for \"" + mediaType + "\" and \"" + elementType + "\""));
}