Browse Source

Polish "Use Java 8 forEach method on Map"

Closes gh-1404
pull/1201/merge
Stephane Nicoll 8 years ago
parent
commit
1b9e12f52f
  1. 2
      spring-beans/src/main/java/org/springframework/beans/factory/BeanFactoryUtils.java
  2. 6
      spring-core/src/main/java/org/springframework/util/ClassUtils.java
  3. 10
      spring-core/src/main/java/org/springframework/util/CollectionUtils.java
  4. 3
      spring-core/src/main/java/org/springframework/util/LinkedCaseInsensitiveMap.java
  5. 5
      spring-core/src/main/java/org/springframework/util/LinkedMultiValueMap.java
  6. 6
      spring-webflux/src/main/java/org/springframework/web/reactive/result/method/AbstractHandlerMethodMapping.java
  7. 6
      spring-webmvc/src/main/java/org/springframework/web/servlet/handler/SimpleUrlHandlerMapping.java

2
spring-beans/src/main/java/org/springframework/beans/factory/BeanFactoryUtils.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

6
spring-core/src/main/java/org/springframework/util/ClassUtils.java

@ -107,9 +107,9 @@ public abstract class ClassUtils {
primitiveWrapperTypeMap.put(Long.class, long.class); primitiveWrapperTypeMap.put(Long.class, long.class);
primitiveWrapperTypeMap.put(Short.class, short.class); primitiveWrapperTypeMap.put(Short.class, short.class);
primitiveWrapperTypeMap.entrySet().forEach(entry -> { primitiveWrapperTypeMap.forEach((key, value) -> {
primitiveTypeToWrapperMap.put(entry.getValue(), entry.getKey()); primitiveTypeToWrapperMap.put(value, key);
registerCommonClasses(entry.getKey()); registerCommonClasses(key);
}); });
Set<Class<?>> primitiveTypes = new HashSet<>(32); Set<Class<?>> primitiveTypes = new HashSet<>(32);

10
spring-core/src/main/java/org/springframework/util/CollectionUtils.java

@ -354,9 +354,9 @@ public abstract class CollectionUtils {
public static <K, V> MultiValueMap<K, V> unmodifiableMultiValueMap(MultiValueMap<? extends K, ? extends V> map) { public static <K, V> MultiValueMap<K, V> unmodifiableMultiValueMap(MultiValueMap<? extends K, ? extends V> map) {
Assert.notNull(map, "'map' must not be null"); Assert.notNull(map, "'map' must not be null");
Map<K, List<V>> result = new LinkedHashMap<>(map.size()); Map<K, List<V>> result = new LinkedHashMap<>(map.size());
map.entrySet().forEach(entry -> { map.forEach((key, value) -> {
List<? extends V> values = Collections.unmodifiableList(entry.getValue()); List<? extends V> values = Collections.unmodifiableList(value);
result.put(entry.getKey(), (List<V>) values); result.put(key, (List<V>) values);
}); });
Map<K, List<V>> unmodifiableMap = Collections.unmodifiableMap(result); Map<K, List<V>> unmodifiableMap = Collections.unmodifiableMap(result);
return toMultiValueMap(unmodifiableMap); return toMultiValueMap(unmodifiableMap);
@ -431,13 +431,13 @@ public abstract class CollectionUtils {
@Override @Override
public void setAll(Map<K, V> values) { public void setAll(Map<K, V> values) {
values.entrySet().forEach(entry -> set(entry.getKey(), entry.getValue())); values.forEach(this::set);
} }
@Override @Override
public Map<K, V> toSingleValueMap() { public Map<K, V> toSingleValueMap() {
LinkedHashMap<K, V> singleValueMap = new LinkedHashMap<>(this.map.size()); LinkedHashMap<K, V> singleValueMap = new LinkedHashMap<>(this.map.size());
map.entrySet().forEach(entry -> singleValueMap.put(entry.getKey(), entry.getValue().get(0))); this.map.forEach((key, value) -> singleValueMap.put(key, value.get(0)));
return singleValueMap; return singleValueMap;
} }

3
spring-core/src/main/java/org/springframework/util/LinkedCaseInsensitiveMap.java

@ -169,8 +169,7 @@ public class LinkedCaseInsensitiveMap<V> implements Map<String, V>, Serializable
if (map.isEmpty()) { if (map.isEmpty()) {
return; return;
} }
map.entrySet().forEach(entry -> put(entry.getKey(), entry.getValue())); map.forEach(this::put);
} }
@Override @Override

5
spring-core/src/main/java/org/springframework/util/LinkedMultiValueMap.java

@ -100,14 +100,13 @@ public class LinkedMultiValueMap<K, V> implements MultiValueMap<K, V>, Serializa
@Override @Override
public void setAll(Map<K, V> values) { public void setAll(Map<K, V> values) {
values.entrySet().forEach(entry -> set(entry.getKey(), entry.getValue())); values.forEach(this::set);
} }
@Override @Override
public Map<K, V> toSingleValueMap() { public Map<K, V> toSingleValueMap() {
LinkedHashMap<K, V> singleValueMap = new LinkedHashMap<>(this.targetMap.size()); LinkedHashMap<K, V> singleValueMap = new LinkedHashMap<>(this.targetMap.size());
this.targetMap.entrySet().forEach(entry -> this.targetMap.forEach((key, value) -> singleValueMap.put(key, value.get(0)));
singleValueMap.put(entry.getKey(), entry.getValue().get(0)));
return singleValueMap; return singleValueMap;
} }

6
spring-webflux/src/main/java/org/springframework/web/reactive/result/method/AbstractHandlerMethodMapping.java

@ -192,10 +192,8 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug(methods.size() + " request handler methods found on " + userType + ": " + methods); logger.debug(methods.size() + " request handler methods found on " + userType + ": " + methods);
} }
methods.forEach((key, mapping) -> {
methods.entrySet().forEach(entry -> { Method invocableMethod = AopUtils.selectInvocableMethod(key, userType);
Method invocableMethod = AopUtils.selectInvocableMethod(entry.getKey(), userType);
T mapping = entry.getValue();
registerHandlerMethod(handler, invocableMethod, mapping); registerHandlerMethod(handler, invocableMethod, mapping);
}); });
} }

6
spring-webmvc/src/main/java/org/springframework/web/servlet/handler/SimpleUrlHandlerMapping.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -114,9 +114,7 @@ public class SimpleUrlHandlerMapping extends AbstractUrlHandlerMapping {
logger.warn("Neither 'urlMap' nor 'mappings' set on SimpleUrlHandlerMapping"); logger.warn("Neither 'urlMap' nor 'mappings' set on SimpleUrlHandlerMapping");
} }
else { else {
urlMap.entrySet().forEach(entry -> { urlMap.forEach((url, handler) -> {
String url = entry.getKey();
Object handler = entry.getValue();
// Prepend with slash if not already present. // Prepend with slash if not already present.
if (!url.startsWith("/")) { if (!url.startsWith("/")) {
url = "/" + url; url = "/" + url;

Loading…
Cancel
Save