Spring Framework

org.springframework.core
Class OrderComparator

java.lang.Object
  extended by org.springframework.core.OrderComparator
All Implemented Interfaces:
java.util.Comparator<java.lang.Object>
Direct Known Subclasses:
AnnotationAwareOrderComparator

public class OrderComparator
extends java.lang.Object
implements java.util.Comparator<java.lang.Object>

Comparator implementation for Ordered objects, sorting by order value ascending (resp. by priority descending).

Non-Ordered objects are treated as greatest order values, thus ending up at the end of the list, in arbitrary order (just like same order values of Ordered objects).

Since:
07.04.2003
Author:
Juergen Hoeller
See Also:
Ordered, Collections.sort(java.util.List, java.util.Comparator), Arrays.sort(Object[], java.util.Comparator)

Field Summary
static OrderComparator INSTANCE
          Shared default instance of OrderComparator.
 
Constructor Summary
OrderComparator()
           
 
Method Summary
 int compare(java.lang.Object o1, java.lang.Object o2)
           
protected  int getOrder(java.lang.Object obj)
          Determine the order value for the given object.
static void sort(java.util.List<?> list)
          Sort the given List with a default OrderComparator.
static void sort(java.lang.Object[] array)
          Sort the given array with a default OrderComparator.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Comparator
equals
 

Field Detail

INSTANCE

public static final OrderComparator INSTANCE
Shared default instance of OrderComparator.

Constructor Detail

OrderComparator

public OrderComparator()
Method Detail

compare

public int compare(java.lang.Object o1,
                   java.lang.Object o2)
Specified by:
compare in interface java.util.Comparator<java.lang.Object>

getOrder

protected int getOrder(java.lang.Object obj)
Determine the order value for the given object.

The default implementation checks against the Ordered interface. Can be overridden in subclasses.

Parameters:
obj - the object to check
Returns:
the order value, or Ordered.LOWEST_PRECEDENCE as fallback

sort

public static void sort(java.util.List<?> list)
Sort the given List with a default OrderComparator.

Optimized to skip sorting for lists with size 0 or 1, in order to avoid unnecessary array extraction.

Parameters:
list - the List to sort
See Also:
Collections.sort(java.util.List, java.util.Comparator)

sort

public static void sort(java.lang.Object[] array)
Sort the given array with a default OrderComparator.

Optimized to skip sorting for lists with size 0 or 1, in order to avoid unnecessary array extraction.

Parameters:
array - the array to sort
See Also:
Arrays.sort(Object[], java.util.Comparator)

Spring Framework