org.rhq.core.util.sort
Class HumaneStringComparator

java.lang.Object
  extended by org.rhq.core.util.sort.HumaneStringComparator
All Implemented Interfaces:
Comparator<String>

public class HumaneStringComparator
extends Object
implements Comparator<String>

A comparator for Strings that imposes a more "human" order on Strings. Modeled on the way the Mac OS X Finder orders file names, it breaks the Strings into segments of non-numbers and numbers (e.g. Foo123Bar into 'Foo', 123, 'Bar') and compares the segments from each String one at a time.

The following rules are applied when comparing segments:

If two Strings are identical after analyzing them in a segment-at-time manner then lhs.compareTo(rhs) will be invoked to ensure that the result provided by the comparator is reflexive (i.e. compare(x,y) = -compare(y,x)) and agrees with String.equals() in all cases.

As an example, the following list of Strings is presented in the order in which the comparator will sort them:

Author:
Tim Fennell (oss@tfenne.com)

Field Summary
static HumaneStringComparator DEFAULT
          A default instance of the comparator that can be used without instantiating a new copy every time one is needed.
 
Constructor Summary
HumaneStringComparator()
           
 
Method Summary
 int compare(String lhs, String rhs)
          The implementation of the Comparable interface method that compares two Strings.
protected  int compareNumberSegments(String lhs, String rhs)
          Converts the two Strings to doubles and then compares then numerically by invoking Double.compareTo()
protected  int compareStringSegments(String lhs, String rhs)
          Compares the left hand String to the right hand String case-insensitively by invoking lhs.compareToIgnoreCase(rhs).
 
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
 

Field Detail

DEFAULT

public static final HumaneStringComparator DEFAULT
A default instance of the comparator that can be used without instantiating a new copy every time one is needed.

Constructor Detail

HumaneStringComparator

public HumaneStringComparator()
Method Detail

compare

public int compare(String lhs,
                   String rhs)
The implementation of the Comparable interface method that compares two Strings. Implements the algorithm described in the class level javadoc.

Specified by:
compare in interface Comparator<String>
Parameters:
lhs - the first of two Strings to compare
rhs - the second of two Strings to compare
Returns:
int -1, 0 or 1 respectively if the first String (lhs) sorts before equally or after the second String

compareNumberSegments

protected int compareNumberSegments(String lhs,
                                    String rhs)
Converts the two Strings to doubles and then compares then numerically by invoking Double.compareTo()


compareStringSegments

protected int compareStringSegments(String lhs,
                                    String rhs)
Compares the left hand String to the right hand String case-insensitively by invoking lhs.compareToIgnoreCase(rhs).



Copyright © 2008-2012 Red Hat, Inc.. All Rights Reserved.