Skip to content

Commit efeede9

Browse files
committed
Merge pull request #12528 from igor-suhorukov
* pr/12528: Polish "Iterate map by using lambda function" Iterate map by using lambda function
2 parents a520056 + 78534a7 commit efeede9

File tree

13 files changed

+53
-94
lines changed

13 files changed

+53
-94
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/context/properties/ConfigurationPropertiesReportEndpoint.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,11 @@ private ContextConfigurationProperties describeConfigurationProperties(
112112
Map<String, Object> beans = getConfigurationPropertiesBeans(context,
113113
beanFactoryMetadata);
114114
Map<String, ConfigurationPropertiesBeanDescriptor> beanDescriptors = new HashMap<>();
115-
for (Map.Entry<String, Object> entry : beans.entrySet()) {
116-
String beanName = entry.getKey();
117-
Object bean = entry.getValue();
115+
beans.forEach((beanName, bean) -> {
118116
String prefix = extractPrefix(context, beanFactoryMetadata, beanName);
119117
beanDescriptors.put(beanName, new ConfigurationPropertiesBeanDescriptor(
120118
prefix, sanitize(prefix, safeSerialize(mapper, bean, prefix))));
121-
}
119+
});
122120
return new ContextConfigurationProperties(beanDescriptors,
123121
context.getParent() == null ? null : context.getParent().getId());
124122
}
@@ -229,10 +227,8 @@ private String extractPrefix(ApplicationContext context,
229227
*/
230228
@SuppressWarnings("unchecked")
231229
private Map<String, Object> sanitize(String prefix, Map<String, Object> map) {
232-
for (Map.Entry<String, Object> entry : map.entrySet()) {
233-
String key = entry.getKey();
230+
map.forEach((key, value) -> {
234231
String qualifiedKey = (prefix.isEmpty() ? prefix : prefix + ".") + key;
235-
Object value = entry.getValue();
236232
if (value instanceof Map) {
237233
map.put(key, sanitize(qualifiedKey, (Map<String, Object>) value));
238234
}
@@ -244,7 +240,7 @@ else if (value instanceof List) {
244240
value = this.sanitizer.sanitize(qualifiedKey, value);
245241
map.put(key, value);
246242
}
247-
}
243+
});
248244
return map;
249245
}
250246

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/AbstractHealthAggregator.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,9 +34,7 @@ public abstract class AbstractHealthAggregator implements HealthAggregator {
3434
@Override
3535
public final Health aggregate(Map<String, Health> healths) {
3636
List<Status> statusCandidates = new ArrayList<>();
37-
for (Map.Entry<String, Health> entry : healths.entrySet()) {
38-
statusCandidates.add(entry.getValue().getStatus());
39-
}
37+
healths.values().forEach((health) -> statusCandidates.add(health.getStatus()));
4038
Status status = aggregateStatus(statusCandidates);
4139
Map<String, Object> details = aggregateDetails(healths);
4240
return new Health.Builder(status, details).build();

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/cloud/CloudFoundryVcapEnvironmentPostProcessor.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,7 +20,6 @@
2020
import java.util.Collections;
2121
import java.util.List;
2222
import java.util.Map;
23-
import java.util.Map.Entry;
2423
import java.util.Properties;
2524

2625
import org.apache.commons.logging.Log;
@@ -197,40 +196,39 @@ private void extractPropertiesFromServices(Properties properties,
197196

198197
@SuppressWarnings("unchecked")
199198
private void flatten(Properties properties, Map<String, Object> input, String path) {
200-
for (Entry<String, Object> entry : input.entrySet()) {
201-
String key = getFullKey(path, entry.getKey());
202-
Object value = entry.getValue();
199+
input.forEach((key, value) -> {
200+
String name = getPropertyName(path, key);
203201
if (value instanceof Map) {
204202
// Need a compound key
205-
flatten(properties, (Map<String, Object>) value, key);
203+
flatten(properties, (Map<String, Object>) value, name);
206204
}
207205
else if (value instanceof Collection) {
208206
// Need a compound key
209207
Collection<Object> collection = (Collection<Object>) value;
210-
properties.put(key,
208+
properties.put(name,
211209
StringUtils.collectionToCommaDelimitedString(collection));
212210
int count = 0;
213211
for (Object item : collection) {
214212
String itemKey = "[" + (count++) + "]";
215-
flatten(properties, Collections.singletonMap(itemKey, item), key);
213+
flatten(properties, Collections.singletonMap(itemKey, item), name);
216214
}
217215
}
218216
else if (value instanceof String) {
219-
properties.put(key, value);
217+
properties.put(name, value);
220218
}
221219
else if (value instanceof Number) {
222-
properties.put(key, value.toString());
220+
properties.put(name, value.toString());
223221
}
224222
else if (value instanceof Boolean) {
225-
properties.put(key, value.toString());
223+
properties.put(name, value.toString());
226224
}
227225
else {
228-
properties.put(key, value == null ? "" : value);
226+
properties.put(name, value == null ? "" : value);
229227
}
230-
}
228+
});
231229
}
232230

233-
private String getFullKey(String path, String key) {
231+
private String getPropertyName(String path, String key) {
234232
if (!StringUtils.hasText(path)) {
235233
return key;
236234
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/SpringApplicationJsonEnvironmentPostProcessor.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,8 @@ private Map<String, Object> flatten(Map<String, Object> map) {
126126

127127
private void flatten(String prefix, Map<String, Object> result,
128128
Map<String, Object> map) {
129-
prefix = (prefix == null ? "" : prefix + ".");
130-
for (Map.Entry<String, Object> entry : map.entrySet()) {
131-
extract(prefix + entry.getKey(), result, entry.getValue());
132-
}
129+
String namePrefix = (prefix == null ? "" : prefix + ".");
130+
map.forEach((key, value) -> extract(namePrefix + key, result, value));
133131
}
134132

135133
@SuppressWarnings("unchecked")

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyServletWebServerFactory.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,11 @@
2323
import java.net.MalformedURLException;
2424
import java.net.URL;
2525
import java.nio.channels.ReadableByteChannel;
26-
import java.nio.charset.Charset;
2726
import java.time.Duration;
2827
import java.util.ArrayList;
2928
import java.util.Arrays;
3029
import java.util.Collection;
3130
import java.util.List;
32-
import java.util.Locale;
33-
import java.util.Map;
3431

3532
import org.eclipse.jetty.http.MimeTypes;
3633
import org.eclipse.jetty.server.AbstractConnector;
@@ -249,11 +246,8 @@ private boolean isNegative(Duration sessionTimeout) {
249246
}
250247

251248
private void addLocaleMappings(WebAppContext context) {
252-
for (Map.Entry<Locale, Charset> entry : getLocaleCharsetMappings().entrySet()) {
253-
Locale locale = entry.getKey();
254-
Charset charset = entry.getValue();
255-
context.addLocaleEncoding(locale.toString(), charset.toString());
256-
}
249+
getLocaleCharsetMappings().forEach((locale, charset) -> context
250+
.addLocaleEncoding(locale.toString(), charset.toString()));
257251
}
258252

259253
private File getTempDirectory() {

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactory.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
import java.util.LinkedHashSet;
3131
import java.util.List;
3232
import java.util.Locale;
33-
import java.util.Map;
34-
import java.util.Map.Entry;
3533
import java.util.Set;
3634

3735
import javax.servlet.ServletContainerInitializer;
@@ -234,12 +232,9 @@ private void resetDefaultLocaleMapping(TomcatEmbeddedContext context) {
234232
}
235233

236234
private void addLocaleMappings(TomcatEmbeddedContext context) {
237-
for (Map.Entry<Locale, Charset> entry : getLocaleCharsetMappings().entrySet()) {
238-
Locale locale = entry.getKey();
239-
Charset charset = entry.getValue();
240-
context.addLocaleEncodingMappingParameter(locale.toString(),
241-
charset.toString());
242-
}
235+
getLocaleCharsetMappings()
236+
.forEach((locale, charset) -> context.addLocaleEncodingMappingParameter(
237+
locale.toString(), charset.toString()));
243238
}
244239

245240
private void configureTldSkipPatterns(TomcatEmbeddedContext context) {
@@ -267,10 +262,7 @@ private void addJspServlet(Context context) {
267262
jspServlet.setName("jsp");
268263
jspServlet.setServletClass(getJsp().getClassName());
269264
jspServlet.addInitParameter("fork", "false");
270-
for (Entry<String, String> initParameter : getJsp().getInitParameters()
271-
.entrySet()) {
272-
jspServlet.addInitParameter(initParameter.getKey(), initParameter.getValue());
273-
}
265+
getJsp().getInitParameters().forEach(jspServlet::addInitParameter);
274266
jspServlet.setLoadOnStartup(3);
275267
context.addChild(jspServlet);
276268
context.addServletMappingDecoded("*.jsp", "jsp");

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/FileSessionPersistence.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -69,10 +69,8 @@ private void save(Map<String, PersistentSession> sessionData, File file)
6969
private void save(Map<String, PersistentSession> sessionData,
7070
ObjectOutputStream stream) throws IOException {
7171
Map<String, Serializable> session = new LinkedHashMap<>();
72-
for (Map.Entry<String, PersistentSession> entry : sessionData.entrySet()) {
73-
session.put(entry.getKey(),
74-
new SerializablePersistentSession(entry.getValue()));
75-
}
72+
sessionData.forEach((key, value) -> session.put(key,
73+
new SerializablePersistentSession(value)));
7674
stream.writeObject(session);
7775
}
7876

@@ -104,13 +102,12 @@ private Map<String, PersistentSession> load(ObjectInputStream stream)
104102
Map<String, SerializablePersistentSession> session = readSession(stream);
105103
long time = System.currentTimeMillis();
106104
Map<String, PersistentSession> result = new LinkedHashMap<>();
107-
for (Map.Entry<String, SerializablePersistentSession> entry : session
108-
.entrySet()) {
109-
PersistentSession entrySession = entry.getValue().getPersistentSession();
105+
session.forEach((key, value) -> {
106+
PersistentSession entrySession = value.getPersistentSession();
110107
if (entrySession.getExpiration().getTime() > time) {
111-
result.put(entry.getKey(), entrySession);
108+
result.put(key, entrySession);
112109
}
113-
}
110+
});
114111
return result;
115112
}
116113

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowServletWebServerFactory.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,12 @@
2020
import java.io.IOException;
2121
import java.net.MalformedURLException;
2222
import java.net.URL;
23-
import java.nio.charset.Charset;
2423
import java.time.Duration;
2524
import java.util.ArrayList;
2625
import java.util.Arrays;
2726
import java.util.Collection;
2827
import java.util.Collections;
2928
import java.util.List;
30-
import java.util.Locale;
31-
import java.util.Map;
3229
import java.util.Set;
3330

3431
import javax.servlet.ServletContainerInitializer;
@@ -331,11 +328,8 @@ private XnioWorker createWorker() throws IOException {
331328
}
332329

333330
private void addLocaleMappings(DeploymentInfo deployment) {
334-
for (Map.Entry<Locale, Charset> entry : getLocaleCharsetMappings().entrySet()) {
335-
Locale locale = entry.getKey();
336-
Charset charset = entry.getValue();
337-
deployment.addLocaleCharsetMapping(locale.toString(), charset.toString());
338-
}
331+
getLocaleCharsetMappings().forEach((locale, charset) -> deployment
332+
.addLocaleCharsetMapping(locale.toString(), charset.toString()));
339333
}
340334

341335
private void registerServletContainerInitializerToDriveServletContextInitializers(

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/MimeMappings.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -244,9 +244,7 @@ public MimeMappings(MimeMappings mappings) {
244244
public MimeMappings(Map<String, String> mappings) {
245245
Assert.notNull(mappings, "Mappings must not be null");
246246
this.map = new LinkedHashMap<>();
247-
for (Map.Entry<String, String> entry : mappings.entrySet()) {
248-
add(entry.getKey(), entry.getValue());
249-
}
247+
mappings.forEach(this::add);
250248
}
251249

252250
/**

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/ServletContextInitializerBeans.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,10 @@ public ServletContextInitializerBeans(ListableBeanFactory beanFactory) {
7979
addServletContextInitializerBeans(beanFactory);
8080
addAdaptableBeans(beanFactory);
8181
List<ServletContextInitializer> sortedInitializers = new ArrayList<>();
82-
for (Map.Entry<?, List<ServletContextInitializer>> entry : this.initializers
83-
.entrySet()) {
84-
AnnotationAwareOrderComparator.sort(entry.getValue());
85-
sortedInitializers.addAll(entry.getValue());
86-
}
82+
this.initializers.values().forEach((contextInitializers) -> {
83+
AnnotationAwareOrderComparator.sort(contextInitializers);
84+
sortedInitializers.addAll(contextInitializers);
85+
});
8786
this.sortedList = Collections.unmodifiableList(sortedInitializers);
8887
}
8988

0 commit comments

Comments
 (0)