Refact iterator of Map with Java 8 forEach

See gh-1451
This commit is contained in:
diguage
2017-06-06 01:03:44 +08:00
committed by Stephane Nicoll
parent 5df053c44b
commit dab7a7f0ee
37 changed files with 117 additions and 184 deletions

View File

@@ -87,9 +87,10 @@ public class MutablePropertyValues implements PropertyValues, Serializable {
// There is no replacement of existing property values.
if (original != null) {
this.propertyValueList = new ArrayList<>(original.size());
for (Map.Entry<?, ?> entry : original.entrySet()) {
this.propertyValueList.add(new PropertyValue(entry.getKey().toString(), entry.getValue()));
}
original.forEach(
(attrName, attrValue)
-> this.propertyValueList.add(new PropertyValue(attrName.toString(), attrValue))
);
}
else {
this.propertyValueList = new ArrayList<>(0);
@@ -151,9 +152,10 @@ public class MutablePropertyValues implements PropertyValues, Serializable {
*/
public MutablePropertyValues addPropertyValues(@Nullable Map<?, ?> other) {
if (other != null) {
for (Map.Entry<?, ?> entry : other.entrySet()) {
addPropertyValue(new PropertyValue(entry.getKey().toString(), entry.getValue()));
}
other.forEach(
(attrName, attrValue)
-> addPropertyValue(new PropertyValue(attrName.toString(), attrValue))
);
}
return this;
}

View File

@@ -457,9 +457,7 @@ public class PropertyEditorRegistrySupport implements PropertyEditorRegistry {
String actualPropertyName =
(nestedProperty != null ? PropertyAccessorUtils.getPropertyName(nestedProperty) : null);
if (this.customEditors != null) {
for (Map.Entry<Class<?>, PropertyEditor> entry : this.customEditors.entrySet()) {
target.registerCustomEditor(entry.getKey(), entry.getValue());
}
this.customEditors.forEach((clazz, propertyEditor) -> target.registerCustomEditor(clazz, propertyEditor));
}
if (this.customEditorsForPath != null) {
for (Map.Entry<String, CustomEditorHolder> entry : this.customEditorsForPath.entrySet()) {

View File

@@ -72,14 +72,12 @@ public class ConstructorArgumentValues {
*/
public void addArgumentValues(@Nullable ConstructorArgumentValues other) {
if (other != null) {
for (Map.Entry<Integer, ValueHolder> entry : other.indexedArgumentValues.entrySet()) {
addOrMergeIndexedArgumentValue(entry.getKey(), entry.getValue().copy());
}
for (ValueHolder valueHolder : other.genericArgumentValues) {
if (!this.genericArgumentValues.contains(valueHolder)) {
addOrMergeGenericArgumentValue(valueHolder.copy());
}
}
other.indexedArgumentValues.forEach(
(index, argValue) -> addOrMergeIndexedArgumentValue(index, argValue.copy())
);
other.genericArgumentValues.stream()
.filter(valueHolder -> !this.genericArgumentValues.contains(valueHolder))
.forEach(valueHolder -> addOrMergeGenericArgumentValue(valueHolder));
}
}

View File

@@ -17,6 +17,7 @@
package org.springframework.beans.factory.config;
import java.beans.PropertyEditor;
import java.util.Arrays;
import java.util.Map;
import org.apache.commons.logging.Log;
@@ -140,16 +141,10 @@ public class CustomEditorConfigurer implements BeanFactoryPostProcessor, Ordered
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
if (this.propertyEditorRegistrars != null) {
for (PropertyEditorRegistrar propertyEditorRegistrar : this.propertyEditorRegistrars) {
beanFactory.addPropertyEditorRegistrar(propertyEditorRegistrar);
}
Arrays.stream(this.propertyEditorRegistrars).forEach(beanFactory::addPropertyEditorRegistrar);
}
if (this.customEditors != null) {
for (Map.Entry<Class<?>, Class<? extends PropertyEditor>> entry : this.customEditors.entrySet()) {
Class<?> requiredType = entry.getKey();
Class<? extends PropertyEditor> propertyEditorClass = entry.getValue();
beanFactory.registerCustomEditor(requiredType, propertyEditorClass);
}
this.customEditors.forEach(beanFactory::registerCustomEditor);
}
}

View File

@@ -18,7 +18,6 @@ package org.springframework.beans.factory.config;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
@@ -123,9 +122,7 @@ public class YamlMapFactoryBean extends YamlProcessor implements FactoryBean<Map
@SuppressWarnings({"unchecked", "rawtypes"})
private void merge(Map<String, Object> output, Map<String, Object> map) {
for (Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
map.forEach((key, value) -> {
Object existing = output.get(key);
if (value instanceof Map && existing instanceof Map) {
// Inner cast required by Eclipse IDE.
@@ -136,7 +133,7 @@ public class YamlMapFactoryBean extends YamlProcessor implements FactoryBean<Map
else {
output.put(key, value);
}
}
});
}
}

View File

@@ -202,12 +202,10 @@ public abstract class YamlProcessor {
}
Map<Object, Object> map = (Map<Object, Object>) object;
for (Entry<Object, Object> entry : map.entrySet()) {
Object value = entry.getValue();
map.forEach((key, value) -> {
if (value instanceof Map) {
value = asMap(value);
}
Object key = entry.getKey();
if (key instanceof CharSequence) {
result.put(key.toString(), value);
}
@@ -215,7 +213,7 @@ public abstract class YamlProcessor {
// It has to be a map key in this case
result.put("[" + key.toString() + "]", value);
}
}
});
return result;
}

View File

@@ -169,9 +169,7 @@ class BeanDefinitionValueResolver {
else if (value instanceof ManagedProperties) {
Properties original = (Properties) value;
Properties copy = new Properties();
for (Map.Entry<Object, Object> propEntry : original.entrySet()) {
Object propKey = propEntry.getKey();
Object propValue = propEntry.getValue();
original.forEach((propKey, propValue) -> {
if (propKey instanceof TypedStringValue) {
propKey = evaluate((TypedStringValue) propKey);
}
@@ -184,7 +182,7 @@ class BeanDefinitionValueResolver {
"Error converting Properties key/value pair for " + argName + ": resolved to null");
}
copy.put(propKey, propValue);
}
});
return copy;
}
else if (value instanceof TypedStringValue) {

View File

@@ -1225,9 +1225,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
private FactoryAwareOrderSourceProvider createFactoryAwareOrderSourceProvider(Map<String, Object> beans) {
IdentityHashMap<Object, String> instancesToBeanNames = new IdentityHashMap<>();
for (Map.Entry<String, Object> entry : beans.entrySet()) {
instancesToBeanNames.put(entry.getValue(), entry.getKey());
}
beans.forEach((beanName, instance) -> instancesToBeanNames.put(instance, beanName));
return new FactoryAwareOrderSourceProvider(instancesToBeanNames);
}

View File

@@ -111,9 +111,7 @@ public class CustomMapEditor extends PropertyEditorSupport {
// Convert Map elements.
Map<?, ?> source = (Map<?, ?>) value;
Map<Object, Object> target = createMap(this.mapType, source.size());
for (Map.Entry<?, ?> entry : source.entrySet()) {
target.put(convertKey(entry.getKey()), convertValue(entry.getValue()));
}
source.forEach((key, val) -> target.put(convertKey(key), convertValue(val)));
super.setValue(target);
}
else {