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).
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(java.lang.Object o1,
java.lang.Object o2) |
private int |
doCompare(java.lang.Object o1,
java.lang.Object o2,
OrderComparator.OrderSourceProvider sourceProvider) |
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.
|
private int |
getOrder(java.lang.Object obj,
OrderComparator.OrderSourceProvider sourceProvider)
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.
|
public static final OrderComparator INSTANCE
public java.util.Comparator<java.lang.Object> withSourceProvider(OrderComparator.OrderSourceProvider sourceProvider)
sourceProvider
- the order source provider to usepublic int compare(java.lang.Object o1, java.lang.Object o2)
compare
in interface java.util.Comparator<java.lang.Object>
private int doCompare(java.lang.Object o1, java.lang.Object o2, OrderComparator.OrderSourceProvider sourceProvider)
private int getOrder(java.lang.Object obj, OrderComparator.OrderSourceProvider sourceProvider)
The default implementation checks against the given OrderComparator.OrderSourceProvider
using findOrder(java.lang.Object)
and falls back to a regular getOrder(Object)
call.
obj
- the object to checkOrdered.LOWEST_PRECEDENCE
as fallbackprotected int getOrder(java.lang.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 java.lang.Integer findOrder(java.lang.Object obj)
The default implementation checks against the Ordered
interface.
Can be overridden in subclasses.
obj
- the object to checknull
if none foundpublic java.lang.Integer getPriority(java.lang.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(java.util.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(java.lang.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(java.lang.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)