org.springframework.core

Class OrderComparator

  • java.lang.Object
    • 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, respectively by priority descending.

    Same Order Objects

    Objects that have the same order value will be sorted with arbitrary ordering with respect to other objects with the same order value.

    Non-ordered Objects

    Any object that does not provide its own order value is implicitly assigned a value of Ordered.LOWEST_PRECEDENCE, thus ending up at the end of a sorted collection in arbitrary order with respect to other objects with the same order value.

    Since:
    07.04.2003
    Author:
    Juergen Hoeller, Sam Brannen
    See Also:
    Ordered, AnnotationAwareOrderComparator, List.sort(java.util.Comparator), Arrays.sort(Object[], java.util.Comparator)
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class and Description
      static interface  OrderComparator.OrderSourceProvider
      Strategy interface to provide an order source for a given object.
    • Field Summary

      Fields 
      Modifier and Type Field and Description
      static OrderComparator INSTANCE
      Shared default instance of OrderComparator.
    • Constructor Summary

      Constructors 
      Constructor and Description
      OrderComparator() 
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method and Description
      int compare(java.lang.Object o1, java.lang.Object o2) 
      protected java.lang.Integer findOrder(java.lang.Object obj)
      Find an order value indicated by the given object.
      protected int getOrder(java.lang.Object obj)
      Determine the order value for the given object.
      java.lang.Integer getPriority(java.lang.Object obj)
      Determine a priority value for the given object, if any.
      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.
      static void sortIfNecessary(java.lang.Object value)
      Sort the given array or List with a default OrderComparator, if necessary.
      java.util.Comparator<java.lang.Object> withSourceProvider(OrderComparator.OrderSourceProvider sourceProvider)
      Build an adapted order comparator with the given source provider.
      • 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

        comparing, comparing, comparingDouble, comparingInt, comparingLong, equals, naturalOrder, nullsFirst, nullsLast, reversed, reverseOrder, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
    • Field Detail

      • INSTANCE

        public static final OrderComparator INSTANCE
        Shared default instance of OrderComparator.
    • Constructor Detail

      • OrderComparator

        public OrderComparator()
    • Method Detail

      • withSourceProvider

        public java.util.Comparator<java.lang.Object> withSourceProvider(OrderComparator.OrderSourceProvider sourceProvider)
        Build an adapted order comparator with the given source provider.
        Parameters:
        sourceProvider - the order source provider to use
        Returns:
        the adapted comparator
        Since:
        4.1
      • compare

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

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

        The default implementation checks against the Ordered interface through delegating to findOrder(java.lang.Object). Can be overridden in subclasses.

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

        @Nullable
        protected java.lang.Integer findOrder(java.lang.Object obj)
        Find an order value indicated by 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 null if none found
      • getPriority

        @Nullable
        public java.lang.Integer getPriority(java.lang.Object obj)
        Determine a priority value for the given object, if any.

        The default implementation always returns null. Subclasses may override this to give specific kinds of values a 'priority' characteristic, in addition to their 'order' semantics. A priority indicates that it may be used for selecting one object over another, in addition to serving for ordering purposes in a list/array.

        Parameters:
        obj - the object to check
        Returns:
        the priority value, or null if none
        Since:
        4.1
      • 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:
        List.sort(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)
      • sortIfNecessary

        public static void sortIfNecessary(java.lang.Object value)
        Sort the given array or List with a default OrderComparator, if necessary. Simply skips sorting when given any other value.

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

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