Commit 4a253ff8 authored by Kedar Joshi's avatar Kedar Joshi Committed by Stephane Nicoll

Polish

1. Removed unnecessary null checks
2. Combined collection initialization and modification
3. Minor collections related optimizations

Closes gh-15837
parent 6e5ffd85
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 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.
...@@ -155,8 +155,7 @@ public class EntityScanPackages { ...@@ -155,8 +155,7 @@ public class EntityScanPackages {
String[] basePackages = attributes.getStringArray("basePackages"); String[] basePackages = attributes.getStringArray("basePackages");
Class<?>[] basePackageClasses = attributes Class<?>[] basePackageClasses = attributes
.getClassArray("basePackageClasses"); .getClassArray("basePackageClasses");
Set<String> packagesToScan = new LinkedHashSet<>(); Set<String> packagesToScan = new LinkedHashSet<>(Arrays.asList(basePackages));
packagesToScan.addAll(Arrays.asList(basePackages));
for (Class<?> basePackageClass : basePackageClasses) { for (Class<?> basePackageClass : basePackageClasses) {
packagesToScan.add(ClassUtils.getPackageName(basePackageClass)); packagesToScan.add(ClassUtils.getPackageName(basePackageClass));
} }
......
...@@ -1185,8 +1185,7 @@ public class SpringApplication { ...@@ -1185,8 +1185,7 @@ public class SpringApplication {
*/ */
public void setInitializers( public void setInitializers(
Collection<? extends ApplicationContextInitializer<?>> initializers) { Collection<? extends ApplicationContextInitializer<?>> initializers) {
this.initializers = new ArrayList<>(); this.initializers = new ArrayList<>(initializers);
this.initializers.addAll(initializers);
} }
/** /**
...@@ -1213,8 +1212,7 @@ public class SpringApplication { ...@@ -1213,8 +1212,7 @@ public class SpringApplication {
* @param listeners the listeners to set * @param listeners the listeners to set
*/ */
public void setListeners(Collection<? extends ApplicationListener<?>> listeners) { public void setListeners(Collection<? extends ApplicationListener<?>> listeners) {
this.listeners = new ArrayList<>(); this.listeners = new ArrayList<>(listeners);
this.listeners.addAll(listeners);
} }
/** /**
......
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 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.
...@@ -132,9 +132,6 @@ class SpringIterableConfigurationPropertySource extends SpringConfigurationPrope ...@@ -132,9 +132,6 @@ class SpringIterableConfigurationPropertySource extends SpringConfigurationPrope
private Cache getCache() { private Cache getCache() {
CacheKey cacheKey = CacheKey.get(getPropertySource()); CacheKey cacheKey = CacheKey.get(getPropertySource());
if (cacheKey == null) {
return null;
}
if (ObjectUtils.nullSafeEquals(cacheKey, this.cacheKey)) { if (ObjectUtils.nullSafeEquals(cacheKey, this.cacheKey)) {
return this.cache; return this.cache;
} }
......
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 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.
...@@ -77,8 +77,7 @@ class ServletComponentScanRegistrar implements ImportBeanDefinitionRegistrar { ...@@ -77,8 +77,7 @@ class ServletComponentScanRegistrar implements ImportBeanDefinitionRegistrar {
metadata.getAnnotationAttributes(ServletComponentScan.class.getName())); metadata.getAnnotationAttributes(ServletComponentScan.class.getName()));
String[] basePackages = attributes.getStringArray("basePackages"); String[] basePackages = attributes.getStringArray("basePackages");
Class<?>[] basePackageClasses = attributes.getClassArray("basePackageClasses"); Class<?>[] basePackageClasses = attributes.getClassArray("basePackageClasses");
Set<String> packagesToScan = new LinkedHashSet<>(); Set<String> packagesToScan = new LinkedHashSet<>(Arrays.asList(basePackages));
packagesToScan.addAll(Arrays.asList(basePackages));
for (Class<?> basePackageClass : basePackageClasses) { for (Class<?> basePackageClass : basePackageClasses) {
packagesToScan.add(ClassUtils.getPackageName(basePackageClass)); packagesToScan.add(ClassUtils.getPackageName(basePackageClass));
} }
......
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 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.
...@@ -238,8 +238,7 @@ public class ServletContextInitializerBeans ...@@ -238,8 +238,7 @@ public class ServletContextInitializerBeans
} }
} }
} }
List<Entry<String, T>> beans = new ArrayList<>(); List<Entry<String, T>> beans = new ArrayList<>(map.entrySet());
beans.addAll(map.entrySet());
beans.sort((o1, o2) -> AnnotationAwareOrderComparator.INSTANCE beans.sort((o1, o2) -> AnnotationAwareOrderComparator.INSTANCE
.compare(o1.getValue(), o2.getValue())); .compare(o1.getValue(), o2.getValue()));
return beans; return beans;
......
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
package org.springframework.boot; package org.springframework.boot;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
...@@ -347,7 +346,7 @@ public class SpringApplicationTests { ...@@ -347,7 +346,7 @@ public class SpringApplicationTests {
SpringApplication application = new SpringApplication(ExampleConfig.class); SpringApplication application = new SpringApplication(ExampleConfig.class);
application.setWebApplicationType(WebApplicationType.NONE); application.setWebApplicationType(WebApplicationType.NONE);
final AtomicReference<ApplicationContext> reference = new AtomicReference<>(); final AtomicReference<ApplicationContext> reference = new AtomicReference<>();
application.setInitializers(Arrays.asList( application.setInitializers(Collections.singletonList(
(ApplicationContextInitializer<ConfigurableApplicationContext>) reference::set)); (ApplicationContextInitializer<ConfigurableApplicationContext>) reference::set));
this.context = application.run("--foo=bar"); this.context = application.run("--foo=bar");
assertThat(this.context).isSameAs(reference.get()); assertThat(this.context).isSameAs(reference.get());
...@@ -387,7 +386,7 @@ public class SpringApplicationTests { ...@@ -387,7 +386,7 @@ public class SpringApplicationTests {
} }
} }
application.setListeners(Arrays.asList(new InitializerListener())); application.setListeners(Collections.singletonList(new InitializerListener()));
this.context = application.run("--foo=bar"); this.context = application.run("--foo=bar");
assertThat(this.context).isSameAs(reference.get()); assertThat(this.context).isSameAs(reference.get());
// Custom initializers do not switch off the defaults // Custom initializers do not switch off the defaults
......
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 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.
...@@ -21,6 +21,7 @@ import java.io.OutputStream; ...@@ -21,6 +21,7 @@ import java.io.OutputStream;
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.Comparator; import java.util.Comparator;
import java.util.Set; import java.util.Set;
...@@ -59,7 +60,8 @@ public class ConfigurationsTests { ...@@ -59,7 +60,8 @@ public class ConfigurationsTests {
public void getClassesShouldMergeByClassAndSort() { public void getClassesShouldMergeByClassAndSort() {
Configurations c1 = new TestSortedConfigurations( Configurations c1 = new TestSortedConfigurations(
Arrays.asList(OutputStream.class, InputStream.class)); Arrays.asList(OutputStream.class, InputStream.class));
Configurations c2 = new TestConfigurations(Arrays.asList(Short.class)); Configurations c2 = new TestConfigurations(
Collections.singletonList(Short.class));
Configurations c3 = new TestSortedConfigurations( Configurations c3 = new TestSortedConfigurations(
Arrays.asList(String.class, Integer.class)); Arrays.asList(String.class, Integer.class));
Configurations c4 = new TestConfigurations(Arrays.asList(Long.class, Byte.class)); Configurations c4 = new TestConfigurations(Arrays.asList(Long.class, Byte.class));
......
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 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.io.File; ...@@ -20,7 +20,6 @@ import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
...@@ -1054,8 +1053,8 @@ public class ConfigFileApplicationListenerTests { ...@@ -1054,8 +1053,8 @@ public class ConfigFileApplicationListenerTests {
@Override @Override
List<EnvironmentPostProcessor> loadPostProcessors() { List<EnvironmentPostProcessor> loadPostProcessors() {
return new ArrayList<>( return new ArrayList<>(Collections
Arrays.asList(new LowestPrecedenceEnvironmentPostProcessor())); .singletonList(new LowestPrecedenceEnvironmentPostProcessor()));
} }
} }
......
...@@ -21,6 +21,7 @@ import java.nio.charset.Charset; ...@@ -21,6 +21,7 @@ import java.nio.charset.Charset;
import java.time.Duration; import java.time.Duration;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
...@@ -209,7 +210,7 @@ public class JettyServletWebServerFactoryTests ...@@ -209,7 +210,7 @@ public class JettyServletWebServerFactoryTests
@Test @Test
public void wrappedHandlers() throws Exception { public void wrappedHandlers() throws Exception {
JettyServletWebServerFactory factory = getFactory(); JettyServletWebServerFactory factory = getFactory();
factory.setServerCustomizers(Arrays.asList((server) -> { factory.setServerCustomizers(Collections.singletonList((server) -> {
Handler handler = server.getHandler(); Handler handler = server.getHandler();
HandlerWrapper wrapper = new HandlerWrapper(); HandlerWrapper wrapper = new HandlerWrapper();
wrapper.setHandler(handler); wrapper.setHandler(handler);
......
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2019 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.
...@@ -77,8 +77,7 @@ public class MimeMappingsTests { ...@@ -77,8 +77,7 @@ public class MimeMappingsTests {
MimeMappings mappings = new MimeMappings(); MimeMappings mappings = new MimeMappings();
mappings.add("foo", "bar"); mappings.add("foo", "bar");
mappings.add("baz", "boo"); mappings.add("baz", "boo");
List<MimeMappings.Mapping> mappingList = new ArrayList<>(); List<MimeMappings.Mapping> mappingList = new ArrayList<>(mappings.getAll());
mappingList.addAll(mappings.getAll());
assertThat(mappingList.get(0).getExtension()).isEqualTo("foo"); assertThat(mappingList.get(0).getExtension()).isEqualTo("foo");
assertThat(mappingList.get(0).getMimeType()).isEqualTo("bar"); assertThat(mappingList.get(0).getMimeType()).isEqualTo("bar");
assertThat(mappingList.get(1).getExtension()).isEqualTo("baz"); assertThat(mappingList.get(1).getExtension()).isEqualTo("baz");
......
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 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.
...@@ -144,7 +144,7 @@ public abstract class AbstractFilterRegistrationBeanTests { ...@@ -144,7 +144,7 @@ public abstract class AbstractFilterRegistrationBeanTests {
AbstractFilterRegistrationBean<?> bean = createFilterRegistrationBean( AbstractFilterRegistrationBean<?> bean = createFilterRegistrationBean(
mockServletRegistration("a")); mockServletRegistration("a"));
bean.setServletRegistrationBeans(new LinkedHashSet<ServletRegistrationBean<?>>( bean.setServletRegistrationBeans(new LinkedHashSet<ServletRegistrationBean<?>>(
Arrays.asList(mockServletRegistration("b")))); Collections.singletonList(mockServletRegistration("b"))));
bean.onStartup(this.servletContext); bean.onStartup(this.servletContext);
verify(this.registration).addMappingForServletNames( verify(this.registration).addMappingForServletNames(
EnumSet.of(DispatcherType.REQUEST), false, "b"); EnumSet.of(DispatcherType.REQUEST), false, "b");
......
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 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.
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
package org.springframework.boot.web.servlet.support; package org.springframework.boot.web.servlet.support;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
...@@ -135,8 +134,8 @@ public class SpringBootServletInitializerTests { ...@@ -135,8 +134,8 @@ public class SpringBootServletInitializerTests {
@Test @Test
public void servletContextPropertySourceIsAvailablePriorToRefresh() { public void servletContextPropertySourceIsAvailablePriorToRefresh() {
ServletContext servletContext = mock(ServletContext.class); ServletContext servletContext = mock(ServletContext.class);
given(servletContext.getInitParameterNames()).willReturn( given(servletContext.getInitParameterNames()).willReturn(Collections
Collections.enumeration(Arrays.asList("spring.profiles.active"))); .enumeration(Collections.singletonList("spring.profiles.active")));
given(servletContext.getInitParameter("spring.profiles.active")) given(servletContext.getInitParameter("spring.profiles.active"))
.willReturn("from-servlet-context"); .willReturn("from-servlet-context");
given(servletContext.getAttributeNames()) given(servletContext.getAttributeNames())
......
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