diff --git a/src/java/org/apache/commons/lang/builder/CompareToBuilder.java b/src/java/org/apache/commons/lang/builder/CompareToBuilder.java index ef88221d9..73a0fe7c9 100644 --- a/src/java/org/apache/commons/lang/builder/CompareToBuilder.java +++ b/src/java/org/apache/commons/lang/builder/CompareToBuilder.java @@ -55,8 +55,13 @@ import java.lang.reflect.Field; import java.lang.reflect.Modifier; +import java.util.Comparator; +import java.util.Iterator; +import java.util.List; +import java.util.Map; import org.apache.commons.lang.NumberUtils; +import org.apache.commons.lang.enum.Enum; /** *

CompareTo generation routines.

* @@ -65,8 +70,8 @@ * hashcode built with {@link EqualsBuilder} and * {@link HashCodeBuilder}.

* - *

Two object that compare equal using equals should compare equals using - * compareTo

. + *

Two object that compare equal using equals should normally compare + * equals using compareTo

. * *

All relevant fields should be included in the calculation of the * comparison. Derived fields may be ignored. The same fields, in the same @@ -101,9 +106,10 @@ * * @author Steve Downey * @author Stephen Colebourne - * @version $Id: CompareToBuilder.java,v 1.5 2002/11/17 21:46:42 scolebourne Exp $ + * @version $Id: CompareToBuilder.java,v 1.6 2002/12/08 21:38:19 scolebourne Exp $ */ public class CompareToBuilder { + /** * If the fields tested are equals. */ @@ -112,9 +118,9 @@ public class CompareToBuilder { /** *

Constructor for CompareToBuilder.

* - *

Starts off assuming that the objects are equal.

- * - * @see java.lang.Object#Object() + *

Starts off assuming that the objects are equal. Multiple calls are + * then made to the various append methods, followed by a call to + * {@link #toComparison} to get the result.

*/ public CompareToBuilder() { super(); @@ -137,8 +143,8 @@ public CompareToBuilder() { * *

Static fields will not be tested.

* - * @param lhs Left Hand Side - * @param rhs Right Hand Side + * @param lhs this object + * @param rhs the other object * @return a negative integer, zero, or a positive integer as this * Object is less than, equal to, or greater than the specified Object. * @throws NullPointerException if either (but not both) parameter is @@ -165,8 +171,8 @@ public static int reflectionCompare(Object lhs, Object rhs) { * *

Static fields will not be tested.

* - * @param lhs Left Hand Side - * @param rhs Right Hand Side + * @param lhs this object + * @param rhs the other object * @param testTransients whether to include transient fields * @return a negative integer, zero, or a positive integer as this * Object is less than, equal to, or greater than the specified Object. @@ -175,8 +181,7 @@ public static int reflectionCompare(Object lhs, Object rhs) { * @throws ClassCastException if the specified Object's type prevents it * from being compared to this Object. */ - public static int reflectionCompare(Object lhs, Object rhs, - boolean testTransients) { + public static int reflectionCompare(Object lhs, Object rhs, boolean testTransients) { if (lhs == rhs) { return 0; } @@ -196,7 +201,7 @@ public static int reflectionCompare(Object lhs, Object rhs, if (!Modifier.isStatic(f.getModifiers())) { try { compareToBuilder.append(f.get(lhs), f.get(rhs)); - } catch (IllegalAccessException e) { + } catch (IllegalAccessException ex) { //this can't happen. Would get a Security exception instead //throw a runtime exception in case the impossible happens. throw new InternalError("Unexpected IllegalAccessException"); @@ -210,38 +215,66 @@ public static int reflectionCompare(Object lhs, Object rhs, //------------------------------------------------------------------------- /** - *

Test if two Objects are equal using either the - * compareTo method, or native comparison if the Objects are - * actually arrays.

+ *

Comparison of two Object references.

+ *
    + *
  1. Check if Objects are same using == + *
  2. Check if either is null, a null object is less than a non-null + *
  3. Check the object contents + *
+ * + *

The first parameter to be compared must either be an array or implement + * Comparable.

* - *

The objects must be Comparable. If they are not, the - * method will throw a ClassCastException.

- * - * @param lhs - Left Hand Side - * @param rhs - Right Hand Side + * @param lhs the Object from this object + * @param rhs the Object from the other object * @return CompareToBuilder - used to chain calls. - * @throws NullPointerException if either (but not both) parameter is - * null * @throws ClassCastException if the specified Object's type prevents it * from being compared to this Object. */ public CompareToBuilder append(Object lhs, Object rhs) { + return append(lhs, rhs, null); + } + + /** + *

Comparison of two Object references.

+ *
    + *
  1. Check if Objects are same using == + *
  2. Check if either is null, a null object is less than a non-null + *
  3. Check the object contents + *
+ * + *

If the first parameter to be compared is an array, the array methods will + * be used. Otherwise the comparator will be used. If the comparator is null, + * the lhs will be cast to Comparable.

+ * + * @param lhs the Object from this object + * @param rhs the Object from the other object + * @param comparator the comparator to use to compare the objects, + * null means to treat the lhs as Comparable. + * @return CompareToBuilder - used to chain calls. + * @throws ClassCastException if the specified Object's type prevents it + * from being compared to this Object. + */ + public CompareToBuilder append(Object lhs, Object rhs, Comparator comparator) { if (comparison != 0) { return this; } if (lhs == rhs) { return this; } - if (lhs == null || rhs == null) { - throw new NullPointerException(); + if (lhs == null) { + comparison = -1; + return this; + } + if (rhs == null) { + comparison = +1; + return this; } Class lhsClass = lhs.getClass(); - if (!lhsClass.isArray()) { - //the simple case, not an array, just test the element - comparison = ((Comparable) lhs).compareTo(rhs); - } else { - //'Switch' on type of array, to dispatch to the correct handler + if (lhsClass.isArray()) { + // 'Switch' on type of array, to dispatch to the correct handler // This handles multi dimensional arrays + // this could throw a ClassCastException is rhs is not the correct array type if (lhs instanceof long[]) { append((long[]) lhs, (long[]) rhs); } else if (lhs instanceof int[]) { @@ -260,7 +293,15 @@ public CompareToBuilder append(Object lhs, Object rhs) { append((boolean[]) lhs, (boolean[]) rhs); } else { // Not an array of primitives - append((Object[]) lhs, (Object[]) rhs); + // this could throw a ClassCastException is rhs is not an array + append((Object[]) lhs, (Object[]) rhs, comparator); + } + } else { + // the simple case, not an array, just test the element + if (comparator == null) { + comparison = ((Comparable) lhs).compareTo(rhs); + } else { + comparison = comparator.compare(lhs, rhs); } } return this; @@ -269,8 +310,8 @@ public CompareToBuilder append(Object lhs, Object rhs) { /** *

Test if two longs are <, > or ==.

* - * @param lhs - Left Hand Side - * @param rhs - Right Hand Side + * @param lhs the long from this object + * @param rhs the long from the other object * @return CompareToBuilder - used to chain calls. */ public CompareToBuilder append(long lhs, long rhs) { @@ -284,8 +325,8 @@ public CompareToBuilder append(long lhs, long rhs) { /** *

Test if two ints are <, > or ==.

* - * @param lhs - Left Hand Side - * @param rhs - Right Hand Side + * @param lhs the int from this object + * @param rhs the int from the other object * @return CompareToBuilder - used to chain calls. */ public CompareToBuilder append(int lhs, int rhs) { @@ -298,8 +339,9 @@ public CompareToBuilder append(int lhs, int rhs) { /** *

Test if two shorts are <, > or ==.

- * @param lhs - Left Hand Side - * @param rhs - Right Hand Side + * + * @param lhs the short from this object + * @param rhs the short from the other object * @return CompareToBuilder - used to chain calls. */ public CompareToBuilder append(short lhs, short rhs) { @@ -313,8 +355,8 @@ public CompareToBuilder append(short lhs, short rhs) { /** *

Test if two chars are <, > or ==.

* - * @param lhs - Left Hand Side - * @param rhs - Right Hand Side + * @param lhs the char from this object + * @param rhs the char from the other object * @return CompareToBuilder - used to chain calls. */ public CompareToBuilder append(char lhs, char rhs) { @@ -327,8 +369,9 @@ public CompareToBuilder append(char lhs, char rhs) { /** *

Test if two bytes are <, > or ==.

- * @param lhs - Left Hand Side - * @param rhs - Right Hand Side + * + * @param lhs the byte from this object + * @param rhs the byte from the other object * @return CompareToBuilder - used to chain calls. */ public CompareToBuilder append(byte lhs, byte rhs) { @@ -347,8 +390,8 @@ public CompareToBuilder append(byte lhs, byte rhs) { *

It is compatible with the hash code generated by * HashCodeBuilder.

* - * @param lhs - Left Hand Side - * @param rhs - Right Hand Side + * @param lhs the double from this object + * @param rhs the double from the other object * @return CompareToBuilder - used to chain calls. */ public CompareToBuilder append(double lhs, double rhs) { @@ -360,15 +403,15 @@ public CompareToBuilder append(double lhs, double rhs) { } /** - *

Test if two doubles are <, > or ==.

+ *

Test if two floats are <, > or ==.

* *

This handles NaNs, Infinties, and -0.0.

* *

It is compatible with the hash code generated by * HashCodeBuilder.

* - * @param lhs - Left Hand Side - * @param rhs - Right Hand Side + * @param lhs the float from this object + * @param rhs the float from the other object * @return CompareToBuilder - used to chain calls. */ public CompareToBuilder append(float lhs, float rhs) { @@ -382,8 +425,8 @@ public CompareToBuilder append(float lhs, float rhs) { /** *

Test if two booleanss are <, > or ==.

* - * @param lhs - Left Hand Side - * @param rhs - Right Hand Side + * @param lhs the boolean from this object + * @param rhs the boolean from the other object * @return CompareToBuilder - used to chain calls. */ public CompareToBuilder append(boolean lhs, boolean rhs) { @@ -402,59 +445,84 @@ public CompareToBuilder append(boolean lhs, boolean rhs) { } /** - *

Performs a deep comparison of two Object arrays.

+ *

Deep comparison of an Object array.

+ *
    + *
  1. Check if arrays are same using == + *
  2. Check if either is null, a null array is less than a non-null + *
  3. Check array length, a short length array is less than a long length array + *
  4. Check array contents element by element using {@link #append(long, long)} + *
* - *

This also will be called for the top level of multi-dimensional, + *

This method will also will be called for the top level of multi-dimensional, * ragged, and multi-typed arrays.

* - *

If two arrays are of different lengths, and all elements of the - * shorter array are equal to the elements in the longer array, the longer - * array is the greater. This is dictionary, or lexical, ordering.

- * - * @param lhs - Left Hand Side - * @param rhs - Right Hand Side + * @param lhs array from this object + * @param rhs array from the other object * @return CompareToBuilder - used to chain calls. - * @throws NullPointerException if either (but not both) parameter is - * null * @throws ClassCastException if the specified Object's type prevents it * from being compared to this Object. */ public CompareToBuilder append(Object[] lhs, Object[] rhs) { + return append(lhs, rhs, null); + } + + /** + *

Deep comparison of an Object array.

+ *
    + *
  1. Check if arrays are same using == + *
  2. Check if either is null, a null array is less than a non-null + *
  3. Check array length, a shorter length array is less than a longer length array + *
  4. Check array contents element by element using {@link #append(Object, Object, Comparator)} + *
+ * + *

This method will also will be called for the top level of multi-dimensional, + * ragged, and multi-typed arrays.

+ * + * @param lhs array from this object + * @param rhs array from the other object + * @param comparator the comparator to use to compare the objects, + * null means to treat the lhs as Comparable. + * @return CompareToBuilder - used to chain calls. + * @throws ClassCastException if the specified Object's type prevents it + * from being compared to this Object. + */ + public CompareToBuilder append(Object[] lhs, Object[] rhs, Comparator comparator) { if (comparison != 0) { return this; } if (lhs == rhs) { return this; } - if (lhs == null || rhs == null) { - throw new NullPointerException(); + if (lhs == null) { + comparison = -1; + return this; } - - int length = (lhs.length < rhs.length) ? lhs.length : rhs.length; - for (int i = 0; i < length && comparison == 0; ++i) { - Class lhsClass = lhs[i].getClass(); - if (!lhsClass.isInstance(rhs[i])) { - throw new ClassCastException(); - } - append(lhs[i], rhs[i]); + if (rhs == null) { + comparison = +1; + return this; } - if (comparison == 0 && lhs.length != rhs.length) { + if (lhs.length != rhs.length) { comparison = (lhs.length < rhs.length) ? -1 : +1; + return this; + } + for (int i = 0; i < lhs.length && comparison == 0; i++) { + append(lhs[i], rhs[i], comparator); } return this; } /** - *

Deep comparison of array of long Length and all values - * are compared.

+ *

Deep comparison of a long array.

+ *
    + *
  1. Check if arrays are same using == + *
  2. Check if either is null, a null array is less than a non-null + *
  3. Check array length, a shorter length array is less than a longer length array + *
  4. Check array contents element by element using {@link #append(long, long)} + *
* - *

The method {@link #append(long, long)} is used.

- * - * @param lhs - Left Hand Side - * @param rhs - Right Hand Side + * @param lhs array from this object + * @param rhs array from the other object * @return CompareToBuilder - used to chain calls. - * @throws NullPointerException if either (but not both) parameter is - * null */ public CompareToBuilder append(long[] lhs, long[] rhs) { if (comparison != 0) { @@ -463,30 +531,36 @@ public CompareToBuilder append(long[] lhs, long[] rhs) { if (lhs == rhs) { return this; } - if (lhs == null || rhs == null) { - throw new NullPointerException(); + if (lhs == null) { + comparison = -1; + return this; } - int length = (lhs.length < rhs.length) ? lhs.length : rhs.length; - for (int i = 0; i < length && comparison == 0; ++i) { - append(lhs[i], rhs[i]); + if (rhs == null) { + comparison = +1; + return this; } - if (comparison == 0 && lhs.length != rhs.length) { + if (lhs.length != rhs.length) { comparison = (lhs.length < rhs.length) ? -1 : +1; + return this; + } + for (int i = 0; i < lhs.length && comparison == 0; i++) { + append(lhs[i], rhs[i]); } return this; } /** - *

Deep comparison of array of int Length and all values - * are compared.

+ *

Deep comparison of an int array.

+ *
    + *
  1. Check if arrays are same using == + *
  2. Check if either is null, a null array is less than a non-null + *
  3. Check array length, a shorter length array is less than a longer length array + *
  4. Check array contents element by element using {@link #append(int, int)} + *
* - *

The method {@link #append(int, int)} is used.

- * - * @param lhs - Left Hand Side - * @param rhs - Right Hand Side + * @param lhs array from this object + * @param rhs array from the other object * @return CompareToBuilder - used to chain calls. - * @throws NullPointerException if either (but not both) parameter is - * null */ public CompareToBuilder append(int[] lhs, int[] rhs) { if (comparison != 0) { @@ -495,30 +569,36 @@ public CompareToBuilder append(int[] lhs, int[] rhs) { if (lhs == rhs) { return this; } - if (lhs == null || rhs == null) { - throw new NullPointerException(); + if (lhs == null) { + comparison = -1; + return this; } - int length = (lhs.length < rhs.length) ? lhs.length : rhs.length; - for (int i = 0; i < length && comparison == 0; ++i) { - append(lhs[i], rhs[i]); + if (rhs == null) { + comparison = +1; + return this; } - if (comparison == 0 && lhs.length != rhs.length) { + if (lhs.length != rhs.length) { comparison = (lhs.length < rhs.length) ? -1 : +1; + return this; + } + for (int i = 0; i < lhs.length && comparison == 0; i++) { + append(lhs[i], rhs[i]); } return this; } /** - *

Deep comparison of array of short Length and all values - * are compared.

+ *

Deep comparison of a short array.

+ *
    + *
  1. Check if arrays are same using == + *
  2. Check if either is null, a null array is less than a non-null + *
  3. Check array length, a shorter length array is less than a longer length array + *
  4. Check array contents element by element using {@link #append(short, short)} + *
* - *

The method {@link #append(short, short)} is used.

- * - * @param lhs - Left Hand Side - * @param rhs - Right Hand Side + * @param lhs array from this object + * @param rhs array from the other object * @return CompareToBuilder - used to chain calls. - * @throws NullPointerException if either (but not both) parameter is - * null */ public CompareToBuilder append(short[] lhs, short[] rhs) { if (comparison != 0) { @@ -527,30 +607,36 @@ public CompareToBuilder append(short[] lhs, short[] rhs) { if (lhs == rhs) { return this; } - if (lhs == null || rhs == null) { - throw new NullPointerException(); + if (lhs == null) { + comparison = -1; + return this; } - int length = (lhs.length < rhs.length) ? lhs.length : rhs.length; - for (int i = 0; i < length && comparison == 0; ++i) { - append(lhs[i], rhs[i]); + if (rhs == null) { + comparison = +1; + return this; } - if (comparison == 0 && lhs.length != rhs.length) { + if (lhs.length != rhs.length) { comparison = (lhs.length < rhs.length) ? -1 : +1; + return this; + } + for (int i = 0; i < lhs.length && comparison == 0; i++) { + append(lhs[i], rhs[i]); } return this; } /** - *

Deep comparison of array of char Length and all values - * are compared.

+ *

Deep comparison of a char array.

+ *
    + *
  1. Check if arrays are same using == + *
  2. Check if either is null, a null array is less than a non-null + *
  3. Check array length, a shorter length array is less than a longer length array + *
  4. Check array contents element by element using {@link #append(char, char)} + *
* - *

The method {@link #append(char, char)} is used.

- * - * @param lhs - Left Hand Side - * @param rhs - Right Hand Side + * @param lhs array from this object + * @param rhs array from the other object * @return CompareToBuilder - used to chain calls. - * @throws NullPointerException if either (but not both) parameter is - * null */ public CompareToBuilder append(char[] lhs, char[] rhs) { if (comparison != 0) { @@ -559,30 +645,36 @@ public CompareToBuilder append(char[] lhs, char[] rhs) { if (lhs == rhs) { return this; } - if (lhs == null || rhs == null) { - throw new NullPointerException(); + if (lhs == null) { + comparison = -1; + return this; } - int length = (lhs.length < rhs.length) ? lhs.length : rhs.length; - for (int i = 0; i < length && comparison == 0; ++i) { - append(lhs[i], rhs[i]); + if (rhs == null) { + comparison = +1; + return this; } - if (comparison == 0 && lhs.length != rhs.length) { + if (lhs.length != rhs.length) { comparison = (lhs.length < rhs.length) ? -1 : +1; + return this; + } + for (int i = 0; i < lhs.length && comparison == 0; i++) { + append(lhs[i], rhs[i]); } return this; } /** - *

Deep comparison of array of byte Length and all values - * are compared.

+ *

Deep comparison of a byte array.

+ *
    + *
  1. Check if arrays are same using == + *
  2. Check if either is null, a null array is less than a non-null + *
  3. Check array length, a shorter length array is less than a longer length array + *
  4. Check array contents element by element using {@link #append(byte, byte)} + *
* - *

The method {@link #append(byte, byte)} is used.

- * - * @param lhs - Left Hand Side - * @param rhs - Right Hand Side + * @param lhs array from this object + * @param rhs array from the other object * @return CompareToBuilder - used to chain calls. - * @throws NullPointerException if either (but not both) parameter is - * null */ public CompareToBuilder append(byte[] lhs, byte[] rhs) { if (comparison != 0) { @@ -591,30 +683,36 @@ public CompareToBuilder append(byte[] lhs, byte[] rhs) { if (lhs == rhs) { return this; } - if (lhs == null || rhs == null) { - throw new NullPointerException(); + if (lhs == null) { + comparison = -1; + return this; } - int length = (lhs.length < rhs.length) ? lhs.length : rhs.length; - for (int i = 0; i < length && comparison == 0; ++i) { - append(lhs[i], rhs[i]); + if (rhs == null) { + comparison = +1; + return this; } - if (comparison == 0 && lhs.length != rhs.length) { + if (lhs.length != rhs.length) { comparison = (lhs.length < rhs.length) ? -1 : +1; + return this; + } + for (int i = 0; i < lhs.length && comparison == 0; i++) { + append(lhs[i], rhs[i]); } return this; } /** - *

Deep comparison of array of double Length and all values - * are compared.

+ *

Deep comparison of a double array.

+ *
    + *
  1. Check if arrays are same using == + *
  2. Check if either is null, a null array is less than a non-null + *
  3. Check array length, a shorter length array is less than a longer length array + *
  4. Check array contents element by element using {@link #append(double, double)} + *
* - *

The method {@link #append(double, double)} is used.

- * - * @param lhs - Left Hand Side - * @param rhs - Right Hand Side + * @param lhs array from this object + * @param rhs array from the other object * @return CompareToBuilder - used to chain calls. - * @throws NullPointerException if either (but not both) parameter is - * null */ public CompareToBuilder append(double[] lhs, double[] rhs) { if (comparison != 0) { @@ -623,30 +721,36 @@ public CompareToBuilder append(double[] lhs, double[] rhs) { if (lhs == rhs) { return this; } - if (lhs == null || rhs == null) { - throw new NullPointerException(); + if (lhs == null) { + comparison = -1; + return this; } - int length = (lhs.length < rhs.length) ? lhs.length : rhs.length; - for (int i = 0; i < length && comparison == 0; ++i) { - append(lhs[i], rhs[i]); + if (rhs == null) { + comparison = +1; + return this; } - if (comparison == 0 && lhs.length != rhs.length) { + if (lhs.length != rhs.length) { comparison = (lhs.length < rhs.length) ? -1 : +1; + return this; + } + for (int i = 0; i < lhs.length && comparison == 0; i++) { + append(lhs[i], rhs[i]); } return this; } /** - *

Deep comparison of array of float Length and all values - * are compared.

+ *

Deep comparison of a float array.

+ *
    + *
  1. Check if arrays are same using == + *
  2. Check if either is null, a null array is less than a non-null + *
  3. Check array length, a shorter length array is less than a longer length array + *
  4. Check array contents element by element using {@link #append(float, float)} + *
* - *

The method {@link #append(float, float)} is used.

- * - * @param lhs - Left Hand Side - * @param rhs - Right Hand Side + * @param lhs array from this object + * @param rhs array from the other object * @return CompareToBuilder - used to chain calls. - * @throws NullPointerException if either (but not both) parameter is - * null */ public CompareToBuilder append(float[] lhs, float[] rhs) { if (comparison != 0) { @@ -655,30 +759,36 @@ public CompareToBuilder append(float[] lhs, float[] rhs) { if (lhs == rhs) { return this; } - if (lhs == null || rhs == null) { - throw new NullPointerException(); + if (lhs == null) { + comparison = -1; + return this; } - int length = (lhs.length < rhs.length) ? lhs.length : rhs.length; - for (int i = 0; i < length && comparison == 0; ++i) { - append(lhs[i], rhs[i]); + if (rhs == null) { + comparison = +1; + return this; } - if (comparison == 0 && lhs.length != rhs.length) { + if (lhs.length != rhs.length) { comparison = (lhs.length < rhs.length) ? -1 : +1; + return this; + } + for (int i = 0; i < lhs.length && comparison == 0; i++) { + append(lhs[i], rhs[i]); } return this; } /** - *

Deep comparison of array of boolean Length and all values - * are compared.

+ *

Deep comparison of a boolean/code> array.

+ *
    + *
  1. Check if arrays are same using == + *
  2. Check if either is null, a null array is less than a non-null + *
  3. Check array length, a shorter length array is less than a longer length array + *
  4. Check array contents element by element using {@link #append(boolean, boolean)} + *
* - *

The method {@link #append(boolean, boolean)} is used.

- * - * @param lhs - Left Hand Side - * @param rhs - Right Hand Side + * @param lhs array from this object + * @param rhs array from the other object * @return CompareToBuilder - used to chain calls. - * @throws NullPointerException if either (but not both) parameter is - * null */ public CompareToBuilder append(boolean[] lhs, boolean[] rhs) { if (comparison != 0) { @@ -687,15 +797,20 @@ public CompareToBuilder append(boolean[] lhs, boolean[] rhs) { if (lhs == rhs) { return this; } - if (lhs == null || rhs == null) { - throw new NullPointerException(); + if (lhs == null) { + comparison = -1; + return this; } - int length = (lhs.length < rhs.length) ? lhs.length : rhs.length; - for (int i = 0; i < length && comparison == 0; ++i) { - append(lhs[i], rhs[i]); + if (rhs == null) { + comparison = +1; + return this; } - if (comparison == 0 && lhs.length != rhs.length) { + if (lhs.length != rhs.length) { comparison = (lhs.length < rhs.length) ? -1 : +1; + return this; + } + for (int i = 0; i < lhs.length && comparison == 0; i++) { + append(lhs[i], rhs[i]); } return this; } diff --git a/src/test/org/apache/commons/lang/builder/CompareToBuilderTest.java b/src/test/org/apache/commons/lang/builder/CompareToBuilderTest.java index e8287fd81..9b639580d 100644 --- a/src/test/org/apache/commons/lang/builder/CompareToBuilderTest.java +++ b/src/test/org/apache/commons/lang/builder/CompareToBuilderTest.java @@ -64,7 +64,7 @@ * * @author Steve Downey * @author Stephen Colebourne - * @version $Id: CompareToBuilderTest.java,v 1.1 2002/09/15 10:27:06 scolebourne Exp $ + * @version $Id: CompareToBuilderTest.java,v 1.2 2002/12/08 21:37:25 scolebourne Exp $ */ public class CompareToBuilderTest extends TestCase { @@ -145,10 +145,8 @@ public void testReflectionCompareEx2() { Object o2 = new Object(); try { CompareToBuilder.reflectionCompare(o1, o2); - } catch (ClassCastException ex) { - return; - } - fail(); + fail(); + } catch (ClassCastException ex) {} } public void testObject() { @@ -159,27 +157,50 @@ public void testObject() { o2.setA(5); assertTrue(new CompareToBuilder().append(o1, o2).toComparison() < 0); assertTrue(new CompareToBuilder().append(o2, o1).toComparison() > 0); - } - - public void testObjectEx1() { - TestObject o1 = new TestObject(4); - try { - new CompareToBuilder().append(o1, null).toComparison(); - } catch (NullPointerException ex) { - return; - } - fail(); + + assertTrue(new CompareToBuilder().append(o1, null).toComparison() > 0); + assertTrue(new CompareToBuilder().append((Object) null, (Object) null).toComparison() == 0); + assertTrue(new CompareToBuilder().append(null, o1).toComparison() < 0); } public void testObjectEx2() { TestObject o1 = new TestObject(4); Object o2 = new Object(); try { - new CompareToBuilder().append(o1, o2).toComparison(); - } catch (ClassCastException ex) { - return; - } - fail(); + new CompareToBuilder().append(o1, o2); + fail(); + } catch (ClassCastException ex) {} + } + + public void testObjectComparator() { + String o1 = "Fred"; + String o2 = "Fred"; + assertTrue(new CompareToBuilder().append(o1, o1, String.CASE_INSENSITIVE_ORDER).toComparison() == 0); + assertTrue(new CompareToBuilder().append(o1, o2, String.CASE_INSENSITIVE_ORDER).toComparison() == 0); + o2 = "FRED"; + assertTrue(new CompareToBuilder().append(o1, o2, String.CASE_INSENSITIVE_ORDER).toComparison() == 0); + assertTrue(new CompareToBuilder().append(o2, o1, String.CASE_INSENSITIVE_ORDER).toComparison() == 0); + o2 = "FREDA"; + assertTrue(new CompareToBuilder().append(o1, o2, String.CASE_INSENSITIVE_ORDER).toComparison() < 0); + assertTrue(new CompareToBuilder().append(o2, o1, String.CASE_INSENSITIVE_ORDER).toComparison() > 0); + + assertTrue(new CompareToBuilder().append(o1, null, String.CASE_INSENSITIVE_ORDER).toComparison() > 0); + assertTrue(new CompareToBuilder().append((Object) null, (Object) null, String.CASE_INSENSITIVE_ORDER).toComparison() == 0); + assertTrue(new CompareToBuilder().append(null, o1, String.CASE_INSENSITIVE_ORDER).toComparison() < 0); + } + + public void testObjectComparatorNull() { + String o1 = "Fred"; + String o2 = "Fred"; + assertTrue(new CompareToBuilder().append(o1, o1, null).toComparison() == 0); + assertTrue(new CompareToBuilder().append(o1, o2, null).toComparison() == 0); + o2 = "Zebra"; + assertTrue(new CompareToBuilder().append(o1, o2, null).toComparison() < 0); + assertTrue(new CompareToBuilder().append(o2, o1, null).toComparison() > 0); + + assertTrue(new CompareToBuilder().append(o1, null, null).toComparison() > 0); + assertTrue(new CompareToBuilder().append((Object) null, (Object) null, null).toComparison() == 0); + assertTrue(new CompareToBuilder().append(null, o1, null).toComparison() < 0); } public void testLong() { @@ -307,22 +328,14 @@ public void testObjectArray() { assertTrue(new CompareToBuilder().append(obj1, obj2).toComparison() == 0); assertTrue(new CompareToBuilder().append(obj1, obj3).toComparison() < 0); assertTrue(new CompareToBuilder().append(obj3, obj1).toComparison() > 0); - + obj1[1] = new TestObject(7); assertTrue(new CompareToBuilder().append(obj1, obj2).toComparison() > 0); assertTrue(new CompareToBuilder().append(obj2, obj1).toComparison() < 0); - } - public void testObjectArrayEx1() { - TestObject[] obj1 = new TestObject[2]; - obj1[0] = new TestObject(4); - obj1[1] = new TestObject(5); - try { - new CompareToBuilder().append(obj1, null).toComparison(); - } catch (NullPointerException ex) { - return; - } - fail(); + assertTrue(new CompareToBuilder().append(obj1, null).toComparison() > 0); + assertTrue(new CompareToBuilder().append((Object[]) null, (Object[]) null).toComparison() == 0); + assertTrue(new CompareToBuilder().append(null, obj1).toComparison() < 0); } public void testLongArray() { @@ -345,6 +358,10 @@ public void testLongArray() { obj1[1] = 7; assertTrue(new CompareToBuilder().append(obj1, obj2).toComparison() > 0); assertTrue(new CompareToBuilder().append(obj2, obj1).toComparison() < 0); + + assertTrue(new CompareToBuilder().append(obj1, null).toComparison() > 0); + assertTrue(new CompareToBuilder().append((long[]) null, (long[]) null).toComparison() == 0); + assertTrue(new CompareToBuilder().append(null, obj1).toComparison() < 0); } public void testIntArray() { @@ -367,6 +384,10 @@ public void testIntArray() { obj1[1] = 7; assertTrue(new CompareToBuilder().append(obj1, obj2).toComparison() > 0); assertTrue(new CompareToBuilder().append(obj2, obj1).toComparison() < 0); + + assertTrue(new CompareToBuilder().append(obj1, null).toComparison() > 0); + assertTrue(new CompareToBuilder().append((int[]) null, (int[]) null).toComparison() == 0); + assertTrue(new CompareToBuilder().append(null, obj1).toComparison() < 0); } public void testShortArray() { @@ -389,6 +410,10 @@ public void testShortArray() { obj1[1] = 7; assertTrue(new CompareToBuilder().append(obj1, obj2).toComparison() > 0); assertTrue(new CompareToBuilder().append(obj2, obj1).toComparison() < 0); + + assertTrue(new CompareToBuilder().append(obj1, null).toComparison() > 0); + assertTrue(new CompareToBuilder().append((short[]) null, (short[]) null).toComparison() == 0); + assertTrue(new CompareToBuilder().append(null, obj1).toComparison() < 0); } public void testCharArray() { @@ -411,6 +436,10 @@ public void testCharArray() { obj1[1] = 7; assertTrue(new CompareToBuilder().append(obj1, obj2).toComparison() > 0); assertTrue(new CompareToBuilder().append(obj2, obj1).toComparison() < 0); + + assertTrue(new CompareToBuilder().append(obj1, null).toComparison() > 0); + assertTrue(new CompareToBuilder().append((char[]) null, (char[]) null).toComparison() == 0); + assertTrue(new CompareToBuilder().append(null, obj1).toComparison() < 0); } public void testByteArray() { @@ -433,6 +462,10 @@ public void testByteArray() { obj1[1] = 7; assertTrue(new CompareToBuilder().append(obj1, obj2).toComparison() > 0); assertTrue(new CompareToBuilder().append(obj2, obj1).toComparison() < 0); + + assertTrue(new CompareToBuilder().append(obj1, null).toComparison() > 0); + assertTrue(new CompareToBuilder().append((byte[]) null, (byte[]) null).toComparison() == 0); + assertTrue(new CompareToBuilder().append(null, obj1).toComparison() < 0); } public void testDoubleArray() { @@ -455,6 +488,10 @@ public void testDoubleArray() { obj1[1] = 7; assertTrue(new CompareToBuilder().append(obj1, obj2).toComparison() > 0); assertTrue(new CompareToBuilder().append(obj2, obj1).toComparison() < 0); + + assertTrue(new CompareToBuilder().append(obj1, null).toComparison() > 0); + assertTrue(new CompareToBuilder().append((double[]) null, (double[]) null).toComparison() == 0); + assertTrue(new CompareToBuilder().append(null, obj1).toComparison() < 0); } public void testFloatArray() { @@ -477,6 +514,10 @@ public void testFloatArray() { obj1[1] = 7; assertTrue(new CompareToBuilder().append(obj1, obj2).toComparison() > 0); assertTrue(new CompareToBuilder().append(obj2, obj1).toComparison() < 0); + + assertTrue(new CompareToBuilder().append(obj1, null).toComparison() > 0); + assertTrue(new CompareToBuilder().append((float[]) null, (float[]) null).toComparison() == 0); + assertTrue(new CompareToBuilder().append(null, obj1).toComparison() < 0); } public void testBooleanArray() { @@ -499,6 +540,10 @@ public void testBooleanArray() { obj1[1] = true; assertTrue(new CompareToBuilder().append(obj1, obj2).toComparison() > 0); assertTrue(new CompareToBuilder().append(obj2, obj1).toComparison() < 0); + + assertTrue(new CompareToBuilder().append(obj1, null).toComparison() > 0); + assertTrue(new CompareToBuilder().append((boolean[]) null, (boolean[]) null).toComparison() == 0); + assertTrue(new CompareToBuilder().append(null, obj1).toComparison() < 0); } public void testMultiLongArray() { @@ -760,7 +805,8 @@ public void testObjectArrayHiddenByObject() { array1[1] = new TestObject(7); assertTrue(new CompareToBuilder().append(obj1, obj2).toComparison() > 0); - assertTrue(new CompareToBuilder().append(obj2, obj1).toComparison() < 0); } + assertTrue(new CompareToBuilder().append(obj2, obj1).toComparison() < 0); + } public void testLongArrayHiddenByObject() { long[] array1 = new long[2];