public class CompareTo
extends java.lang.Object
Comparable.compareTo(Object)
methods.
Two Objects that compare equal using equals(Object)
should normally
also compare equal using compareTo(Object)
.
To use this class write code as follows:
public class MyClass { String field1; int field2; boolean field3;//do not use in the comparison ... public int compareTo(Object o) { MyClass myClass = (MyClass) o; return new CompareTo(this, o, "field1", "field2").getComparison(); } }
Values are compared in the order they are given to the constructor. In the example above the values in the "field1" will be compared first, the the values in the "field2".
Constructor and Description |
---|
CompareTo(java.lang.Object lhs,
java.lang.Object rhs,
java.lang.String field0,
java.lang.String... moreFields)
Constructor for CompareTo.
|
Modifier and Type | Method and Description |
---|---|
int |
append(boolean[] lhs,
boolean[] rhs,
java.lang.String fieldName)
Appends to the
fieldComparisons the deep comparison of
two boolean arrays. |
int |
append(boolean lhs,
boolean rhs,
java.lang.String fieldName)
Appends to the
builder the comparison of
two booleans s. |
int |
append(byte[] lhs,
byte[] rhs,
java.lang.String fieldName)
Appends to the
fieldComparisons the deep comparison of
two byte arrays. |
int |
append(byte lhs,
byte rhs,
java.lang.String fieldName)
Appends to the
fieldComparisons the comparison of
two byte s. |
int |
append(char[] lhs,
char[] rhs,
java.lang.String fieldName)
Appends to the
fieldComparisons the deep comparison of
two char arrays. |
int |
append(char lhs,
char rhs,
java.lang.String fieldName)
Appends to the
fieldComparisons the comparison of
two char s. |
int |
append(double[] lhs,
double[] rhs,
java.lang.String fieldName)
Appends to the
fieldComparisons the deep comparison of
two double arrays. |
int |
append(double lhs,
double rhs,
java.lang.String fieldName)
Appends to the
fieldComparisons the comparison of
two double s. |
int |
append(float[] lhs,
float[] rhs,
java.lang.String fieldName)
Appends to the
fieldComparisons the deep comparison of
two float arrays. |
int |
append(float lhs,
float rhs,
java.lang.String fieldName)
Appends to the
fieldComparisons the comparison of
two float s. |
int |
append(int[] lhs,
int[] rhs,
java.lang.String fieldName)
Appends to the
fieldComparisons the deep comparison of
two int arrays. |
int |
append(int lhs,
int rhs,
java.lang.String fieldName)
Appends to the
fieldComparisons the comparison of
two int s. |
int |
append(long[] lhs,
long[] rhs,
java.lang.String fieldName)
Appends to the
fieldComparisons the deep comparison of
two long arrays. |
int |
append(long lhs,
long rhs,
java.lang.String fieldName)
Appends to the
fieldComparisons the comparison of
two long s. |
int |
append(java.lang.Object[] lhs,
java.lang.Object[] rhs,
java.util.Comparator<?> comparator,
java.lang.String fieldName)
Appends to the
fieldComparisons the deep comparison of
two Object arrays. |
int |
append(java.lang.Object[] lhs,
java.lang.Object[] rhs,
java.lang.String fieldName)
Appends to the
fieldComparisons the deep comparison of
two Object arrays. |
int |
append(java.lang.Object lhs,
java.lang.Object rhs,
java.util.Comparator<?> comparator,
java.lang.String fieldName)
Appends to the
fieldComparisons the resultat of comparison of
two Object s for the given field fieldName . |
int |
append(java.lang.Object lhs,
java.lang.Object rhs,
java.lang.String fieldName)
Appends to the
fieldComparisons the result of comparison of
two Object s for a given field name, |
int |
append(short[] lhs,
short[] rhs,
java.lang.String fieldName)
Appends to the
fieldComparisons the deep comparison of
two short arrays. |
int |
append(short lhs,
short rhs,
java.lang.String fieldName)
Appends to the
fieldComparisons the comparison of
two short s. |
int |
getComparison()
Returns a negative integer, a positive integer, or zero as
the
builder has judged the "left-hand" side
as less than, greater than, or equal to the "right-hand"
side. |
void |
reflectionCompare()
Compares two
Object s via reflection. |
public CompareTo(java.lang.Object lhs, java.lang.Object rhs, java.lang.String field0, java.lang.String... moreFields)
Constructor for CompareTo.
lhs
- left-hand objectrhs
- right-hand objectfield0
- including moreFieldsare fields to compare in the given order
moreFields
- including field0are fields to compare in the given order
public void reflectionCompare()
Compares two Object
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.
If both lhs
and rhs
are null
,
they are considered equal.
java.lang.NullPointerException
- if either lhs
or rhs
(but not both) is null
java.lang.ClassCastException
- if rhs
is not assignment-compatible
with lhs
public int append(java.lang.Object lhs, java.lang.Object rhs, java.lang.String fieldName)
Appends to the fieldComparisons
the result of comparison of
two Object
s for a given field name,
lhs
- left-hand objectrhs
- right-hand objectfieldName
- a field name for the value of which we want to compare the two objectsjava.lang.ClassCastException
- if rhs
is not assignment-compatible
with lhs
public int append(java.lang.Object lhs, java.lang.Object rhs, java.util.Comparator<?> comparator, java.lang.String fieldName)
Appends to the fieldComparisons
the resultat of comparison of
two Object
s for the given field fieldName
.
lhs == rhs
lhs
or rhs
is null
,
a null
object is less than a non-null
objectIf lhs
is an array, array comparison methods will be used.
Otherwise comparator
will be used to compare the objects.
If comparator
is null
, lhs
must
implement Comparable
instead.
lhs
- left-hand objectrhs
- right-hand objectcomparator
- Comparator
used to compare the objects,
null
means treat lhs as Comparable
fieldName
- field to comparejava.lang.ClassCastException
- if rhs
is not assignment-compatible
with lhs
public int append(long lhs, long rhs, java.lang.String fieldName)
fieldComparisons
the comparison of
two long
s.lhs
- left-hand valuerhs
- right-hand valuefieldName
- a field name for the value of which we want to compare the two objectspublic int append(int lhs, int rhs, java.lang.String fieldName)
fieldComparisons
the comparison of
two int
s.lhs
- left-hand valuerhs
- right-hand valuefieldName
- a field name for the value of which we want to compare the two objectspublic int append(short lhs, short rhs, java.lang.String fieldName)
fieldComparisons
the comparison of
two short
s.lhs
- left-hand valuerhs
- right-hand valuefieldName
- a field name for the value of which we want to compare the two objectspublic int append(char lhs, char rhs, java.lang.String fieldName)
fieldComparisons
the comparison of
two char
s.lhs
- left-hand valuerhs
- right-hand valuefieldName
- a field name for the value of which we want to compare the two objectspublic int append(byte lhs, byte rhs, java.lang.String fieldName)
fieldComparisons
the comparison of
two byte
s.lhs
- left-hand valuerhs
- right-hand valuefieldName
- a field name for the value of which we want to compare the two objectspublic int append(double lhs, double rhs, java.lang.String fieldName)
Appends to the fieldComparisons
the comparison of
two double
s.
This handles NaNs, Infinities, and -0.0
.
It is compatible with the hash code generated by
HashCodeBuilder
.
lhs
- left-hand valuerhs
- right-hand valuefieldName
- a field name for the value of which we want to compare the two objectspublic int append(float lhs, float rhs, java.lang.String fieldName)
Appends to the fieldComparisons
the comparison of
two float
s.
This handles NaNs, Infinities, and -0.0
.
lhs
- left-hand valuerhs
- right-hand valuefieldName
- a field name for the value of which we want to compare the two objectspublic int append(boolean lhs, boolean rhs, java.lang.String fieldName)
builder
the comparison of
two booleans
s.lhs
- left-hand valuerhs
- right-hand valuefieldName
- a field name for the value of which we want to compare the two objectspublic int append(java.lang.Object[] lhs, java.lang.Object[] rhs, java.lang.String fieldName)
Appends to the fieldComparisons
the deep comparison of
two Object
arrays.
==
null
, null
is less than non-null
append(Object, Object, Comparator)
This method will also will be called for the top level of multi-dimensional, ragged, and multi-typed arrays.
lhs
- left-hand arrayrhs
- right-hand arrayfieldName
- a field name for the value of which we want to compare the two objectsjava.lang.ClassCastException
- if rhs
is not assignment-compatible
with lhs
public int append(java.lang.Object[] lhs, java.lang.Object[] rhs, java.util.Comparator<?> comparator, java.lang.String fieldName)
Appends to the fieldComparisons
the deep comparison of
two Object
arrays.
==
null
, null
is less than non-null
append(Object, Object, Comparator)
This method will also will be called for the top level of multi-dimensional, ragged, and multi-typed arrays.
lhs
- left-hand arrayrhs
- right-hand arraycomparator
- Comparator
to use to compare the array elements,
null
means to treat lhs
elements as Comparable
.fieldName
- a field name for the value of which we want to compare the two objectsjava.lang.ClassCastException
- if rhs
is not assignment-compatible
with lhs
public int append(long[] lhs, long[] rhs, java.lang.String fieldName)
Appends to the fieldComparisons
the deep comparison of
two long
arrays.
==
null
, null
is less than non-null
append(long, long)
lhs
- left-hand arrayrhs
- right-hand arrayfieldName
- a field name for the value of which we want to compare the two objectspublic int append(int[] lhs, int[] rhs, java.lang.String fieldName)
Appends to the fieldComparisons
the deep comparison of
two int
arrays.
==
null
, null
is less than non-null
append(int, int)
lhs
- left-hand arrayrhs
- right-hand arrayfieldName
- a field name for the value of which we want to compare the two objectspublic int append(short[] lhs, short[] rhs, java.lang.String fieldName)
Appends to the fieldComparisons
the deep comparison of
two short
arrays.
==
null
, null
is less than non-null
#append(short, short)
lhs
- left-hand arrayrhs
- right-hand arrayfieldName
- a field name for the value of which we want to compare the two objectspublic int append(char[] lhs, char[] rhs, java.lang.String fieldName)
Appends to the fieldComparisons
the deep comparison of
two char
arrays.
==
null
, null
is less than non-null
#append(char, char)
lhs
- left-hand arrayrhs
- right-hand arrayfieldName
- a field name for the value of which we want to compare the two objectspublic int append(byte[] lhs, byte[] rhs, java.lang.String fieldName)
Appends to the fieldComparisons
the deep comparison of
two byte
arrays.
==
null
, null
is less than non-null
#append(byte, byte)
lhs
- left-hand arrayrhs
- right-hand arrayfieldName
- a field name for the value of which we want to compare the two objectspublic int append(double[] lhs, double[] rhs, java.lang.String fieldName)
Appends to the fieldComparisons
the deep comparison of
two double
arrays.
==
null
, null
is less than non-null
#append(double, double)
lhs
- left-hand arrayrhs
- right-hand arrayfieldName
- a field name for the value of which we want to compare the two objectspublic int append(float[] lhs, float[] rhs, java.lang.String fieldName)
Appends to the fieldComparisons
the deep comparison of
two float
arrays.
==
null
, null
is less than non-null
#append(float, float)
lhs
- left-hand arrayrhs
- right-hand arrayfieldName
- a field name for the value of which we want to compare the two objectspublic int append(boolean[] lhs, boolean[] rhs, java.lang.String fieldName)
Appends to the fieldComparisons
the deep comparison of
two boolean
arrays.
==
null
, null
is less than non-null
append(boolean, boolean)
lhs
- left-hand arrayrhs
- right-hand arrayfieldName
- a field name for the value of which we want to compare the two objectspublic int getComparison()
builder
has judged the "left-hand" side
as less than, greater than, or equal to the "right-hand"
side.