Skip to content

Commit

Permalink
Iterate map by using lambda function
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-suhorukov authored and philwebb committed Mar 19, 2018
1 parent a520056 commit ffc883b
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,11 @@ private ContextConfigurationProperties describeConfigurationProperties(
Map<String, Object> beans = getConfigurationPropertiesBeans(context,
beanFactoryMetadata);
Map<String, ConfigurationPropertiesBeanDescriptor> beanDescriptors = new HashMap<>();
for (Map.Entry<String, Object> entry : beans.entrySet()) {
String beanName = entry.getKey();
Object bean = entry.getValue();
beans.forEach((beanName, bean) -> {
String prefix = extractPrefix(context, beanFactoryMetadata, beanName);
beanDescriptors.put(beanName, new ConfigurationPropertiesBeanDescriptor(
prefix, sanitize(prefix, safeSerialize(mapper, bean, prefix))));
}
});
return new ContextConfigurationProperties(beanDescriptors,
context.getParent() == null ? null : context.getParent().getId());
}
Expand Down Expand Up @@ -229,10 +227,8 @@ private String extractPrefix(ApplicationContext context,
*/
@SuppressWarnings("unchecked")
private Map<String, Object> sanitize(String prefix, Map<String, Object> map) {
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
map.forEach((key, value) -> {
String qualifiedKey = (prefix.isEmpty() ? prefix : prefix + ".") + key;
Object value = entry.getValue();
if (value instanceof Map) {
map.put(key, sanitize(qualifiedKey, (Map<String, Object>) value));
}
Expand All @@ -244,7 +240,7 @@ else if (value instanceof List) {
value = this.sanitizer.sanitize(qualifiedKey, value);
map.put(key, value);
}
}
});
return map;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ public abstract class AbstractHealthAggregator implements HealthAggregator {
@Override
public final Health aggregate(Map<String, Health> healths) {
List<Status> statusCandidates = new ArrayList<>();
for (Map.Entry<String, Health> entry : healths.entrySet()) {
statusCandidates.add(entry.getValue().getStatus());
}
healths.values().forEach(health -> statusCandidates.add(health.getStatus()));
Status status = aggregateStatus(statusCandidates);
Map<String, Object> details = aggregateDetails(healths);
return new Health.Builder(status, details).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import org.apache.commons.logging.Log;
Expand Down Expand Up @@ -197,9 +196,8 @@ private void extractPropertiesFromServices(Properties properties,

@SuppressWarnings("unchecked")
private void flatten(Properties properties, Map<String, Object> input, String path) {
for (Entry<String, Object> entry : input.entrySet()) {
String key = getFullKey(path, entry.getKey());
Object value = entry.getValue();
input.forEach((entryKey, value) -> {
String key = getFullKey(path, entryKey);
if (value instanceof Map) {
// Need a compound key
flatten(properties, (Map<String, Object>) value, key);
Expand Down Expand Up @@ -227,7 +225,7 @@ else if (value instanceof Boolean) {
else {
properties.put(key, value == null ? "" : value);
}
}
});
}

private String getFullKey(String path, String key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,8 @@ private Map<String, Object> flatten(Map<String, Object> map) {

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

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,11 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.Charset;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.server.AbstractConnector;
Expand Down Expand Up @@ -249,11 +246,8 @@ private boolean isNegative(Duration sessionTimeout) {
}

private void addLocaleMappings(WebAppContext context) {
for (Map.Entry<Locale, Charset> entry : getLocaleCharsetMappings().entrySet()) {
Locale locale = entry.getKey();
Charset charset = entry.getValue();
context.addLocaleEncoding(locale.toString(), charset.toString());
}
getLocaleCharsetMappings().forEach((locale, charset) ->
context.addLocaleEncoding(locale.toString(), charset.toString()));
}

private File getTempDirectory() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.ServletContainerInitializer;
Expand Down Expand Up @@ -234,12 +232,8 @@ private void resetDefaultLocaleMapping(TomcatEmbeddedContext context) {
}

private void addLocaleMappings(TomcatEmbeddedContext context) {
for (Map.Entry<Locale, Charset> entry : getLocaleCharsetMappings().entrySet()) {
Locale locale = entry.getKey();
Charset charset = entry.getValue();
context.addLocaleEncodingMappingParameter(locale.toString(),
charset.toString());
}
getLocaleCharsetMappings().forEach((locale, charset) ->
context.addLocaleEncodingMappingParameter(locale.toString(), charset.toString()));
}

private void configureTldSkipPatterns(TomcatEmbeddedContext context) {
Expand Down Expand Up @@ -267,10 +261,7 @@ private void addJspServlet(Context context) {
jspServlet.setName("jsp");
jspServlet.setServletClass(getJsp().getClassName());
jspServlet.addInitParameter("fork", "false");
for (Entry<String, String> initParameter : getJsp().getInitParameters()
.entrySet()) {
jspServlet.addInitParameter(initParameter.getKey(), initParameter.getValue());
}
getJsp().getInitParameters().forEach(jspServlet::addInitParameter);
jspServlet.setLoadOnStartup(3);
context.addChild(jspServlet);
context.addServletMappingDecoded("*.jsp", "jsp");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,8 @@ private void save(Map<String, PersistentSession> sessionData, File file)
private void save(Map<String, PersistentSession> sessionData,
ObjectOutputStream stream) throws IOException {
Map<String, Serializable> session = new LinkedHashMap<>();
for (Map.Entry<String, PersistentSession> entry : sessionData.entrySet()) {
session.put(entry.getKey(),
new SerializablePersistentSession(entry.getValue()));
}
sessionData.forEach((key, value) ->
session.put(key, new SerializablePersistentSession(value)));
stream.writeObject(session);
}

Expand Down Expand Up @@ -104,13 +102,12 @@ private Map<String, PersistentSession> load(ObjectInputStream stream)
Map<String, SerializablePersistentSession> session = readSession(stream);
long time = System.currentTimeMillis();
Map<String, PersistentSession> result = new LinkedHashMap<>();
for (Map.Entry<String, SerializablePersistentSession> entry : session
.entrySet()) {
PersistentSession entrySession = entry.getValue().getPersistentSession();
session.forEach((key, value) -> {
PersistentSession entrySession = value.getPersistentSession();
if (entrySession.getExpiration().getTime() > time) {
result.put(entry.getKey(), entrySession);
result.put(key, entrySession);
}
}
});
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,12 @@
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletContainerInitializer;
Expand Down Expand Up @@ -331,11 +328,8 @@ private XnioWorker createWorker() throws IOException {
}

private void addLocaleMappings(DeploymentInfo deployment) {
for (Map.Entry<Locale, Charset> entry : getLocaleCharsetMappings().entrySet()) {
Locale locale = entry.getKey();
Charset charset = entry.getValue();
deployment.addLocaleCharsetMapping(locale.toString(), charset.toString());
}
getLocaleCharsetMappings().forEach((locale, charset) ->
deployment.addLocaleCharsetMapping(locale.toString(), charset.toString()));
}

private void registerServletContainerInitializerToDriveServletContextInitializers(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,7 @@ public MimeMappings(MimeMappings mappings) {
public MimeMappings(Map<String, String> mappings) {
Assert.notNull(mappings, "Mappings must not be null");
this.map = new LinkedHashMap<>();
for (Map.Entry<String, String> entry : mappings.entrySet()) {
add(entry.getKey(), entry.getValue());
}
mappings.forEach(this::add);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,10 @@ public ServletContextInitializerBeans(ListableBeanFactory beanFactory) {
addServletContextInitializerBeans(beanFactory);
addAdaptableBeans(beanFactory);
List<ServletContextInitializer> sortedInitializers = new ArrayList<>();
for (Map.Entry<?, List<ServletContextInitializer>> entry : this.initializers
.entrySet()) {
AnnotationAwareOrderComparator.sort(entry.getValue());
sortedInitializers.addAll(entry.getValue());
}
this.initializers.values().forEach(contextInitializers -> {
AnnotationAwareOrderComparator.sort(contextInitializers);
sortedInitializers.addAll(contextInitializers);
});
this.sortedList = Collections.unmodifiableList(sortedInitializers);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,12 +384,12 @@ public ExistingWebApplicationScopes(ConfigurableListableBeanFactory beanFactory)
}

public void restore() {
for (Map.Entry<String, Scope> entry : this.scopes.entrySet()) {
this.scopes.forEach((key, value) -> {
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);
});
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ public void replacedPropertySourceShouldBeOriginAware() {
.getPropertySources().get("systemEnvironment");
Map<String, Object> originalMap = (Map<String, Object>) original.getSource();
Map<String, Object> replacedMap = replaced.getSource();
for (Map.Entry<String, Object> entry : originalMap.entrySet()) {
Object actual = replacedMap.get(entry.getKey());
assertThat(actual).isEqualTo(entry.getValue());
assertThat(replaced.getOrigin(entry.getKey()))
originalMap.forEach((key, value) -> {
Object actual = replacedMap.get(key);
assertThat(actual).isEqualTo(value);
assertThat(replaced.getOrigin(key))
.isInstanceOf(SystemEnvironmentOrigin.class);
}
});
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.zip.GZIPInputStream;
Expand Down Expand Up @@ -866,12 +864,9 @@ public void mimeMappingsAreCorrectlyConfigured() {
AbstractServletWebServerFactory factory = getFactory();
this.webServer = factory.getWebServer();
Map<String, String> configuredMimeMappings = getActualMimeMappings();
Set<Entry<String, String>> entrySet = configuredMimeMappings.entrySet();
Collection<MimeMappings.Mapping> expectedMimeMappings = getExpectedMimeMappings();
for (Entry<String, String> entry : entrySet) {
assertThat(expectedMimeMappings)
.contains(new MimeMappings.Mapping(entry.getKey(), entry.getValue()));
}
configuredMimeMappings.forEach((key, value) -> assertThat(expectedMimeMappings).
contains(new MimeMappings.Mapping(key, value)));
for (MimeMappings.Mapping mapping : expectedMimeMappings) {
assertThat(configuredMimeMappings).containsEntry(mapping.getExtension(),
mapping.getMimeType());
Expand Down

0 comments on commit ffc883b

Please sign in to comment.