Commit efeede9f authored by Phillip Webb's avatar Phillip Webb

Merge pull request #12528 from igor-suhorukov

* pr/12528:
  Polish "Iterate map by using lambda function"
  Iterate map by using lambda function
parents a5200568 78534a75
...@@ -112,13 +112,11 @@ public class ConfigurationPropertiesReportEndpoint implements ApplicationContext ...@@ -112,13 +112,11 @@ public class ConfigurationPropertiesReportEndpoint implements ApplicationContext
Map<String, Object> beans = getConfigurationPropertiesBeans(context, Map<String, Object> beans = getConfigurationPropertiesBeans(context,
beanFactoryMetadata); beanFactoryMetadata);
Map<String, ConfigurationPropertiesBeanDescriptor> beanDescriptors = new HashMap<>(); Map<String, ConfigurationPropertiesBeanDescriptor> beanDescriptors = new HashMap<>();
for (Map.Entry<String, Object> entry : beans.entrySet()) { beans.forEach((beanName, bean) -> {
String beanName = entry.getKey();
Object bean = entry.getValue();
String prefix = extractPrefix(context, beanFactoryMetadata, beanName); String prefix = extractPrefix(context, beanFactoryMetadata, beanName);
beanDescriptors.put(beanName, new ConfigurationPropertiesBeanDescriptor( beanDescriptors.put(beanName, new ConfigurationPropertiesBeanDescriptor(
prefix, sanitize(prefix, safeSerialize(mapper, bean, prefix)))); prefix, sanitize(prefix, safeSerialize(mapper, bean, prefix))));
} });
return new ContextConfigurationProperties(beanDescriptors, return new ContextConfigurationProperties(beanDescriptors,
context.getParent() == null ? null : context.getParent().getId()); context.getParent() == null ? null : context.getParent().getId());
} }
...@@ -229,10 +227,8 @@ public class ConfigurationPropertiesReportEndpoint implements ApplicationContext ...@@ -229,10 +227,8 @@ public class ConfigurationPropertiesReportEndpoint implements ApplicationContext
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private Map<String, Object> sanitize(String prefix, Map<String, Object> map) { private Map<String, Object> sanitize(String prefix, Map<String, Object> map) {
for (Map.Entry<String, Object> entry : map.entrySet()) { map.forEach((key, value) -> {
String key = entry.getKey();
String qualifiedKey = (prefix.isEmpty() ? prefix : prefix + ".") + key; String qualifiedKey = (prefix.isEmpty() ? prefix : prefix + ".") + key;
Object value = entry.getValue();
if (value instanceof Map) { if (value instanceof Map) {
map.put(key, sanitize(qualifiedKey, (Map<String, Object>) value)); map.put(key, sanitize(qualifiedKey, (Map<String, Object>) value));
} }
...@@ -244,7 +240,7 @@ public class ConfigurationPropertiesReportEndpoint implements ApplicationContext ...@@ -244,7 +240,7 @@ public class ConfigurationPropertiesReportEndpoint implements ApplicationContext
value = this.sanitizer.sanitize(qualifiedKey, value); value = this.sanitizer.sanitize(qualifiedKey, value);
map.put(key, value); map.put(key, value);
} }
} });
return map; return map;
} }
......
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 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.
...@@ -34,9 +34,7 @@ public abstract class AbstractHealthAggregator implements HealthAggregator { ...@@ -34,9 +34,7 @@ public abstract class AbstractHealthAggregator implements HealthAggregator {
@Override @Override
public final Health aggregate(Map<String, Health> healths) { public final Health aggregate(Map<String, Health> healths) {
List<Status> statusCandidates = new ArrayList<>(); List<Status> statusCandidates = new ArrayList<>();
for (Map.Entry<String, Health> entry : healths.entrySet()) { healths.values().forEach((health) -> statusCandidates.add(health.getStatus()));
statusCandidates.add(entry.getValue().getStatus());
}
Status status = aggregateStatus(statusCandidates); Status status = aggregateStatus(statusCandidates);
Map<String, Object> details = aggregateDetails(healths); Map<String, Object> details = aggregateDetails(healths);
return new Health.Builder(status, details).build(); return new Health.Builder(status, details).build();
......
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 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.
...@@ -20,7 +20,6 @@ import java.util.Collection; ...@@ -20,7 +20,6 @@ import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties; import java.util.Properties;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
...@@ -197,40 +196,39 @@ public class CloudFoundryVcapEnvironmentPostProcessor ...@@ -197,40 +196,39 @@ public class CloudFoundryVcapEnvironmentPostProcessor
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private void flatten(Properties properties, Map<String, Object> input, String path) { private void flatten(Properties properties, Map<String, Object> input, String path) {
for (Entry<String, Object> entry : input.entrySet()) { input.forEach((key, value) -> {
String key = getFullKey(path, entry.getKey()); String name = getPropertyName(path, key);
Object value = entry.getValue();
if (value instanceof Map) { if (value instanceof Map) {
// Need a compound key // Need a compound key
flatten(properties, (Map<String, Object>) value, key); flatten(properties, (Map<String, Object>) value, name);
} }
else if (value instanceof Collection) { else if (value instanceof Collection) {
// Need a compound key // Need a compound key
Collection<Object> collection = (Collection<Object>) value; Collection<Object> collection = (Collection<Object>) value;
properties.put(key, properties.put(name,
StringUtils.collectionToCommaDelimitedString(collection)); StringUtils.collectionToCommaDelimitedString(collection));
int count = 0; int count = 0;
for (Object item : collection) { for (Object item : collection) {
String itemKey = "[" + (count++) + "]"; String itemKey = "[" + (count++) + "]";
flatten(properties, Collections.singletonMap(itemKey, item), key); flatten(properties, Collections.singletonMap(itemKey, item), name);
} }
} }
else if (value instanceof String) { else if (value instanceof String) {
properties.put(key, value); properties.put(name, value);
} }
else if (value instanceof Number) { else if (value instanceof Number) {
properties.put(key, value.toString()); properties.put(name, value.toString());
} }
else if (value instanceof Boolean) { else if (value instanceof Boolean) {
properties.put(key, value.toString()); properties.put(name, value.toString());
} }
else { else {
properties.put(key, value == null ? "" : value); properties.put(name, value == null ? "" : value);
}
} }
});
} }
private String getFullKey(String path, String key) { private String getPropertyName(String path, String key) {
if (!StringUtils.hasText(path)) { if (!StringUtils.hasText(path)) {
return key; return key;
} }
......
...@@ -126,10 +126,8 @@ public class SpringApplicationJsonEnvironmentPostProcessor ...@@ -126,10 +126,8 @@ public class SpringApplicationJsonEnvironmentPostProcessor
private void flatten(String prefix, Map<String, Object> result, private void flatten(String prefix, Map<String, Object> result,
Map<String, Object> map) { Map<String, Object> map) {
prefix = (prefix == null ? "" : prefix + "."); String namePrefix = (prefix == null ? "" : prefix + ".");
for (Map.Entry<String, Object> entry : map.entrySet()) { map.forEach((key, value) -> extract(namePrefix + key, result, value));
extract(prefix + entry.getKey(), result, entry.getValue());
}
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
......
...@@ -23,14 +23,11 @@ import java.net.InetSocketAddress; ...@@ -23,14 +23,11 @@ import java.net.InetSocketAddress;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.nio.channels.ReadableByteChannel; import java.nio.channels.ReadableByteChannel;
import java.nio.charset.Charset;
import java.time.Duration; import java.time.Duration;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.eclipse.jetty.http.MimeTypes; import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.server.AbstractConnector; import org.eclipse.jetty.server.AbstractConnector;
...@@ -249,11 +246,8 @@ public class JettyServletWebServerFactory extends AbstractServletWebServerFactor ...@@ -249,11 +246,8 @@ public class JettyServletWebServerFactory extends AbstractServletWebServerFactor
} }
private void addLocaleMappings(WebAppContext context) { private void addLocaleMappings(WebAppContext context) {
for (Map.Entry<Locale, Charset> entry : getLocaleCharsetMappings().entrySet()) { getLocaleCharsetMappings().forEach((locale, charset) -> context
Locale locale = entry.getKey(); .addLocaleEncoding(locale.toString(), charset.toString()));
Charset charset = entry.getValue();
context.addLocaleEncoding(locale.toString(), charset.toString());
}
} }
private File getTempDirectory() { private File getTempDirectory() {
......
...@@ -30,8 +30,6 @@ import java.util.Collections; ...@@ -30,8 +30,6 @@ import java.util.Collections;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import javax.servlet.ServletContainerInitializer; import javax.servlet.ServletContainerInitializer;
...@@ -234,12 +232,9 @@ public class TomcatServletWebServerFactory extends AbstractServletWebServerFacto ...@@ -234,12 +232,9 @@ public class TomcatServletWebServerFactory extends AbstractServletWebServerFacto
} }
private void addLocaleMappings(TomcatEmbeddedContext context) { private void addLocaleMappings(TomcatEmbeddedContext context) {
for (Map.Entry<Locale, Charset> entry : getLocaleCharsetMappings().entrySet()) { getLocaleCharsetMappings()
Locale locale = entry.getKey(); .forEach((locale, charset) -> context.addLocaleEncodingMappingParameter(
Charset charset = entry.getValue(); locale.toString(), charset.toString()));
context.addLocaleEncodingMappingParameter(locale.toString(),
charset.toString());
}
} }
private void configureTldSkipPatterns(TomcatEmbeddedContext context) { private void configureTldSkipPatterns(TomcatEmbeddedContext context) {
...@@ -267,10 +262,7 @@ public class TomcatServletWebServerFactory extends AbstractServletWebServerFacto ...@@ -267,10 +262,7 @@ public class TomcatServletWebServerFactory extends AbstractServletWebServerFacto
jspServlet.setName("jsp"); jspServlet.setName("jsp");
jspServlet.setServletClass(getJsp().getClassName()); jspServlet.setServletClass(getJsp().getClassName());
jspServlet.addInitParameter("fork", "false"); jspServlet.addInitParameter("fork", "false");
for (Entry<String, String> initParameter : getJsp().getInitParameters() getJsp().getInitParameters().forEach(jspServlet::addInitParameter);
.entrySet()) {
jspServlet.addInitParameter(initParameter.getKey(), initParameter.getValue());
}
jspServlet.setLoadOnStartup(3); jspServlet.setLoadOnStartup(3);
context.addChild(jspServlet); context.addChild(jspServlet);
context.addServletMappingDecoded("*.jsp", "jsp"); context.addServletMappingDecoded("*.jsp", "jsp");
......
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 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.
...@@ -69,10 +69,8 @@ class FileSessionPersistence implements SessionPersistenceManager { ...@@ -69,10 +69,8 @@ class FileSessionPersistence implements SessionPersistenceManager {
private void save(Map<String, PersistentSession> sessionData, private void save(Map<String, PersistentSession> sessionData,
ObjectOutputStream stream) throws IOException { ObjectOutputStream stream) throws IOException {
Map<String, Serializable> session = new LinkedHashMap<>(); Map<String, Serializable> session = new LinkedHashMap<>();
for (Map.Entry<String, PersistentSession> entry : sessionData.entrySet()) { sessionData.forEach((key, value) -> session.put(key,
session.put(entry.getKey(), new SerializablePersistentSession(value)));
new SerializablePersistentSession(entry.getValue()));
}
stream.writeObject(session); stream.writeObject(session);
} }
...@@ -104,13 +102,12 @@ class FileSessionPersistence implements SessionPersistenceManager { ...@@ -104,13 +102,12 @@ class FileSessionPersistence implements SessionPersistenceManager {
Map<String, SerializablePersistentSession> session = readSession(stream); Map<String, SerializablePersistentSession> session = readSession(stream);
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
Map<String, PersistentSession> result = new LinkedHashMap<>(); Map<String, PersistentSession> result = new LinkedHashMap<>();
for (Map.Entry<String, SerializablePersistentSession> entry : session session.forEach((key, value) -> {
.entrySet()) { PersistentSession entrySession = value.getPersistentSession();
PersistentSession entrySession = entry.getValue().getPersistentSession();
if (entrySession.getExpiration().getTime() > time) { if (entrySession.getExpiration().getTime() > time) {
result.put(entry.getKey(), entrySession); result.put(key, entrySession);
}
} }
});
return result; return result;
} }
......
...@@ -20,15 +20,12 @@ import java.io.File; ...@@ -20,15 +20,12 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.nio.charset.Charset;
import java.time.Duration; import java.time.Duration;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set; import java.util.Set;
import javax.servlet.ServletContainerInitializer; import javax.servlet.ServletContainerInitializer;
...@@ -331,11 +328,8 @@ public class UndertowServletWebServerFactory extends AbstractServletWebServerFac ...@@ -331,11 +328,8 @@ public class UndertowServletWebServerFactory extends AbstractServletWebServerFac
} }
private void addLocaleMappings(DeploymentInfo deployment) { private void addLocaleMappings(DeploymentInfo deployment) {
for (Map.Entry<Locale, Charset> entry : getLocaleCharsetMappings().entrySet()) { getLocaleCharsetMappings().forEach((locale, charset) -> deployment
Locale locale = entry.getKey(); .addLocaleCharsetMapping(locale.toString(), charset.toString()));
Charset charset = entry.getValue();
deployment.addLocaleCharsetMapping(locale.toString(), charset.toString());
}
} }
private void registerServletContainerInitializerToDriveServletContextInitializers( private void registerServletContainerInitializerToDriveServletContextInitializers(
......
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 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.
...@@ -244,9 +244,7 @@ public final class MimeMappings implements Iterable<MimeMappings.Mapping> { ...@@ -244,9 +244,7 @@ public final class MimeMappings implements Iterable<MimeMappings.Mapping> {
public MimeMappings(Map<String, String> mappings) { public MimeMappings(Map<String, String> mappings) {
Assert.notNull(mappings, "Mappings must not be null"); Assert.notNull(mappings, "Mappings must not be null");
this.map = new LinkedHashMap<>(); this.map = new LinkedHashMap<>();
for (Map.Entry<String, String> entry : mappings.entrySet()) { mappings.forEach(this::add);
add(entry.getKey(), entry.getValue());
}
} }
/** /**
......
...@@ -79,11 +79,10 @@ public class ServletContextInitializerBeans ...@@ -79,11 +79,10 @@ public class ServletContextInitializerBeans
addServletContextInitializerBeans(beanFactory); addServletContextInitializerBeans(beanFactory);
addAdaptableBeans(beanFactory); addAdaptableBeans(beanFactory);
List<ServletContextInitializer> sortedInitializers = new ArrayList<>(); List<ServletContextInitializer> sortedInitializers = new ArrayList<>();
for (Map.Entry<?, List<ServletContextInitializer>> entry : this.initializers this.initializers.values().forEach((contextInitializers) -> {
.entrySet()) { AnnotationAwareOrderComparator.sort(contextInitializers);
AnnotationAwareOrderComparator.sort(entry.getValue()); sortedInitializers.addAll(contextInitializers);
sortedInitializers.addAll(entry.getValue()); });
}
this.sortedList = Collections.unmodifiableList(sortedInitializers); this.sortedList = Collections.unmodifiableList(sortedInitializers);
} }
......
...@@ -384,12 +384,12 @@ public class ServletWebServerApplicationContext extends GenericWebApplicationCon ...@@ -384,12 +384,12 @@ public class ServletWebServerApplicationContext extends GenericWebApplicationCon
} }
public void restore() { public void restore() {
for (Map.Entry<String, Scope> entry : this.scopes.entrySet()) { this.scopes.forEach((key, value) -> {
if (logger.isInfoEnabled()) { if (logger.isInfoEnabled()) {
logger.info("Restoring user defined scope " + entry.getKey()); logger.info("Restoring user defined scope " + key);
}
this.beanFactory.registerScope(entry.getKey(), entry.getValue());
} }
this.beanFactory.registerScope(key, value);
});
} }
} }
......
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 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.
...@@ -66,12 +66,12 @@ public class SystemEnvironmentPropertySourceEnvironmentPostProcessorTests { ...@@ -66,12 +66,12 @@ public class SystemEnvironmentPropertySourceEnvironmentPostProcessorTests {
.getPropertySources().get("systemEnvironment"); .getPropertySources().get("systemEnvironment");
Map<String, Object> originalMap = (Map<String, Object>) original.getSource(); Map<String, Object> originalMap = (Map<String, Object>) original.getSource();
Map<String, Object> replacedMap = replaced.getSource(); Map<String, Object> replacedMap = replaced.getSource();
for (Map.Entry<String, Object> entry : originalMap.entrySet()) { originalMap.forEach((key, value) -> {
Object actual = replacedMap.get(entry.getKey()); Object actual = replacedMap.get(key);
assertThat(actual).isEqualTo(entry.getValue()); assertThat(actual).isEqualTo(value);
assertThat(replaced.getOrigin(entry.getKey())) assertThat(replaced.getOrigin(key))
.isInstanceOf(SystemEnvironmentOrigin.class); .isInstanceOf(SystemEnvironmentOrigin.class);
} });
} }
@Test @Test
......
...@@ -44,8 +44,6 @@ import java.util.EnumSet; ...@@ -44,8 +44,6 @@ import java.util.EnumSet;
import java.util.HashMap; import java.util.HashMap;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
...@@ -866,12 +864,9 @@ public abstract class AbstractServletWebServerFactoryTests { ...@@ -866,12 +864,9 @@ public abstract class AbstractServletWebServerFactoryTests {
AbstractServletWebServerFactory factory = getFactory(); AbstractServletWebServerFactory factory = getFactory();
this.webServer = factory.getWebServer(); this.webServer = factory.getWebServer();
Map<String, String> configuredMimeMappings = getActualMimeMappings(); Map<String, String> configuredMimeMappings = getActualMimeMappings();
Set<Entry<String, String>> entrySet = configuredMimeMappings.entrySet();
Collection<MimeMappings.Mapping> expectedMimeMappings = getExpectedMimeMappings(); Collection<MimeMappings.Mapping> expectedMimeMappings = getExpectedMimeMappings();
for (Entry<String, String> entry : entrySet) { configuredMimeMappings.forEach((key, value) -> assertThat(expectedMimeMappings).
assertThat(expectedMimeMappings) contains(new MimeMappings.Mapping(key, value)));
.contains(new MimeMappings.Mapping(entry.getKey(), entry.getValue()));
}
for (MimeMappings.Mapping mapping : expectedMimeMappings) { for (MimeMappings.Mapping mapping : expectedMimeMappings) {
assertThat(configuredMimeMappings).containsEntry(mapping.getExtension(), assertThat(configuredMimeMappings).containsEntry(mapping.getExtension(),
mapping.getMimeType()); mapping.getMimeType());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment