Polish "Use lambdas for map entry iteration where possible"

Closes gh-12626
This commit is contained in:
Phillip Webb
2018-04-04 19:35:41 -07:00
parent 69bc19e0ca
commit 685babc829
27 changed files with 209 additions and 176 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,9 +22,11 @@ import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
@@ -34,6 +36,7 @@ import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeMirror;
@@ -155,21 +158,25 @@ public class AutoConfigureAnnotationProcessor extends AbstractProcessor {
return result.toString();
}
@SuppressWarnings("unchecked")
private List<Object> getValues(AnnotationMirror annotation) {
return annotation .getElementValues().entrySet().stream().filter(entry -> {
String attributeName = entry.getKey().getSimpleName().toString();
return "name".equals(attributeName) || "value".equals(attributeName);
}).map((entry) -> {
Object value = entry.getValue().getValue();
if (value instanceof List) {
return ((List<AnnotationValue>) value).stream().
map(annotationValue -> processValue(annotationValue.getValue())).collect(Collectors.toList());
}
else {
return Collections.singletonList(processValue(value));
}
}).flatMap(List::stream).collect(Collectors.toList());
return annotation.getElementValues().entrySet().stream()
.filter(this::isNameOrValueAttribute).flatMap(this::getValues)
.collect(Collectors.toList());
}
private boolean isNameOrValueAttribute(Entry<? extends ExecutableElement, ?> entry) {
String attributeName = entry.getKey().getSimpleName().toString();
return "name".equals(attributeName) || "value".equals(attributeName);
}
@SuppressWarnings("unchecked")
private Stream<Object> getValues(Entry<?, ? extends AnnotationValue> entry) {
Object value = entry.getValue().getValue();
if (value instanceof List) {
return ((List<AnnotationValue>) value).stream()
.map((annotation) -> processValue(annotation.getValue()));
}
return Stream.of(processValue(value));
}
private Object processValue(Object value) {