Top Description Fields Constructors Methods
de.undercouch.citeproc.helper

public Class AlphanumComparator

extends Object
implements Comparator<CharSequence>
Class Inheritance
All Implemented Interfaces
java.util.Comparator
Imports
java.math.BigInteger, java.text.Collator, java.util.Comparator, .Locale

Performs locale-sensitive comparison of Strings with numbers to produce a natural order that appears logical to humans.

Examples:

compare("Hello", "Hello")    =  0
compare("10", "2")           =  1
compare("10th", "2nd")       =  1
compare("File 10", "File 2") =  1
compare("Manana", "Mañana")  = -1

The implementation is loosely based on the Alphanum algorithm by Dave Koelle and the Java implementation released under the MIT license. However, it has been extended to use a Collator for locale-sensitive comparison, and it is also able to compare arbitrarily large numbers.

Author
Michel Kraemer

Field Summary

Modifier and TypeField and Description
private final Collator

Constructor Summary

AccessConstructor and Description
public
AlphanumComparator(Locale
the locale
locale
)

Create a new comparator for the given locale

Method Summary

Modifier and TypeMethod and Description
public int
compare(CharSequence a, CharSequence b)

Implements java.util.Comparator.compare.

Compares its two arguments for order.

private int

Returns:

the index of the first digit of the number found or the length of the character sequence if no number was found
findNextNumber
(CharSequence
the character sequence
s
,
int
the index of the character where the search should start
start
)

Find the next number in the given character sequence

private int

Returns:

the index of the first character that is not a digit or the length of the character sequence if there is no other character
findNumberEnd
(CharSequence
the character sequence
s
,
int
the index of the character where the search should start
start
)

Find the end of a number in the given character sequence

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

collatorback to summary
private final Collator collator

Constructor Detail

AlphanumComparatorback to summary
public AlphanumComparator(Locale locale)

Create a new comparator for the given locale

Parameters
locale:Locale

the locale

Method Detail

compareback to summary
public int compare(CharSequence a, CharSequence b)

Implements java.util.Comparator.compare.

Doc from java.util.Comparator.compare.

Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

The implementor must ensure that signum(compare(x, y)) == -signum(compare(y, x)) for all x and y. (This implies that compare(x, y) must throw an exception if and only if compare(y, x) throws an exception.)

The implementor must also ensure that the relation is transitive: ((compare(x, y)>0) && (compare(y, z)>0)) implies compare(x, z)>0.

Finally, the implementor must ensure that compare(x, y)==0 implies that signum(compare(x, z))==signum(compare(y, z)) for all z.

Annotations
@Override
findNextNumberback to summary
private int findNextNumber(CharSequence s, int start)

Find the next number in the given character sequence

Parameters
s:CharSequence

the character sequence

start:int

the index of the character where the search should start

Returns:int

the index of the first digit of the number found or the length of the character sequence if no number was found

findNumberEndback to summary
private int findNumberEnd(CharSequence s, int start)

Find the end of a number in the given character sequence

Parameters
s:CharSequence

the character sequence

start:int

the index of the character where the search should start

Returns:int

the index of the first character that is not a digit or the length of the character sequence if there is no other character