public class OrderComparator extends Object implements Comparator<Object>
Comparator
implementation for Ordered
objects, sorting
by order value ascending, respectively by priority descending.
PriorityOrdered
ObjectsPriorityOrdered
objects will be sorted with higher priority than
plain Ordered
objects.
Objects that have the same order value will be sorted with arbitrary ordering with respect to other objects with the same order value.
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.
Ordered
,
PriorityOrdered
,
AnnotationAwareOrderComparator
,
List.sort(java.util.Comparator)
,
Arrays.sort(Object[], java.util.Comparator)
Modifier and Type | Class and Description |
---|---|
static interface |
OrderComparator.OrderSourceProvider
Strategy interface to provide an order source for a given object.
|
Modifier and Type | Field and Description |
---|---|
static OrderComparator |
INSTANCE
Shared default instance of
OrderComparator . |
Constructor and Description |
---|
OrderComparator() |
Modifier and Type | Method and Description |
---|---|
int |
compare(Object o1,
Object o2) |
protected Integer |
findOrder(Object obj)
Find an order value indicated by the given object.
|
protected int |
getOrder(Object obj)
Determine the order value for the given object.
|
Integer |
getPriority(Object obj)
Determine a priority value for the given object, if any.
|
static void |
sort(List<?> list)
Sort the given List with a default OrderComparator.
|
static void |
sort(Object[] array)
Sort the given array with a default OrderComparator.
|
static void |
sortIfNecessary(Object value)
Sort the given array or List with a default OrderComparator,
if necessary.
|
Comparator<Object> |
withSourceProvider(OrderComparator.OrderSourceProvider sourceProvider)
Build an adapted order comparator with the given source provider.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
comparing, comparing, comparingDouble, comparingInt, comparingLong, equals, naturalOrder, nullsFirst, nullsLast, reversed, reverseOrder, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
public static final OrderComparator INSTANCE
OrderComparator
.public Comparator<Object> withSourceProvider(OrderComparator.OrderSourceProvider sourceProvider)
sourceProvider
- the order source provider to usepublic int compare(@Nullable Object o1, @Nullable Object o2)
compare
in interface Comparator<Object>
protected int getOrder(@Nullable Object obj)
The default implementation checks against the Ordered
interface
through delegating to findOrder(java.lang.Object)
. Can be overridden in subclasses.
obj
- the object to checkOrdered.LOWEST_PRECEDENCE
as fallback@Nullable protected Integer findOrder(Object obj)
The default implementation checks against the Ordered
interface.
Can be overridden in subclasses.
obj
- the object to checknull
if none found@Nullable public Integer getPriority(Object obj)
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.
obj
- the object to checknull
if nonepublic static void sort(List<?> list)
Optimized to skip sorting for lists with size 0 or 1, in order to avoid unnecessary array extraction.
list
- the List to sortList.sort(java.util.Comparator)
public static void sort(Object[] array)
Optimized to skip sorting for lists with size 0 or 1, in order to avoid unnecessary array extraction.
array
- the array to sortArrays.sort(Object[], java.util.Comparator)
public static void sortIfNecessary(Object value)
Optimized to skip sorting for lists with size 0 or 1, in order to avoid unnecessary array extraction.
value
- the array or List to sortArrays.sort(Object[], java.util.Comparator)