Package org.openstreetmap.josm.tools
Class AlphanumComparator
java.lang.Object
org.openstreetmap.josm.tools.AlphanumComparator
- All Implemented Interfaces:
Serializable,Comparator<String>
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:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final byte[]A mapping from ASCII characters to the defaultCollatororder.(package private) static final StringThe sort order for the fast ASCII sort method.private static final AlphanumComparatorprivate static final long(package private) static booleantrueto use the faster ASCII sorting algorithm. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintprivate static intcompareChunk(String thisChunk, int thisChunkLength, String thatChunk, int thatChunkLength) Compare two string chunksprivate static intcompareString(String string1, int len1, String string2, int len2) Compare two ASCII strings in a manner compatible with the defaultCollatorprivate static StringReturns an alphanum chunk.static AlphanumComparatorReplies the unique instance.private static booleanCheck if a string is ASCII onlyMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.Comparator
equals, reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
-
Field Details
-
useFastASCIISort
trueto use the faster ASCII sorting algorithm. Set tofalsewhen testing compatibility. -
ASCII_SORT_ORDER
The sort order for the fast ASCII sort method.- See Also:
-
serialVersionUID
- See Also:
-
INSTANCE
-
ASCII_MAPPING
A mapping from ASCII characters to the defaultCollatororder. At writing, the default rules can be found in CollationRules#DEFAULTRULES.
-
-
Constructor Details
-
AlphanumComparator
private AlphanumComparator()Constructs a new Alphanum Comparator.
-
-
Method Details
-
getInstance
Replies the unique instance.- Returns:
- the unique instance
-
compareString
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
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
Check if a string is ASCII only- Parameters:
string- The string to checkstringLength- The length of the string (for performance reasons)- Returns:
trueif the string only contains ascii characters
-
compareChunk
private static int compareChunk(String thisChunk, int thisChunkLength, 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
Comparatorresult
-
compare
- Specified by:
comparein interfaceComparator<String>
-