public class TotalOrder extends Object
This class provides total ordering for objects of any type.
When comparing two objects, it is first determined if the objects are mutually comparable. The rules below specify which objects are comparable and how are they compared.
If two objects are not comparable, then their relative order is determined by
comparing their classes. Among classes, numbers and ComparableTuple
instances come first, then come
String
instances, then come all other classes, in order of their class names.
Rules for comparable objects:
Long
, Double
, etc, but not BigDecimal
) are
compared as numbers.TotalOrder
.
Strict comparison, case-insensitive comparison and collator-based comparison are available. See the factory methods.ComparableTuple
are compared according to the rules of ComparableTuple
. Also,
ComparableTuple
and numbers (as in the first rule) are mutually comparable, as if the number was a first element of a tuple.Comparable
are compared using the
class' compareTo()
method.Comparable
are compared as their toString()
values.Notes:
String
objects. They do not apply when comparing ComparableTuple
elements, when comparing toString()
values,
or when comparing anything inside some class' compareTo()
method.String
and ComparableTuple
are not mutually comparable (unlike numbers and ComparableTuple
), because of the different
rules for comparing strings.
To sort a set of values, one needs to create a image of that set using wrap(java.lang.Object)
function, then sort
that image using COMPARATOR
. To link back to the original values or some other keys,
use wrappers with payload - see wrap(Object, Object)
.
Although there is a convenience method compare(Object, Object)
for single comparison,
this class does not implement Comparator<Object>
, to avoid performance pitfall when sorting
non-prepared values.
Modifier and Type | Class and Description |
---|---|
class |
TotalOrder.PayloadWrapper<T> |
class |
TotalOrder.ValueWrapper |
Modifier and Type | Field and Description |
---|---|
static Comparator<TotalOrder.ValueWrapper> |
COMPARATOR |
Modifier and Type | Method and Description |
---|---|
int |
compare(Object o1,
Object o2) |
static TotalOrder |
withCaseInsensitiveStringComparison() |
static TotalOrder |
withCaseInsensitiveStringComparison(Locale locale) |
static TotalOrder |
withCollator(Collator collator) |
static TotalOrder |
withCollatorStringComparison(Locale locale) |
static TotalOrder |
withCollatorStringComparison(Locale locale,
int strength) |
static TotalOrder |
withCollatorStringComparison(Locale locale,
int strength,
int decomposition) |
static TotalOrder |
withStrictStringComparison() |
TotalOrder.ValueWrapper |
wrap(Object value) |
<T> TotalOrder.PayloadWrapper<T> |
wrap(Object value,
T payload) |
public static final Comparator<TotalOrder.ValueWrapper> COMPARATOR
public static TotalOrder withStrictStringComparison()
public static TotalOrder withCaseInsensitiveStringComparison()
public static TotalOrder withCaseInsensitiveStringComparison(Locale locale)
public static TotalOrder withCollatorStringComparison(Locale locale)
public static TotalOrder withCollatorStringComparison(Locale locale, int strength)
public static TotalOrder withCollatorStringComparison(Locale locale, int strength, int decomposition)
public static TotalOrder withCollator(Collator collator)
public TotalOrder.ValueWrapper wrap(Object value)
public <T> TotalOrder.PayloadWrapper<T> wrap(Object value, T payload)
Copyright © 2024 Tempo Software. All Rights Reserved.