Package org.openstreetmap.josm.tools
Class AlphanumComparator
- java.lang.Object
-
- org.openstreetmap.josm.tools.AlphanumComparator
-
- All Implemented Interfaces:
java.io.Serializable
,java.util.Comparator<java.lang.String>
public final class AlphanumComparator extends java.lang.Object implements java.util.Comparator<java.lang.String>, java.io.Serializable
The Alphanum Algorithm is an improved sorting algorithm for strings containing numbers: Instead of sorting numbers in ASCII order like a standard sort, this algorithm sorts numbers in numeric order.The Alphanum Algorithm is discussed at DaveKoelle.com
This is an updated version with enhancements made by Daniel Migowski, Andre Bogus, David Koelle and others.
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private static byte[]
ASCII_MAPPING
A mapping from ASCII characters to the defaultCollator
order.(package private) static java.lang.String
ASCII_SORT_ORDER
The sort order for the fast ASCII sort method.private static AlphanumComparator
INSTANCE
private static long
serialVersionUID
(package private) static boolean
useFastASCIISort
true
to use the faster ASCII sorting algorithm.
-
Constructor Summary
Constructors Modifier Constructor Description private
AlphanumComparator()
Constructs a new Alphanum Comparator.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description int
compare(java.lang.String s1, java.lang.String s2)
private static int
compareChunk(java.lang.String thisChunk, int thisChunkLength, java.lang.String thatChunk, int thatChunkLength)
Compare two string chunksprivate static int
compareString(java.lang.String string1, int len1, java.lang.String string2, int len2)
Compare two ASCII strings in a manner compatible with the defaultCollator
private static java.lang.String
getChunk(java.lang.String s, int slength, int marker)
Returns an alphanum chunk.static AlphanumComparator
getInstance()
Replies the unique instance.private static boolean
isAscii(java.lang.String string, int stringLength)
Check if a string is ASCII only
-
-
-
Field Detail
-
useFastASCIISort
static boolean useFastASCIISort
true
to use the faster ASCII sorting algorithm. Set tofalse
when testing compatibility.
-
ASCII_SORT_ORDER
static final java.lang.String ASCII_SORT_ORDER
The sort order for the fast ASCII sort method.- See Also:
- Constant Field Values
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
INSTANCE
private static final AlphanumComparator INSTANCE
-
ASCII_MAPPING
private static final byte[] ASCII_MAPPING
A mapping from ASCII characters to the defaultCollator
order. At writing, the default rules can be found in CollationRules#DEFAULTRULES.
-
-
Constructor Detail
-
AlphanumComparator
private AlphanumComparator()
Constructs a new Alphanum Comparator.
-
-
Method Detail
-
getInstance
public static AlphanumComparator getInstance()
Replies the unique instance.- Returns:
- the unique instance
-
compareString
private static int compareString(java.lang.String string1, int len1, java.lang.String string2, int len2)
Compare two ASCII strings in a manner compatible with the defaultCollator
- Parameters:
string1
- The first string to comparelen1
- The length of the first stringstring2
- The second string to comparelen2
- The length of the second string- Returns:
- See
String.compareToIgnoreCase(String)
(e.g.string1.compareToIgnoreCase(string2)
).
-
getChunk
private static java.lang.String getChunk(java.lang.String s, int slength, int marker)
Returns an alphanum chunk. Length of string is passed in for improved efficiency (only need to calculate it once).- Parameters:
s
- stringslength
- string lengthmarker
- position- Returns:
- alphanum chunk found at given position
-
isAscii
private static boolean isAscii(java.lang.String string, int stringLength)
Check if a string is ASCII only- Parameters:
string
- The string to checkstringLength
- The length of the string (for performance reasons)- Returns:
true
if the string only contains ascii characters
-
compareChunk
private static int compareChunk(java.lang.String thisChunk, int thisChunkLength, java.lang.String thatChunk, int thatChunkLength)
Compare two string chunks- Parameters:
thisChunk
- The first chunk to comparethisChunkLength
- The length of the first chunk (for performance reasons)thatChunk
- The second chunk to comparethatChunkLength
- The length of the second chunk (for performance reasons)- Returns:
- The
Comparator
result
-
compare
public int compare(java.lang.String s1, java.lang.String s2)
- Specified by:
compare
in interfacejava.util.Comparator<java.lang.String>
-
-