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

Closes gh-15837
pull/15888/head
Kedar Joshi 6 years ago committed by Stephane Nicoll
parent 6e5ffd8587
commit 4a253ff87c

@ -1,5 +1,5 @@
/*
* 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");
* you may not use this file except in compliance with the License.
@ -155,8 +155,7 @@ public class EntityScanPackages {
String[] basePackages = attributes.getStringArray("basePackages");
Class<?>[] basePackageClasses = attributes
.getClassArray("basePackageClasses");
Set<String> packagesToScan = new LinkedHashSet<>();
packagesToScan.addAll(Arrays.asList(basePackages));
Set<String> packagesToScan = new LinkedHashSet<>(Arrays.asList(basePackages));
for (Class<?> basePackageClass : basePackageClasses) {
packagesToScan.add(ClassUtils.getPackageName(basePackageClass));
}

@ -1185,8 +1185,7 @@ public class SpringApplication {
*/
public void setInitializers(
Collection<? extends ApplicationContextInitializer<?>> initializers) {
this.initializers = new ArrayList<>();
this.initializers.addAll(initializers);
this.initializers = new ArrayList<>(initializers);
}
/**
@ -1213,8 +1212,7 @@ public class SpringApplication {
* @param listeners the listeners to set
*/
public void setListeners(Collection<? extends ApplicationListener<?>> listeners) {
this.listeners = new ArrayList<>();
this.listeners.addAll(listeners);
this.listeners = new ArrayList<>(listeners);
}
/**

@ -1,5 +1,5 @@
/*
* 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");
* you may not use this file except in compliance with the License.
@ -132,9 +132,6 @@ class SpringIterableConfigurationPropertySource extends SpringConfigurationPrope
private Cache getCache() {
CacheKey cacheKey = CacheKey.get(getPropertySource());
if (cacheKey == null) {
return null;
}
if (ObjectUtils.nullSafeEquals(cacheKey, this.cacheKey)) {
return this.cache;
}

@ -1,5 +1,5 @@
/*
* 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");
* you may not use this file except in compliance with the License.
@ -77,8 +77,7 @@ class ServletComponentScanRegistrar implements ImportBeanDefinitionRegistrar {
metadata.getAnnotationAttributes(ServletComponentScan.class.getName()));
String[] basePackages = attributes.getStringArray("basePackages");
Class<?>[] basePackageClasses = attributes.getClassArray("basePackageClasses");
Set<String> packagesToScan = new LinkedHashSet<>();
packagesToScan.addAll(Arrays.asList(basePackages));
Set<String> packagesToScan = new LinkedHashSet<>(Arrays.asList(basePackages));
for (Class<?> basePackageClass : basePackageClasses) {
packagesToScan.add(ClassUtils.getPackageName(basePackageClass));
}

@ -1,5 +1,5 @@
/*
* 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");
* you may not use this file except in compliance with the License.
@ -238,8 +238,7 @@ public class ServletContextInitializerBeans
}
}
}
List<Entry<String, T>> beans = new ArrayList<>();
beans.addAll(map.entrySet());
List<Entry<String, T>> beans = new ArrayList<>(map.entrySet());
beans.sort((o1, o2) -> AnnotationAwareOrderComparator.INSTANCE
.compare(o1.getValue(), o2.getValue()));
return beans;

@ -16,7 +16,6 @@
package org.springframework.boot;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
@ -347,7 +346,7 @@ public class SpringApplicationTests {
SpringApplication application = new SpringApplication(ExampleConfig.class);
application.setWebApplicationType(WebApplicationType.NONE);
final AtomicReference<ApplicationContext> reference = new AtomicReference<>();
application.setInitializers(Arrays.asList(
application.setInitializers(Collections.singletonList(
(ApplicationContextInitializer<ConfigurableApplicationContext>) reference::set));
this.context = application.run("--foo=bar");
assertThat(this.context).isSameAs(reference.get());
@ -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");
assertThat(this.context).isSameAs(reference.get());
// Custom initializers do not switch off the defaults

@ -1,5 +1,5 @@
/*
* 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");
* you may not use this file except in compliance with the License.
@ -21,6 +21,7 @@ import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Set;
@ -59,7 +60,8 @@ public class ConfigurationsTests {
public void getClassesShouldMergeByClassAndSort() {
Configurations c1 = new TestSortedConfigurations(
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(
Arrays.asList(String.class, Integer.class));
Configurations c4 = new TestConfigurations(Arrays.asList(Long.class, Byte.class));

@ -1,5 +1,5 @@
/*
* 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");
* you may not use this file except in compliance with the License.
@ -20,7 +20,6 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
@ -1054,8 +1053,8 @@ public class ConfigFileApplicationListenerTests {
@Override
List<EnvironmentPostProcessor> loadPostProcessors() {
return new ArrayList<>(
Arrays.asList(new LowestPrecedenceEnvironmentPostProcessor()));
return new ArrayList<>(Collections
.singletonList(new LowestPrecedenceEnvironmentPostProcessor()));
}
}

@ -21,6 +21,7 @@ import java.nio.charset.Charset;
import java.time.Duration;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Locale;
import java.util.Map;
@ -209,7 +210,7 @@ public class JettyServletWebServerFactoryTests
@Test
public void wrappedHandlers() throws Exception {
JettyServletWebServerFactory factory = getFactory();
factory.setServerCustomizers(Arrays.asList((server) -> {
factory.setServerCustomizers(Collections.singletonList((server) -> {
Handler handler = server.getHandler();
HandlerWrapper wrapper = new HandlerWrapper();
wrapper.setHandler(handler);

@ -1,5 +1,5 @@
/*
* 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");
* you may not use this file except in compliance with the License.
@ -77,8 +77,7 @@ public class MimeMappingsTests {
MimeMappings mappings = new MimeMappings();
mappings.add("foo", "bar");
mappings.add("baz", "boo");
List<MimeMappings.Mapping> mappingList = new ArrayList<>();
mappingList.addAll(mappings.getAll());
List<MimeMappings.Mapping> mappingList = new ArrayList<>(mappings.getAll());
assertThat(mappingList.get(0).getExtension()).isEqualTo("foo");
assertThat(mappingList.get(0).getMimeType()).isEqualTo("bar");
assertThat(mappingList.get(1).getExtension()).isEqualTo("baz");

@ -1,5 +1,5 @@
/*
* 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");
* you may not use this file except in compliance with the License.
@ -144,7 +144,7 @@ public abstract class AbstractFilterRegistrationBeanTests {
AbstractFilterRegistrationBean<?> bean = createFilterRegistrationBean(
mockServletRegistration("a"));
bean.setServletRegistrationBeans(new LinkedHashSet<ServletRegistrationBean<?>>(
Arrays.asList(mockServletRegistration("b"))));
Collections.singletonList(mockServletRegistration("b"))));
bean.onStartup(this.servletContext);
verify(this.registration).addMappingForServletNames(
EnumSet.of(DispatcherType.REQUEST), false, "b");

@ -1,5 +1,5 @@
/*
* 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");
* you may not use this file except in compliance with the License.
@ -16,7 +16,6 @@
package org.springframework.boot.web.servlet.support;
import java.util.Arrays;
import java.util.Collections;
import javax.servlet.ServletContext;
@ -135,8 +134,8 @@ public class SpringBootServletInitializerTests {
@Test
public void servletContextPropertySourceIsAvailablePriorToRefresh() {
ServletContext servletContext = mock(ServletContext.class);
given(servletContext.getInitParameterNames()).willReturn(
Collections.enumeration(Arrays.asList("spring.profiles.active")));
given(servletContext.getInitParameterNames()).willReturn(Collections
.enumeration(Collections.singletonList("spring.profiles.active")));
given(servletContext.getInitParameter("spring.profiles.active"))
.willReturn("from-servlet-context");
given(servletContext.getAttributeNames())

Loading…
Cancel
Save