| java.lang.Object org.apache.commons.lang.builder.CompareToBuilder
CompareToBuilder | public class CompareToBuilder (Code) | | 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
CompareToBuilder.reflectionCompare(Object,Object) 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);
}
See Also: java.lang.Comparable See Also: java.lang.Object.equals(Object) See Also: java.lang.Object.hashCode See Also: EqualsBuilder See Also: HashCodeBuilder author: Steve Downey author: Stephen Colebourne author: Gary Gregory author: Pete Gieser since: 1.0 version: $Id: CompareToBuilder.java 447139 2006-09-17 20:36:53Z bayard $ |
Constructor Summary | |
public | CompareToBuilder() Constructor for CompareToBuilder.
Starts off assuming that the objects are equal. |
Method Summary | |
public CompareToBuilder | append(Object lhs, Object rhs) | public CompareToBuilder | append(Object lhs, Object rhs, Comparator comparator) Appends to the builder the comparison of
two Object s.
- Check if
lhs == rhs
- Check if either
lhs or rhs is null ,
a null object is less than a non-null object
- Check the object contents
If lhs is an array, array comparison methods will be used. | public CompareToBuilder | append(long lhs, long rhs) Appends to the builder the comparison of
two long s. | public CompareToBuilder | append(int lhs, int rhs) Appends to the builder the comparison of
two int s. | public CompareToBuilder | append(short lhs, short rhs) Appends to the builder the comparison of
two short s. | public CompareToBuilder | append(char lhs, char rhs) Appends to the builder the comparison of
two char s. | public CompareToBuilder | append(byte lhs, byte rhs) Appends to the builder the comparison of
two byte s. | public CompareToBuilder | append(double lhs, double rhs) | public CompareToBuilder | append(float lhs, float rhs) | public CompareToBuilder | append(boolean lhs, boolean rhs) Appends to the builder the comparison of
two booleans s. | public CompareToBuilder | append(Object[] lhs, Object[] rhs) | public CompareToBuilder | append(Object[] lhs, Object[] rhs, Comparator comparator) Appends to the builder the deep comparison of
two Object arrays.
- Check if arrays are the same using
==
- Check if for
null , null is less than non-null
- Check array length, a short length array is less than a long length array
- Check array contents element by element using
CompareToBuilder.append(Object,Object,Comparator)
This method will also will be called for the top level of multi-dimensional,
ragged, and multi-typed arrays.
Parameters: lhs - left-hand array Parameters: rhs - right-hand array Parameters: comparator - Comparator to use to compare the array elements,null means to treat lhs elements as Comparable . | public CompareToBuilder | append(long[] lhs, long[] rhs) | public CompareToBuilder | append(int[] lhs, int[] rhs) | public CompareToBuilder | append(short[] lhs, short[] rhs) | public CompareToBuilder | append(char[] lhs, char[] rhs) | public CompareToBuilder | append(byte[] lhs, byte[] rhs) | public CompareToBuilder | append(double[] lhs, double[] rhs) | public CompareToBuilder | append(float[] lhs, float[] rhs) | public CompareToBuilder | append(boolean[] lhs, boolean[] rhs) | public CompareToBuilder | appendSuper(int superCompareTo) | public static int | reflectionCompare(Object lhs, Object rhs) Compares two Object s via reflection.
Fields can be private, thus AccessibleObject.setAccessible
is used to bypass normal access control checks. | public static int | reflectionCompare(Object lhs, Object rhs, boolean compareTransients) Compares two Object s via reflection.
Fields can be private, thus AccessibleObject.setAccessible
is used to bypass normal access control checks. | public static int | reflectionCompare(Object lhs, Object rhs, Collection excludeFields) Compares two Object s via reflection.
Fields can be private, thus AccessibleObject.setAccessible
is used to bypass normal access control checks. | public static int | reflectionCompare(Object lhs, Object rhs, String[] excludeFields) Compares two Object s via reflection.
Fields can be private, thus AccessibleObject.setAccessible
is used to bypass normal access control checks. | public static int | reflectionCompare(Object lhs, Object rhs, boolean compareTransients, Class reflectUpToClass) Compares two Object s via reflection.
Fields can be private, thus AccessibleObject.setAccessible
is used to bypass normal access control checks. | public static int | reflectionCompare(Object lhs, Object rhs, boolean compareTransients, Class reflectUpToClass, String[] excludeFields) Compares two Object s via reflection.
Fields can be private, thus AccessibleObject.setAccessible
is used to bypass normal access control checks. | 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. |
CompareToBuilder | public CompareToBuilder()(Code) | | Constructor for CompareToBuilder.
Starts off assuming that the objects are equal. Multiple calls are
then made to the various append methods, followed by a call to
CompareToBuilder.toComparison to get the result.
|
append | public CompareToBuilder append(Object lhs, Object rhs)(Code) | | Appends to the builder the comparison of
two Object s.
- Check if
lhs == rhs
- Check if either
lhs or rhs is null ,
a null object is less than a non-null object
- Check the object contents
lhs must either be an array or implement
Comparable .
Parameters: lhs - left-hand object Parameters: rhs - right-hand object this - used to chain append calls throws: ClassCastException - if rhs is not assignment-compatiblewith lhs |
append | public CompareToBuilder append(Object lhs, Object rhs, Comparator comparator)(Code) | | Appends to the builder the comparison of
two Object s.
- Check if
lhs == rhs
- Check if either
lhs or rhs is null ,
a null object is less than a non-null object
- 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.
Parameters: lhs - left-hand object Parameters: rhs - right-hand object Parameters: comparator - Comparator used to compare the objects,null means treat lhs as Comparable this - used to chain append calls throws: ClassCastException - if rhs is not assignment-compatiblewith lhs since: 2.0 |
append | public CompareToBuilder append(long lhs, long rhs)(Code) | | Appends to the builder the comparison of
two long s.
Parameters: lhs - left-hand value Parameters: rhs - right-hand value this - used to chain append calls |
append | public CompareToBuilder append(int lhs, int rhs)(Code) | | Appends to the builder the comparison of
two int s.
Parameters: lhs - left-hand value Parameters: rhs - right-hand value this - used to chain append calls |
append | public CompareToBuilder append(short lhs, short rhs)(Code) | | Appends to the builder the comparison of
two short s.
Parameters: lhs - left-hand value Parameters: rhs - right-hand value this - used to chain append calls |
append | public CompareToBuilder append(char lhs, char rhs)(Code) | | Appends to the builder the comparison of
two char s.
Parameters: lhs - left-hand value Parameters: rhs - right-hand value this - used to chain append calls |
append | public CompareToBuilder append(byte lhs, byte rhs)(Code) | | Appends to the builder the comparison of
two byte s.
Parameters: lhs - left-hand value Parameters: rhs - right-hand value this - used to chain append calls |
append | public CompareToBuilder append(double lhs, double rhs)(Code) | | Appends to the builder the comparison of
two double s.
This handles NaNs, Infinities, and -0.0 .
It is compatible with the hash code generated by
HashCodeBuilder .
Parameters: lhs - left-hand value Parameters: rhs - right-hand value this - used to chain append calls |
append | public CompareToBuilder append(float lhs, float rhs)(Code) | | Appends to the builder the comparison of
two float s.
This handles NaNs, Infinities, and -0.0 .
It is compatible with the hash code generated by
HashCodeBuilder .
Parameters: lhs - left-hand value Parameters: rhs - right-hand value this - used to chain append calls |
append | public CompareToBuilder append(boolean lhs, boolean rhs)(Code) | | Appends to the builder the comparison of
two booleans s.
Parameters: lhs - left-hand value Parameters: rhs - right-hand value this - used to chain append calls |
append | public CompareToBuilder append(Object[] lhs, Object[] rhs)(Code) | | Appends to the builder the deep comparison of
two Object arrays.
- Check if arrays are the same using
==
- Check if for
null , null is less than non-null
- Check array length, a short length array is less than a long length array
- Check array contents element by element using
CompareToBuilder.append(Object,Object,Comparator)
This method will also will be called for the top level of multi-dimensional,
ragged, and multi-typed arrays.
Parameters: lhs - left-hand array Parameters: rhs - right-hand array this - used to chain append calls throws: ClassCastException - if rhs is not assignment-compatiblewith lhs |
append | public CompareToBuilder append(Object[] lhs, Object[] rhs, Comparator comparator)(Code) | | Appends to the builder the deep comparison of
two Object arrays.
- Check if arrays are the same using
==
- Check if for
null , null is less than non-null
- Check array length, a short length array is less than a long length array
- Check array contents element by element using
CompareToBuilder.append(Object,Object,Comparator)
This method will also will be called for the top level of multi-dimensional,
ragged, and multi-typed arrays.
Parameters: lhs - left-hand array Parameters: rhs - right-hand array Parameters: comparator - Comparator to use to compare the array elements,null means to treat lhs elements as Comparable . this - used to chain append calls throws: ClassCastException - if rhs is not assignment-compatiblewith lhs since: 2.0 |
append | public CompareToBuilder append(long[] lhs, long[] rhs)(Code) | | Appends to the builder the deep comparison of
two long arrays.
- Check if arrays are the same using
==
- Check if for
null , null is less than non-null
- Check array length, a shorter length array is less than a longer length array
- Check array contents element by element using
CompareToBuilder.append(long,long)
Parameters: lhs - left-hand array Parameters: rhs - right-hand array this - used to chain append calls |
append | public CompareToBuilder append(int[] lhs, int[] rhs)(Code) | | Appends to the builder the deep comparison of
two int arrays.
- Check if arrays are the same using
==
- Check if for
null , null is less than non-null
- Check array length, a shorter length array is less than a longer length array
- Check array contents element by element using
CompareToBuilder.append(int,int)
Parameters: lhs - left-hand array Parameters: rhs - right-hand array this - used to chain append calls |
append | public CompareToBuilder append(short[] lhs, short[] rhs)(Code) | | Appends to the builder the deep comparison of
two short arrays.
- Check if arrays are the same using
==
- Check if for
null , null is less than non-null
- Check array length, a shorter length array is less than a longer length array
- Check array contents element by element using
CompareToBuilder.append(short,short)
Parameters: lhs - left-hand array Parameters: rhs - right-hand array this - used to chain append calls |
append | public CompareToBuilder append(char[] lhs, char[] rhs)(Code) | | Appends to the builder the deep comparison of
two char arrays.
- Check if arrays are the same using
==
- Check if for
null , null is less than non-null
- Check array length, a shorter length array is less than a longer length array
- Check array contents element by element using
CompareToBuilder.append(char,char)
Parameters: lhs - left-hand array Parameters: rhs - right-hand array this - used to chain append calls |
append | public CompareToBuilder append(byte[] lhs, byte[] rhs)(Code) | | Appends to the builder the deep comparison of
two byte arrays.
- Check if arrays are the same using
==
- Check if for
null , null is less than non-null
- Check array length, a shorter length array is less than a longer length array
- Check array contents element by element using
CompareToBuilder.append(byte,byte)
Parameters: lhs - left-hand array Parameters: rhs - right-hand array this - used to chain append calls |
append | public CompareToBuilder append(double[] lhs, double[] rhs)(Code) | | Appends to the builder the deep comparison of
two double arrays.
- Check if arrays are the same using
==
- Check if for
null , null is less than non-null
- Check array length, a shorter length array is less than a longer length array
- Check array contents element by element using
CompareToBuilder.append(double,double)
Parameters: lhs - left-hand array Parameters: rhs - right-hand array this - used to chain append calls |
append | public CompareToBuilder append(float[] lhs, float[] rhs)(Code) | | Appends to the builder the deep comparison of
two float arrays.
- Check if arrays are the same using
==
- Check if for
null , null is less than non-null
- Check array length, a shorter length array is less than a longer length array
- Check array contents element by element using
CompareToBuilder.append(float,float)
Parameters: lhs - left-hand array Parameters: rhs - right-hand array this - used to chain append calls |
append | public CompareToBuilder append(boolean[] lhs, boolean[] rhs)(Code) | | Appends to the builder the deep comparison of
two boolean arrays.
- Check if arrays are the same using
==
- Check if for
null , null is less than non-null
- Check array length, a shorter length array is less than a longer length array
- Check array contents element by element using
CompareToBuilder.append(boolean,boolean)
Parameters: lhs - left-hand array Parameters: rhs - right-hand array this - used to chain append calls |
appendSuper | public CompareToBuilder appendSuper(int superCompareTo)(Code) | | Appends to the builder the compareTo(Object)
result of the superclass.
Parameters: superCompareTo - result of calling super.compareTo(Object) this - used to chain append calls since: 2.0 |
reflectionCompare | public static int reflectionCompare(Object lhs, Object rhs)(Code) | | Compares two Object s 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.
Parameters: lhs - left-hand object Parameters: rhs - right-hand object a negative integer, zero, or a positive integer as lhs is less than, equal to, or greater than rhs throws: NullPointerException - if either (but not both) parameters arenull throws: ClassCastException - if rhs is not assignment-compatiblewith lhs |
reflectionCompare | public static int reflectionCompare(Object lhs, Object rhs, boolean compareTransients)(Code) | | Compares two Object s 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.
Parameters: lhs - left-hand object Parameters: rhs - right-hand object Parameters: compareTransients - whether to compare transient fields a negative integer, zero, or a positive integer as lhs is less than, equal to, or greater than rhs throws: NullPointerException - if either lhs or rhs (but not both) is null throws: ClassCastException - if rhs is not assignment-compatiblewith lhs |
reflectionCompare | public static int reflectionCompare(Object lhs, Object rhs, Collection excludeFields)(Code) | | Compares two Object s 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.
Parameters: lhs - left-hand object Parameters: rhs - right-hand object Parameters: excludeFields - Collection of String fields to exclude a negative integer, zero, or a positive integer as lhs is less than, equal to, or greater than rhs throws: NullPointerException - if either lhs or rhs (but not both) is null throws: ClassCastException - if rhs is not assignment-compatiblewith lhs since: 2.2 |
reflectionCompare | public static int reflectionCompare(Object lhs, Object rhs, String[] excludeFields)(Code) | | Compares two Object s 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.
Parameters: lhs - left-hand object Parameters: rhs - right-hand object Parameters: excludeFields - array of fields to exclude a negative integer, zero, or a positive integer as lhs is less than, equal to, or greater than rhs throws: NullPointerException - if either lhs or rhs (but not both) is null throws: ClassCastException - if rhs is not assignment-compatiblewith lhs since: 2.2 |
reflectionCompare | public static int reflectionCompare(Object lhs, Object rhs, boolean compareTransients, Class reflectUpToClass)(Code) | | Compares two Object s 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.
Parameters: lhs - left-hand object Parameters: rhs - right-hand object Parameters: compareTransients - whether to compare transient fields Parameters: reflectUpToClass - last superclass for which fields are compared a negative integer, zero, or a positive integer as lhs is less than, equal to, or greater than rhs throws: NullPointerException - if either lhs or rhs (but not both) is null throws: ClassCastException - if rhs is not assignment-compatiblewith lhs since: 2.0 |
reflectionCompare | public static int reflectionCompare(Object lhs, Object rhs, boolean compareTransients, Class reflectUpToClass, String[] excludeFields)(Code) | | Compares two Object s 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.
Parameters: lhs - left-hand object Parameters: rhs - right-hand object Parameters: compareTransients - whether to compare transient fields Parameters: reflectUpToClass - last superclass for which fields are compared Parameters: excludeFields - fields to exclude a negative integer, zero, or a positive integer as lhs is less than, equal to, or greater than rhs throws: NullPointerException - if either lhs or rhs (but not both) is null throws: ClassCastException - if rhs is not assignment-compatiblewith lhs since: 2.2 |
toComparison | public int toComparison()(Code) | | 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.
final comparison result |
|
|