Class CompareToBuilder
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(); } }
Values are compared in the order they are appended to the builder. If any comparison returns
a non-zero result, then that value will be the result returned by toComparison()
and all
subsequent comparisons are skipped.
Alternatively, there are reflectionCompare
methods that use
reflection to determine the fields to append. Because fields can be private,
reflectionCompare
uses 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); }
The reflective methods compare object fields in the order returned by
Class.getDeclaredFields()
. The fields of the class are compared first, followed by those
of its parent classes (in order from the bottom to the top of the class hierarchy).
- Since:
- 1.0
- See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionappend
(boolean[] lhs, boolean[] rhs) Appends to thebuilder
the deep comparison of twoboolean
arrays.append
(boolean lhs, boolean rhs) Appends to thebuilder
the comparison of twobooleans
s.append
(byte[] lhs, byte[] rhs) Appends to thebuilder
the deep comparison of twobyte
arrays.append
(byte lhs, byte rhs) Appends to thebuilder
the comparison of twobyte
s.append
(char[] lhs, char[] rhs) Appends to thebuilder
the deep comparison of twochar
arrays.append
(char lhs, char rhs) Appends to thebuilder
the comparison of twochar
s.append
(double[] lhs, double[] rhs) Appends to thebuilder
the deep comparison of twodouble
arrays.append
(double lhs, double rhs) Appends to thebuilder
the comparison of twodouble
s.append
(float[] lhs, float[] rhs) Appends to thebuilder
the deep comparison of twofloat
arrays.append
(float lhs, float rhs) Appends to thebuilder
the comparison of twofloat
s.append
(int[] lhs, int[] rhs) Appends to thebuilder
the deep comparison of twoint
arrays.append
(int lhs, int rhs) Appends to thebuilder
the comparison of twoint
s.append
(long[] lhs, long[] rhs) Appends to thebuilder
the deep comparison of twolong
arrays.append
(long lhs, long rhs) Appends to thebuilder
the comparison of twolong
s.append
(short[] lhs, short[] rhs) Appends to thebuilder
the deep comparison of twoshort
arrays.append
(short lhs, short rhs) Appends to thebuilder
the comparison of twoshort
s.Appends to thebuilder
the deep comparison of twoObject
arrays.append
(Object[] lhs, Object[] rhs, Comparator<?> comparator) Appends to thebuilder
the deep comparison of twoObject
arrays.Appends to thebuilder
the comparison of twoObject
s.append
(Object lhs, Object rhs, Comparator<?> comparator) Appends to thebuilder
the comparison of twoObject
s.appendSuper
(int superCompareTo) Appends to thebuilder
thecompareTo(Object)
result of the superclass.build()
Returns a negative Integer, a positive Integer, or zero as thebuilder
has judged the "left-hand" side as less than, greater than, or equal to the "right-hand" side.static int
reflectionCompare
(Object lhs, Object rhs) Compares twoObject
s via reflection.static int
reflectionCompare
(Object lhs, Object rhs, boolean compareTransients) Compares twoObject
s via reflection.static int
reflectionCompare
(Object lhs, Object rhs, boolean compareTransients, Class<?> reflectUpToClass, String... excludeFields) Compares twoObject
s via reflection.static int
reflectionCompare
(Object lhs, Object rhs, String... excludeFields) Compares twoObject
s via reflection.static int
reflectionCompare
(Object lhs, Object rhs, Collection<String> excludeFields) Compares twoObject
s via reflection.int
Returns a negative integer, a positive integer, or zero as thebuilder
has judged the "left-hand" side as less than, greater than, or equal to the "right-hand" side.
-
Constructor Details
-
CompareToBuilder
public CompareToBuilder()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
toComparison()
to get the result.
-
-
Method Details
-
reflectionCompare
Compares twoObject
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
andrhs
arenull
, they are considered equal.- Parameters:
lhs
- left-hand side objectrhs
- right-hand side object- Returns:
- a negative integer, zero, or a positive integer as
lhs
is less than, equal to, or greater thanrhs
- Throws:
NullPointerException
- if either (but not both) parameters arenull
ClassCastException
- ifrhs
is not assignment-compatible withlhs
-
reflectionCompare
Compares twoObject
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
istrue
, compares transient members. Otherwise ignores them, as they are likely derived fields. - Superclass fields will be compared
If both
lhs
andrhs
arenull
, they are considered equal.- Parameters:
lhs
- left-hand side objectrhs
- right-hand side objectcompareTransients
- whether to compare transient fields- Returns:
- a negative integer, zero, or a positive integer as
lhs
is less than, equal to, or greater thanrhs
- Throws:
NullPointerException
- if eitherlhs
orrhs
(but not both) isnull
ClassCastException
- ifrhs
is not assignment-compatible withlhs
-
reflectionCompare
public static int reflectionCompare(Object lhs, Object rhs, boolean compareTransients, Class<?> reflectUpToClass, String... excludeFields) Compares twoObject
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
istrue
, compares transient members. Otherwise ignores them, as they are likely derived fields. - Compares superclass fields up to and including
reflectUpToClass
. IfreflectUpToClass
isnull
, compares all superclass fields.
If both
lhs
andrhs
arenull
, they are considered equal.- Parameters:
lhs
- left-hand side objectrhs
- right-hand side objectcompareTransients
- whether to compare transient fieldsreflectUpToClass
- last superclass for which fields are comparedexcludeFields
- fields to exclude- Returns:
- a negative integer, zero, or a positive integer as
lhs
is less than, equal to, or greater thanrhs
- Throws:
NullPointerException
- if eitherlhs
orrhs
(but not both) isnull
ClassCastException
- ifrhs
is not assignment-compatible withlhs
- Since:
- 2.2 (2.0 as
reflectionCompare(Object, Object, boolean, Class)
)
-
reflectionCompare
Compares twoObject
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
istrue
, compares transient members. Otherwise ignores them, as they are likely derived fields. - Superclass fields will be compared
If both
lhs
andrhs
arenull
, they are considered equal.- Parameters:
lhs
- left-hand side objectrhs
- right-hand side objectexcludeFields
- Collection of String fields to exclude- Returns:
- a negative integer, zero, or a positive integer as
lhs
is less than, equal to, or greater thanrhs
- Throws:
NullPointerException
- if eitherlhs
orrhs
(but not both) isnull
ClassCastException
- ifrhs
is not assignment-compatible withlhs
- Since:
- 2.2
-
reflectionCompare
Compares twoObject
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
istrue
, compares transient members. Otherwise ignores them, as they are likely derived fields. - Superclass fields will be compared
If both
lhs
andrhs
arenull
, they are considered equal.- Parameters:
lhs
- left-hand side objectrhs
- right-hand side objectexcludeFields
- array of fields to exclude- Returns:
- a negative integer, zero, or a positive integer as
lhs
is less than, equal to, or greater thanrhs
- Throws:
NullPointerException
- if eitherlhs
orrhs
(but not both) isnull
ClassCastException
- ifrhs
is not assignment-compatible withlhs
- Since:
- 2.2
-
append
Appends to thebuilder
the comparison of twobooleans
s.- Parameters:
lhs
- left-hand side valuerhs
- right-hand side value- Returns:
this
instance.
-
append
Appends to thebuilder
the deep comparison of twoboolean
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
append(boolean, boolean)
- Parameters:
lhs
- left-hand side arrayrhs
- right-hand side array- Returns:
this
instance.
- Check if arrays are the same using
-
append
Appends to thebuilder
the comparison of twobyte
s.- Parameters:
lhs
- left-hand side valuerhs
- right-hand side value- Returns:
this
instance.
-
append
Appends to thebuilder
the deep comparison of twobyte
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
append(byte, byte)
- Parameters:
lhs
- left-hand side arrayrhs
- right-hand side array- Returns:
this
instance.
- Check if arrays are the same using
-
append
Appends to thebuilder
the comparison of twochar
s.- Parameters:
lhs
- left-hand side valuerhs
- right-hand side value- Returns:
this
instance.
-
append
Appends to thebuilder
the deep comparison of twochar
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
append(char, char)
- Parameters:
lhs
- left-hand side arrayrhs
- right-hand side array- Returns:
this
instance.
- Check if arrays are the same using
-
append
Appends to thebuilder
the comparison of twodouble
s.This handles NaNs, Infinities, and
-0.0
.It is compatible with the hash code generated by
HashCodeBuilder
.- Parameters:
lhs
- left-hand side valuerhs
- right-hand side value- Returns:
this
instance.
-
append
Appends to thebuilder
the deep comparison of twodouble
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
append(double, double)
- Parameters:
lhs
- left-hand side arrayrhs
- right-hand side array- Returns:
this
instance.
- Check if arrays are the same using
-
append
Appends to thebuilder
the comparison of twofloat
s.This handles NaNs, Infinities, and
-0.0
.It is compatible with the hash code generated by
HashCodeBuilder
.- Parameters:
lhs
- left-hand side valuerhs
- right-hand side value- Returns:
this
instance.
-
append
Appends to thebuilder
the deep comparison of twofloat
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
append(float, float)
- Parameters:
lhs
- left-hand side arrayrhs
- right-hand side array- Returns:
this
instance.
- Check if arrays are the same using
-
append
Appends to thebuilder
the comparison of twoint
s.- Parameters:
lhs
- left-hand side valuerhs
- right-hand side value- Returns:
this
instance.
-
append
Appends to thebuilder
the deep comparison of twoint
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
append(int, int)
- Parameters:
lhs
- left-hand side arrayrhs
- right-hand side array- Returns:
this
instance.
- Check if arrays are the same using
-
append
Appends to thebuilder
the comparison of twolong
s.- Parameters:
lhs
- left-hand side valuerhs
- right-hand side value- Returns:
this
instance.
-
append
Appends to thebuilder
the deep comparison of twolong
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
append(long, long)
- Parameters:
lhs
- left-hand side arrayrhs
- right-hand side array- Returns:
this
instance.
- Check if arrays are the same using
-
append
Appends to thebuilder
the comparison of twoObject
s.- Check if
lhs == rhs
- Check if either
lhs
orrhs
isnull
, anull
object is less than a non-null
object - Check the object contents
lhs
must either be an array or implementComparable
.- Parameters:
lhs
- left-hand side objectrhs
- right-hand side object- Returns:
this
instance.- Throws:
ClassCastException
- ifrhs
is not assignment-compatible withlhs
- Check if
-
append
Appends to thebuilder
the comparison of twoObject
s.- Check if
lhs == rhs
- Check if either
lhs
orrhs
isnull
, anull
object is less than a non-null
object - Check the object contents
If
lhs
is an array, array comparison methods will be used. Otherwisecomparator
will be used to compare the objects. Ifcomparator
isnull
,lhs
must implementComparable
instead.- Parameters:
lhs
- left-hand side objectrhs
- right-hand side objectcomparator
-Comparator
used to compare the objects,null
means treat lhs asComparable
- Returns:
this
instance.- Throws:
ClassCastException
- ifrhs
is not assignment-compatible withlhs
- Since:
- 2.0
- Check if
-
append
Appends to thebuilder
the deep comparison of twoObject
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
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 side arrayrhs
- right-hand side array- Returns:
this
instance.- Throws:
ClassCastException
- ifrhs
is not assignment-compatible withlhs
- Check if arrays are the same using
-
append
Appends to thebuilder
the deep comparison of twoObject
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
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 side arrayrhs
- right-hand side arraycomparator
-Comparator
to use to compare the array elements,null
means to treatlhs
elements asComparable
.- Returns:
this
instance.- Throws:
ClassCastException
- ifrhs
is not assignment-compatible withlhs
- Since:
- 2.0
- Check if arrays are the same using
-
append
Appends to thebuilder
the comparison of twoshort
s.- Parameters:
lhs
- left-hand side valuerhs
- right-hand side value- Returns:
this
instance.
-
append
Appends to thebuilder
the deep comparison of twoshort
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
append(short, short)
- Parameters:
lhs
- left-hand side arrayrhs
- right-hand side array- Returns:
this
instance.
- Check if arrays are the same using
-
appendSuper
Appends to thebuilder
thecompareTo(Object)
result of the superclass.- Parameters:
superCompareTo
- result of callingsuper.compareTo(Object)
- Returns:
this
instance.- Since:
- 2.0
-
build
Returns a negative Integer, a positive Integer, or zero as thebuilder
has judged the "left-hand" side as less than, greater than, or equal to the "right-hand" side. -
toComparison
Returns a negative integer, a positive integer, or zero as thebuilder
has judged the "left-hand" side as less than, greater than, or equal to the "right-hand" side.- Returns:
- final comparison result
- See Also:
-