Skip to content

Commit 685babc

Browse files
committed
Polish "Use lambdas for map entry iteration where possible"
Closes spring-projectsgh-12626
1 parent 69bc19e commit 685babc

File tree

27 files changed

+207
-174
lines changed

27 files changed

+207
-174
lines changed

Diff for: spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/condition/ConditionsReportEndpoint.java

+10-18
Original file line numberDiff line numberDiff line change
@@ -123,29 +123,21 @@ public ContextConditionEvaluation(ConfigurableApplicationContext context) {
123123
this.negativeMatches = new LinkedHashMap<>();
124124
this.exclusions = report.getExclusions();
125125
this.unconditionalClasses = report.getUnconditionalClasses();
126-
report.getConditionAndOutcomesBySource().forEach((key, value) -> {
127-
if (value.isFullMatch()) {
128-
add(this.positiveMatches, key, value);
129-
}
130-
else {
131-
add(this.negativeMatches, key, value);
132-
}
133-
});
126+
report.getConditionAndOutcomesBySource().forEach(
127+
(source, conditionAndOutcomes) -> add(source, conditionAndOutcomes));
134128
this.parentId = context.getParent() == null ? null
135129
: context.getParent().getId();
136130
}
137131

138-
private void add(Map<String, MessageAndConditions> map, String source,
139-
ConditionAndOutcomes conditionAndOutcomes) {
132+
private void add(String source, ConditionAndOutcomes conditionAndOutcomes) {
140133
String name = ClassUtils.getShortName(source);
141-
map.put(name, new MessageAndConditions(conditionAndOutcomes));
142-
}
143-
144-
private void add(MultiValueMap<String, MessageAndCondition> map, String source,
145-
ConditionAndOutcomes conditionAndOutcomes) {
146-
String name = ClassUtils.getShortName(source);
147-
for (ConditionAndOutcome conditionAndOutcome : conditionAndOutcomes) {
148-
map.add(name, new MessageAndCondition(conditionAndOutcome));
134+
if (conditionAndOutcomes.isFullMatch()) {
135+
conditionAndOutcomes.forEach((conditionAndOutcome) -> this.positiveMatches
136+
.add(name, new MessageAndCondition(conditionAndOutcome)));
137+
}
138+
else {
139+
this.negativeMatches.put(name,
140+
new MessageAndConditions(conditionAndOutcomes));
149141
}
150142
}
151143

Diff for: spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/health/CompositeHealthIndicatorConfiguration.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -44,7 +44,8 @@ protected HealthIndicator createHealthIndicator(Map<String, S> beans) {
4444
}
4545
CompositeHealthIndicator composite = new CompositeHealthIndicator(
4646
this.healthAggregator);
47-
beans.forEach((key, value) -> composite.addHealthIndicator(key, createHealthIndicator(value)));
47+
beans.forEach((name, source) -> composite.addHealthIndicator(name,
48+
createHealthIndicator(source)));
4849
return composite;
4950
}
5051

Diff for: spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/health/CompositeReactiveHealthIndicatorConfiguration.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,7 +43,8 @@ protected ReactiveHealthIndicator createHealthIndicator(Map<String, S> beans) {
4343
}
4444
CompositeReactiveHealthIndicator composite = new CompositeReactiveHealthIndicator(
4545
this.healthAggregator);
46-
beans.forEach((key, value) -> composite.addHealthIndicator(key, createHealthIndicator(value)));
46+
beans.forEach((name, source) -> composite.addHealthIndicator(name,
47+
createHealthIndicator(source)));
4748
return composite;
4849
}
4950

Diff for: spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/jdbc/DataSourceHealthIndicatorAutoConfiguration.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -84,9 +84,9 @@ private Map<String, DataSource> filterDataSources(
8484
return null;
8585
}
8686
Map<String, DataSource> dataSources = new LinkedHashMap<>();
87-
candidates.forEach((key, value) -> {
88-
if (!(value instanceof AbstractRoutingDataSource)) {
89-
dataSources.put(key, value);
87+
candidates.forEach((name, dataSource) -> {
88+
if (!(dataSource instanceof AbstractRoutingDataSource)) {
89+
dataSources.put(name, dataSource);
9090
}
9191
});
9292
return dataSources;

Diff for: spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/env/EnvironmentEndpoint.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,13 @@ private List<PropertySourceEntryDescriptor> toPropertySourceDescriptors(
120120

121121
private PropertySummaryDescriptor getPropertySummaryDescriptor(
122122
Map<String, PropertyValueDescriptor> descriptors) {
123-
return descriptors.entrySet().stream().
124-
filter((entry) -> entry.getValue() != null).
125-
map((entry) -> new PropertySummaryDescriptor(entry.getKey(), entry.getValue().getValue())).
126-
findFirst().orElse(null);
123+
for (Map.Entry<String, PropertyValueDescriptor> entry : descriptors.entrySet()) {
124+
if (entry.getValue() != null) {
125+
return new PropertySummaryDescriptor(entry.getKey(),
126+
entry.getValue().getValue());
127+
}
128+
}
129+
return null;
127130
}
128131

129132
private Map<String, PropertyValueDescriptor> getPropertySourceDescriptors(

Diff for: spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfigurationSorter.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,14 @@ public AutoConfigurationClass get(String className) {
140140
}
141141

142142
public Set<String> getClassesRequestedAfter(String className) {
143-
Set<String> rtn = new LinkedHashSet<>();
144-
rtn.addAll(get(className).getAfter());
145-
this.classes.forEach((key, value) -> {
146-
if (value.getBefore().contains(className)) {
147-
rtn.add(key);
143+
Set<String> classesRequestedAfter = new LinkedHashSet<>();
144+
classesRequestedAfter.addAll(get(className).getAfter());
145+
this.classes.forEach((name, autoConfigurationClass) -> {
146+
if (autoConfigurationClass.getBefore().contains(className)) {
147+
classesRequestedAfter.add(name);
148148
}
149149
});
150-
return rtn;
150+
return classesRequestedAfter;
151151
}
152152

153153
}

Diff for: spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ImportAutoConfigurationImportSelector.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -74,9 +74,8 @@ protected List<String> getCandidateConfigurations(AnnotationMetadata metadata,
7474
AnnotationAttributes attributes) {
7575
List<String> candidates = new ArrayList<>();
7676
Map<Class<?>, List<Annotation>> annotations = getAnnotations(metadata);
77-
annotations.forEach((key, value) -> {
78-
collectCandidateConfigurations(key, value, candidates);
79-
});
77+
annotations.forEach((source, sourceAnnotations) -> collectCandidateConfigurations(
78+
source, sourceAnnotations, candidates));
8079
return candidates;
8180
}
8281

Diff for: spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheConfigurations.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,13 @@ public static String getConfigurationClass(CacheType cacheType) {
5757
}
5858

5959
public static CacheType getType(String configurationClassName) {
60-
return MAPPINGS.entrySet().stream().filter((entry) ->
61-
entry.getValue().getName().equals(configurationClassName)).
62-
map(Map.Entry::getKey).findFirst().
63-
orElseThrow(() -> new IllegalStateException("Unknown configuration class " + configurationClassName));
60+
for (Map.Entry<CacheType, Class<?>> entry : MAPPINGS.entrySet()) {
61+
if (entry.getValue().getName().equals(configurationClassName)) {
62+
return entry.getKey();
63+
}
64+
}
65+
throw new IllegalStateException(
66+
"Unknown configuration class " + configurationClassName);
6467
}
6568

6669
}

Diff for: spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/AbstractNestedCondition.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,9 @@ private Condition getCondition(String conditionClassName) {
159159

160160
public List<ConditionOutcome> getMatchOutcomes() {
161161
List<ConditionOutcome> outcomes = new ArrayList<>();
162-
this.memberConditions.forEach((metadata, conditions) ->
163-
outcomes.add(new MemberOutcomes(this.context, metadata, conditions).getUltimateOutcome()));
162+
this.memberConditions.forEach((metadata, conditions) -> outcomes
163+
.add(new MemberOutcomes(this.context, metadata, conditions)
164+
.getUltimateOutcome()));
164165
return Collections.unmodifiableList(outcomes);
165166
}
166167

Diff for: spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/BeanTypeRegistry.java

+10-6
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,11 @@ static BeanTypeRegistry get(ListableBeanFactory beanFactory) {
113113
*/
114114
Set<String> getNamesForType(Class<?> type) {
115115
updateTypesIfNecessary();
116-
return this.beanTypes.entrySet().stream().filter((entry) -> entry.getValue() != null &&
117-
type.isAssignableFrom(entry.getValue())).
118-
map(Map.Entry::getKey).collect(Collectors.toCollection(LinkedHashSet::new));
116+
return this.beanTypes.entrySet().stream()
117+
.filter((entry) -> entry.getValue() != null
118+
&& type.isAssignableFrom(entry.getValue()))
119+
.map(Map.Entry::getKey)
120+
.collect(Collectors.toCollection(LinkedHashSet::new));
119121
}
120122

121123
/**
@@ -129,9 +131,11 @@ Set<String> getNamesForType(Class<?> type) {
129131
*/
130132
Set<String> getNamesForAnnotation(Class<? extends Annotation> annotation) {
131133
updateTypesIfNecessary();
132-
return this.beanTypes.entrySet().stream().filter((entry) -> entry.getValue() != null &&
133-
AnnotationUtils.findAnnotation(entry.getValue(), annotation) != null).
134-
map(Map.Entry::getKey).collect(Collectors.toCollection(LinkedHashSet::new));
134+
return this.beanTypes.entrySet().stream()
135+
.filter((entry) -> entry.getValue() != null && AnnotationUtils
136+
.findAnnotation(entry.getValue(), annotation) != null)
137+
.map(Map.Entry::getKey)
138+
.collect(Collectors.toCollection(LinkedHashSet::new));
135139
}
136140

137141
@Override

Diff for: spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionEvaluationReport.java

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -111,9 +111,9 @@ public void recordEvaluationCandidates(List<String> evaluationCandidates) {
111111
*/
112112
public Map<String, ConditionAndOutcomes> getConditionAndOutcomesBySource() {
113113
if (!this.addedAncestorOutcomes) {
114-
this.outcomes.forEach((key, value) -> {
115-
if (!value.isFullMatch()) {
116-
addNoMatchOutcomeToAncestors(key);
114+
this.outcomes.forEach((source, sourceOutcomes) -> {
115+
if (!sourceOutcomes.isFullMatch()) {
116+
addNoMatchOutcomeToAncestors(source);
117117
}
118118
});
119119
this.addedAncestorOutcomes = true;
@@ -123,11 +123,11 @@ public Map<String, ConditionAndOutcomes> getConditionAndOutcomesBySource() {
123123

124124
private void addNoMatchOutcomeToAncestors(String source) {
125125
String prefix = source + "$";
126-
this.outcomes.forEach((key, value) -> {
127-
if (key.startsWith(prefix)) {
126+
this.outcomes.forEach((candidateSource, sourceOutcomes) -> {
127+
if (candidateSource.startsWith(prefix)) {
128128
ConditionOutcome outcome = ConditionOutcome.noMatch(ConditionMessage
129129
.forCondition("Ancestor " + source).because("did not match"));
130-
value.add(ANCESTOR_CONDITION, outcome);
130+
sourceOutcomes.add(ANCESTOR_CONDITION, outcome);
131131
}
132132
});
133133
}
@@ -188,12 +188,13 @@ private static void locateParent(BeanFactory beanFactory,
188188

189189
public ConditionEvaluationReport getDelta(ConditionEvaluationReport previousReport) {
190190
ConditionEvaluationReport delta = new ConditionEvaluationReport();
191-
this.outcomes.forEach((key, value) -> {
192-
ConditionAndOutcomes previous = previousReport.outcomes.get(key);
191+
this.outcomes.forEach((source, sourceOutcomes) -> {
192+
ConditionAndOutcomes previous = previousReport.outcomes.get(source);
193193
if (previous == null
194-
|| previous.isFullMatch() != value.isFullMatch()) {
195-
value.forEach((conditionAndOutcome) -> delta.recordConditionEvaluation(
196-
key, conditionAndOutcome.getCondition(),
194+
|| previous.isFullMatch() != sourceOutcomes.isFullMatch()) {
195+
sourceOutcomes.forEach(
196+
(conditionAndOutcome) -> delta.recordConditionEvaluation(source,
197+
conditionAndOutcome.getCondition(),
197198
conditionAndOutcome.getOutcome()));
198199
}
199200
});

Diff for: spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnBeanCondition.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,7 @@ private void appendMessageForMatches(StringBuilder reason,
175175
reason.append(" '");
176176
reason.append(key);
177177
reason.append("' ");
178-
reason.append(
179-
StringUtils.collectionToDelimitedString(value, ", "));
178+
reason.append(StringUtils.collectionToDelimitedString(value, ", "));
180179
});
181180
}
182181
}

Diff for: spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/diagnostics/analyzer/NoSuchBeanDefinitionFailureAnalyzer.java

+20-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@
3030
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
3131
import org.springframework.boot.autoconfigure.condition.ConditionEvaluationReport;
3232
import org.springframework.boot.autoconfigure.condition.ConditionEvaluationReport.ConditionAndOutcome;
33+
import org.springframework.boot.autoconfigure.condition.ConditionEvaluationReport.ConditionAndOutcomes;
3334
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
3435
import org.springframework.boot.diagnostics.FailureAnalysis;
3536
import org.springframework.boot.diagnostics.analyzer.AbstractInjectionFailureAnalyzer;
@@ -123,20 +124,26 @@ private List<AutoConfigurationResult> getAutoConfigurationResults(
123124

124125
private void collectReportedConditionOutcomes(NoSuchBeanDefinitionException cause,
125126
List<AutoConfigurationResult> results) {
126-
this.report.getConditionAndOutcomesBySource().forEach((key, conditionAndOutcomes) -> {
127-
Source source = new Source(key);
128-
if (!conditionAndOutcomes.isFullMatch()) {
129-
BeanMethods methods = new BeanMethods(source, cause);
130-
for (ConditionAndOutcome conditionAndOutcome : conditionAndOutcomes) {
131-
if (!conditionAndOutcome.getOutcome().isMatch()) {
132-
for (MethodMetadata method : methods) {
133-
results.add(new AutoConfigurationResult(method,
134-
conditionAndOutcome.getOutcome(), source.isMethod()));
135-
}
136-
}
127+
this.report.getConditionAndOutcomesBySource().forEach(
128+
(source, sourceOutcomes) -> collectReportedConditionOutcomes(cause,
129+
new Source(source), sourceOutcomes, results));
130+
}
131+
132+
private void collectReportedConditionOutcomes(NoSuchBeanDefinitionException cause,
133+
Source source, ConditionAndOutcomes sourceOutcomes,
134+
List<AutoConfigurationResult> results) {
135+
if (sourceOutcomes.isFullMatch()) {
136+
return;
137+
}
138+
BeanMethods methods = new BeanMethods(source, cause);
139+
for (ConditionAndOutcome conditionAndOutcome : sourceOutcomes) {
140+
if (!conditionAndOutcome.getOutcome().isMatch()) {
141+
for (MethodMetadata method : methods) {
142+
results.add(new AutoConfigurationResult(method,
143+
conditionAndOutcome.getOutcome(), source.isMethod()));
137144
}
138145
}
139-
});
146+
}
140147
}
141148

142149
private void collectExcludedAutoConfiguration(NoSuchBeanDefinitionException cause,

0 commit comments

Comments
 (0)