Use Collections.isEmpty() instead of .size() == 0

Ensure that Collections.isEmpty() is used to check if there are no
elements in a collections. This is more explicit and can be faster than
calling .size().

Closes gh-4783
pull/4783/merge
Kirill Vlasov 9 years ago committed by Phillip Webb
parent 6113643cbe
commit 786aacf2e9

@ -39,7 +39,7 @@ public class HealthIndicatorAutoConfigurationProperties {
} }
public void setOrder(List<String> statusOrder) { public void setOrder(List<String> statusOrder) {
if (statusOrder != null && statusOrder.size() > 0) { if (statusOrder != null && !statusOrder.isEmpty()) {
this.order = statusOrder; this.order = statusOrder;
} }
} }

@ -76,7 +76,7 @@ public class OrderedHealthAggregator extends AbstractHealthAggregator {
} }
} }
// If no status is given return UNKNOWN // If no status is given return UNKNOWN
if (filteredCandidates.size() == 0) { if (filteredCandidates.isEmpty()) {
return Status.UNKNOWN; return Status.UNKNOWN;
} }
// Sort given Status instances by configured order // Sort given Status instances by configured order

@ -50,7 +50,7 @@ public abstract class AnyNestedCondition extends AbstractNestedCondition {
@Override @Override
protected ConditionOutcome getFinalMatchOutcome(MemberMatchOutcomes memberOutcomes) { protected ConditionOutcome getFinalMatchOutcome(MemberMatchOutcomes memberOutcomes) {
return new ConditionOutcome(memberOutcomes.getMatches().size() > 0, return new ConditionOutcome(!memberOutcomes.getMatches().isEmpty(),
"nested any match resulted in " + memberOutcomes.getMatches() "nested any match resulted in " + memberOutcomes.getMatches()
+ " matches and " + memberOutcomes.getNonMatches() + " matches and " + memberOutcomes.getNonMatches()
+ " non matches"); + " non matches");

@ -47,7 +47,7 @@ class OnResourceCondition extends SpringBootCondition {
? this.defaultResourceLoader : context.getResourceLoader(); ? this.defaultResourceLoader : context.getResourceLoader();
List<String> locations = new ArrayList<String>(); List<String> locations = new ArrayList<String>();
collectValues(locations, attributes.get("resources")); collectValues(locations, attributes.get("resources"));
Assert.isTrue(locations.size() > 0, Assert.isTrue(!locations.isEmpty(),
"@ConditionalOnResource annotations must specify at least one resource location"); "@ConditionalOnResource annotations must specify at least one resource location");
for (String location : locations) { for (String location : locations) {
if (!loader if (!loader

@ -106,7 +106,7 @@ public class AutoConfigurationReportLoggingInitializer
this.report = ConditionEvaluationReport this.report = ConditionEvaluationReport
.get(this.applicationContext.getBeanFactory()); .get(this.applicationContext.getBeanFactory());
} }
if (this.report.getConditionAndOutcomesBySource().size() > 0) { if (!this.report.getConditionAndOutcomesBySource().isEmpty()) {
if (isCrashReport && this.logger.isInfoEnabled() if (isCrashReport && this.logger.isInfoEnabled()
&& !this.logger.isDebugEnabled()) { && !this.logger.isDebugEnabled()) {
this.logger.info("\n\nError starting ApplicationContext. " this.logger.info("\n\nError starting ApplicationContext. "

@ -57,7 +57,7 @@ public class HintCommand extends AbstractCommand {
if (index == 0) { if (index == 0) {
showCommandHints(starting); showCommandHints(starting);
} }
else if ((arguments.size() > 0) && (starting.length() > 0)) { else if (!arguments.isEmpty() && (starting.length() > 0)) {
String command = arguments.remove(0); String command = arguments.remove(0);
showCommandOptionHints(command, Collections.unmodifiableList(arguments), showCommandOptionHints(command, Collections.unmodifiableList(arguments),
starting); starting);

@ -385,7 +385,7 @@ class ProjectGenerationRequest {
if (types.size() == 1) { if (types.size() == 1) {
return types.values().iterator().next(); return types.values().iterator().next();
} }
else if (types.size() == 0) { else if (types.isEmpty()) {
throw new ReportableException("No type found with build '" + this.build throw new ReportableException("No type found with build '" + this.build
+ "' and format '" + this.format + "' and format '" + this.format
+ "' check the service capabilities (--list)"); + "' check the service capabilities (--list)");

@ -96,7 +96,7 @@ public class SourceOptions {
} }
this.args = Collections.unmodifiableList( this.args = Collections.unmodifiableList(
nonOptionArguments.subList(sourceArgCount, nonOptionArguments.size())); nonOptionArguments.subList(sourceArgCount, nonOptionArguments.size()));
Assert.isTrue(sources.size() > 0, "Please specify at least one file"); Assert.isTrue(!sources.isEmpty(), "Please specify at least one file");
this.sources = Collections.unmodifiableList(sources); this.sources = Collections.unmodifiableList(sources);
} }

@ -187,7 +187,7 @@ public class ExtendedGroovyClassLoader extends GroovyClassLoader {
if (urls.isEmpty()) { if (urls.isEmpty()) {
findGroovyJarsFromClassPath(parent, urls); findGroovyJarsFromClassPath(parent, urls);
} }
Assert.state(urls.size() > 0, "Unable to find groovy JAR"); Assert.state(!urls.isEmpty(), "Unable to find groovy JAR");
return new ArrayList<URL>(urls).toArray(new URL[urls.size()]); return new ArrayList<URL>(urls).toArray(new URL[urls.size()]);
} }

@ -42,7 +42,7 @@ public class SnakeTimer {
private static final ConcurrentHashMap<Integer, Snake> snakes = new ConcurrentHashMap<Integer, Snake>(); private static final ConcurrentHashMap<Integer, Snake> snakes = new ConcurrentHashMap<Integer, Snake>();
public static synchronized void addSnake(Snake snake) { public static synchronized void addSnake(Snake snake) {
if (snakes.size() == 0) { if (snakes.isEmpty()) {
startTimer(); startTimer();
} }
snakes.put(Integer.valueOf(snake.getId()), snake); snakes.put(Integer.valueOf(snake.getId()), snake);
@ -54,7 +54,7 @@ public class SnakeTimer {
public static synchronized void removeSnake(Snake snake) { public static synchronized void removeSnake(Snake snake) {
snakes.remove(Integer.valueOf(snake.getId())); snakes.remove(Integer.valueOf(snake.getId()));
if (snakes.size() == 0) { if (snakes.isEmpty()) {
stopTimer(); stopTimer();
} }
} }

@ -42,7 +42,7 @@ public class SnakeTimer {
private static final ConcurrentHashMap<Integer, Snake> snakes = new ConcurrentHashMap<Integer, Snake>(); private static final ConcurrentHashMap<Integer, Snake> snakes = new ConcurrentHashMap<Integer, Snake>();
public static synchronized void addSnake(Snake snake) { public static synchronized void addSnake(Snake snake) {
if (snakes.size() == 0) { if (snakes.isEmpty()) {
startTimer(); startTimer();
} }
snakes.put(Integer.valueOf(snake.getId()), snake); snakes.put(Integer.valueOf(snake.getId()), snake);
@ -54,7 +54,7 @@ public class SnakeTimer {
public static synchronized void removeSnake(Snake snake) { public static synchronized void removeSnake(Snake snake) {
snakes.remove(Integer.valueOf(snake.getId())); snakes.remove(Integer.valueOf(snake.getId()));
if (snakes.size() == 0) { if (snakes.isEmpty()) {
stopTimer(); stopTimer();
} }
} }

@ -42,7 +42,7 @@ public class SnakeTimer {
private static final ConcurrentHashMap<Integer, Snake> snakes = new ConcurrentHashMap<Integer, Snake>(); private static final ConcurrentHashMap<Integer, Snake> snakes = new ConcurrentHashMap<Integer, Snake>();
public static synchronized void addSnake(Snake snake) { public static synchronized void addSnake(Snake snake) {
if (snakes.size() == 0) { if (snakes.isEmpty()) {
startTimer(); startTimer();
} }
snakes.put(Integer.valueOf(snake.getId()), snake); snakes.put(Integer.valueOf(snake.getId()), snake);
@ -54,7 +54,7 @@ public class SnakeTimer {
public static synchronized void removeSnake(Snake snake) { public static synchronized void removeSnake(Snake snake) {
snakes.remove(Integer.valueOf(snake.getId())); snakes.remove(Integer.valueOf(snake.getId()));
if (snakes.size() == 0) { if (snakes.isEmpty()) {
stopTimer(); stopTimer();
} }
} }

@ -73,7 +73,7 @@ public class PropertiesMergingResourceTransformer implements ResourceTransformer
@Override @Override
public boolean hasTransformedResource() { public boolean hasTransformedResource() {
return this.data.size() > 0; return !this.data.isEmpty();
} }
@Override @Override

@ -504,7 +504,7 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor,
} }
return; return;
} }
if (profiles.size() > 0) { if (!profiles.isEmpty()) {
addProfiles(profiles); addProfiles(profiles);
this.logger.debug("Activated profiles " this.logger.debug("Activated profiles "
+ StringUtils.collectionToCommaDelimitedString(profiles)); + StringUtils.collectionToCommaDelimitedString(profiles));

@ -52,7 +52,7 @@ public class DelegatingApplicationContextInitializer implements
public void initialize(ConfigurableApplicationContext context) { public void initialize(ConfigurableApplicationContext context) {
ConfigurableEnvironment environment = context.getEnvironment(); ConfigurableEnvironment environment = context.getEnvironment();
List<Class<?>> initializerClasses = getInitializerClasses(environment); List<Class<?>> initializerClasses = getInitializerClasses(environment);
if (initializerClasses.size() > 0) { if (!initializerClasses.isEmpty()) {
applyInitializerClasses(context, initializerClasses); applyInitializerClasses(context, initializerClasses);
} }
} }

@ -261,13 +261,13 @@ abstract class AbstractFilterRegistrationBean extends RegistrationBean {
DEFAULT_URL_MAPPINGS); DEFAULT_URL_MAPPINGS);
} }
else { else {
if (servletNames.size() > 0) { if (!servletNames.isEmpty()) {
this.logger.info("Mapping filter: '" + registration.getName() this.logger.info("Mapping filter: '" + registration.getName()
+ "' to servlets: " + servletNames); + "' to servlets: " + servletNames);
registration.addMappingForServletNames(dispatcherTypes, this.matchAfter, registration.addMappingForServletNames(dispatcherTypes, this.matchAfter,
servletNames.toArray(new String[servletNames.size()])); servletNames.toArray(new String[servletNames.size()]));
} }
if (this.urlPatterns.size() > 0) { if (!this.urlPatterns.isEmpty()) {
this.logger.info("Mapping filter: '" + registration.getName() this.logger.info("Mapping filter: '" + registration.getName()
+ "' to urls: " + this.urlPatterns); + "' to urls: " + this.urlPatterns);
registration.addMappingForUrlPatterns(dispatcherTypes, this.matchAfter, registration.addMappingForUrlPatterns(dispatcherTypes, this.matchAfter,

@ -137,7 +137,7 @@ public abstract class RegistrationBean implements ServletContextInitializer, Ord
"Registration is null. Was something already registered for name=[" "Registration is null. Was something already registered for name=["
+ this.name + "]?"); + this.name + "]?");
registration.setAsyncSupported(this.asyncSupported); registration.setAsyncSupported(this.asyncSupported);
if (this.initParameters.size() > 0) { if (!this.initParameters.isEmpty()) {
registration.setInitParameters(this.initParameters); registration.setInitParameters(this.initParameters);
} }
} }

@ -119,7 +119,7 @@ public abstract class SpringBootServletInitializer implements WebApplicationInit
.findAnnotation(getClass(), Configuration.class) != null) { .findAnnotation(getClass(), Configuration.class) != null) {
application.getSources().add(getClass()); application.getSources().add(getClass());
} }
Assert.state(application.getSources().size() > 0, Assert.state(!application.getSources().isEmpty(),
"No SpringApplication sources have been defined. Either override the " "No SpringApplication sources have been defined. Either override the "
+ "configure method or add an @Configuration annotation"); + "configure method or add an @Configuration annotation");
// Ensure error pages are registered // Ensure error pages are registered

@ -123,7 +123,7 @@ public class SpringApplicationContextLoader extends AbstractContextLoader {
Set<Object> sources = new LinkedHashSet<Object>(); Set<Object> sources = new LinkedHashSet<Object>();
sources.addAll(Arrays.asList(mergedConfig.getClasses())); sources.addAll(Arrays.asList(mergedConfig.getClasses()));
sources.addAll(Arrays.asList(mergedConfig.getLocations())); sources.addAll(Arrays.asList(mergedConfig.getLocations()));
Assert.state(sources.size() > 0, "No configuration classes " Assert.state(!sources.isEmpty(), "No configuration classes "
+ "or locations found in @SpringApplicationConfiguration. " + "or locations found in @SpringApplicationConfiguration. "
+ "For default configuration detection to work you need " + "For default configuration detection to work you need "
+ "Spring 4.0.3 or better (found " + SpringVersion.getVersion() + ")."); + "Spring 4.0.3 or better (found " + SpringVersion.getVersion() + ").");

Loading…
Cancel
Save