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

@@ -57,9 +57,7 @@ public class NameMatchCacheOperationSource implements CacheOperationSource, Seri
* @see CacheOperation
*/
public void setNameMap(Map<String, Collection<CacheOperation>> nameMap) {
for (Map.Entry<String, Collection<CacheOperation>> entry : nameMap.entrySet()) {
addCacheMethod(entry.getKey(), entry.getValue());
}
nameMap.forEach(this::addCacheMethod);
}
/**

View File

@@ -351,9 +351,7 @@ class ConfigurationClassBeanDefinitionReader {
}
private void loadBeanDefinitionsFromRegistrars(Map<ImportBeanDefinitionRegistrar, AnnotationMetadata> registrars) {
for (Map.Entry<ImportBeanDefinitionRegistrar, AnnotationMetadata> entry : registrars.entrySet()) {
entry.getKey().registerBeanDefinitions(entry.getValue(), this.registry);
}
registrars.forEach((registrar, metadata) -> registrar.registerBeanDefinitions(metadata, this.registry));
}

View File

@@ -75,13 +75,12 @@ public class CandidateComponentsIndex {
private static MultiValueMap<String, String> parseIndex(List<Properties> content) {
MultiValueMap<String, String> index = new LinkedMultiValueMap<>();
for (Properties entry : content) {
for (Map.Entry<Object, Object> entries : entry.entrySet()) {
String type = (String) entries.getKey();
String[] stereotypes = ((String) entries.getValue()).split(",");
entry.forEach((type, values) -> {
String[] stereotypes = ((String) values).split(",");
for (String stereotype : stereotypes) {
index.add(stereotype, type);
index.add(stereotype, (String) type);
}
}
});
}
return index;
}

View File

@@ -132,8 +132,7 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor
private void startBeans(boolean autoStartupOnly) {
Map<String, Lifecycle> lifecycleBeans = getLifecycleBeans();
Map<Integer, LifecycleGroup> phases = new HashMap<>();
for (Map.Entry<String, ? extends Lifecycle> entry : lifecycleBeans.entrySet()) {
Lifecycle bean = entry.getValue();
lifecycleBeans.forEach((beanName, bean) -> {
if (!autoStartupOnly || (bean instanceof SmartLifecycle && ((SmartLifecycle) bean).isAutoStartup())) {
int phase = getPhase(bean);
LifecycleGroup group = phases.get(phase);
@@ -141,9 +140,9 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor
group = new LifecycleGroup(phase, this.timeoutPerShutdownPhase, lifecycleBeans, autoStartupOnly);
phases.put(phase, group);
}
group.add(entry.getKey(), bean);
group.add(beanName, bean);
}
}
});
if (!phases.isEmpty()) {
List<Integer> keys = new ArrayList<>(phases.keySet());
Collections.sort(keys);
@@ -187,16 +186,15 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor
private void stopBeans() {
Map<String, Lifecycle> lifecycleBeans = getLifecycleBeans();
Map<Integer, LifecycleGroup> phases = new HashMap<>();
for (Map.Entry<String, Lifecycle> entry : lifecycleBeans.entrySet()) {
Lifecycle bean = entry.getValue();
lifecycleBeans.forEach((beanName, bean) -> {
int shutdownOrder = getPhase(bean);
LifecycleGroup group = phases.get(shutdownOrder);
if (group == null) {
group = new LifecycleGroup(shutdownOrder, this.timeoutPerShutdownPhase, lifecycleBeans, false);
phases.put(shutdownOrder, group);
}
group.add(entry.getKey(), bean);
}
group.add(beanName, bean);
});
if (!phases.isEmpty()) {
List<Integer> keys = new ArrayList<>(phases.keySet());
Collections.sort(keys, Collections.reverseOrder());

View File

@@ -87,9 +87,7 @@ public class StaticMessageSource extends AbstractMessageSource {
*/
public void addMessages(Map<String, String> messages, Locale locale) {
Assert.notNull(messages, "Messages Map must not be null");
for (Map.Entry<String, String> entry : messages.entrySet()) {
addMessage(entry.getKey(), locale, entry.getValue());
}
messages.forEach((code, msg) -> addMessage(code, locale, msg));
}

View File

@@ -367,17 +367,16 @@ public class MBeanExporter extends MBeanRegistrationSupport implements MBeanExpo
List<NotificationListenerBean> notificationListeners =
new ArrayList<>(listeners.size());
for (Map.Entry<?, ? extends NotificationListener> entry : listeners.entrySet()) {
listeners.forEach((key, listener) -> {
// Get the listener from the map value.
NotificationListenerBean bean = new NotificationListenerBean(entry.getValue());
NotificationListenerBean bean = new NotificationListenerBean(listener);
// Get the ObjectName from the map key.
Object key = entry.getKey();
if (key != null && !WILDCARD.equals(key)) {
// This listener is mapped to a specific ObjectName.
bean.setMappedObjectName(entry.getKey());
bean.setMappedObjectName(key);
}
notificationListeners.add(bean);
}
});
this.notificationListeners =
notificationListeners.toArray(new NotificationListenerBean[notificationListeners.size()]);
@@ -545,9 +544,7 @@ public class MBeanExporter extends MBeanRegistrationSupport implements MBeanExpo
}
if (!this.beans.isEmpty()) {
for (Map.Entry<String, Object> entry : this.beans.entrySet()) {
registerBeanNameOrInstance(entry.getValue(), entry.getKey());
}
this.beans.forEach((beanName, instance) -> registerBeanNameOrInstance(instance, beanName));
}
}
@@ -1008,9 +1005,7 @@ public class MBeanExporter extends MBeanRegistrationSupport implements MBeanExpo
* from the {@link MBeanServer}.
*/
private void unregisterNotificationListeners() {
for (Map.Entry<NotificationListenerBean, ObjectName[]> entry : this.registeredNotificationListeners.entrySet()) {
NotificationListenerBean bean = entry.getKey();
ObjectName[] mappedObjectNames = entry.getValue();
this.registeredNotificationListeners.forEach((bean, mappedObjectNames) -> {
for (ObjectName mappedObjectName : mappedObjectNames) {
try {
this.server.removeNotificationListener(mappedObjectName, bean.getNotificationListener(),
@@ -1022,7 +1017,7 @@ public class MBeanExporter extends MBeanRegistrationSupport implements MBeanExpo
}
}
}
}
});
this.registeredNotificationListeners.clear();
}

View File

@@ -53,9 +53,10 @@ public abstract class AbstractConfigurableMBeanInfoAssembler extends AbstractRef
}
public void setNotificationInfoMappings(Map<String, Object> notificationInfoMappings) {
for (Map.Entry<String, Object> entry : notificationInfoMappings.entrySet()) {
this.notificationInfoMappings.put(entry.getKey(), extractNotificationMetadata(entry.getValue()));
}
notificationInfoMappings.forEach(
(beanKey, result)
-> this.notificationInfoMappings.put(beanKey, extractNotificationMetadata(result))
);
}

View File

@@ -315,12 +315,11 @@ public class ScheduledAnnotationBeanPostProcessor
}
else {
// Non-empty set of methods
for (Map.Entry<Method, Set<Scheduled>> entry : annotatedMethods.entrySet()) {
Method method = entry.getKey();
for (Scheduled scheduled : entry.getValue()) {
annotatedMethods.forEach((method, scheduleds) -> {
for (Scheduled scheduled : scheduleds) {
processScheduled(scheduled, method, bean);
}
}
});
if (logger.isDebugEnabled()) {
logger.debug(annotatedMethods.size() + " @Scheduled methods processed on bean '" + beanName +
"': " + annotatedMethods);

View File

@@ -114,9 +114,7 @@ public class ScheduledTaskRegistrar implements InitializingBean, DisposableBean
*/
public void setTriggerTasks(Map<Runnable, Trigger> triggerTasks) {
this.triggerTasks = new ArrayList<>();
for (Map.Entry<Runnable, Trigger> task : triggerTasks.entrySet()) {
addTriggerTask(new TriggerTask(task.getKey(), task.getValue()));
}
triggerTasks.forEach((task, trigger) -> addTriggerTask(new TriggerTask(task, trigger)));
}
/**
@@ -145,9 +143,7 @@ public class ScheduledTaskRegistrar implements InitializingBean, DisposableBean
*/
public void setCronTasks(Map<Runnable, String> cronTasks) {
this.cronTasks = new ArrayList<>();
for (Map.Entry<Runnable, String> task : cronTasks.entrySet()) {
addCronTask(task.getKey(), task.getValue());
}
cronTasks.forEach(this::addCronTask);
}
/**
@@ -176,9 +172,7 @@ public class ScheduledTaskRegistrar implements InitializingBean, DisposableBean
*/
public void setFixedRateTasks(Map<Runnable, Long> fixedRateTasks) {
this.fixedRateTasks = new ArrayList<>();
for (Map.Entry<Runnable, Long> task : fixedRateTasks.entrySet()) {
addFixedRateTask(task.getKey(), task.getValue());
}
fixedRateTasks.forEach(this::addFixedRateTask);
}
/**
@@ -207,9 +201,7 @@ public class ScheduledTaskRegistrar implements InitializingBean, DisposableBean
*/
public void setFixedDelayTasks(Map<Runnable, Long> fixedDelayTasks) {
this.fixedDelayTasks = new ArrayList<>();
for (Map.Entry<Runnable, Long> task : fixedDelayTasks.entrySet()) {
addFixedDelayTask(task.getKey(), task.getValue());
}
fixedDelayTasks.forEach(this::addFixedDelayTask);
}
/**

View File

@@ -126,12 +126,11 @@ public class ConcurrentModel extends ConcurrentHashMap<String, Object> implement
*/
public ConcurrentModel mergeAttributes(@Nullable Map<String, ?> attributes) {
if (attributes != null) {
for (Map.Entry<String, ?> entry : attributes.entrySet()) {
String key = entry.getKey();
attributes.forEach((key, value) -> {
if (!containsKey(key)) {
put(key, entry.getValue());
put(key, value);
}
}
});
}
return this;
}

View File

@@ -126,12 +126,11 @@ public class ModelMap extends LinkedHashMap<String, Object> {
*/
public ModelMap mergeAttributes(@Nullable Map<String, ?> attributes) {
if (attributes != null) {
for (Map.Entry<String, ?> entry : attributes.entrySet()) {
String key = entry.getKey();
attributes.forEach((key, value) -> {
if (!containsKey(key)) {
put(key, entry.getValue());
put(key, value);
}
}
});
}
return this;
}

View File

@@ -287,9 +287,7 @@ public class LocalValidatorFactoryBean extends SpringValidatorAdapter
}
}
for (Map.Entry<String, String> entry : this.validationPropertyMap.entrySet()) {
configuration.addProperty(entry.getKey(), entry.getValue());
}
this.validationPropertyMap.forEach(configuration::addProperty);
// Allow for custom post-processing before we actually build the ValidatorFactory.
postProcessConfiguration(configuration);

View File

@@ -215,16 +215,14 @@ public class SpringValidatorAdapter implements SmartValidator, javax.validation.
arguments.add(getResolvableField(objectName, field));
// Using a TreeMap for alphabetical ordering of attribute names
Map<String, Object> attributesToExpose = new TreeMap<>();
for (Map.Entry<String, Object> entry : descriptor.getAttributes().entrySet()) {
String attributeName = entry.getKey();
Object attributeValue = entry.getValue();
descriptor.getAttributes().forEach((attributeName, attributeValue) -> {
if (!internalAnnotationAttributes.contains(attributeName)) {
if (attributeValue instanceof String) {
attributeValue = new ResolvableAttribute(attributeValue.toString());
}
attributesToExpose.put(attributeName, attributeValue);
}
}
});
arguments.addAll(attributesToExpose.values());
return arguments.toArray(new Object[arguments.size()]);
}

View File

@@ -46,9 +46,7 @@ public class BindingAwareModelMap extends ExtendedModelMap {
@Override
public void putAll(Map<? extends String, ?> map) {
for (Map.Entry<? extends String, ?> entry : map.entrySet()) {
removeBindingResultIfNecessary(entry.getKey(), entry.getValue());
}
map.forEach(this::removeBindingResultIfNecessary);
super.putAll(map);
}