Skip to content

Commit 236b1bc

Browse files
committed
Merge pull request #12626 from igor-suhorukov
* pr/12626: Polish "Use lambdas for map entry iteration where possible" Use lambdas for map entry iteration where possible
2 parents 78a94ca + 685babc commit 236b1bc

File tree

29 files changed

+222
-296
lines changed

29 files changed

+222
-296
lines changed

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

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -123,30 +123,21 @@ public ContextConditionEvaluation(ConfigurableApplicationContext context) {
123123
this.negativeMatches = new LinkedHashMap<>();
124124
this.exclusions = report.getExclusions();
125125
this.unconditionalClasses = report.getUnconditionalClasses();
126-
for (Map.Entry<String, ConditionAndOutcomes> entry : report
127-
.getConditionAndOutcomesBySource().entrySet()) {
128-
if (entry.getValue().isFullMatch()) {
129-
add(this.positiveMatches, entry.getKey(), entry.getValue());
130-
}
131-
else {
132-
add(this.negativeMatches, entry.getKey(), entry.getValue());
133-
}
134-
}
126+
report.getConditionAndOutcomesBySource().forEach(
127+
(source, conditionAndOutcomes) -> add(source, conditionAndOutcomes));
135128
this.parentId = context.getParent() == null ? null
136129
: context.getParent().getId();
137130
}
138131

139-
private void add(Map<String, MessageAndConditions> map, String source,
140-
ConditionAndOutcomes conditionAndOutcomes) {
141-
String name = ClassUtils.getShortName(source);
142-
map.put(name, new MessageAndConditions(conditionAndOutcomes));
143-
}
144-
145-
private void add(MultiValueMap<String, MessageAndCondition> map, String source,
146-
ConditionAndOutcomes conditionAndOutcomes) {
132+
private void add(String source, ConditionAndOutcomes conditionAndOutcomes) {
147133
String name = ClassUtils.getShortName(source);
148-
for (ConditionAndOutcome conditionAndOutcome : conditionAndOutcomes) {
149-
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));
150141
}
151142
}
152143

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

Lines changed: 3 additions & 5 deletions
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,10 +44,8 @@ protected HealthIndicator createHealthIndicator(Map<String, S> beans) {
4444
}
4545
CompositeHealthIndicator composite = new CompositeHealthIndicator(
4646
this.healthAggregator);
47-
for (Map.Entry<String, S> entry : beans.entrySet()) {
48-
composite.addHealthIndicator(entry.getKey(),
49-
createHealthIndicator(entry.getValue()));
50-
}
47+
beans.forEach((name, source) -> composite.addHealthIndicator(name,
48+
createHealthIndicator(source)));
5149
return composite;
5250
}
5351

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

Lines changed: 3 additions & 5 deletions
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,10 +43,8 @@ protected ReactiveHealthIndicator createHealthIndicator(Map<String, S> beans) {
4343
}
4444
CompositeReactiveHealthIndicator composite = new CompositeReactiveHealthIndicator(
4545
this.healthAggregator);
46-
for (Map.Entry<String, S> entry : beans.entrySet()) {
47-
composite.addHealthIndicator(entry.getKey(),
48-
createHealthIndicator(entry.getValue()));
49-
}
46+
beans.forEach((name, source) -> composite.addHealthIndicator(name,
47+
createHealthIndicator(source)));
5048
return composite;
5149
}
5250

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

Lines changed: 5 additions & 5 deletions
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,11 +84,11 @@ private Map<String, DataSource> filterDataSources(
8484
return null;
8585
}
8686
Map<String, DataSource> dataSources = new LinkedHashMap<>();
87-
for (Map.Entry<String, DataSource> entry : candidates.entrySet()) {
88-
if (!(entry.getValue() instanceof AbstractRoutingDataSource)) {
89-
dataSources.put(entry.getKey(), entry.getValue());
87+
candidates.forEach((name, dataSource) -> {
88+
if (!(dataSource instanceof AbstractRoutingDataSource)) {
89+
dataSources.put(name, dataSource);
9090
}
91-
}
91+
});
9292
return dataSources;
9393
}
9494

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +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-
for (Map.Entry<String, AutoConfigurationClass> entry : this.classes
146-
.entrySet()) {
147-
if (entry.getValue().getBefore().contains(className)) {
148-
rtn.add(entry.getKey());
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);
149148
}
150-
}
151-
return rtn;
149+
});
150+
return classesRequestedAfter;
152151
}
153152

154153
}

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

Lines changed: 3 additions & 4 deletions
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-
for (Map.Entry<Class<?>, List<Annotation>> entry : annotations.entrySet()) {
78-
collectCandidateConfigurations(entry.getKey(), entry.getValue(), candidates);
79-
}
77+
annotations.forEach((source, sourceAnnotations) -> collectCandidateConfigurations(
78+
source, sourceAnnotations, candidates));
8079
return candidates;
8180
}
8281

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,9 @@ private Condition getCondition(String conditionClassName) {
159159

160160
public List<ConditionOutcome> getMatchOutcomes() {
161161
List<ConditionOutcome> outcomes = new ArrayList<>();
162-
for (Map.Entry<AnnotationMetadata, List<Condition>> entry : this.memberConditions
163-
.entrySet()) {
164-
AnnotationMetadata metadata = entry.getKey();
165-
List<Condition> conditions = entry.getValue();
166-
outcomes.add(new MemberOutcomes(this.context, metadata, conditions)
167-
.getUltimateOutcome());
168-
}
162+
this.memberConditions.forEach((metadata, conditions) -> outcomes
163+
.add(new MemberOutcomes(this.context, metadata, conditions)
164+
.getUltimateOutcome()));
169165
return Collections.unmodifiableList(outcomes);
170166
}
171167

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

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.LinkedHashSet;
2525
import java.util.Map;
2626
import java.util.Set;
27+
import java.util.stream.Collectors;
2728

2829
import org.apache.commons.logging.Log;
2930
import org.apache.commons.logging.LogFactory;
@@ -112,13 +113,11 @@ static BeanTypeRegistry get(ListableBeanFactory beanFactory) {
112113
*/
113114
Set<String> getNamesForType(Class<?> type) {
114115
updateTypesIfNecessary();
115-
Set<String> matches = new LinkedHashSet<>();
116-
for (Map.Entry<String, Class<?>> entry : this.beanTypes.entrySet()) {
117-
if (entry.getValue() != null && type.isAssignableFrom(entry.getValue())) {
118-
matches.add(entry.getKey());
119-
}
120-
}
121-
return matches;
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));
122121
}
123122

124123
/**
@@ -132,14 +131,11 @@ Set<String> getNamesForType(Class<?> type) {
132131
*/
133132
Set<String> getNamesForAnnotation(Class<? extends Annotation> annotation) {
134133
updateTypesIfNecessary();
135-
Set<String> matches = new LinkedHashSet<>();
136-
for (Map.Entry<String, Class<?>> entry : this.beanTypes.entrySet()) {
137-
if (entry.getValue() != null && AnnotationUtils
138-
.findAnnotation(entry.getValue(), annotation) != null) {
139-
matches.add(entry.getKey());
140-
}
141-
}
142-
return matches;
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));
143139
}
144140

145141
@Override

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

Lines changed: 16 additions & 18 deletions
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.
@@ -24,7 +24,6 @@
2424
import java.util.LinkedHashSet;
2525
import java.util.List;
2626
import java.util.Map;
27-
import java.util.Map.Entry;
2827
import java.util.Set;
2928
import java.util.SortedMap;
3029
import java.util.TreeMap;
@@ -112,26 +111,25 @@ public void recordEvaluationCandidates(List<String> evaluationCandidates) {
112111
*/
113112
public Map<String, ConditionAndOutcomes> getConditionAndOutcomesBySource() {
114113
if (!this.addedAncestorOutcomes) {
115-
for (Map.Entry<String, ConditionAndOutcomes> entry : this.outcomes
116-
.entrySet()) {
117-
if (!entry.getValue().isFullMatch()) {
118-
addNoMatchOutcomeToAncestors(entry.getKey());
114+
this.outcomes.forEach((source, sourceOutcomes) -> {
115+
if (!sourceOutcomes.isFullMatch()) {
116+
addNoMatchOutcomeToAncestors(source);
119117
}
120-
}
118+
});
121119
this.addedAncestorOutcomes = true;
122120
}
123121
return Collections.unmodifiableMap(this.outcomes);
124122
}
125123

126124
private void addNoMatchOutcomeToAncestors(String source) {
127125
String prefix = source + "$";
128-
for (Entry<String, ConditionAndOutcomes> entry : this.outcomes.entrySet()) {
129-
if (entry.getKey().startsWith(prefix)) {
126+
this.outcomes.forEach((candidateSource, sourceOutcomes) -> {
127+
if (candidateSource.startsWith(prefix)) {
130128
ConditionOutcome outcome = ConditionOutcome.noMatch(ConditionMessage
131129
.forCondition("Ancestor " + source).because("did not match"));
132-
entry.getValue().add(ANCESTOR_CONDITION, outcome);
130+
sourceOutcomes.add(ANCESTOR_CONDITION, outcome);
133131
}
134-
}
132+
});
135133
}
136134

137135
/**
@@ -190,16 +188,16 @@ private static void locateParent(BeanFactory beanFactory,
190188

191189
public ConditionEvaluationReport getDelta(ConditionEvaluationReport previousReport) {
192190
ConditionEvaluationReport delta = new ConditionEvaluationReport();
193-
for (Entry<String, ConditionAndOutcomes> entry : this.outcomes.entrySet()) {
194-
ConditionAndOutcomes previous = previousReport.outcomes.get(entry.getKey());
191+
this.outcomes.forEach((source, sourceOutcomes) -> {
192+
ConditionAndOutcomes previous = previousReport.outcomes.get(source);
195193
if (previous == null
196-
|| previous.isFullMatch() != entry.getValue().isFullMatch()) {
197-
entry.getValue()
198-
.forEach((conditionAndOutcome) -> delta.recordConditionEvaluation(
199-
entry.getKey(), conditionAndOutcome.getCondition(),
194+
|| previous.isFullMatch() != sourceOutcomes.isFullMatch()) {
195+
sourceOutcomes.forEach(
196+
(conditionAndOutcome) -> delta.recordConditionEvaluation(source,
197+
conditionAndOutcome.getCondition(),
200198
conditionAndOutcome.getOutcome()));
201199
}
202-
}
200+
});
203201
List<String> newExclusions = new ArrayList<>(this.exclusions);
204202
newExclusions.removeAll(previousReport.getExclusions());
205203
delta.recordExclusions(newExclusions);

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,18 +166,17 @@ private String createOnMissingBeanNoMatchReason(MatchResult matchResult) {
166166
private void appendMessageForMatches(StringBuilder reason,
167167
Map<String, Collection<String>> matches, String description) {
168168
if (!matches.isEmpty()) {
169-
for (Map.Entry<String, Collection<String>> match : matches.entrySet()) {
169+
matches.forEach((key, value) -> {
170170
if (reason.length() > 0) {
171171
reason.append(" and ");
172172
}
173173
reason.append("found beans ");
174174
reason.append(description);
175175
reason.append(" '");
176-
reason.append(match.getKey());
176+
reason.append(key);
177177
reason.append("' ");
178-
reason.append(
179-
StringUtils.collectionToDelimitedString(match.getValue(), ", "));
180-
}
178+
reason.append(StringUtils.collectionToDelimitedString(value, ", "));
179+
});
181180
}
182181
}
183182

0 commit comments

Comments
 (0)