|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--org.apache.commons.lang.builder.CompareToBuilder
CompareTo
generation routines.
This class provides methods to build a good equals
and hashcode
built
with EqualsBuilder and HashCodeBuilder.
Two object that compare equal using equals should 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 order, should be used in
both compareTo
and equals
.
Typical use for the code is as follows:
public int comapareTo(Object o) { MyClass rhs = (MyClass) o; return new CompareToBuilder() .append(field1, rhs.field1) .append(field2, rhs.field2) .appendb(field3, rhs.field3) .toComparison(); }
Alternatively, there is a method that uses reflection to determine
the fields to test. Because these fields are usually private, the method,
reflectionCompare
, uses Field.setAccessible
to change
the visibility of the fields. This will fail under a security manager,
unless the appropriate permissions are set. It is also slower than testing
explicitly.
A typical invocation for this method would look like:
public int compareTo(Object o) { return CompareToBuilder.reflectionCompare(this, obj); }
Field Summary | |
private int |
comparison
If the fields tested are equals. |
Constructor Summary | |
CompareToBuilder()
Constructor for CompareToBuilder. |
Method Summary | |
CompareToBuilder |
append(boolean[] lhs,
boolean[] rhs)
Deep comparison of array of boolean Length and all values
are compared. |
CompareToBuilder |
append(boolean lhs,
boolean rhs)
Test if two booleans s are <, > or ==. |
CompareToBuilder |
append(byte[] lhs,
byte[] rhs)
Deep comparison of array of byte Length and all values
are compared. |
CompareToBuilder |
append(byte lhs,
byte rhs)
Test if two byte s are <, > or ==. |
CompareToBuilder |
append(char[] lhs,
char[] rhs)
Deep comparison of array of char Length and all values
are compared. |
CompareToBuilder |
append(char lhs,
char rhs)
Test if two char s are <, > or ==. |
CompareToBuilder |
append(double[] lhs,
double[] rhs)
Deep comparison of array of double Length and all values
are compared. |
CompareToBuilder |
append(double lhs,
double rhs)
Test if two double s are <, > or ==. |
CompareToBuilder |
append(float[] lhs,
float[] rhs)
Deep comparison of array of float Length and all values
are compared. |
CompareToBuilder |
append(float lhs,
float rhs)
Test if two double s are <, > or ==. |
CompareToBuilder |
append(int[] lhs,
int[] rhs)
Deep comparison of array of int Length and all values
are compared. |
CompareToBuilder |
append(int lhs,
int rhs)
Test if two int s are <, > or ==. |
CompareToBuilder |
append(long[] lhs,
long[] rhs)
Deep comparison of array of long Length and all values
are compared. |
CompareToBuilder |
append(long lhs,
long rhs)
Test if two long s are <, > or ==. |
CompareToBuilder |
append(java.lang.Object[] lhs,
java.lang.Object[] rhs)
Performs a deep comparison of two object arrays. |
CompareToBuilder |
append(java.lang.Object lhs,
java.lang.Object rhs)
Test if two Object s are equal using either the
compareTo method, or native comparison if the Objects are
actually arrays. |
CompareToBuilder |
append(short[] lhs,
short[] rhs)
Deep comparison of array of short Length and all values
are compared. |
CompareToBuilder |
append(short lhs,
short rhs)
Test if two short s are <, > or ==. |
static int |
reflectionCompare(java.lang.Object lhs,
java.lang.Object rhs)
This method uses reflection to determine the ordering between two objects. |
static int |
reflectionCompare(java.lang.Object lhs,
java.lang.Object rhs,
boolean testTransients)
This method uses reflection to determine if the two object are equal. |
int |
toComparison()
Return a negative integer if the object is less than, a positive integer if the object is greater than, or 0 if the object is equal. |
Methods inherited from class java.lang.Object |
|
Field Detail |
private int comparison
Constructor Detail |
public CompareToBuilder()
Object.Object()
Method Detail |
public static int reflectionCompare(java.lang.Object lhs, java.lang.Object rhs)
It uses Field.setAccessible to gain access to private fields. This means that it will throw a security exception if run under a security manger, if the permissions are not set up. It is also not as efficient as testing explicitly. Transient members will be not be tested, as they are likely derived fields, and not part of the value of the object. Static fields will not be tested.
lhs
- Left Hand Siderhs
- Right Hand SideNullPointerException
- if either (but not both) parameter is nullClassCastException
- if the specified object's type prevents it
from being compared to this Object.public static int reflectionCompare(java.lang.Object lhs, java.lang.Object rhs, boolean testTransients)
It uses Field.setAccessible to gain access to private fields. This means that it will throw a security exception if run under a security manger, if the permissions are not set up. It is also not as efficient as testing explicitly. If the TestTransients parameter is set to true, transient members will be tested, otherwise they are ignored, as they are likely derived fields, and not part of the value of the object. Static fields will not be tested.
lhs
- Left Hand Siderhs
- Right Hand SidetestTransients
- whether to include transient fieldsNullPointerException
- if either (but not both) parameter is nullClassCastException
- if the specified object's type prevents it
from being compared to this Object.public CompareToBuilder append(java.lang.Object lhs, java.lang.Object rhs)
Object
s are equal using either the
compareTo
method, or native comparison if the Objects are
actually arrays.
The objects must be Comparable
. If they are not, the method
will throw a ClassCastException
.
lhs
- - Left Hand Siderhs
- - Right Hand SideNullPointerException
- if either (but not both) parameter is nullClassCastException
- if the specified object's type prevents it
from being compared to this Object.public CompareToBuilder append(long lhs, long rhs)
long
s are <, > or ==.lhs
- - Left Hand Siderhs
- - Right Hand Sidepublic CompareToBuilder append(int lhs, int rhs)
int
s are <, > or ==.lhs
- - Left Hand Siderhs
- - Right Hand Sidepublic CompareToBuilder append(short lhs, short rhs)
short
s are <, > or ==.lhs
- - Left Hand Siderhs
- - Right Hand Sidepublic CompareToBuilder append(char lhs, char rhs)
char
s are <, > or ==.lhs
- - Left Hand Siderhs
- - Right Hand Sidepublic CompareToBuilder append(byte lhs, byte rhs)
byte
s are <, > or ==.lhs
- - Left Hand Siderhs
- - Right Hand Sidepublic CompareToBuilder append(double lhs, double rhs)
double
s are <, > or ==. This handles NaNs,
Infinties, and -0.0. It is compatible with the hash code generated by
HashCodeBuilder
.lhs
- - Left Hand Siderhs
- - Right Hand Sidepublic CompareToBuilder append(float lhs, float rhs)
double
s are <, > or ==. This handles NaNs,
Infinties, and -0.0. It is compatible with the hash code generated by
HashCodeBuilder
.lhs
- - Left Hand Siderhs
- - Right Hand Sidepublic CompareToBuilder append(boolean lhs, boolean rhs)
booleans
s are <, > or ==.lhs
- - Left Hand Siderhs
- - Right Hand Sidepublic CompareToBuilder append(java.lang.Object[] lhs, java.lang.Object[] rhs)
lhs
- - Left Hand Siderhs
- - Right Hand SideNullPointerException
- if either (but not both) parameter is nullClassCastException
- if the specified object's type prevents it
from being compared to this Object.public CompareToBuilder append(long[] lhs, long[] rhs)
long
Length and all values
are compared. The method append(long, long) is used.lhs
- - Left Hand Siderhs
- - Right Hand SideNullPointerException
- if either (but not both) parameter is nullpublic CompareToBuilder append(int[] lhs, int[] rhs)
int
Length and all values
are compared. The method append(int, int) is used.lhs
- - Left Hand Siderhs
- - Right Hand SideNullPointerException
- if either (but not both) parameter is nullpublic CompareToBuilder append(short[] lhs, short[] rhs)
short
Length and all values
are compared. The method append(short, short) is used.lhs
- - Left Hand Siderhs
- - Right Hand SideNullPointerException
- if either (but not both) parameter is nullpublic CompareToBuilder append(char[] lhs, char[] rhs)
char
Length and all values
are compared. The method append(char, char) is used.lhs
- - Left Hand Siderhs
- - Right Hand SideNullPointerException
- if either (but not both) parameter is nullpublic CompareToBuilder append(byte[] lhs, byte[] rhs)
byte
Length and all values
are compared. The method append(byte, byte) is used.lhs
- - Left Hand Siderhs
- - Right Hand SideNullPointerException
- if either (but not both) parameter is nullpublic CompareToBuilder append(double[] lhs, double[] rhs)
double
Length and all values
are compared. The method append(double, double) is used.lhs
- - Left Hand Siderhs
- - Right Hand SideNullPointerException
- if either (but not both) parameter is nullpublic CompareToBuilder append(float[] lhs, float[] rhs)
float
Length and all values
are compared. The method append(float, float) is used.lhs
- - Left Hand Siderhs
- - Right Hand SideNullPointerException
- if either (but not both) parameter is nullpublic CompareToBuilder append(boolean[] lhs, boolean[] rhs)
boolean
Length and all values
are compared. The method append(boolean, boolean) is used.lhs
- - Left Hand Siderhs
- - Right Hand SideNullPointerException
- if either (but not both) parameter is nullpublic int toComparison()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |