Skip to content

Commit

Permalink
Use lambdas for map entry iteration where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-suhorukov authored and philwebb committed Apr 5, 2018
1 parent 78a94ca commit 69bc19e
Show file tree
Hide file tree
Showing 31 changed files with 125 additions and 232 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,14 @@ public ContextConditionEvaluation(ConfigurableApplicationContext context) {
this.negativeMatches = new LinkedHashMap<>();
this.exclusions = report.getExclusions();
this.unconditionalClasses = report.getUnconditionalClasses();
for (Map.Entry<String, ConditionAndOutcomes> entry : report
.getConditionAndOutcomesBySource().entrySet()) {
if (entry.getValue().isFullMatch()) {
add(this.positiveMatches, entry.getKey(), entry.getValue());
report.getConditionAndOutcomesBySource().forEach((key, value) -> {
if (value.isFullMatch()) {
add(this.positiveMatches, key, value);
}
else {
add(this.negativeMatches, entry.getKey(), entry.getValue());
add(this.negativeMatches, key, value);
}
}
});
this.parentId = context.getParent() == null ? null
: context.getParent().getId();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ protected HealthIndicator createHealthIndicator(Map<String, S> beans) {
}
CompositeHealthIndicator composite = new CompositeHealthIndicator(
this.healthAggregator);
for (Map.Entry<String, S> entry : beans.entrySet()) {
composite.addHealthIndicator(entry.getKey(),
createHealthIndicator(entry.getValue()));
}
beans.forEach((key, value) -> composite.addHealthIndicator(key, createHealthIndicator(value)));
return composite;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ protected ReactiveHealthIndicator createHealthIndicator(Map<String, S> beans) {
}
CompositeReactiveHealthIndicator composite = new CompositeReactiveHealthIndicator(
this.healthAggregator);
for (Map.Entry<String, S> entry : beans.entrySet()) {
composite.addHealthIndicator(entry.getKey(),
createHealthIndicator(entry.getValue()));
}
beans.forEach((key, value) -> composite.addHealthIndicator(key, createHealthIndicator(value)));
return composite;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ private Map<String, DataSource> filterDataSources(
return null;
}
Map<String, DataSource> dataSources = new LinkedHashMap<>();
for (Map.Entry<String, DataSource> entry : candidates.entrySet()) {
if (!(entry.getValue() instanceof AbstractRoutingDataSource)) {
dataSources.put(entry.getKey(), entry.getValue());
candidates.forEach((key, value) -> {
if (!(value instanceof AbstractRoutingDataSource)) {
dataSources.put(key, value);
}
}
});
return dataSources;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,10 @@ private List<PropertySourceEntryDescriptor> toPropertySourceDescriptors(

private PropertySummaryDescriptor getPropertySummaryDescriptor(
Map<String, PropertyValueDescriptor> descriptors) {
for (Map.Entry<String, PropertyValueDescriptor> entry : descriptors.entrySet()) {
if (entry.getValue() != null) {
return new PropertySummaryDescriptor(entry.getKey(),
entry.getValue().getValue());
}
}
return null;
return descriptors.entrySet().stream().
filter((entry) -> entry.getValue() != null).
map((entry) -> new PropertySummaryDescriptor(entry.getKey(), entry.getValue().getValue())).
findFirst().orElse(null);
}

private Map<String, PropertyValueDescriptor> getPropertySourceDescriptors(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,11 @@ public AutoConfigurationClass get(String className) {
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ protected List<String> getCandidateConfigurations(AnnotationMetadata metadata,
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,10 @@ public static String getConfigurationClass(CacheType cacheType) {
}

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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,8 @@ private Condition getCondition(String conditionClassName) {

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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
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;
Expand Down Expand Up @@ -112,13 +113,9 @@ static BeanTypeRegistry get(ListableBeanFactory beanFactory) {
*/
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));
}

/**
Expand All @@ -132,14 +129,9 @@ Set<String> getNamesForType(Class<?> type) {
*/
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
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;
Expand Down Expand Up @@ -112,26 +111,25 @@ public void recordEvaluationCandidates(List<String> evaluationCandidates) {
*/
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);
}

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);
}
}
});
}

/**
Expand Down Expand Up @@ -190,16 +188,15 @@ private static void locateParent(BeanFactory beanFactory,

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,18 +166,18 @@ private String createOnMissingBeanNoMatchReason(MatchResult matchResult) {
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, ", "));
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
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;
Expand Down Expand Up @@ -69,8 +68,8 @@ public ConditionOutcome getMatchOutcome(ConditionContext context,
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);
Expand All @@ -79,9 +78,9 @@ private List<AnnotationAttributes> annotationAttributesFromMultiValueMap(
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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
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;
Expand Down Expand Up @@ -124,10 +123,8 @@ private List<AutoConfigurationResult> getAutoConfigurationResults(

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) {
Expand All @@ -139,7 +136,7 @@ private void collectReportedConditionOutcomes(NoSuchBeanDefinitionException caus
}
}
}
}
});
}

private void collectExcludedAutoConfiguration(NoSuchBeanDefinitionException cause,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
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;
Expand Down Expand Up @@ -181,9 +180,7 @@ private String getServletRegistrationName() {
}

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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;
Expand Down Expand Up @@ -82,18 +83,12 @@ static String getConfigurationClass(WebApplicationType webApplicationType,

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));
}

}
Loading

0 comments on commit 69bc19e

Please sign in to comment.