Use Map.forEach instead of manual Map.Entry iteration wherever possible

Issue: SPR-16646
This commit is contained in:
Juergen Hoeller
2018-03-27 00:38:32 +02:00
parent 10cb2ccaef
commit e3d0ef6015
50 changed files with 231 additions and 366 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-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.
@@ -215,11 +215,11 @@ public class Constants {
public Set<Object> getValues(@Nullable String namePrefix) {
String prefixToUse = (namePrefix != null ? namePrefix.trim().toUpperCase(Locale.ENGLISH) : "");
Set<Object> values = new HashSet<>();
for (String code : this.fieldCache.keySet()) {
this.fieldCache.forEach((code, value) -> {
if (code.startsWith(prefixToUse)) {
values.add(this.fieldCache.get(code));
values.add(value);
}
}
});
return values;
}
@@ -247,11 +247,11 @@ public class Constants {
public Set<Object> getValuesForSuffix(@Nullable String nameSuffix) {
String suffixToUse = (nameSuffix != null ? nameSuffix.trim().toUpperCase(Locale.ENGLISH) : "");
Set<Object> values = new HashSet<>();
for (String code : this.fieldCache.keySet()) {
this.fieldCache.forEach((code, value) -> {
if (code.endsWith(suffixToUse)) {
values.add(this.fieldCache.get(code));
values.add(value);
}
}
});
return values;
}

View File

@@ -141,8 +141,7 @@ public class SimpleAliasRegistry implements AliasRegistry {
Assert.notNull(valueResolver, "StringValueResolver must not be null");
synchronized (this.aliasMap) {
Map<String, String> aliasCopy = new HashMap<>(this.aliasMap);
for (String alias : aliasCopy.keySet()) {
String registeredName = aliasCopy.get(alias);
aliasCopy.forEach((alias, registeredName) -> {
String resolvedAlias = valueResolver.resolveStringValue(alias);
String resolvedName = valueResolver.resolveStringValue(registeredName);
if (resolvedAlias == null || resolvedName == null || resolvedAlias.equals(resolvedName)) {
@@ -154,7 +153,7 @@ public class SimpleAliasRegistry implements AliasRegistry {
if (existingName.equals(resolvedName)) {
// Pointing to existing alias - just remove placeholder
this.aliasMap.remove(alias);
break;
return;
}
throw new IllegalStateException(
"Cannot register resolved alias '" + resolvedAlias + "' (original: '" + alias +
@@ -168,7 +167,7 @@ public class SimpleAliasRegistry implements AliasRegistry {
else if (!registeredName.equals(resolvedName)) {
this.aliasMap.put(alias, resolvedName);
}
}
});
}
}

View File

@@ -1246,21 +1246,18 @@ public abstract class AnnotationUtils {
if (!attributes.validated) {
// Validate @AliasFor configuration
Map<String, List<String>> aliasMap = getAttributeAliasMap(annotationType);
for (String attributeName : aliasMap.keySet()) {
aliasMap.forEach((attributeName, aliasedAttributeNames) -> {
if (valuesAlreadyReplaced.contains(attributeName)) {
continue;
return;
}
Object value = attributes.get(attributeName);
boolean valuePresent = (value != null && !(value instanceof DefaultValueHolder));
for (String aliasedAttributeName : aliasMap.get(attributeName)) {
for (String aliasedAttributeName : aliasedAttributeNames) {
if (valuesAlreadyReplaced.contains(aliasedAttributeName)) {
continue;
}
Object aliasedValue = attributes.get(aliasedAttributeName);
boolean aliasPresent = (aliasedValue != null && !(aliasedValue instanceof DefaultValueHolder));
// Something to validate or replace with an alias?
if (valuePresent || aliasPresent) {
if (valuePresent && aliasPresent) {
@@ -1290,7 +1287,7 @@ public abstract class AnnotationUtils {
}
}
}
}
});
attributes.validated = true;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-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.
@@ -121,8 +121,10 @@ public abstract class SpringFactoriesLoader {
private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {
MultiValueMap<String, String> result = cache.get(classLoader);
if (result != null)
if (result != null) {
return result;
}
try {
Enumeration<URL> urls = (classLoader != null ?
classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :

View File

@@ -196,8 +196,7 @@ public class LinkedMultiValueMap<K, V> implements MultiValueMap<K, V>, Serializa
*/
public LinkedMultiValueMap<K, V> deepCopy() {
LinkedMultiValueMap<K, V> copy = new LinkedMultiValueMap<>(this.targetMap.size());
this.targetMap.forEach((k, v) -> copy.put(k, new LinkedList<>(v)));
this.targetMap.forEach((key, value) -> copy.put(key, new LinkedList<>(value)));
return copy;
}

View File

@@ -409,7 +409,8 @@ public class MimeType implements Comparable<MimeType>, Serializable {
return false;
}
for (String key : this.parameters.keySet()) {
for (Map.Entry<String, String> entry : this.parameters.entrySet()) {
String key = entry.getKey();
if (!other.parameters.containsKey(key)) {
return false;
}
@@ -418,7 +419,7 @@ public class MimeType implements Comparable<MimeType>, Serializable {
return false;
}
}
else if (!ObjectUtils.nullSafeEquals(this.parameters.get(key), other.parameters.get(key))) {
else if (!ObjectUtils.nullSafeEquals(entry.getValue(), other.parameters.get(key))) {
return false;
}
}