public class OrderComparator extends Object implements Comparator<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).
Ordered
,
Collections.sort(java.util.List, 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 soruce 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
public Comparator<Object> withSourceProvider(OrderComparator.OrderSourceProvider sourceProvider)
sourceProvider
- the order source provider to usepublic int compare(Object o1, Object o2)
compare
in interface Comparator<Object>
protected int getOrder(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 fallbackprotected Integer findOrder(Object obj)
The default implementation checks against the Ordered
interface.
Can be overridden in subclasses.
obj
- the object to checknull
if none foundpublic 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 sortCollections.sort(java.util.List, 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)