Class TotalOrder
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:
- Numbers of primitive-equivalent types (
Long,Double, etc, but notBigDecimal) are compared as numbers. - Strings are compared according to parameters used to create an instance of
TotalOrder. Strict comparison, case-insensitive comparison and collator-based comparison are available. See the factory methods. - Instances of
ComparableTupleare compared according to the rules ofComparableTuple. Also,ComparableTupleand numbers (as in the first rule) are mutually comparable, as if the number was a first element of a tuple. - If none of the above apply, instances of the same class that implements
Comparableare compared using the class'compareTo()method. - Two objects of the same class that is not
Comparableare compared as theirtoString()values.
Notes:
- String comparison rules (collator-based or case-insensitive comparison) apply only when comparing
Stringobjects. They do not apply when comparingComparableTupleelements, when comparingtoString()values, or when comparing anything inside some class'compareTo()method. StringandComparableTupleare not mutually comparable (unlike numbers andComparableTuple), because of the different rules for comparing strings.
Usage
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclassclass -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionintstatic TotalOrderstatic TotalOrderstatic TotalOrderwithCollator(Collator collator) static TotalOrderwithCollatorStringComparison(Locale locale) static TotalOrderwithCollatorStringComparison(Locale locale, int strength) static TotalOrderwithCollatorStringComparison(Locale locale, int strength, int decomposition) static TotalOrder<T> TotalOrder.PayloadWrapper<T>
-
Field Details
-
COMPARATOR
-
-
Method Details
-
withStrictStringComparison
-
withCaseInsensitiveStringComparison
-
withCaseInsensitiveStringComparison
-
withCollatorStringComparison
-
withCollatorStringComparison
-
withCollatorStringComparison
public static TotalOrder withCollatorStringComparison(Locale locale, int strength, int decomposition) -
withCollator
-
wrap
-
wrap
-
compare
-