Save This Page
Home » commons-lang-2.4-src » org.apache.commons » lang » builder » [javadoc | source]
org.apache.commons.lang.builder
public class: CompareToBuilder [javadoc | source]
java.lang.Object
   org.apache.commons.lang.builder.CompareToBuilder
Assists in implementing java.lang.Comparable#compareTo(Object) methods. It is consistent with equals(Object) and hashcode() built with EqualsBuilder and HashCodeBuilder .

Two Objects that compare equal using equals(Object) should normally also compare equal using compareTo(Object).

All relevant fields should be included in the calculation of the comparison. Derived fields may be ignored. The same fields, in the same order, should be used in both compareTo(Object) and equals(Object).

To use this class write code as follows:

public class MyClass {
String field1;
int field2;
boolean field3;

...

public int compareTo(Object o) {
MyClass myClass = (MyClass) o;
return new CompareToBuilder()
.appendSuper(super.compareTo(o)
.append(this.field1, myClass.field1)
.append(this.field2, myClass.field2)
.append(this.field3, myClass.field3)
.toComparison();
}
}

Alternatively, there are reflectionCompare methods that use reflection to determine the fields to append. Because fields can be private, reflectionCompare uses java.lang.reflect.AccessibleObject#setAccessible(boolean) to bypass normal access control checks. This will fail under a security manager, unless the appropriate permissions are set up correctly. It is also slower than appending explicitly.

A typical implementation of compareTo(Object) using reflectionCompare looks like:

public int compareTo(Object o) {
return CompareToBuilder.reflectionCompare(this, o);
}
Constructor:
 public CompareToBuilder() 
Method from org.apache.commons.lang.builder.CompareToBuilder Summary:
append,   append,   append,   append,   append,   append,   append,   append,   append,   append,   append,   append,   append,   append,   append,   append,   append,   append,   append,   append,   appendSuper,   reflectionCompare,   reflectionCompare,   reflectionCompare,   reflectionCompare,   reflectionCompare,   reflectionCompare,   toComparison
Methods from java.lang.Object:
equals,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from org.apache.commons.lang.builder.CompareToBuilder Detail:
 public CompareToBuilder append(Object lhs,
    Object rhs) 

    Appends to the builder the comparison of two Objects.

    1. Check if lhs == rhs
    2. Check if either lhs or rhs is null, a null object is less than a non-null object
    3. Check the object contents

    lhs must either be an array or implement Comparable .

 public CompareToBuilder append(long lhs,
    long rhs) 
    Appends to the builder the comparison of two longs.
 public CompareToBuilder append(int lhs,
    int rhs) 
    Appends to the builder the comparison of two ints.
 public CompareToBuilder append(short lhs,
    short rhs) 
    Appends to the builder the comparison of two shorts.
 public CompareToBuilder append(char lhs,
    char rhs) 
    Appends to the builder the comparison of two chars.
 public CompareToBuilder append(byte lhs,
    byte rhs) 
    Appends to the builder the comparison of two bytes.
 public CompareToBuilder append(double lhs,
    double rhs) 

    Appends to the builder the comparison of two doubles.

    This handles NaNs, Infinities, and -0.0.

    It is compatible with the hash code generated by HashCodeBuilder.

 public CompareToBuilder append(float lhs,
    float rhs) 

    Appends to the builder the comparison of two floats.

    This handles NaNs, Infinities, and -0.0.

    It is compatible with the hash code generated by HashCodeBuilder.

 public CompareToBuilder append(boolean lhs,
    boolean rhs) 
    Appends to the builder the comparison of two booleanss.
 public CompareToBuilder append(Object[] lhs,
    Object[] rhs) 

    Appends to the builder the deep comparison of two Object arrays.

    1. Check if arrays are the same using ==
    2. Check if for null, null is less than 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 #append(Object, Object, Comparator)

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

 public CompareToBuilder append(long[] lhs,
    long[] rhs) 

    Appends to the builder the deep comparison of two long arrays.

    1. Check if arrays are the same using ==
    2. Check if for null, null is less than 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 #append(long, long)
 public CompareToBuilder append(int[] lhs,
    int[] rhs) 

    Appends to the builder the deep comparison of two int arrays.

    1. Check if arrays are the same using ==
    2. Check if for null, null is less than 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 #append(int, int)
 public CompareToBuilder append(short[] lhs,
    short[] rhs) 

    Appends to the builder the deep comparison of two short arrays.

    1. Check if arrays are the same using ==
    2. Check if for null, null is less than 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 #append(short, short)
 public CompareToBuilder append(char[] lhs,
    char[] rhs) 

    Appends to the builder the deep comparison of two char arrays.

    1. Check if arrays are the same using ==
    2. Check if for null, null is less than 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 #append(char, char)
 public CompareToBuilder append(byte[] lhs,
    byte[] rhs) 

    Appends to the builder the deep comparison of two byte arrays.

    1. Check if arrays are the same using ==
    2. Check if for null, null is less than 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 #append(byte, byte)
 public CompareToBuilder append(double[] lhs,
    double[] rhs) 

    Appends to the builder the deep comparison of two double arrays.

    1. Check if arrays are the same using ==
    2. Check if for null, null is less than 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 #append(double, double)
 public CompareToBuilder append(float[] lhs,
    float[] rhs) 

    Appends to the builder the deep comparison of two float arrays.

    1. Check if arrays are the same using ==
    2. Check if for null, null is less than 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 #append(float, float)
 public CompareToBuilder append(boolean[] lhs,
    boolean[] rhs) 

    Appends to the builder the deep comparison of two boolean arrays.

    1. Check if arrays are the same using ==
    2. Check if for null, null is less than 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 #append(boolean, boolean)
 public CompareToBuilder append(Object lhs,
    Object rhs,
    Comparator comparator) 

    Appends to the builder the comparison of two Objects.

    1. Check if lhs == rhs
    2. Check if either lhs or rhs is null, a null object is less than a non-null object
    3. Check the object contents

    If lhs is an array, array comparison methods will be used. Otherwise comparator will be used to compare the objects. If comparator is null, lhs must implement Comparable instead.

 public CompareToBuilder append(Object[] lhs,
    Object[] rhs,
    Comparator comparator) 

    Appends to the builder the deep comparison of two Object arrays.

    1. Check if arrays are the same using ==
    2. Check if for null, null is less than 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 #append(Object, Object, Comparator)

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

 public CompareToBuilder appendSuper(int superCompareTo) 

    Appends to the builder the compareTo(Object) result of the superclass.

 public static int reflectionCompare(Object lhs,
    Object rhs) 

    Compares two Objects via reflection.

    Fields can be private, thus AccessibleObject.setAccessible is used to bypass normal access control checks. This will fail under a security manager unless the appropriate permissions are set.

    • Static fields will not be compared
    • Transient members will be not be compared, as they are likely derived fields
    • Superclass fields will be compared

    If both lhs and rhs are null, they are considered equal.

 public static int reflectionCompare(Object lhs,
    Object rhs,
    boolean compareTransients) 

    Compares two Objects via reflection.

    Fields can be private, thus AccessibleObject.setAccessible is used to bypass normal access control checks. This will fail under a security manager unless the appropriate permissions are set.

    • Static fields will not be compared
    • If compareTransients is true, compares transient members. Otherwise ignores them, as they are likely derived fields.
    • Superclass fields will be compared

    If both lhs and rhs are null, they are considered equal.

 public static int reflectionCompare(Object lhs,
    Object rhs,
    Collection excludeFields) 

    Compares two Objects via reflection.

    Fields can be private, thus AccessibleObject.setAccessible is used to bypass normal access control checks. This will fail under a security manager unless the appropriate permissions are set.

    • Static fields will not be compared
    • If compareTransients is true, compares transient members. Otherwise ignores them, as they are likely derived fields.
    • Superclass fields will be compared

    If both lhs and rhs are null, they are considered equal.

 public static int reflectionCompare(Object lhs,
    Object rhs,
    String[] excludeFields) 

    Compares two Objects via reflection.

    Fields can be private, thus AccessibleObject.setAccessible is used to bypass normal access control checks. This will fail under a security manager unless the appropriate permissions are set.

    • Static fields will not be compared
    • If compareTransients is true, compares transient members. Otherwise ignores them, as they are likely derived fields.
    • Superclass fields will be compared

    If both lhs and rhs are null, they are considered equal.

 public static int reflectionCompare(Object lhs,
    Object rhs,
    boolean compareTransients,
    Class reflectUpToClass) 

    Compares two Objects via reflection.

    Fields can be private, thus AccessibleObject.setAccessible is used to bypass normal access control checks. This will fail under a security manager unless the appropriate permissions are set.

    • Static fields will not be compared
    • If the compareTransients is true, compares transient members. Otherwise ignores them, as they are likely derived fields.
    • Compares superclass fields up to and including reflectUpToClass. If reflectUpToClass is null, compares all superclass fields.

    If both lhs and rhs are null, they are considered equal.

 public static int reflectionCompare(Object lhs,
    Object rhs,
    boolean compareTransients,
    Class reflectUpToClass,
    String[] excludeFields) 

    Compares two Objects via reflection.

    Fields can be private, thus AccessibleObject.setAccessible is used to bypass normal access control checks. This will fail under a security manager unless the appropriate permissions are set.

    • Static fields will not be compared
    • If the compareTransients is true, compares transient members. Otherwise ignores them, as they are likely derived fields.
    • Compares superclass fields up to and including reflectUpToClass. If reflectUpToClass is null, compares all superclass fields.

    If both lhs and rhs are null, they are considered equal.

 public int toComparison() 
    Returns a negative integer, a positive integer, or zero as the builder has judged the "left-hand" side as less than, greater than, or equal to the "right-hand" side.