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

@@ -140,14 +140,14 @@ class AutoConfigurationSorter {
}
public Set<String> getClassesRequestedAfter(String className) {
Set<String> rtn = new LinkedHashSet<>();
rtn.addAll(get(className).getAfter());
this.classes.forEach((key, value) -> {
if (value.getBefore().contains(className)) {
rtn.add(key);
Set<String> classesRequestedAfter = new LinkedHashSet<>();
classesRequestedAfter.addAll(get(className).getAfter());
this.classes.forEach((name, autoConfigurationClass) -> {
if (autoConfigurationClass.getBefore().contains(className)) {
classesRequestedAfter.add(name);
}
});
return rtn;
return classesRequestedAfter;
}
}

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.
@@ -74,9 +74,8 @@ class ImportAutoConfigurationImportSelector extends AutoConfigurationImportSelec
AnnotationAttributes attributes) {
List<String> candidates = new ArrayList<>();
Map<Class<?>, List<Annotation>> annotations = getAnnotations(metadata);
annotations.forEach((key, value) -> {
collectCandidateConfigurations(key, value, candidates);
});
annotations.forEach((source, sourceAnnotations) -> collectCandidateConfigurations(
source, sourceAnnotations, candidates));
return candidates;
}

View File

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

View File

@@ -159,8 +159,9 @@ public abstract class AbstractNestedCondition extends SpringBootCondition
public List<ConditionOutcome> getMatchOutcomes() {
List<ConditionOutcome> outcomes = new ArrayList<>();
this.memberConditions.forEach((metadata, conditions) ->
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

@@ -113,9 +113,11 @@ final class BeanTypeRegistry implements SmartInitializingSingleton {
*/
Set<String> getNamesForType(Class<?> type) {
updateTypesIfNecessary();
return this.beanTypes.entrySet().stream().filter((entry) -> entry.getValue() != null &&
type.isAssignableFrom(entry.getValue())).
map(Map.Entry::getKey).collect(Collectors.toCollection(LinkedHashSet::new));
return this.beanTypes.entrySet().stream()
.filter((entry) -> entry.getValue() != null
&& type.isAssignableFrom(entry.getValue()))
.map(Map.Entry::getKey)
.collect(Collectors.toCollection(LinkedHashSet::new));
}
/**
@@ -129,9 +131,11 @@ final class BeanTypeRegistry implements SmartInitializingSingleton {
*/
Set<String> getNamesForAnnotation(Class<? extends Annotation> annotation) {
updateTypesIfNecessary();
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));
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

@@ -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.
@@ -111,9 +111,9 @@ public final class ConditionEvaluationReport {
*/
public Map<String, ConditionAndOutcomes> getConditionAndOutcomesBySource() {
if (!this.addedAncestorOutcomes) {
this.outcomes.forEach((key, value) -> {
if (!value.isFullMatch()) {
addNoMatchOutcomeToAncestors(key);
this.outcomes.forEach((source, sourceOutcomes) -> {
if (!sourceOutcomes.isFullMatch()) {
addNoMatchOutcomeToAncestors(source);
}
});
this.addedAncestorOutcomes = true;
@@ -123,11 +123,11 @@ public final class ConditionEvaluationReport {
private void addNoMatchOutcomeToAncestors(String source) {
String prefix = source + "$";
this.outcomes.forEach((key, value) -> {
if (key.startsWith(prefix)) {
this.outcomes.forEach((candidateSource, sourceOutcomes) -> {
if (candidateSource.startsWith(prefix)) {
ConditionOutcome outcome = ConditionOutcome.noMatch(ConditionMessage
.forCondition("Ancestor " + source).because("did not match"));
value.add(ANCESTOR_CONDITION, outcome);
sourceOutcomes.add(ANCESTOR_CONDITION, outcome);
}
});
}
@@ -188,12 +188,13 @@ public final class ConditionEvaluationReport {
public ConditionEvaluationReport getDelta(ConditionEvaluationReport previousReport) {
ConditionEvaluationReport delta = new ConditionEvaluationReport();
this.outcomes.forEach((key, value) -> {
ConditionAndOutcomes previous = previousReport.outcomes.get(key);
this.outcomes.forEach((source, sourceOutcomes) -> {
ConditionAndOutcomes previous = previousReport.outcomes.get(source);
if (previous == null
|| previous.isFullMatch() != value.isFullMatch()) {
value.forEach((conditionAndOutcome) -> delta.recordConditionEvaluation(
key, conditionAndOutcome.getCondition(),
|| previous.isFullMatch() != sourceOutcomes.isFullMatch()) {
sourceOutcomes.forEach(
(conditionAndOutcome) -> delta.recordConditionEvaluation(source,
conditionAndOutcome.getCondition(),
conditionAndOutcome.getOutcome()));
}
});

View File

@@ -175,8 +175,7 @@ class OnBeanCondition extends SpringBootCondition implements ConfigurationCondit
reason.append(" '");
reason.append(key);
reason.append("' ");
reason.append(
StringUtils.collectionToDelimitedString(value, ", "));
reason.append(StringUtils.collectionToDelimitedString(value, ", "));
});
}
}

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.
@@ -30,6 +30,7 @@ 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;
@@ -123,20 +124,26 @@ class NoSuchBeanDefinitionFailureAnalyzer
private void collectReportedConditionOutcomes(NoSuchBeanDefinitionException cause,
List<AutoConfigurationResult> results) {
this.report.getConditionAndOutcomesBySource().forEach((key, conditionAndOutcomes) -> {
Source source = new Source(key);
if (!conditionAndOutcomes.isFullMatch()) {
BeanMethods methods = new BeanMethods(source, cause);
for (ConditionAndOutcome conditionAndOutcome : conditionAndOutcomes) {
if (!conditionAndOutcome.getOutcome().isMatch()) {
for (MethodMetadata method : methods) {
results.add(new AutoConfigurationResult(method,
conditionAndOutcome.getOutcome(), source.isMethod()));
}
}
this.report.getConditionAndOutcomesBySource().forEach(
(source, sourceOutcomes) -> collectReportedConditionOutcomes(cause,
new Source(source), sourceOutcomes, results));
}
private void collectReportedConditionOutcomes(NoSuchBeanDefinitionException cause,
Source source, ConditionAndOutcomes sourceOutcomes,
List<AutoConfigurationResult> results) {
if (sourceOutcomes.isFullMatch()) {
return;
}
BeanMethods methods = new BeanMethods(source, cause);
for (ConditionAndOutcome conditionAndOutcome : sourceOutcomes) {
if (!conditionAndOutcome.getOutcome().isMatch()) {
for (MethodMetadata method : methods) {
results.add(new AutoConfigurationResult(method,
conditionAndOutcome.getOutcome(), source.isMethod()));
}
}
});
}
}
private void collectExcludedAutoConfiguration(NoSuchBeanDefinitionException cause,

View File

@@ -19,10 +19,10 @@ 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;
import org.springframework.util.ObjectUtils;
/**
* Mappings between {@link StoreType} and {@code @Configuration}.
@@ -32,63 +32,70 @@ import org.springframework.util.Assert;
*/
final class SessionStoreMappings {
private static final Map<StoreType, Map<WebApplicationType, Class<?>>> MAPPINGS;
private static final Map<StoreType, Configurations> MAPPINGS;
static {
Map<StoreType, Map<WebApplicationType, Class<?>>> mappings = new EnumMap<>(
StoreType.class);
mappings.put(StoreType.REDIS, createMapping(RedisSessionConfiguration.class,
Map<StoreType, Configurations> mappings = new EnumMap<>(StoreType.class);
mappings.put(StoreType.REDIS, new Configurations(RedisSessionConfiguration.class,
RedisReactiveSessionConfiguration.class));
mappings.put(StoreType.MONGODB, createMapping(MongoSessionConfiguration.class,
MongoReactiveSessionConfiguration.class));
mappings.put(StoreType.JDBC, createMapping(JdbcSessionConfiguration.class));
mappings.put(StoreType.MONGODB,
new Configurations(MongoSessionConfiguration.class,
MongoReactiveSessionConfiguration.class));
mappings.put(StoreType.JDBC,
new Configurations(JdbcSessionConfiguration.class, null));
mappings.put(StoreType.HAZELCAST,
createMapping(HazelcastSessionConfiguration.class));
mappings.put(StoreType.NONE, createMapping(NoOpSessionConfiguration.class,
new Configurations(HazelcastSessionConfiguration.class, null));
mappings.put(StoreType.NONE, new Configurations(NoOpSessionConfiguration.class,
NoOpReactiveSessionConfiguration.class));
MAPPINGS = Collections.unmodifiableMap(mappings);
}
static Map<WebApplicationType, Class<?>> createMapping(
Class<?> servletConfiguration) {
return createMapping(servletConfiguration, null);
}
static Map<WebApplicationType, Class<?>> createMapping(Class<?> servletConfiguration,
Class<?> reactiveConfiguration) {
Map<WebApplicationType, Class<?>> mapping = new EnumMap<>(
WebApplicationType.class);
mapping.put(WebApplicationType.SERVLET, servletConfiguration);
if (reactiveConfiguration != null) {
mapping.put(WebApplicationType.REACTIVE, reactiveConfiguration);
}
return mapping;
}
private SessionStoreMappings() {
}
static String getConfigurationClass(WebApplicationType webApplicationType,
public static String getConfigurationClass(WebApplicationType webApplicationType,
StoreType sessionStoreType) {
Map<WebApplicationType, Class<?>> configurationClasses = MAPPINGS
.get(sessionStoreType);
Assert.state(configurationClasses != null,
Configurations configurations = MAPPINGS.get(sessionStoreType);
Assert.state(configurations != null,
() -> "Unknown session store type " + sessionStoreType);
Class<?> configurationClass = configurationClasses.get(webApplicationType);
if (configurationClass == null) {
return null;
}
return configurationClass.getName();
return configurations.getConfiguration(webApplicationType);
}
static StoreType getType(WebApplicationType webApplicationType,
String 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));
public static StoreType getType(WebApplicationType webApplicationType,
String configurationClass) {
return MAPPINGS.entrySet().stream()
.filter((entry) -> ObjectUtils.nullSafeEquals(configurationClass,
entry.getValue().getConfiguration(webApplicationType)))
.map(Map.Entry::getKey).findFirst()
.orElseThrow(() -> new IllegalStateException(
"Unknown configuration class " + configurationClass));
}
private static class Configurations {
private final Class<?> servletConfiguration;
private final Class<?> reactiveConfiguration;
Configurations(Class<?> servletConfiguration, Class<?> reactiveConfiguration) {
this.servletConfiguration = servletConfiguration;
this.reactiveConfiguration = reactiveConfiguration;
}
public String getConfiguration(WebApplicationType webApplicationType) {
switch (webApplicationType) {
case SERVLET:
return getName(this.servletConfiguration);
case REACTIVE:
return getName(this.reactiveConfiguration);
}
return null;
}
private String getName(Class<?> configuration) {
return (configuration == null ? null : configuration.getName());
}
}
}

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.