Skip to content

Commit dab7a7f

Browse files
diguagesnicoll
authored andcommitted
Refact iterator of Map with Java 8 forEach
See gh-1451
1 parent 5df053c commit dab7a7f

File tree

37 files changed

+117
-184
lines changed

37 files changed

+117
-184
lines changed

spring-beans/src/main/java/org/springframework/beans/MutablePropertyValues.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,10 @@ public MutablePropertyValues(@Nullable Map<?, ?> original) {
8787
// There is no replacement of existing property values.
8888
if (original != null) {
8989
this.propertyValueList = new ArrayList<>(original.size());
90-
for (Map.Entry<?, ?> entry : original.entrySet()) {
91-
this.propertyValueList.add(new PropertyValue(entry.getKey().toString(), entry.getValue()));
92-
}
90+
original.forEach(
91+
(attrName, attrValue)
92+
-> this.propertyValueList.add(new PropertyValue(attrName.toString(), attrValue))
93+
);
9394
}
9495
else {
9596
this.propertyValueList = new ArrayList<>(0);
@@ -151,9 +152,10 @@ public MutablePropertyValues addPropertyValues(@Nullable PropertyValues other) {
151152
*/
152153
public MutablePropertyValues addPropertyValues(@Nullable Map<?, ?> other) {
153154
if (other != null) {
154-
for (Map.Entry<?, ?> entry : other.entrySet()) {
155-
addPropertyValue(new PropertyValue(entry.getKey().toString(), entry.getValue()));
156-
}
155+
other.forEach(
156+
(attrName, attrValue)
157+
-> addPropertyValue(new PropertyValue(attrName.toString(), attrValue))
158+
);
157159
}
158160
return this;
159161
}

spring-beans/src/main/java/org/springframework/beans/PropertyEditorRegistrySupport.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -457,9 +457,7 @@ protected void copyCustomEditorsTo(PropertyEditorRegistry target, @Nullable Stri
457457
String actualPropertyName =
458458
(nestedProperty != null ? PropertyAccessorUtils.getPropertyName(nestedProperty) : null);
459459
if (this.customEditors != null) {
460-
for (Map.Entry<Class<?>, PropertyEditor> entry : this.customEditors.entrySet()) {
461-
target.registerCustomEditor(entry.getKey(), entry.getValue());
462-
}
460+
this.customEditors.forEach((clazz, propertyEditor) -> target.registerCustomEditor(clazz, propertyEditor));
463461
}
464462
if (this.customEditorsForPath != null) {
465463
for (Map.Entry<String, CustomEditorHolder> entry : this.customEditorsForPath.entrySet()) {

spring-beans/src/main/java/org/springframework/beans/factory/config/ConstructorArgumentValues.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,12 @@ public ConstructorArgumentValues(ConstructorArgumentValues original) {
7272
*/
7373
public void addArgumentValues(@Nullable ConstructorArgumentValues other) {
7474
if (other != null) {
75-
for (Map.Entry<Integer, ValueHolder> entry : other.indexedArgumentValues.entrySet()) {
76-
addOrMergeIndexedArgumentValue(entry.getKey(), entry.getValue().copy());
77-
}
78-
for (ValueHolder valueHolder : other.genericArgumentValues) {
79-
if (!this.genericArgumentValues.contains(valueHolder)) {
80-
addOrMergeGenericArgumentValue(valueHolder.copy());
81-
}
82-
}
75+
other.indexedArgumentValues.forEach(
76+
(index, argValue) -> addOrMergeIndexedArgumentValue(index, argValue.copy())
77+
);
78+
other.genericArgumentValues.stream()
79+
.filter(valueHolder -> !this.genericArgumentValues.contains(valueHolder))
80+
.forEach(valueHolder -> addOrMergeGenericArgumentValue(valueHolder));
8381
}
8482
}
8583

spring-beans/src/main/java/org/springframework/beans/factory/config/CustomEditorConfigurer.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.beans.factory.config;
1818

1919
import java.beans.PropertyEditor;
20+
import java.util.Arrays;
2021
import java.util.Map;
2122

2223
import org.apache.commons.logging.Log;
@@ -140,16 +141,10 @@ public void setCustomEditors(Map<Class<?>, Class<? extends PropertyEditor>> cust
140141
@Override
141142
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
142143
if (this.propertyEditorRegistrars != null) {
143-
for (PropertyEditorRegistrar propertyEditorRegistrar : this.propertyEditorRegistrars) {
144-
beanFactory.addPropertyEditorRegistrar(propertyEditorRegistrar);
145-
}
144+
Arrays.stream(this.propertyEditorRegistrars).forEach(beanFactory::addPropertyEditorRegistrar);
146145
}
147146
if (this.customEditors != null) {
148-
for (Map.Entry<Class<?>, Class<? extends PropertyEditor>> entry : this.customEditors.entrySet()) {
149-
Class<?> requiredType = entry.getKey();
150-
Class<? extends PropertyEditor> propertyEditorClass = entry.getValue();
151-
beanFactory.registerCustomEditor(requiredType, propertyEditorClass);
152-
}
147+
this.customEditors.forEach(beanFactory::registerCustomEditor);
153148
}
154149
}
155150

spring-beans/src/main/java/org/springframework/beans/factory/config/YamlMapFactoryBean.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.util.LinkedHashMap;
2020
import java.util.Map;
21-
import java.util.Map.Entry;
2221

2322
import org.springframework.beans.factory.FactoryBean;
2423
import org.springframework.beans.factory.InitializingBean;
@@ -123,9 +122,7 @@ protected Map<String, Object> createMap() {
123122

124123
@SuppressWarnings({"unchecked", "rawtypes"})
125124
private void merge(Map<String, Object> output, Map<String, Object> map) {
126-
for (Entry<String, Object> entry : map.entrySet()) {
127-
String key = entry.getKey();
128-
Object value = entry.getValue();
125+
map.forEach((key, value) -> {
129126
Object existing = output.get(key);
130127
if (value instanceof Map && existing instanceof Map) {
131128
// Inner cast required by Eclipse IDE.
@@ -136,7 +133,7 @@ private void merge(Map<String, Object> output, Map<String, Object> map) {
136133
else {
137134
output.put(key, value);
138135
}
139-
}
136+
});
140137
}
141138

142139
}

spring-beans/src/main/java/org/springframework/beans/factory/config/YamlProcessor.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,20 +202,18 @@ private Map<String, Object> asMap(Object object) {
202202
}
203203

204204
Map<Object, Object> map = (Map<Object, Object>) object;
205-
for (Entry<Object, Object> entry : map.entrySet()) {
206-
Object value = entry.getValue();
205+
map.forEach((key, value) -> {
207206
if (value instanceof Map) {
208207
value = asMap(value);
209208
}
210-
Object key = entry.getKey();
211209
if (key instanceof CharSequence) {
212210
result.put(key.toString(), value);
213211
}
214212
else {
215213
// It has to be a map key in this case
216214
result.put("[" + key.toString() + "]", value);
217215
}
218-
}
216+
});
219217
return result;
220218
}
221219

spring-beans/src/main/java/org/springframework/beans/factory/support/BeanDefinitionValueResolver.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,7 @@ else if (value instanceof ManagedMap) {
169169
else if (value instanceof ManagedProperties) {
170170
Properties original = (Properties) value;
171171
Properties copy = new Properties();
172-
for (Map.Entry<Object, Object> propEntry : original.entrySet()) {
173-
Object propKey = propEntry.getKey();
174-
Object propValue = propEntry.getValue();
172+
original.forEach((propKey, propValue) -> {
175173
if (propKey instanceof TypedStringValue) {
176174
propKey = evaluate((TypedStringValue) propKey);
177175
}
@@ -184,7 +182,7 @@ else if (value instanceof ManagedProperties) {
184182
"Error converting Properties key/value pair for " + argName + ": resolved to null");
185183
}
186184
copy.put(propKey, propValue);
187-
}
185+
});
188186
return copy;
189187
}
190188
else if (value instanceof TypedStringValue) {

spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,9 +1225,7 @@ private Comparator<Object> adaptDependencyComparator(Map<String, Object> matchin
12251225

12261226
private FactoryAwareOrderSourceProvider createFactoryAwareOrderSourceProvider(Map<String, Object> beans) {
12271227
IdentityHashMap<Object, String> instancesToBeanNames = new IdentityHashMap<>();
1228-
for (Map.Entry<String, Object> entry : beans.entrySet()) {
1229-
instancesToBeanNames.put(entry.getValue(), entry.getKey());
1230-
}
1228+
beans.forEach((beanName, instance) -> instancesToBeanNames.put(instance, beanName));
12311229
return new FactoryAwareOrderSourceProvider(instancesToBeanNames);
12321230
}
12331231

spring-beans/src/main/java/org/springframework/beans/propertyeditors/CustomMapEditor.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,7 @@ else if (value instanceof Map) {
111111
// Convert Map elements.
112112
Map<?, ?> source = (Map<?, ?>) value;
113113
Map<Object, Object> target = createMap(this.mapType, source.size());
114-
for (Map.Entry<?, ?> entry : source.entrySet()) {
115-
target.put(convertKey(entry.getKey()), convertValue(entry.getValue()));
116-
}
114+
source.forEach((key, val) -> target.put(convertKey(key), convertValue(val)));
117115
super.setValue(target);
118116
}
119117
else {

spring-context/src/main/java/org/springframework/cache/interceptor/NameMatchCacheOperationSource.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ public class NameMatchCacheOperationSource implements CacheOperationSource, Seri
5757
* @see CacheOperation
5858
*/
5959
public void setNameMap(Map<String, Collection<CacheOperation>> nameMap) {
60-
for (Map.Entry<String, Collection<CacheOperation>> entry : nameMap.entrySet()) {
61-
addCacheMethod(entry.getKey(), entry.getValue());
62-
}
60+
nameMap.forEach(this::addCacheMethod);
6361
}
6462

6563
/**

0 commit comments

Comments
 (0)