CompareToBuilder¶
- public class org.apache.commons.lang3.builder.CompareToBuilder extends AbstractReflection implements Builder<Integer>[source]¶
Assists in implementing
Comparable#compareTo(Object)methods.It is consistent with
equals(Object)andhashCode()built withEqualsBuilderandHashCodeBuilder.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(); } }
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
reflectionComparemethods that use reflection to determine the fields to append. Because fields can be private,reflectionCompareusesjava.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.See also
AbstractBuilder#setForceAccessible(boolean)A typical implementation of
compareTo(Object)usingreflectionComparelooks 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).
Constructors¶
Constructor for CompareToBuilder. |
- public CompareToBuilder()[source]¶
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
#toComparisonto get the result.
Methods¶
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Returns a negative Integer, a positive Integer, or zero as the |
|
Constructs a new Builder. |
|
Compares two |
|
Compares two |
|
Compares two |
|
Compares two |
|
Compares two |
|
Returns a negative integer, a positive integer, or zero as the |
- public CompareToBuilder append(boolean lhs, boolean rhs)[source]¶
Appends to the
builderthe comparison of twobooleanss.- Parameters:
lhs – left-hand side value.
rhs – right-hand side value.
- Returns:
thisinstance.
- public CompareToBuilder append(boolean[] lhs, boolean[] rhs)[source]¶
Appends to the
builderthe deep comparison of twobooleanarrays.Check if arrays are the same using
==Check if for
null,nullis less than non-nullCheck 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 array.
rhs – right-hand side array.
- Returns:
thisinstance.
- public CompareToBuilder append(byte lhs, byte rhs)[source]¶
Appends to the
builderthe comparison of twobytes.- Parameters:
lhs – left-hand side value.
rhs – right-hand side value.
- Returns:
thisinstance.
- public CompareToBuilder append(byte[] lhs, byte[] rhs)[source]¶
Appends to the
builderthe deep comparison of twobytearrays.Check if arrays are the same using
==Check if for
null,nullis less than non-nullCheck 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 array.
rhs – right-hand side array.
- Returns:
thisinstance.
- public CompareToBuilder append(char lhs, char rhs)[source]¶
Appends to the
builderthe comparison of twochars.- Parameters:
lhs – left-hand side value.
rhs – right-hand side value.
- Returns:
thisinstance.
- public CompareToBuilder append(char[] lhs, char[] rhs)[source]¶
Appends to the
builderthe deep comparison of twochararrays.Check if arrays are the same using
==Check if for
null,nullis less than non-nullCheck 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 array.
rhs – right-hand side array.
- Returns:
thisinstance.
- public CompareToBuilder append(double lhs, double rhs)[source]¶
Appends to the
builderthe comparison of twodoubles.This handles NaNs, Infinities, and
-0.0.It is compatible with the hash code generated by
HashCodeBuilder.- Parameters:
lhs – left-hand side value.
rhs – right-hand side value.
- Returns:
thisinstance.
- public CompareToBuilder append(double[] lhs, double[] rhs)[source]¶
Appends to the
builderthe deep comparison of twodoublearrays.Check if arrays are the same using
==Check if for
null,nullis less than non-nullCheck 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 array.
rhs – right-hand side array.
- Returns:
thisinstance.
- public CompareToBuilder append(float lhs, float rhs)[source]¶
Appends to the
builderthe comparison of twofloats.This handles NaNs, Infinities, and
-0.0.It is compatible with the hash code generated by
HashCodeBuilder.- Parameters:
lhs – left-hand side value.
rhs – right-hand side value.
- Returns:
thisinstance.
- public CompareToBuilder append(float[] lhs, float[] rhs)[source]¶
Appends to the
builderthe deep comparison of twofloatarrays.Check if arrays are the same using
==Check if for
null,nullis less than non-nullCheck 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 array.
rhs – right-hand side array.
- Returns:
thisinstance.
- public CompareToBuilder append(int lhs, int rhs)[source]¶
Appends to the
builderthe comparison of twoints.- Parameters:
lhs – left-hand side value.
rhs – right-hand side value.
- Returns:
thisinstance.
- public CompareToBuilder append(int[] lhs, int[] rhs)[source]¶
Appends to the
builderthe deep comparison of twointarrays.Check if arrays are the same using
==Check if for
null,nullis less than non-nullCheck 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 array.
rhs – right-hand side array.
- Returns:
thisinstance.
- public CompareToBuilder append(Object lhs, Object rhs)[source]¶
Appends to the
builderthe comparison of twoObjects.Check if
lhs == rhsCheck if either
lhsorrhsisnull, anullobject is less than a non-nullobjectCheck the object contents
lhsmust either be an array or implementComparable.- Parameters:
lhs – left-hand side object.
rhs – right-hand side object.
- Returns:
thisinstance.- Throws:
java.lang.ClassCastException – Thrown if
rhsis not assignment-compatible withlhs.
- public CompareToBuilder append(Object lhs, Object rhs, Comparator<?> comparator)[source]¶
Appends to the
builderthe comparison of twoObjects.Check if
lhs == rhsCheck if either
lhsorrhsisnull, anullobject is less than a non-nullobjectCheck the object contents
If
lhsis an array, array comparison methods will be used. Otherwisecomparatorwill be used to compare the objects. Ifcomparatorisnull,lhsmust implementComparableinstead.- Parameters:
lhs – left-hand side object.
rhs – right-hand side object.
comparator –
Comparatorused to compare the objects,nullmeans treat lhs asComparable
- Returns:
thisinstance.- Throws:
java.lang.ClassCastException – Thrown if
rhsis not assignment-compatible withlhs.- Since:
2.0
- public CompareToBuilder append(Object[] lhs, Object[] rhs)[source]¶
Appends to the
builderthe deep comparison of twoObjectarrays.Check if arrays are the same using
==Check if for
null,nullis less than non-nullCheck 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 array.
rhs – right-hand side array.
- Returns:
thisinstance.- Throws:
java.lang.ClassCastException – Thrown if
rhsis not assignment-compatible withlhs.
- public CompareToBuilder append(Object[] lhs, Object[] rhs, Comparator<?> comparator)[source]¶
Appends to the
builderthe deep comparison of twoObjectarrays.Check if arrays are the same using
==Check if for
null,nullis less than non-nullCheck 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 array.
rhs – right-hand side array.
comparator –
Comparatorto use to compare the array elements,nullmeans to treatlhselements asComparable.
- Returns:
thisinstance.- Throws:
java.lang.ClassCastException – Thrown if
rhsis not assignment-compatible withlhs.- Since:
2.0
- public CompareToBuilder append(long lhs, long rhs)[source]¶
Appends to the
builderthe comparison of twolongs.- Parameters:
lhs – left-hand side value.
rhs – right-hand side value.
- Returns:
thisinstance.
- public CompareToBuilder append(long[] lhs, long[] rhs)[source]¶
Appends to the
builderthe deep comparison of twolongarrays.Check if arrays are the same using
==Check if for
null,nullis less than non-nullCheck 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 array.
rhs – right-hand side array.
- Returns:
thisinstance.
- public CompareToBuilder append(short lhs, short rhs)[source]¶
Appends to the
builderthe comparison of twoshorts.- Parameters:
lhs – left-hand side value.
rhs – right-hand side value.
- Returns:
thisinstance.
- public CompareToBuilder append(short[] lhs, short[] rhs)[source]¶
Appends to the
builderthe deep comparison of twoshortarrays.Check if arrays are the same using
==Check if for
null,nullis less than non-nullCheck 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 array.
rhs – right-hand side array.
- Returns:
thisinstance.
- public CompareToBuilder appendSuper(int superCompareTo)[source]¶
Appends to the
builderthecompareTo(Object)result of the superclass.- Parameters:
superCompareTo – result of calling
super.compareTo(Object).- Returns:
thisinstance.- Since:
2.0
- @Override public Integer build()[source]¶
Returns a negative Integer, a positive Integer, or zero as the
builderhas judged the “left-hand” side as less than, greater than, or equal to the “right-hand” side.- Returns:
final comparison result as an Integer.
- See also:
- Since:
3.0
- public static CompareToBuilder.Builder builder()[source]¶
Constructs a new Builder.
- Returns:
A new Builder.
- public static int reflectionCompare(Object lhs, Object rhs)[source]¶
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 derived fields
Superclass fields will be compared
If both
lhsandrhsarenull, they are considered equal.- Parameters:
lhs – left-hand side object.
rhs – right-hand side object.
- Returns:
A negative integer, zero, or a positive integer as
lhsis less than, equal to, or greater thanrhs.- Throws:
java.lang.NullPointerException – Thrown if either (but not both) parameters are
null.java.lang.ClassCastException – Thrown if
rhsis not assignment-compatible withlhs.
- public static int reflectionCompare(Object lhs, Object rhs, boolean compareTransients)[source]¶
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 they are likely derived fields.Superclass fields will be compared
If both
lhsandrhsarenull, they are considered equal.- Parameters:
lhs – left-hand side object.
rhs – right-hand side object.
compareTransients – whether to compare transient fields.
- Returns:
A negative integer, zero, or a positive integer as
lhsis less than, equal to, or greater thanrhs.- Throws:
java.lang.NullPointerException – Thrown if either
lhsorrhs(but not both) isnull.java.lang.ClassCastException – Thrown if
rhsis not assignment-compatible withlhs.
- public static int reflectionCompare(Object lhs, Object rhs, boolean compareTransients, Class<?> reflectUpToClass, String... excludeFields)[source]¶
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 they are 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 side object.
rhs – right-hand side object.
compareTransients – whether to compare transient fields.
reflectUpToClass – last superclass for which fields are compared.
excludeFields – fields to exclude.
- Returns:
A negative integer, zero, or a positive integer as
lhsis less than, equal to, or greater thanrhs.- Throws:
java.lang.NullPointerException – Thrown if either
lhsorrhs(but not both) isnull.java.lang.ClassCastException – Thrown if
rhsis not assignment-compatible withlhs.
- Since:
2.2 (2.0 as
reflectionCompare(Object, Object, boolean, Class)).
- public static int reflectionCompare(Object lhs, Object rhs, String... excludeFields)[source]¶
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 they are likely derived fields.Superclass fields will be compared
If both
lhsandrhsarenull, they are considered equal.- Parameters:
lhs – left-hand side object.
rhs – right-hand side object.
excludeFields – array of fields to exclude.
- Returns:
A negative integer, zero, or a positive integer as
lhsis less than, equal to, or greater thanrhs.- Throws:
java.lang.NullPointerException – Thrown if either
lhsorrhs(but not both) isnull.java.lang.ClassCastException – Thrown if
rhsis not assignment-compatible withlhs.
- Since:
2.2
- public static int reflectionCompare(Object lhs, Object rhs, Collection<String> excludeFields)[source]¶
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 they are likely derived fields.Superclass fields will be compared
If both
lhsandrhsarenull, they are considered equal.- Parameters:
lhs – left-hand side object.
rhs – right-hand side object.
excludeFields – Collection of String fields to exclude.
- Returns:
A negative integer, zero, or a positive integer as
lhsis less than, equal to, or greater thanrhs.- Throws:
java.lang.NullPointerException – Thrown if either
lhsorrhs(but not both) isnull.java.lang.ClassCastException – Thrown if
rhsis not assignment-compatible withlhs.
- Since:
2.2