mckay.utilities.general
Class SmartComparator

java.lang.Object
  extended by mckay.utilities.general.SmartComparator
All Implemented Interfaces:
java.util.Comparator

public class SmartComparator
extends java.lang.Object
implements java.util.Comparator

A Comparator that can be used for making comparisons in tasks such as sorting. Performs numerical comparisons for integers, shorts, longs, doubles and floats and performs string comparisons for all other types of objects, where case is ignored and a Collator for the default location is used.


Constructor Summary
SmartComparator()
          Basic constructor that stores the settings for the default locale.
 
Method Summary
 int compare(boolean a, boolean b)
          Compares the two arguments for order.
 int compare(double a, float b)
          Compares the two arguments for order.
 int compare(float a, float b)
          Compares the two arguments for order.
 int compare(int a, int b)
          Compares the two arguments for order.
 int compare(long a, long b)
          Compares the two arguments for order.
 int compare(java.lang.Object a, java.lang.Object b)
          Compares the two arguments for order.
 int compare(short a, short b)
          Compares the two arguments for order.
 int compare(java.lang.String a, java.lang.String b)
          Compares the two arguments for order.
 boolean equals(java.lang.Object object)
          Indicates whether the given object is "equal to" this Comparator.
 java.text.Collator getCollator()
          Get the Collator used by this object.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SmartComparator

public SmartComparator()
Basic constructor that stores the settings for the default locale.

Method Detail

compare

public int compare(java.lang.Object a,
                   java.lang.Object b)
Compares the 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 argument. If the arguments are both Integers, Longs, Shorts, Floats, Doubles or Booleans, then they are compared based on the wrapped baseic data type. Any other objects are compared to as strings, with the order determined using a Collator with the default Locale. Case is not considered in the comparison, however, as is normally the case with Collators.

Specified by:
compare in interface java.util.Comparator
Parameters:
a - The first item to compare.
b - The second item to compare.
Returns:
The comparison result.

compare

public int compare(java.lang.String a,
                   java.lang.String b)
Compares the 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 argument. The order is determined using a Collator with the default Locale. Case is not considered in the comparison, however, as is normally the case with Collators.

Parameters:
a - The first item to compare.
b - The second item to compare.
Returns:
The comparison result.

compare

public int compare(int a,
                   int b)
Compares the 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 argument.

Parameters:
a - The first item to compare.
b - The second item to compare.
Returns:
The comparison result.

compare

public int compare(long a,
                   long b)
Compares the 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 argument.

Parameters:
a - The first item to compare.
b - The second item to compare.
Returns:
The comparison result.

compare

public int compare(short a,
                   short b)
Compares the 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 argument.

Parameters:
a - The first item to compare.
b - The second item to compare.
Returns:
The comparison result.

compare

public int compare(double a,
                   float b)
Compares the 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 argument.

Parameters:
a - The first item to compare.
b - The second item to compare.
Returns:
The comparison result.

compare

public int compare(float a,
                   float b)
Compares the 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 argument.

Parameters:
a - The first item to compare.
b - The second item to compare.
Returns:
The comparison result.

compare

public int compare(boolean a,
                   boolean b)
Compares the 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 argument. False is considered to be before True.

Parameters:
a - The first item to compare.
b - The second item to compare.
Returns:
The comparison result.

equals

public boolean equals(java.lang.Object object)
Indicates whether the given object is "equal to" this Comparator. It is considered equal if it is a SmartComparator and it has an equal Collator for use with strings.

Specified by:
equals in interface java.util.Comparator
Overrides:
equals in class java.lang.Object
Parameters:
object - The object to check for equality.
Returns:
Whether there is equality.

getCollator

public java.text.Collator getCollator()
Get the Collator used by this object.

Returns:
The Collator used by this object.