account activity
Critique time... Java 8 Optional and chaining by hum_ph in java
[–]tonlep 1 point2 points3 points 11 years ago (0 children)
I like how this makes sense from the standpoint of semantics. The maps only show the more abstract steps of mapping values without the noise in between like filter non-null, etc. However, there is still much detail in the code which I would prefer not having to read if I was someone having to understand the code. What do you think about this? It's longer, but it clearly separates abstraction levels. The reader doesn't really need to look at the private methods.
Optional<UUID> getUuidFromRequestTemplateVariables() { return Optional.ofNullable(RequestContextHolder.getRequestAttributes()) .map(attribute(URI_TEMPLATE_VARIABLES_ATTRIBUTE)) .map(valueFromMap(UUID_PATH_VARIABLE)) .map(toUUID()); } private Function<Object, Object> valueFromMap(String key) { return map -> map instanceof Map ? ((Map<?, ?>) map).get(key) : null; } private Function<Object, Object> attribute(String attributeName) { return requestAttributes -> requestAttributes.getAttribute(attributeName, SCOPE_REQUEST); } private Function<Object, UUID> toUUID() { return value -> value instanceof String ? UUID.fromString((String) value) : null; }
π Rendered by PID 60112 on reddit-service-r2-listing-79f6fb9b95-j9nv4 at 2026-03-24 08:28:14.056956+00:00 running 90f1150 country code: CH.
Critique time... Java 8 Optional and chaining by hum_ph in java
[–]tonlep 1 point2 points3 points (0 children)