Use lambdas for map entry iteration where possible

See gh-12626
This commit is contained in:
igor-suhorukov
2018-03-24 09:19:03 +03:00
committed by Phillip Webb
parent 78a94cafe1
commit 69bc19e0ca
31 changed files with 125 additions and 232 deletions

View File

@@ -142,12 +142,11 @@ class AutoConfigurationSorter {
public Set<String> getClassesRequestedAfter(String className) {
Set<String> rtn = new LinkedHashSet<>();
rtn.addAll(get(className).getAfter());
for (Map.Entry<String, AutoConfigurationClass> entry : this.classes
.entrySet()) {
if (entry.getValue().getBefore().contains(className)) {
rtn.add(entry.getKey());
this.classes.forEach((key, value) -> {
if (value.getBefore().contains(className)) {
rtn.add(key);
}
}
});
return rtn;
}

View File

@@ -74,9 +74,9 @@ class ImportAutoConfigurationImportSelector extends AutoConfigurationImportSelec
AnnotationAttributes attributes) {
List<String> candidates = new ArrayList<>();
Map<Class<?>, List<Annotation>> annotations = getAnnotations(metadata);
for (Map.Entry<Class<?>, List<Annotation>> entry : annotations.entrySet()) {
collectCandidateConfigurations(entry.getKey(), entry.getValue(), candidates);
}
annotations.forEach((key, value) -> {
collectCandidateConfigurations(key, value, candidates);
});
return candidates;
}

View File

@@ -57,13 +57,10 @@ final class CacheConfigurations {
}
public static CacheType getType(String configurationClassName) {
for (Map.Entry<CacheType, Class<?>> entry : MAPPINGS.entrySet()) {
if (entry.getValue().getName().equals(configurationClassName)) {
return entry.getKey();
}
}
throw new IllegalStateException(
"Unknown configuration class " + configurationClassName);
return MAPPINGS.entrySet().stream().filter((entry) ->
entry.getValue().getName().equals(configurationClassName)).
map(Map.Entry::getKey).findFirst().
orElseThrow(() -> new IllegalStateException("Unknown configuration class " + configurationClassName));
}
}

View File

@@ -159,13 +159,8 @@ public abstract class AbstractNestedCondition extends SpringBootCondition
public List<ConditionOutcome> getMatchOutcomes() {
List<ConditionOutcome> outcomes = new ArrayList<>();
for (Map.Entry<AnnotationMetadata, List<Condition>> entry : this.memberConditions
.entrySet()) {
AnnotationMetadata metadata = entry.getKey();
List<Condition> conditions = entry.getValue();
outcomes.add(new MemberOutcomes(this.context, metadata, conditions)
.getUltimateOutcome());
}
this.memberConditions.forEach((metadata, conditions) ->
outcomes.add(new MemberOutcomes(this.context, metadata, conditions).getUltimateOutcome()));
return Collections.unmodifiableList(outcomes);
}

View File

@@ -24,6 +24,7 @@ import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -112,13 +113,9 @@ final class BeanTypeRegistry implements SmartInitializingSingleton {
*/
Set<String> getNamesForType(Class<?> type) {
updateTypesIfNecessary();
Set<String> matches = new LinkedHashSet<>();
for (Map.Entry<String, Class<?>> entry : this.beanTypes.entrySet()) {
if (entry.getValue() != null && type.isAssignableFrom(entry.getValue())) {
matches.add(entry.getKey());
}
}
return matches;
return this.beanTypes.entrySet().stream().filter((entry) -> entry.getValue() != null &&
type.isAssignableFrom(entry.getValue())).
map(Map.Entry::getKey).collect(Collectors.toCollection(LinkedHashSet::new));
}
/**
@@ -132,14 +129,9 @@ final class BeanTypeRegistry implements SmartInitializingSingleton {
*/
Set<String> getNamesForAnnotation(Class<? extends Annotation> annotation) {
updateTypesIfNecessary();
Set<String> matches = new LinkedHashSet<>();
for (Map.Entry<String, Class<?>> entry : this.beanTypes.entrySet()) {
if (entry.getValue() != null && AnnotationUtils
.findAnnotation(entry.getValue(), annotation) != null) {
matches.add(entry.getKey());
}
}
return matches;
return this.beanTypes.entrySet().stream().filter((entry) -> entry.getValue() != null &&
AnnotationUtils.findAnnotation(entry.getValue(), annotation) != null).
map(Map.Entry::getKey).collect(Collectors.toCollection(LinkedHashSet::new));
}
@Override

View File

@@ -24,7 +24,6 @@ import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
@@ -112,12 +111,11 @@ public final class ConditionEvaluationReport {
*/
public Map<String, ConditionAndOutcomes> getConditionAndOutcomesBySource() {
if (!this.addedAncestorOutcomes) {
for (Map.Entry<String, ConditionAndOutcomes> entry : this.outcomes
.entrySet()) {
if (!entry.getValue().isFullMatch()) {
addNoMatchOutcomeToAncestors(entry.getKey());
this.outcomes.forEach((key, value) -> {
if (!value.isFullMatch()) {
addNoMatchOutcomeToAncestors(key);
}
}
});
this.addedAncestorOutcomes = true;
}
return Collections.unmodifiableMap(this.outcomes);
@@ -125,13 +123,13 @@ public final class ConditionEvaluationReport {
private void addNoMatchOutcomeToAncestors(String source) {
String prefix = source + "$";
for (Entry<String, ConditionAndOutcomes> entry : this.outcomes.entrySet()) {
if (entry.getKey().startsWith(prefix)) {
this.outcomes.forEach((key, value) -> {
if (key.startsWith(prefix)) {
ConditionOutcome outcome = ConditionOutcome.noMatch(ConditionMessage
.forCondition("Ancestor " + source).because("did not match"));
entry.getValue().add(ANCESTOR_CONDITION, outcome);
value.add(ANCESTOR_CONDITION, outcome);
}
}
});
}
/**
@@ -190,16 +188,15 @@ public final class ConditionEvaluationReport {
public ConditionEvaluationReport getDelta(ConditionEvaluationReport previousReport) {
ConditionEvaluationReport delta = new ConditionEvaluationReport();
for (Entry<String, ConditionAndOutcomes> entry : this.outcomes.entrySet()) {
ConditionAndOutcomes previous = previousReport.outcomes.get(entry.getKey());
this.outcomes.forEach((key, value) -> {
ConditionAndOutcomes previous = previousReport.outcomes.get(key);
if (previous == null
|| previous.isFullMatch() != entry.getValue().isFullMatch()) {
entry.getValue()
.forEach((conditionAndOutcome) -> delta.recordConditionEvaluation(
entry.getKey(), conditionAndOutcome.getCondition(),
|| previous.isFullMatch() != value.isFullMatch()) {
value.forEach((conditionAndOutcome) -> delta.recordConditionEvaluation(
key, conditionAndOutcome.getCondition(),
conditionAndOutcome.getOutcome()));
}
}
});
List<String> newExclusions = new ArrayList<>(this.exclusions);
newExclusions.removeAll(previousReport.getExclusions());
delta.recordExclusions(newExclusions);

View File

@@ -166,18 +166,18 @@ class OnBeanCondition extends SpringBootCondition implements ConfigurationCondit
private void appendMessageForMatches(StringBuilder reason,
Map<String, Collection<String>> matches, String description) {
if (!matches.isEmpty()) {
for (Map.Entry<String, Collection<String>> match : matches.entrySet()) {
matches.forEach((key, value) -> {
if (reason.length() > 0) {
reason.append(" and ");
}
reason.append("found beans ");
reason.append(description);
reason.append(" '");
reason.append(match.getKey());
reason.append(key);
reason.append("' ");
reason.append(
StringUtils.collectionToDelimitedString(match.getValue(), ", "));
}
StringUtils.collectionToDelimitedString(value, ", "));
});
}
}

View File

@@ -20,7 +20,6 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.springframework.boot.autoconfigure.condition.ConditionMessage.Style;
import org.springframework.context.annotation.Condition;
@@ -69,8 +68,8 @@ class OnPropertyCondition extends SpringBootCondition {
private List<AnnotationAttributes> annotationAttributesFromMultiValueMap(
MultiValueMap<String, Object> multiValueMap) {
List<Map<String, Object>> maps = new ArrayList<>();
for (Entry<String, List<Object>> entry : multiValueMap.entrySet()) {
for (int i = 0; i < entry.getValue().size(); i++) {
multiValueMap.forEach((key, value) -> {
for (int i = 0; i < value.size(); i++) {
Map<String, Object> map;
if (i < maps.size()) {
map = maps.get(i);
@@ -79,9 +78,9 @@ class OnPropertyCondition extends SpringBootCondition {
map = new HashMap<>();
maps.add(map);
}
map.put(entry.getKey(), entry.getValue().get(i));
map.put(key, value.get(i));
}
}
});
List<AnnotationAttributes> annotationAttributes = new ArrayList<>(maps.size());
for (Map<String, Object> map : maps) {
annotationAttributes.add(AnnotationAttributes.fromMap(map));

View File

@@ -30,7 +30,6 @@ import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.autoconfigure.condition.ConditionEvaluationReport;
import org.springframework.boot.autoconfigure.condition.ConditionEvaluationReport.ConditionAndOutcome;
import org.springframework.boot.autoconfigure.condition.ConditionEvaluationReport.ConditionAndOutcomes;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.diagnostics.FailureAnalysis;
import org.springframework.boot.diagnostics.analyzer.AbstractInjectionFailureAnalyzer;
@@ -124,10 +123,8 @@ class NoSuchBeanDefinitionFailureAnalyzer
private void collectReportedConditionOutcomes(NoSuchBeanDefinitionException cause,
List<AutoConfigurationResult> results) {
for (Map.Entry<String, ConditionAndOutcomes> entry : this.report
.getConditionAndOutcomesBySource().entrySet()) {
Source source = new Source(entry.getKey());
ConditionAndOutcomes conditionAndOutcomes = entry.getValue();
this.report.getConditionAndOutcomesBySource().forEach((key, conditionAndOutcomes) -> {
Source source = new Source(key);
if (!conditionAndOutcomes.isFullMatch()) {
BeanMethods methods = new BeanMethods(source, cause);
for (ConditionAndOutcome conditionAndOutcome : conditionAndOutcomes) {
@@ -139,7 +136,7 @@ class NoSuchBeanDefinitionFailureAnalyzer
}
}
}
}
});
}
private void collectExcludedAutoConfiguration(NoSuchBeanDefinitionException cause,

View File

@@ -19,7 +19,6 @@ package org.springframework.boot.autoconfigure.jersey;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;
import java.util.Map.Entry;
import javax.annotation.PostConstruct;
import javax.servlet.DispatcherType;
@@ -181,9 +180,7 @@ public class JerseyAutoConfiguration implements ServletContextAware {
}
private void addInitParameters(DynamicRegistrationBean<?> registration) {
for (Entry<String, String> entry : this.jersey.getInit().entrySet()) {
registration.addInitParameter(entry.getKey(), entry.getValue());
}
this.jersey.getInit().forEach(registration::addInitParameter);
}
private static String findApplicationPath(ApplicationPath annotation) {

View File

@@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.session;
import java.util.Collections;
import java.util.EnumMap;
import java.util.Map;
import java.util.Objects;
import org.springframework.boot.WebApplicationType;
import org.springframework.util.Assert;
@@ -82,18 +83,12 @@ final class SessionStoreMappings {
static StoreType getType(WebApplicationType webApplicationType,
String configurationClassName) {
for (Map.Entry<StoreType, Map<WebApplicationType, Class<?>>> storeEntry : MAPPINGS
.entrySet()) {
for (Map.Entry<WebApplicationType, Class<?>> entry : storeEntry.getValue()
.entrySet()) {
if (entry.getKey() == webApplicationType
&& entry.getValue().getName().equals(configurationClassName)) {
return storeEntry.getKey();
}
}
}
throw new IllegalStateException(
"Unknown configuration class " + configurationClassName);
return MAPPINGS.entrySet().stream().map(entry ->
entry.getValue().entrySet().stream().filter(webAppEntry -> webAppEntry.getKey() == webApplicationType
&& webAppEntry.getValue().getName().equals(configurationClassName)).
map(webAppEntry -> entry.getKey()).findFirst().orElse(null)).filter(Objects::nonNull).
findFirst().orElseThrow(() ->
new IllegalStateException("Unknown configuration class " + configurationClassName));
}
}

View File

@@ -24,7 +24,6 @@ import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import javax.servlet.Servlet;
@@ -236,9 +235,7 @@ public class WebMvcAutoConfiguration {
}
Map<String, MediaType> mediaTypes = this.mvcProperties.getContentnegotiation()
.getMediaTypes();
for (Entry<String, MediaType> mediaType : mediaTypes.entrySet()) {
configurer.mediaType(mediaType.getKey(), mediaType.getValue());
}
mediaTypes.forEach(configurer::mediaType);
}
@Bean

View File

@@ -19,7 +19,6 @@ package org.springframework.boot.autoconfigure.webservices;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
@@ -83,9 +82,7 @@ public class WebServicesAutoConfiguration {
servlet, urlMapping);
WebServicesProperties.Servlet servletProperties = this.properties.getServlet();
registration.setLoadOnStartup(servletProperties.getLoadOnStartup());
for (Map.Entry<String, String> entry : servletProperties.getInit().entrySet()) {
registration.addInitParameter(entry.getKey(), entry.getValue());
}
servletProperties.getInit().forEach(registration::addInitParameter);
return registration;
}