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 java. 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).
Comparable, Object#equals(Object), Object#hashCode(), EqualsBuilder, HashCodeBuilder
| Modifier and Type | Field and Description |
|---|---|
| private int | comparison
Current state of the comparison as appended fields are checked. |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public CompareToBuilder | Returns: this instance.left-hand side value lhs, final boolean right-hand side value rhs)Appends to the |
| public CompareToBuilder | Returns: this instance.left-hand side array lhs, final boolean[] right-hand side array rhs)Appends to the |
| public CompareToBuilder | Returns: this instance.left-hand side value lhs, final byte right-hand side value rhs)Appends to the |
| public CompareToBuilder | Returns: this instance.left-hand side array lhs, final byte[] right-hand side array rhs)Appends to the |
| public CompareToBuilder | Returns: this instance.left-hand side value lhs, final char right-hand side value rhs)Appends to the |
| public CompareToBuilder | Returns: this instance.left-hand side array lhs, final char[] right-hand side array rhs)Appends to the |
| public CompareToBuilder | Returns: this instance.left-hand side value lhs, final double right-hand side value rhs)Appends to the |
| public CompareToBuilder | Returns: this instance.left-hand side array lhs, final double[] right-hand side array rhs)Appends to the |
| public CompareToBuilder | Returns: this instance.left-hand side value lhs, final float right-hand side value rhs)Appends to the |
| public CompareToBuilder | Returns: this instance.left-hand side array lhs, final float[] right-hand side array rhs)Appends to the |
| public CompareToBuilder | Returns: this instance.left-hand side value lhs, final int right-hand side value rhs)Appends to the |
| public CompareToBuilder | Returns: this instance.left-hand side array lhs, final int[] right-hand side array rhs)Appends to the |
| public CompareToBuilder | Returns: this instance.left-hand side value lhs, final long right-hand side value rhs)Appends to the |
| public CompareToBuilder | Returns: this instance.left-hand side array lhs, final long[] right-hand side array rhs)Appends to the |
| public CompareToBuilder | |
| public CompareToBuilder | Returns: this instance.left-hand side object lhs, final Object right-hand side object rhs, final Comparator<?> comparator)Appends to the |
| public CompareToBuilder | |
| public CompareToBuilder | Returns: this instance.left-hand side array lhs, final Object[] right-hand side array rhs, final Comparator<?> comparator)Appends to the |
| public CompareToBuilder | Returns: this instance.left-hand side value lhs, final short right-hand side value rhs)Appends to the |
| public CompareToBuilder | Returns: this instance.left-hand side array lhs, final short[] right-hand side array rhs)Appends to the |
| private void | |
| public CompareToBuilder | Returns: this instance.result of calling superCompareTo)super.compareTo(Object)Appends to the |
| public Integer | Returns: final comparison result as an IntegerImplements org. Returns a negative Integer, a positive Integer, or zero as
the |
| private static void | reflectionAppend(final Object
left-hand side object lhs, final Object right-hand side object rhs, final Class<?> Class that defines fields to be comparedCompareToBuilder to append towhether to compare transient fields useTransients, final String[] fields to exclude excludeFields)Appends to |
| public static int | Returns: a negative integer, zero, or a positive integer aslhs
is less than, equal to, or greater than rhsleft-hand side object lhs, final Object right-hand side object rhs)Compares two |
| public static int | Returns: a negative integer, zero, or a positive integer aslhs
is less than, equal to, or greater than rhsleft-hand side object lhs, final Object right-hand side object rhs, final boolean whether to compare transient fields compareTransients)Compares two |
| public static int | Returns: a negative integer, zero, or a positive integer aslhs
is less than, equal to, or greater than rhsleft-hand side object lhs, final Object right-hand side object rhs, final boolean whether to compare transient fields compareTransients, final Class<?> last superclass for which fields are compared reflectUpToClass, final String... fields to exclude excludeFields)Compares two |
| public static int | Returns: a negative integer, zero, or a positive integer aslhs
is less than, equal to, or greater than rhsleft-hand side object lhs, final Object right-hand side object rhs, final Collection<String> Collection of String fields to exclude excludeFields)Compares two |
| public static int | Returns: a negative integer, zero, or a positive integer aslhs
is less than, equal to, or greater than rhsleft-hand side object lhs, final Object right-hand side object rhs, final String... array of fields to exclude excludeFields)Compares two |
| public int | Returns: final comparison resultReturns a negative integer, a positive integer, or zero as
the |
| comparison | back to summary |
|---|---|
| private int comparison Current state of the comparison as appended fields are checked. | |
| CompareToBuilder | back to summary |
|---|---|
| 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
| |
| append | back to summary |
|---|---|
| public CompareToBuilder append(final boolean lhs, final boolean rhs) Appends to the
| |
| append | back to summary |
|---|---|
| public CompareToBuilder append(final boolean[] lhs, final boolean[] rhs) Appends to the
| |
| append | back to summary |
|---|---|
| public CompareToBuilder append(final byte lhs, final byte rhs) Appends to the
| |
| append | back to summary |
|---|---|
| public CompareToBuilder append(final byte[] lhs, final byte[] rhs) Appends to the
| |
| append | back to summary |
|---|---|
| public CompareToBuilder append(final char lhs, final char rhs) Appends to the
| |
| append | back to summary |
|---|---|
| public CompareToBuilder append(final char[] lhs, final char[] rhs) Appends to the
| |
| append | back to summary |
|---|---|
| public CompareToBuilder append(final double lhs, final double rhs) Appends to the This handles NaNs, Infinities, and It is compatible with the hash code generated by
| |
| append | back to summary |
|---|---|
| public CompareToBuilder append(final double[] lhs, final double[] rhs) Appends to the
| |
| append | back to summary |
|---|---|
| public CompareToBuilder append(final float lhs, final float rhs) Appends to the This handles NaNs, Infinities, and It is compatible with the hash code generated by
| |
| append | back to summary |
|---|---|
| public CompareToBuilder append(final float[] lhs, final float[] rhs) Appends to the
| |
| append | back to summary |
|---|---|
| public CompareToBuilder append(final int lhs, final int rhs) Appends to the
| |
| append | back to summary |
|---|---|
| public CompareToBuilder append(final int[] lhs, final int[] rhs) Appends to the
| |
| append | back to summary |
|---|---|
| public CompareToBuilder append(final long lhs, final long rhs) Appends to the
| |
| append | back to summary |
|---|---|
| public CompareToBuilder append(final long[] lhs, final long[] rhs) Appends to the
| |
| append | back to summary |
|---|---|
| public CompareToBuilder append(final Object lhs, final Object rhs) Appends to the
| |
| append | back to summary |
|---|---|
| public CompareToBuilder append(final Object lhs, final Object rhs, final Comparator<?> comparator) Appends to the
If
| |
| append | back to summary |
|---|---|
| public CompareToBuilder append(final Object[] lhs, final Object[] rhs) Appends to the
This method will also will be called for the top level of multi-dimensional, ragged, and multi-typed arrays.
| |
| append | back to summary |
|---|---|
| public CompareToBuilder append(final Object[] lhs, final Object[] rhs, final Comparator<?> comparator) Appends to the
This method will also will be called for the top level of multi-dimensional, ragged, and multi-typed arrays.
| |
| append | back to summary |
|---|---|
| public CompareToBuilder append(final short lhs, final short rhs) Appends to the
| |
| append | back to summary |
|---|---|
| public CompareToBuilder append(final short[] lhs, final short[] rhs) Appends to the
| |
| appendArray | back to summary |
|---|---|
| private void appendArray(final Object lhs, final Object rhs, final Comparator<?> comparator) | |
| appendSuper | back to summary |
|---|---|
| public CompareToBuilder appendSuper(final int superCompareTo) Appends to the
| |
| build | back to summary |
|---|---|
| public Integer build() Implements org. Returns a negative Integer, a positive Integer, or zero as
the
| |
| reflectionAppend | back to summary |
|---|---|
| private static void reflectionAppend(final Object lhs, final Object rhs, final Class<?> clazz, final CompareToBuilder builder, final boolean useTransients, final String[] excludeFields) Appends to
| |
| reflectionCompare | back to summary |
|---|---|
| public static int reflectionCompare(final Object lhs, final Object rhs) Compares two Fields can be private, thus
If both
| |
| reflectionCompare | back to summary |
|---|---|
| public static int reflectionCompare(final Object lhs, final Object rhs, final boolean compareTransients) Compares two Fields can be private, thus
If both
| |
| reflectionCompare | back to summary |
|---|---|
| public static int reflectionCompare(final Object lhs, final Object rhs, final boolean compareTransients, final Class<?> reflectUpToClass, final String... excludeFields) Compares two Fields can be private, thus
If both
| |
| reflectionCompare | back to summary |
|---|---|
| public static int reflectionCompare(final Object lhs, final Object rhs, final Collection<String> excludeFields) Compares two Fields can be private, thus
If both
| |
| reflectionCompare | back to summary |
|---|---|
| public static int reflectionCompare(final Object lhs, final Object rhs, final String... excludeFields) Compares two Fields can be private, thus
If both
| |
| toComparison | back to summary |
|---|---|
| public int toComparison() Returns a negative integer, a positive integer, or zero as
the
| |