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 a24f2cde3a..bf7232e10a 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-2012 the original author or authors. + * Copyright 2002-2013 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,6 +16,10 @@ package org.springframework.core.annotation; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + import org.springframework.core.OrderComparator; import org.springframework.core.Ordered; @@ -37,7 +41,7 @@ public class AnnotationAwareOrderComparator extends OrderComparator { /** * Shared default instance of AnnotationAwareOrderComparator. */ - public static AnnotationAwareOrderComparator INSTANCE = new AnnotationAwareOrderComparator(); + public static final AnnotationAwareOrderComparator INSTANCE = new AnnotationAwareOrderComparator(); @Override @@ -54,4 +58,31 @@ public class AnnotationAwareOrderComparator extends OrderComparator { return Ordered.LOWEST_PRECEDENCE; } + + /** + * Sort the given List with a default AnnotationAwareOrderComparator. + *

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) + */ + public static void sort(List list) { + if (list.size() > 1) { + Collections.sort(list, INSTANCE); + } + } + + /** + * Sort the given array with a default AnnotationAwareOrderComparator. + *

Optimized to skip sorting for lists with size 0 or 1, + * in order to avoid unnecessary array extraction. + * @param array the array to sort + * @see java.util.Arrays#sort(Object[], java.util.Comparator) + */ + public static void sort(Object[] array) { + if (array.length > 1) { + Arrays.sort(array, INSTANCE); + } + } + }