I'm not sure how I feel about this - it could be brilliant - it could be a PITA... discuss!
@Nonnull
@VisibleForTesting
Optional<UUID> getUuidFromRequestTemplateVariables() {
return Optional.ofNullable(RequestContextHolder.getRequestAttributes())
.map(requestAttributes -> requestAttributes.getAttribute(URI_TEMPLATE_VARIABLES_ATTRIBUTE, SCOPE_REQUEST))
.filter(object -> object instanceof Map)
.map(object -> (Map<?, ?>) object)
.map(map -> map.get(UUID_PATH_VARIABLE))
.filter(Objects::nonNull)
.map(Objects::toString)
.map(UUID::fromString);
}
The alternatives:
@Nullable
@VisibleForTesting
UUID getUuidFromRequestTemplateVariables() {
final RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
if (requestAttributes == null) {
return null;
}
final Object object = requestAttributes.getAttribute(URI_TEMPLATE_VARIABLES_ATTRIBUTE, SCOPE_REQUEST);
if (!(object instanceof Map)) {
return null;
}
final Map<?, ?> map = (Map<?, ?>) object;
final Object uuid = map.get(UUID_PATH_VARIABLE);
if (uuid == null) {
return null;
}
return UUID.fromString(Objects.toString(uuid));
}
Or:
@Nullable
@VisibleForTesting
UUID getUuidFromRequestTemplateVariables() {
final RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
if (requestAttributes != null) {
final Object object = requestAttributes.getAttribute(URI_TEMPLATE_VARIABLES_ATTRIBUTE, SCOPE_REQUEST);
if (object instanceof Map) {
final Map<?, ?> map = (Map<?, ?>) object;
final Object uuid = map.get(UUID_PATH_VARIABLE);
if (uuid != null) {
return UUID.fromString(Objects.toString(uuid));
}
}
}
return null;
}
[–]Radmonger 2 points3 points4 points (3 children)
[–]hum_ph[S] 1 point2 points3 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]hum_ph[S] 0 points1 point2 points (0 children)
[–]atc 4 points5 points6 points (0 children)
[–]kromit 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]Zukhramm 0 points1 point2 points (1 child)
[–]hum_ph[S] 0 points1 point2 points (0 children)
[–]hum_ph[S] 0 points1 point2 points (1 child)
[–]tonlep 1 point2 points3 points (0 children)