-
- All Implemented Interfaces:
-
external.org.apache.commons.lang3.builder.Builder
public class CompareToBuilder implements Builder<Integer>
Assists in implementing compareTo methods. It is consistent with
equals(Object)andhashcode()built with EqualsBuilder and HashCodeBuilder.Two Objects that compare equal using
equals(Object)should normally also compare equal usingcompareTo(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)andequals(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,
reflectionCompareuses setAccessible 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)usingreflectionComparelooks like:public int compareTo(Object o) { return CompareToBuilder.reflectionCompare(this, o); }
-
-
Constructor Summary
Constructors Constructor Description CompareToBuilder()Constructor for CompareToBuilder.
-
Method Summary
Modifier and Type Method Description static intreflectionCompare(Object lhs, Object rhs)Compares two Objects via reflection.static intreflectionCompare(Object lhs, Object rhs, boolean compareTransients)Compares two Objects via reflection.static intreflectionCompare(Object lhs, Object rhs, Collection<String> excludeFields)Compares two Objects via reflection.static intreflectionCompare(Object lhs, Object rhs, Array<String> excludeFields)Compares two Objects via reflection.static intreflectionCompare(Object lhs, Object rhs, boolean compareTransients, Class<out Object> reflectUpToClass, Array<String> excludeFields)Compares two Objects via reflection.CompareToBuilderappendSuper(int superCompareTo)Appends to the builderthecompareTo(Object)result of the superclass.CompareToBuilderappend(Object lhs, Object rhs)Appends to the builderthe comparison oftwoObjects.CompareToBuilderappend(Object lhs, Object rhs, Comparator<out Object> comparator)Appends to the builderthe comparison oftwoObjects.CompareToBuilderappend(long lhs, long rhs)Appends to the builderthe comparison oftwolongs.CompareToBuilderappend(int lhs, int rhs)Appends to the builderthe comparison oftwoints.CompareToBuilderappend(short lhs, short rhs)Appends to the builderthe comparison oftwoshorts.CompareToBuilderappend(char lhs, char rhs)Appends to the builderthe comparison oftwochars.CompareToBuilderappend(byte lhs, byte rhs)Appends to the builderthe comparison oftwobytes.CompareToBuilderappend(double lhs, double rhs)Appends to the builderthe comparison oftwodoubles.CompareToBuilderappend(float lhs, float rhs)Appends to the builderthe comparison oftwofloats.CompareToBuilderappend(boolean lhs, boolean rhs)Appends to the builderthe comparison oftwobooleanss.CompareToBuilderappend(Array<Object> lhs, Array<Object> rhs)Appends to the builderthe deep comparison oftwoObjectarrays.CompareToBuilderappend(Array<Object> lhs, Array<Object> rhs, Comparator<out Object> comparator)Appends to the builderthe deep comparison oftwoObjectarrays.CompareToBuilderappend(Array<long> lhs, Array<long> rhs)Appends to the builderthe deep comparison oftwolongarrays.CompareToBuilderappend(Array<int> lhs, Array<int> rhs)Appends to the builderthe deep comparison oftwointarrays.CompareToBuilderappend(Array<short> lhs, Array<short> rhs)Appends to the builderthe deep comparison oftwoshortarrays.CompareToBuilderappend(Array<char> lhs, Array<char> rhs)Appends to the builderthe deep comparison oftwochararrays.CompareToBuilderappend(Array<byte> lhs, Array<byte> rhs)Appends to the builderthe deep comparison oftwobytearrays.CompareToBuilderappend(Array<double> lhs, Array<double> rhs)Appends to the builderthe deep comparison oftwodoublearrays.CompareToBuilderappend(Array<float> lhs, Array<float> rhs)Appends to the builderthe deep comparison oftwofloatarrays.CompareToBuilderappend(Array<boolean> lhs, Array<boolean> rhs)Appends to the builderthe deep comparison oftwobooleanarrays.inttoComparison()Returns a negative integer, a positive integer, or zero asthe builderhas judged the "left-hand" sideas less than, greater than, or equal to the "right-hand"side.Integerbuild()Returns a negative Integer, a positive Integer, or zero asthe builderhas judged the "left-hand" sideas less than, greater than, or equal to the "right-hand"side.-
-
Method Detail
-
reflectionCompare
static int reflectionCompare(Object lhs, Object rhs)
Compares two
Objects via reflection.Fields can be private, thus
AccessibleObject.setAccessibleis 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 derivedfields
- Superclass fields will be compared
If both
lhsandrhsarenull,they are considered equal.- Parameters:
lhs- left-hand objectrhs- right-hand object
-
reflectionCompare
static int reflectionCompare(Object lhs, Object rhs, boolean compareTransients)
Compares two
Objects via reflection.Fields can be private, thus
AccessibleObject.setAccessibleis 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
compareTransientsistrue,compares transient members. Otherwise ignores them, as theyare likely derived fields. - Superclass fields will be compared
If both
lhsandrhsarenull,they are considered equal.- Parameters:
lhs- left-hand objectrhs- right-hand objectcompareTransients- whether to compare transient fields
-
reflectionCompare
static int reflectionCompare(Object lhs, Object rhs, Collection<String> excludeFields)
Compares two
Objects via reflection.Fields can be private, thus
AccessibleObject.setAccessibleis 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
compareTransientsistrue,compares transient members. Otherwise ignores them, as theyare likely derived fields. - Superclass fields will be compared
If both
lhsandrhsarenull,they are considered equal.- Parameters:
lhs- left-hand objectrhs- right-hand objectexcludeFields- Collection of String fields to exclude
-
reflectionCompare
static int reflectionCompare(Object lhs, Object rhs, Array<String> excludeFields)
Compares two
Objects via reflection.Fields can be private, thus
AccessibleObject.setAccessibleis 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
compareTransientsistrue,compares transient members. Otherwise ignores them, as theyare likely derived fields. - Superclass fields will be compared
If both
lhsandrhsarenull,they are considered equal.- Parameters:
lhs- left-hand objectrhs- right-hand objectexcludeFields- array of fields to exclude
-
reflectionCompare
static int reflectionCompare(Object lhs, Object rhs, boolean compareTransients, Class<out Object> reflectUpToClass, Array<String> excludeFields)
Compares two
Objects via reflection.Fields can be private, thus
AccessibleObject.setAccessibleis 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
compareTransientsistrue,compares transient members. Otherwise ignores them, as theyare likely derived fields. - Compares superclass fields up to and including
reflectUpToClass.IfreflectUpToClassisnull, compares all superclass fields.
If both
lhsandrhsarenull,they are considered equal.- Parameters:
lhs- left-hand objectrhs- right-hand objectcompareTransients- whether to compare transient fieldsreflectUpToClass- last superclass for which fields are comparedexcludeFields- fields to exclude
-
appendSuper
CompareToBuilder appendSuper(int superCompareTo)
Appends to the
builderthecompareTo(Object)result of the superclass.- Parameters:
superCompareTo- result of callingsuper.compareTo(Object)
-
append
CompareToBuilder append(Object lhs, Object rhs)
Appends to the
builderthe comparison oftwoObjects.- Check if
lhs == rhs - Check if either
lhsorrhsisnull,anullobject is less than a non-nullobject - Check the object contents
lhsmust either be an array or implement Comparable.- Parameters:
lhs- left-hand objectrhs- right-hand object
- Check if
-
append
CompareToBuilder append(Object lhs, Object rhs, Comparator<out Object> comparator)
Appends to the
builderthe comparison oftwoObjects.- Check if
lhs == rhs - Check if either
lhsorrhsisnull,anullobject is less than a non-nullobject - Check the object contents
If
lhsis an array, array comparison methods will be used.Otherwisecomparatorwill be used to compare the objects.Ifcomparatorisnull,lhsmustimplement Comparable instead.- Parameters:
lhs- left-hand objectrhs- right-hand objectcomparator-Comparatorused to compare the objects,nullmeans treat lhs asComparable
- Check if
-
append
CompareToBuilder append(long lhs, long rhs)
Appends to the
builderthe comparison oftwolongs.- Parameters:
lhs- left-hand valuerhs- right-hand value
-
append
CompareToBuilder append(int lhs, int rhs)
Appends to the
builderthe comparison oftwoints.- Parameters:
lhs- left-hand valuerhs- right-hand value
-
append
CompareToBuilder append(short lhs, short rhs)
Appends to the
builderthe comparison oftwoshorts.- Parameters:
lhs- left-hand valuerhs- right-hand value
-
append
CompareToBuilder append(char lhs, char rhs)
Appends to the
builderthe comparison oftwochars.- Parameters:
lhs- left-hand valuerhs- right-hand value
-
append
CompareToBuilder append(byte lhs, byte rhs)
Appends to the
builderthe comparison oftwobytes.- Parameters:
lhs- left-hand valuerhs- right-hand value
-
append
CompareToBuilder append(double lhs, double rhs)
Appends to the
builderthe comparison oftwodoubles.This handles NaNs, Infinities, and
-0.0.It is compatible with the hash code generated by
HashCodeBuilder.- Parameters:
lhs- left-hand valuerhs- right-hand value
-
append
CompareToBuilder append(float lhs, float rhs)
Appends to the
builderthe comparison oftwofloats.This handles NaNs, Infinities, and
-0.0.It is compatible with the hash code generated by
HashCodeBuilder.- Parameters:
lhs- left-hand valuerhs- right-hand value
-
append
CompareToBuilder append(boolean lhs, boolean rhs)
Appends to the
builderthe comparison oftwobooleanss.- Parameters:
lhs- left-hand valuerhs- right-hand value
-
append
CompareToBuilder append(Array<Object> lhs, Array<Object> rhs)
Appends to the
builderthe deep comparison oftwoObjectarrays.- Check if arrays are the same using
== - Check if for
null,nullis 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
This method will also will be called for the top level of multi-dimensional,ragged, and multi-typed arrays.
- Parameters:
lhs- left-hand arrayrhs- right-hand array
- Check if arrays are the same using
-
append
CompareToBuilder append(Array<Object> lhs, Array<Object> rhs, Comparator<out Object> comparator)
Appends to the
builderthe deep comparison oftwoObjectarrays.- Check if arrays are the same using
== - Check if for
null,nullis 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
This method will also will be called for the top level of multi-dimensional,ragged, and multi-typed arrays.
- Parameters:
lhs- left-hand arrayrhs- right-hand arraycomparator-Comparatorto use to compare the array elements,nullmeans to treatlhselements asComparable.
- Check if arrays are the same using
-
append
CompareToBuilder append(Array<long> lhs, Array<long> rhs)
Appends to the
builderthe deep comparison oftwolongarrays.- Check if arrays are the same using
== - Check if for
null,nullis 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
- Parameters:
lhs- left-hand arrayrhs- right-hand array
- Check if arrays are the same using
-
append
CompareToBuilder append(Array<int> lhs, Array<int> rhs)
Appends to the
builderthe deep comparison oftwointarrays.- Check if arrays are the same using
== - Check if for
null,nullis 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
- Parameters:
lhs- left-hand arrayrhs- right-hand array
- Check if arrays are the same using
-
append
CompareToBuilder append(Array<short> lhs, Array<short> rhs)
Appends to the
builderthe deep comparison oftwoshortarrays.- Check if arrays are the same using
== - Check if for
null,nullis 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
- Parameters:
lhs- left-hand arrayrhs- right-hand array
- Check if arrays are the same using
-
append
CompareToBuilder append(Array<char> lhs, Array<char> rhs)
Appends to the
builderthe deep comparison oftwochararrays.- Check if arrays are the same using
== - Check if for
null,nullis 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
- Parameters:
lhs- left-hand arrayrhs- right-hand array
- Check if arrays are the same using
-
append
CompareToBuilder append(Array<byte> lhs, Array<byte> rhs)
Appends to the
builderthe deep comparison oftwobytearrays.- Check if arrays are the same using
== - Check if for
null,nullis 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
- Parameters:
lhs- left-hand arrayrhs- right-hand array
- Check if arrays are the same using
-
append
CompareToBuilder append(Array<double> lhs, Array<double> rhs)
Appends to the
builderthe deep comparison oftwodoublearrays.- Check if arrays are the same using
== - Check if for
null,nullis 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
- Parameters:
lhs- left-hand arrayrhs- right-hand array
- Check if arrays are the same using
-
append
CompareToBuilder append(Array<float> lhs, Array<float> rhs)
Appends to the
builderthe deep comparison oftwofloatarrays.- Check if arrays are the same using
== - Check if for
null,nullis 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
- Parameters:
lhs- left-hand arrayrhs- right-hand array
- Check if arrays are the same using
-
append
CompareToBuilder append(Array<boolean> lhs, Array<boolean> rhs)
Appends to the
builderthe deep comparison oftwobooleanarrays.- Check if arrays are the same using
== - Check if for
null,nullis 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
- Parameters:
lhs- left-hand arrayrhs- right-hand array
- Check if arrays are the same using
-
toComparison
int toComparison()
Returns a negative integer, a positive integer, or zero asthe
builderhas judged the "left-hand" sideas less than, greater than, or equal to the "right-hand"side.
-
-
-
-