From 8d3264f680018ccbec6a40253b477283aa3a7e6e Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 16 Feb 2018 10:23:18 +0100 Subject: [PATCH] Prefer List.sort(Comparator) over Collections.sort(List, Comparator) --- .../ReflectiveAspectJAdvisorFactory.java | 10 ++--- .../support/DefaultListableBeanFactory.java | 3 +- .../beans/support/PropertyComparator.java | 5 +-- .../annotation/ConfigurationClassParser.java | 2 +- .../support/DefaultLifecycleProcessor.java | 2 +- .../PostProcessorRegistrationDelegate.java | 4 +- .../core/ExceptionDepthComparator.java | 8 ++-- .../springframework/core/OrderComparator.java | 9 ++--- .../AnnotationAwareOrderComparator.java | 7 ++-- .../springframework/util/AntPathMatcher.java | 4 +- .../springframework/util/MimeTypeUtils.java | 4 +- .../org/springframework/util/PathMatcher.java | 8 ++-- .../config/SortedResourcesFactoryBean.java | 9 ++--- .../DestinationPatternsMessageCondition.java | 4 +- ...bstractExceptionHandlerMethodResolver.java | 5 +-- .../AbstractMethodMessageHandler.java | 2 +- .../test/web/ModelAndViewAssert.java | 39 +++++++------------ .../org/springframework/http/MediaType.java | 9 ++--- .../ExceptionHandlerMethodResolver.java | 4 +- .../resource/VersionResourceResolver.java | 5 +-- .../handler/AbstractHandlerMethodMapping.java | 7 ++-- .../handler/AbstractUrlHandlerMapping.java | 4 +- .../condition/PatternsRequestCondition.java | 4 +- .../servlet/resource/ResourceUrlProvider.java | 4 +- .../resource/VersionResourceResolver.java | 5 +-- 25 files changed, 71 insertions(+), 96 deletions(-) diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/ReflectiveAspectJAdvisorFactory.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/ReflectiveAspectJAdvisorFactory.java index e1846517e7..1b4ea1ed7c 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/ReflectiveAspectJAdvisorFactory.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/ReflectiveAspectJAdvisorFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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.Serializable; import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.lang.reflect.Method; -import java.util.Collections; import java.util.Comparator; import java.util.LinkedList; import java.util.List; @@ -155,7 +154,7 @@ public class ReflectiveAspectJAdvisorFactory extends AbstractAspectJAdvisorFacto methods.add(method); } }); - Collections.sort(methods, METHOD_COMPARATOR); + methods.sort(METHOD_COMPARATOR); return methods; } @@ -164,7 +163,7 @@ public class ReflectiveAspectJAdvisorFactory extends AbstractAspectJAdvisorFacto * for the given introduction field. *

Resulting Advisors will need to be evaluated for targets. * @param introductionField the field to introspect - * @return {@code null} if not an Advisor + * @return the Advisor instance, or {@code null} if not an Advisor */ @Nullable private Advisor getDeclareParentsAdvisor(Field introductionField) { @@ -293,6 +292,7 @@ public class ReflectiveAspectJAdvisorFactory extends AbstractAspectJAdvisorFacto springAdvice.setArgumentNamesFromStringArray(argNames); } springAdvice.calculateArgumentBindings(); + return springAdvice; } @@ -307,7 +307,7 @@ public class ReflectiveAspectJAdvisorFactory extends AbstractAspectJAdvisorFacto public SyntheticInstantiationAdvisor(final MetadataAwareAspectInstanceFactory aif) { super(aif.getAspectMetadata().getPerClausePointcut(), (MethodBeforeAdvice) - (method, args, target) -> aif.getAspectInstance());// Simply instantiate the aspect + (method, args, target) -> aif.getAspectInstance()); } } diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java index 578698519b..244de92c56 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java @@ -30,7 +30,6 @@ import java.security.PrivilegedAction; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.Collections; import java.util.Comparator; import java.util.IdentityHashMap; import java.util.Iterator; @@ -1201,7 +1200,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto TypeConverter converter = (typeConverter != null ? typeConverter : getTypeConverter()); Object result = converter.convertIfNecessary(matchingBeans.values(), type); if (getDependencyComparator() != null && result instanceof List) { - Collections.sort((List) result, adaptDependencyComparator(matchingBeans)); + ((List) result).sort(adaptDependencyComparator(matchingBeans)); } return result; } diff --git a/spring-beans/src/main/java/org/springframework/beans/support/PropertyComparator.java b/spring-beans/src/main/java/org/springframework/beans/support/PropertyComparator.java index 55ac70efdf..afc3f03906 100644 --- a/spring-beans/src/main/java/org/springframework/beans/support/PropertyComparator.java +++ b/spring-beans/src/main/java/org/springframework/beans/support/PropertyComparator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -17,7 +17,6 @@ package org.springframework.beans.support; import java.util.Arrays; -import java.util.Collections; import java.util.Comparator; import java.util.List; @@ -135,7 +134,7 @@ public class PropertyComparator implements Comparator { */ public static void sort(List source, SortDefinition sortDefinition) throws BeansException { if (StringUtils.hasText(sortDefinition.getProperty())) { - Collections.sort(source, new PropertyComparator<>(sortDefinition)); + source.sort(new PropertyComparator<>(sortDefinition)); } } diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java index b6d9f9049c..046889c47e 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java @@ -540,7 +540,7 @@ class ConfigurationClassParser { return; } - Collections.sort(deferredImports, DEFERRED_IMPORT_COMPARATOR); + deferredImports.sort(DEFERRED_IMPORT_COMPARATOR); for (DeferredImportSelectorHolder deferredImport : deferredImports) { ConfigurationClass configClass = deferredImport.getConfigurationClass(); try { diff --git a/spring-context/src/main/java/org/springframework/context/support/DefaultLifecycleProcessor.java b/spring-context/src/main/java/org/springframework/context/support/DefaultLifecycleProcessor.java index 6a2f9538ff..38713f4e24 100644 --- a/spring-context/src/main/java/org/springframework/context/support/DefaultLifecycleProcessor.java +++ b/spring-context/src/main/java/org/springframework/context/support/DefaultLifecycleProcessor.java @@ -365,7 +365,7 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor if (logger.isInfoEnabled()) { logger.info("Stopping beans in phase " + this.phase); } - Collections.sort(this.members, Collections.reverseOrder()); + this.members.sort(Collections.reverseOrder()); CountDownLatch latch = new CountDownLatch(this.smartMemberCount); Set countDownBeanNames = Collections.synchronizedSet(new LinkedHashSet<>()); for (LifecycleGroupMember member : this.members) { diff --git a/spring-context/src/main/java/org/springframework/context/support/PostProcessorRegistrationDelegate.java b/spring-context/src/main/java/org/springframework/context/support/PostProcessorRegistrationDelegate.java index 00f7e3d409..a316814ed2 100644 --- a/spring-context/src/main/java/org/springframework/context/support/PostProcessorRegistrationDelegate.java +++ b/spring-context/src/main/java/org/springframework/context/support/PostProcessorRegistrationDelegate.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -260,7 +260,7 @@ class PostProcessorRegistrationDelegate { if (comparatorToUse == null) { comparatorToUse = OrderComparator.INSTANCE; } - Collections.sort(postProcessors, comparatorToUse); + postProcessors.sort(comparatorToUse); } /** diff --git a/spring-core/src/main/java/org/springframework/core/ExceptionDepthComparator.java b/spring-core/src/main/java/org/springframework/core/ExceptionDepthComparator.java index 6bcf98d05b..f24653571c 100644 --- a/spring-core/src/main/java/org/springframework/core/ExceptionDepthComparator.java +++ b/spring-core/src/main/java/org/springframework/core/ExceptionDepthComparator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2018 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. @@ -18,7 +18,6 @@ package org.springframework.core; import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.Comparator; import java.util.List; @@ -88,9 +87,8 @@ public class ExceptionDepthComparator implements Comparator> handledExceptions = - new ArrayList<>(exceptionTypes); - Collections.sort(handledExceptions, new ExceptionDepthComparator(targetException)); + List> handledExceptions = new ArrayList<>(exceptionTypes); + handledExceptions.sort(new ExceptionDepthComparator(targetException)); return handledExceptions.get(0); } diff --git a/spring-core/src/main/java/org/springframework/core/OrderComparator.java b/spring-core/src/main/java/org/springframework/core/OrderComparator.java index d2852bd9d0..513e631481 100644 --- a/spring-core/src/main/java/org/springframework/core/OrderComparator.java +++ b/spring-core/src/main/java/org/springframework/core/OrderComparator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -17,7 +17,6 @@ package org.springframework.core; import java.util.Arrays; -import java.util.Collections; import java.util.Comparator; import java.util.List; @@ -43,7 +42,7 @@ import org.springframework.util.ObjectUtils; * @since 07.04.2003 * @see Ordered * @see org.springframework.core.annotation.AnnotationAwareOrderComparator - * @see java.util.Collections#sort(java.util.List, java.util.Comparator) + * @see java.util.List#sort(java.util.Comparator) * @see java.util.Arrays#sort(Object[], java.util.Comparator) */ public class OrderComparator implements Comparator { @@ -165,11 +164,11 @@ public class OrderComparator implements Comparator { *

Optimized to skip sorting for lists with size 0 or 1, * in order to avoid unnecessary array extraction. * @param list the List to sort - * @see java.util.Collections#sort(java.util.List, java.util.Comparator) + * @see java.util.List#sort(java.util.Comparator) */ public static void sort(List list) { if (list.size() > 1) { - Collections.sort(list, INSTANCE); + list.sort(INSTANCE); } } diff --git a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationAwareOrderComparator.java b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationAwareOrderComparator.java index 0e19e8cf44..ec4471b1d3 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationAwareOrderComparator.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationAwareOrderComparator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -19,7 +19,6 @@ package org.springframework.core.annotation; import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Method; import java.util.Arrays; -import java.util.Collections; import java.util.List; import org.springframework.core.DecoratingProxy; @@ -119,11 +118,11 @@ public class AnnotationAwareOrderComparator extends OrderComparator { *

Optimized to skip sorting for lists with size 0 or 1, * in order to avoid unnecessary array extraction. * @param list the List to sort - * @see java.util.Collections#sort(java.util.List, java.util.Comparator) + * @see java.util.List#sort(java.util.Comparator) */ public static void sort(List list) { if (list.size() > 1) { - Collections.sort(list, INSTANCE); + list.sort(INSTANCE); } } diff --git a/spring-core/src/main/java/org/springframework/util/AntPathMatcher.java b/spring-core/src/main/java/org/springframework/util/AntPathMatcher.java index ee784517a0..f0aee9b2e2 100644 --- a/spring-core/src/main/java/org/springframework/util/AntPathMatcher.java +++ b/spring-core/src/main/java/org/springframework/util/AntPathMatcher.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -598,7 +598,7 @@ public class AntPathMatcher implements PathMatcher { /** * Given a full path, returns a {@link Comparator} suitable for sorting patterns in order of * explicitness. - *

This{@code Comparator} will {@linkplain java.util.Collections#sort(List, Comparator) sort} + *

This{@code Comparator} will {@linkplain java.util.List#sort(Comparator) sort} * a list so that more specific patterns (without uri templates or wild cards) come before * generic patterns. So given a list with the following patterns: *

    diff --git a/spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java b/spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java index 23e516b3d0..b1ee00961f 100644 --- a/spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java +++ b/spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -308,7 +308,7 @@ public abstract class MimeTypeUtils { public static void sortBySpecificity(List mimeTypes) { Assert.notNull(mimeTypes, "'mimeTypes' must not be null"); if (mimeTypes.size() > 1) { - Collections.sort(mimeTypes, SPECIFICITY_COMPARATOR); + mimeTypes.sort(SPECIFICITY_COMPARATOR); } } diff --git a/spring-core/src/main/java/org/springframework/util/PathMatcher.java b/spring-core/src/main/java/org/springframework/util/PathMatcher.java index 0e0cfb33b7..30312f0228 100644 --- a/spring-core/src/main/java/org/springframework/util/PathMatcher.java +++ b/spring-core/src/main/java/org/springframework/util/PathMatcher.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2018 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. @@ -104,9 +104,9 @@ public interface PathMatcher { /** * Given a full path, returns a {@link Comparator} suitable for sorting patterns * in order of explicitness for that path. - *

    The full algorithm used depends on the underlying implementation, but generally, - * the returned {@code Comparator} will - * {@linkplain java.util.Collections#sort(java.util.List, java.util.Comparator) sort} + *

    The full algorithm used depends on the underlying implementation, + * but generally, the returned {@code Comparator} will + * {@linkplain java.util.List#sort(java.util.Comparator) sort} * a list so that more specific patterns come before generic patterns. * @param path the full path to use for comparison * @return a comparator capable of sorting patterns in order of explicitness diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/config/SortedResourcesFactoryBean.java b/spring-jdbc/src/main/java/org/springframework/jdbc/config/SortedResourcesFactoryBean.java index bdee530cc4..fb6dbc9e6e 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/config/SortedResourcesFactoryBean.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/config/SortedResourcesFactoryBean.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -19,7 +19,6 @@ package org.springframework.jdbc.config; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.List; import org.springframework.beans.factory.FactoryBean; @@ -75,7 +74,7 @@ public class SortedResourcesFactoryBean extends AbstractFactoryBean for (String location : this.locations) { List resources = new ArrayList<>( Arrays.asList(this.resourcePatternResolver.getResources(location))); - Collections.sort(resources, (r1, r2) -> { + resources.sort((r1, r2) -> { try { return r1.getURL().toString().compareTo(r2.getURL().toString()); } @@ -83,9 +82,7 @@ public class SortedResourcesFactoryBean extends AbstractFactoryBean return 0; } }); - for (Resource resource : resources) { - scripts.add(resource); - } + scripts.addAll(resources); } return scripts.toArray(new Resource[scripts.size()]); } diff --git a/spring-messaging/src/main/java/org/springframework/messaging/handler/DestinationPatternsMessageCondition.java b/spring-messaging/src/main/java/org/springframework/messaging/handler/DestinationPatternsMessageCondition.java index c8f529709c..f99566e62b 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/handler/DestinationPatternsMessageCondition.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/handler/DestinationPatternsMessageCondition.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -168,7 +168,7 @@ public class DestinationPatternsMessageCondition return null; } - Collections.sort(matches, this.pathMatcher.getPatternComparator(destination)); + matches.sort(this.pathMatcher.getPatternComparator(destination)); return new DestinationPatternsMessageCondition(matches, this.pathMatcher); } diff --git a/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/AbstractExceptionHandlerMethodResolver.java b/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/AbstractExceptionHandlerMethodResolver.java index 58bbe60cfd..82d07e260d 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/AbstractExceptionHandlerMethodResolver.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/AbstractExceptionHandlerMethodResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -18,7 +18,6 @@ package org.springframework.messaging.handler.invocation; import java.lang.reflect.Method; import java.util.ArrayList; -import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -126,7 +125,7 @@ public abstract class AbstractExceptionHandlerMethodResolver { } } if (!matches.isEmpty()) { - Collections.sort(matches, new ExceptionDepthComparator(exceptionType)); + matches.sort(new ExceptionDepthComparator(exceptionType)); return this.mappedMethods.get(matches.get(0)); } else { diff --git a/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/AbstractMethodMessageHandler.java b/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/AbstractMethodMessageHandler.java index 8cd4828418..76923a748f 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/AbstractMethodMessageHandler.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/AbstractMethodMessageHandler.java @@ -453,7 +453,7 @@ public abstract class AbstractMethodMessageHandler return; } Comparator comparator = new MatchComparator(getMappingComparator(message)); - Collections.sort(matches, comparator); + matches.sort(comparator); if (logger.isTraceEnabled()) { logger.trace("Found " + matches.size() + " handler methods: " + matches); diff --git a/spring-test/src/main/java/org/springframework/test/web/ModelAndViewAssert.java b/spring-test/src/main/java/org/springframework/test/web/ModelAndViewAssert.java index 578fb682cd..8fb715b5a2 100644 --- a/spring-test/src/main/java/org/springframework/test/web/ModelAndViewAssert.java +++ b/spring-test/src/main/java/org/springframework/test/web/ModelAndViewAssert.java @@ -16,7 +16,6 @@ package org.springframework.test.web; -import java.util.Collections; import java.util.Comparator; import java.util.HashSet; import java.util.List; @@ -32,8 +31,8 @@ import static org.springframework.test.util.AssertionErrors.*; * A collection of assertions intended to simplify testing scenarios dealing * with Spring Web MVC {@link org.springframework.web.servlet.ModelAndView * ModelAndView} objects. - *

    - * Intended for use with JUnit 4 and TestNG. All {@code assert*()} methods + * + *

    Intended for use with JUnit 4 and TestNG. All {@code assert*()} methods * throw {@link AssertionError}s. * * @author Sam Brannen @@ -78,11 +77,10 @@ public abstract class ModelAndViewAssert { public static void assertCompareListModelAttribute(ModelAndView mav, String modelName, List expectedList) { assertTrue("ModelAndView is null", mav != null); List modelList = assertAndReturnModelAttributeOfType(mav, modelName, List.class); - assertTrue( - "Size of model list is '" + modelList.size() + "' while size of expected list is '" + expectedList.size() - + "'", expectedList.size() == modelList.size()); + assertTrue("Size of model list is '" + modelList.size() + "' while size of expected list is '" + + expectedList.size() + "'", expectedList.size() == modelList.size()); assertTrue("List in model under name '" + modelName + "' is not equal to the expected list.", - expectedList.equals(modelList)); + expectedList.equals(modelList)); } /** @@ -155,28 +153,21 @@ public abstract class ModelAndViewAssert { * @param comparator the comparator to use (may be {@code null}). If not * specifying the comparator, both lists will be sorted not using any comparator. */ - @SuppressWarnings({ "unchecked", "rawtypes" }) + @SuppressWarnings({"unchecked", "rawtypes"}) public static void assertSortAndCompareListModelAttribute( ModelAndView mav, String modelName, List expectedList, Comparator comparator) { assertTrue("ModelAndView is null", mav != null); List modelList = assertAndReturnModelAttributeOfType(mav, modelName, List.class); - assertTrue( - "Size of model list is '" + modelList.size() + "' while size of expected list is '" + expectedList.size() - + "'", expectedList.size() == modelList.size()); + assertTrue("Size of model list is '" + modelList.size() + "' while size of expected list is '" + + expectedList.size() + "'", expectedList.size() == modelList.size()); - if (comparator != null) { - Collections.sort(modelList, comparator); - Collections.sort(expectedList, comparator); - } - else { - Collections.sort(modelList); - Collections.sort(expectedList); - } + modelList.sort(comparator); + expectedList.sort(comparator); assertTrue("List in model under name '" + modelName + "' is not equal to the expected list.", - expectedList.equals(modelList)); + expectedList.equals(modelList)); } /** @@ -190,15 +181,14 @@ public abstract class ModelAndViewAssert { fail("ModelAndView is null"); } assertTrue("View name is not equal to '" + expectedName + "' but was '" + mav.getViewName() + "'", - ObjectUtils.nullSafeEquals(expectedName, mav.getViewName())); + ObjectUtils.nullSafeEquals(expectedName, mav.getViewName())); } private static void appendNonMatchingSetsErrorMessage( Set assertionSet, Set incorrectSet, StringBuilder sb) { - Set tempSet = new HashSet<>(); - tempSet.addAll(incorrectSet); + Set tempSet = new HashSet<>(incorrectSet); tempSet.removeAll(assertionSet); if (!tempSet.isEmpty()) { @@ -210,8 +200,7 @@ public abstract class ModelAndViewAssert { } } - tempSet = new HashSet<>(); - tempSet.addAll(assertionSet); + tempSet = new HashSet<>(assertionSet); tempSet.removeAll(incorrectSet); if (!tempSet.isEmpty()) { diff --git a/spring-web/src/main/java/org/springframework/http/MediaType.java b/spring-web/src/main/java/org/springframework/http/MediaType.java index ca5421a71c..5e0aa4d362 100644 --- a/spring-web/src/main/java/org/springframework/http/MediaType.java +++ b/spring-web/src/main/java/org/springframework/http/MediaType.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -622,7 +622,7 @@ public class MediaType extends MimeType implements Serializable { public static void sortBySpecificity(List mediaTypes) { Assert.notNull(mediaTypes, "'mediaTypes' must not be null"); if (mediaTypes.size() > 1) { - Collections.sort(mediaTypes, SPECIFICITY_COMPARATOR); + mediaTypes.sort(SPECIFICITY_COMPARATOR); } } @@ -649,7 +649,7 @@ public class MediaType extends MimeType implements Serializable { public static void sortByQualityValue(List mediaTypes) { Assert.notNull(mediaTypes, "'mediaTypes' must not be null"); if (mediaTypes.size() > 1) { - Collections.sort(mediaTypes, QUALITY_VALUE_COMPARATOR); + mediaTypes.sort(QUALITY_VALUE_COMPARATOR); } } @@ -662,8 +662,7 @@ public class MediaType extends MimeType implements Serializable { public static void sortBySpecificityAndQuality(List mediaTypes) { Assert.notNull(mediaTypes, "'mediaTypes' must not be null"); if (mediaTypes.size() > 1) { - Collections.sort(mediaTypes, - MediaType.SPECIFICITY_COMPARATOR.thenComparing(MediaType.QUALITY_VALUE_COMPARATOR)); + mediaTypes.sort(MediaType.SPECIFICITY_COMPARATOR.thenComparing(MediaType.QUALITY_VALUE_COMPARATOR)); } } diff --git a/spring-web/src/main/java/org/springframework/web/method/annotation/ExceptionHandlerMethodResolver.java b/spring-web/src/main/java/org/springframework/web/method/annotation/ExceptionHandlerMethodResolver.java index 692fc9722e..47da35286d 100644 --- a/spring-web/src/main/java/org/springframework/web/method/annotation/ExceptionHandlerMethodResolver.java +++ b/spring-web/src/main/java/org/springframework/web/method/annotation/ExceptionHandlerMethodResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -169,7 +169,7 @@ public class ExceptionHandlerMethodResolver { } } if (!matches.isEmpty()) { - Collections.sort(matches, new ExceptionDepthComparator(exceptionType)); + matches.sort(new ExceptionDepthComparator(exceptionType)); return this.mappedMethods.get(matches.get(0)); } else { diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/VersionResourceResolver.java b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/VersionResourceResolver.java index b022e54f45..84fcc4a92f 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/VersionResourceResolver.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/VersionResourceResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -23,7 +23,6 @@ import java.net.URI; import java.net.URL; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.Comparator; import java.util.LinkedHashMap; import java.util.List; @@ -248,7 +247,7 @@ public class VersionResourceResolver extends AbstractResourceResolver { } if (!matchingPatterns.isEmpty()) { Comparator comparator = this.pathMatcher.getPatternComparator(path); - Collections.sort(matchingPatterns, comparator); + matchingPatterns.sort(comparator); return this.versionStrategyMap.get(matchingPatterns.get(0)); } return null; diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMethodMapping.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMethodMapping.java index b12c7cd904..8247c9f033 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMethodMapping.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMethodMapping.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -353,10 +353,9 @@ public abstract class AbstractHandlerMethodMapping extends AbstractHandlerMap if (!matches.isEmpty()) { Comparator comparator = new MatchComparator(getMappingComparator(request)); - Collections.sort(matches, comparator); + matches.sort(comparator); if (logger.isTraceEnabled()) { - logger.trace("Found " + matches.size() + " matching mapping(s) for [" + - lookupPath + "] : " + matches); + logger.trace("Found " + matches.size() + " matching mapping(s) for [" + lookupPath + "] : " + matches); } Match bestMatch = matches.get(0); if (matches.size() > 1) { diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractUrlHandlerMapping.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractUrlHandlerMapping.java index 507f68971f..7732d9d6c5 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractUrlHandlerMapping.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractUrlHandlerMapping.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -194,7 +194,7 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping i String bestMatch = null; Comparator patternComparator = getPathMatcher().getPatternComparator(urlPath); if (!matchingPatterns.isEmpty()) { - Collections.sort(matchingPatterns, patternComparator); + matchingPatterns.sort(patternComparator); if (logger.isDebugEnabled()) { logger.debug("Matching patterns for request [" + urlPath + "] are " + matchingPatterns); } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/PatternsRequestCondition.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/PatternsRequestCondition.java index 8fa1c40243..4b02317bd0 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/PatternsRequestCondition.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/PatternsRequestCondition.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -228,7 +228,7 @@ public final class PatternsRequestCondition extends AbstractRequestCondition patternComparator = getPathMatcher().getPatternComparator(lookupPath); - Collections.sort(matchingPatterns, patternComparator); + matchingPatterns.sort(patternComparator); for (String pattern : matchingPatterns) { String pathWithinMapping = getPathMatcher().extractPathWithinPattern(pattern, lookupPath); String pathMapping = lookupPath.substring(0, lookupPath.indexOf(pathWithinMapping)); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/VersionResourceResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/VersionResourceResolver.java index 60286103ce..fc8c70c9d8 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/VersionResourceResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/VersionResourceResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -23,7 +23,6 @@ import java.net.URI; import java.net.URL; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.Comparator; import java.util.LinkedHashMap; import java.util.List; @@ -241,7 +240,7 @@ public class VersionResourceResolver extends AbstractResourceResolver { } if (!matchingPatterns.isEmpty()) { Comparator comparator = this.pathMatcher.getPatternComparator(path); - Collections.sort(matchingPatterns, comparator); + matchingPatterns.sort(comparator); return this.versionStrategyMap.get(matchingPatterns.get(0)); } return null;