Package org.apache.wiki.util.comparators
Class HumanComparator
- java.lang.Object
-
- org.apache.wiki.util.comparators.HumanComparator
-
- All Implemented Interfaces:
java.util.Comparator<java.lang.String>
public class HumanComparator extends java.lang.Object implements java.util.Comparator<java.lang.String>
A comparator that sorts Strings using "human" ordering, including decimal ordering. Only works for languages where every character is lexigraphically distinct and correctly unicode ordered (e.g. English). Other languages should useCollatedHumanComparator
. Pretty efficient but still slower than String.compareTo().
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
HumanComparator.CharType
-
Field Summary
Fields Modifier and Type Field Description static java.util.Comparator<java.lang.String>
DEFAULT_HUMAN_COMPARATOR
-
Constructor Summary
Constructors Constructor Description HumanComparator()
Default constructor which does nothing.HumanComparator(HumanComparator.CharType[] sortOrder)
Constructor specifying all the character type order.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description int
compare(java.lang.String str1, java.lang.String str2)
static java.util.Comparator<java.lang.String>
getInstance()
Returns a singleton comparator that implements the default behaviour.HumanComparator.CharType[]
getSortOrder()
void
setSortOrder(HumanComparator.CharType[] sortOrder)
Set the order in which letters, numbers and everything else is presented.
-
-
-
Field Detail
-
DEFAULT_HUMAN_COMPARATOR
public static final java.util.Comparator<java.lang.String> DEFAULT_HUMAN_COMPARATOR
-
-
Constructor Detail
-
HumanComparator
public HumanComparator()
Default constructor which does nothing. Here because it has a non-default constructor.
-
HumanComparator
public HumanComparator(HumanComparator.CharType[] sortOrder)
Constructor specifying all the character type order.- Parameters:
sortOrder
- see setSortOrder
-
-
Method Detail
-
getInstance
public static java.util.Comparator<java.lang.String> getInstance()
Returns a singleton comparator that implements the default behaviour.- Returns:
- the singleton comparator.
-
compare
public int compare(java.lang.String str1, java.lang.String str2)
- Specified by:
compare
in interfacejava.util.Comparator<java.lang.String>
-
getSortOrder
public HumanComparator.CharType[] getSortOrder()
-
setSortOrder
public void setSortOrder(HumanComparator.CharType[] sortOrder)
Set the order in which letters, numbers and everything else is presented. Default is other, digits and then letters. For example, the strings "abb", "a1b" and "a b" will sort in the order "a b", "a1b" then "abb" by default.- Parameters:
sortOrder
- Must be an array ofCharType
containing exactly 3 elements each of which must be distinct.- Throws:
java.lang.IllegalArgumentException
- if being called on the result ofHumanStringComparator.getInstance()
orsortOrder
is not exactly 3 differentCharType
.
-
-