Class AlphaNumericComparator

  • All Implemented Interfaces:
    java.io.Serializable, java.util.Comparator<org.restlet.data.Reference>

    public class AlphaNumericComparator
    extends AlphabeticalComparator
    Optimized public-domain implementation of a Java alphanumeric sort.

    This implementation uses a single comparison pass over the characters in a CharSequence, and returns as soon as a differing character is found, unless the difference occurs in a series of numeric characters, in which case that series is followed to its end. Numeric series of equal length are compared numerically, that is, according to the most significant (leftmost) differing digit. Series of unequal length are compared by their length.

    This implementation appears to be 2-5 times faster than alphanumeric comparators based based on substring analysis, with a lighter memory footprint.

    This alphanumeric comparator has approximately 20%-50% the performance of the lexical String.compareTo() operation. Character sequences without numeric data are compared more quickly.

    Dedicated to the public domain by the original author: http://creativecommons.org/licenses/publicdomain/

    Author:
    Rob Heittman, Solertium Corporation
    See Also:
    Serialized Form
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int compare​(java.lang.String uri0, java.lang.String uri1)
      Compares two strings.
      protected boolean isNotDigit​(char x)
      Indicates if the character is a digit.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.util.Comparator

        equals, reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
    • Constructor Detail

      • AlphaNumericComparator

        public AlphaNumericComparator()
    • Method Detail

      • compare

        public int compare​(java.lang.String uri0,
                           java.lang.String uri1)
        Description copied from class: AlphabeticalComparator
        Compares two strings.
        Overrides:
        compare in class AlphabeticalComparator
        Parameters:
        uri0 - The first string.
        uri1 - The second string.
        Returns:
        The comparison result.
        See Also:
        Comparator
      • isNotDigit

        protected boolean isNotDigit​(char x)
        Indicates if the character is a digit.
        Parameters:
        x - The character to test.
        Returns:
        True if the character is a digit.