Top Description Inners Fields Constructors Methods
org.springframework.core

public Class OrderComparator

extends Object
implements Comparator<Object>
Class Inheritance
All Implemented Interfaces
java.util.Comparator
Known Direct Subclasses
org.springframework.core.annotation.AnnotationAwareOrderComparator
Imports
java.util.Arrays, .Comparator, .List, org.springframework.lang.Nullable, org.springframework.util.ObjectUtils

Comparator implementation for Ordered objects, sorting by order value ascending, respectively by priority descending.

PriorityOrdered Objects

PriorityOrdered objects will be sorted with higher priority than plain Ordered objects.

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.

Authors
Juergen Hoeller, Sam Brannen
Since
07.04.2003
See Also
Ordered, PriorityOrdered, org.springframework.core.annotation.AnnotationAwareOrderComparator, java.util.List#sort(java.util.Comparator), java.util.Arrays#sort(Object[], java.util.Comparator)

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
OrderComparator.OrderSourceProvider

Strategy interface to provide an order source for a given object.

Field Summary

Modifier and TypeField and Description
public static final OrderComparator
INSTANCE

Shared default instance of OrderComparator.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public int
compare(Object
the first object to be compared.
o1
,
Object
the second object to be compared.
o2
)

Implements java.util.Comparator.compare.

Compares its two arguments for order.

private int
protected Integer

Returns:

the order value, or null if none found
findOrder
(Object
the object to check
obj
)

Find an order value indicated by the given object.

private int

Returns:

the order value, or Ordered.LOWEST_PRECEDENCE as fallback
getOrder
(Object
the object to check
obj
,
OrderComparator.OrderSourceProvider sourceProvider)

Determine the order value for the given object.

protected int

Returns:

the order value, or Ordered.LOWEST_PRECEDENCE as fallback
getOrder
(Object
the object to check
obj
)

Determine the order value for the given object.

public Integer

Returns:

the priority value, or null if none
getPriority
(Object
the object to check
obj
)

Determine a priority value for the given object, if any.

public static void
sort(List<?>
the List to sort
list
)

Sort the given List with a default OrderComparator.

public static void
sort(Object[]
the array to sort
array
)

Sort the given array with a default OrderComparator.

public static void
sortIfNecessary(Object
the array or List to sort
value
)

Sort the given array or List with a default OrderComparator, if necessary.

public Comparator<Object>

Returns:

the adapted comparator
withSourceProvider
(OrderComparator.OrderSourceProvider
the order source provider to use
sourceProvider
)

Build an adapted order comparator with the given source provider.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

INSTANCEback to summary
public static final OrderComparator INSTANCE

Shared default instance of OrderComparator.

Constructor Detail

OrderComparatorback to summary
public OrderComparator()

Method Detail

compareback to summary
public int compare(Object o1, Object o2)

Implements java.util.Comparator.compare.

Doc from java.util.Comparator.compare.

Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

The implementor must ensure that signum(compare(x, y)) == -signum(compare(y, x)) for all x and y. (This implies that compare(x, y) must throw an exception if and only if compare(y, x) throws an exception.)

The implementor must also ensure that the relation is transitive: ((compare(x, y)>0) && (compare(y, z)>0)) implies compare(x, z)>0.

Finally, the implementor must ensure that compare(x, y)==0 implies that signum(compare(x, z))==signum(compare(y, z)) for all z.

Parameters
o1:Object

the first object to be compared.

o2:Object

the second object to be compared.

Returns:int

a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

Annotations
@Override
doCompareback to summary
private int doCompare(Object o1, Object o2, OrderComparator.OrderSourceProvider sourceProvider)
findOrderback to summary
protected Integer findOrder(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:Object

the object to check

Returns:Integer

the order value, or null if none found

Annotations
@Nullable
getOrderback to summary
private int getOrder(Object obj, OrderComparator.OrderSourceProvider sourceProvider)

Determine the order value for the given object.

The default implementation checks against the given OrderSourceProvider using findOrder and falls back to a regular getOrder(Object) call.

Parameters
obj:Object

the object to check

Returns:int

the order value, or Ordered.LOWEST_PRECEDENCE as fallback

getOrderback to summary
protected int getOrder(Object obj)

Determine the order value for the given object.

The default implementation checks against the Ordered interface through delegating to findOrder. Can be overridden in subclasses.

Parameters
obj:Object

the object to check

Returns:int

the order value, or Ordered.LOWEST_PRECEDENCE as fallback

getPriorityback to summary
public Integer getPriority(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:Object

the object to check

Returns:Integer

the priority value, or null if none

Annotations
@Nullable
Since
4.1
sortback to summary
public static void sort(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:List<?>

the List to sort

See Also
java.util.List#sort(java.util.Comparator)
sortback to summary
public static void sort(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:Object[]

the array to sort

See Also
java.util.Arrays#sort(Object[], java.util.Comparator)
sortIfNecessaryback to summary
public static void sortIfNecessary(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:Object

the array or List to sort

See Also
java.util.Arrays#sort(Object[], java.util.Comparator)
withSourceProviderback to summary
public Comparator<Object> withSourceProvider(OrderComparator.OrderSourceProvider sourceProvider)

Build an adapted order comparator with the given source provider.

Parameters
sourceProvider:OrderComparator.OrderSourceProvider

the order source provider to use

Returns:Comparator<Object>

the adapted comparator

Since
4.1
org.springframework.core back to summary

public Interface OrderComparator.OrderSourceProvider

Annotations
@FunctionalInterface

Strategy interface to provide an order source for a given object.
Since
4.1

Method Summary

Modifier and TypeMethod and Description
public Object

Returns:

the order source for that object, or null if none found
getOrderSource
(Object
the object to find an order source for
obj
)

Return an order source for the specified object, i.e. an object that should be checked for an order value as a replacement to the given object.

Method Detail

getOrderSourceback to summary
public Object getOrderSource(Object obj)

Return an order source for the specified object, i.e. an object that should be checked for an order value as a replacement to the given object.

Can also be an array of order source objects.

If the returned object does not indicate any order, the comparator will fall back to checking the original object.

Parameters
obj:Object

the object to find an order source for

Returns:Object

the order source for that object, or null if none found

Annotations
@Nullable