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).
| Method from org.apache.commons.lang.builder.CompareToBuilder Detail: |
public CompareToBuilder append(Object lhs,
Object rhs) {
return append(lhs, rhs, null);
}
Appends to the builder the comparison of
two Objects.
- Check if
lhs == rhs
- Check if either
lhs or rhs is null,
a null object is less than a non-null object
- Check the object contents
lhs must either be an array or implement Comparable .
|
public CompareToBuilder append(long lhs,
long rhs) {
if (comparison != 0) {
return this;
}
comparison = ((lhs < rhs) ? -1 : ((lhs > rhs) ? 1 : 0));
return this;
}
Appends to the builder the comparison of
two longs. |
public CompareToBuilder append(int lhs,
int rhs) {
if (comparison != 0) {
return this;
}
comparison = ((lhs < rhs) ? -1 : ((lhs > rhs) ? 1 : 0));
return this;
}
Appends to the builder the comparison of
two ints. |
public CompareToBuilder append(short lhs,
short rhs) {
if (comparison != 0) {
return this;
}
comparison = ((lhs < rhs) ? -1 : ((lhs > rhs) ? 1 : 0));
return this;
}
Appends to the builder the comparison of
two shorts. |
public CompareToBuilder append(char lhs,
char rhs) {
if (comparison != 0) {
return this;
}
comparison = ((lhs < rhs) ? -1 : ((lhs > rhs) ? 1 : 0));
return this;
}
Appends to the builder the comparison of
two chars. |
public CompareToBuilder append(byte lhs,
byte rhs) {
if (comparison != 0) {
return this;
}
comparison = ((lhs < rhs) ? -1 : ((lhs > rhs) ? 1 : 0));
return this;
}
Appends to the builder the comparison of
two bytes. |
public CompareToBuilder append(double lhs,
double rhs) {
if (comparison != 0) {
return this;
}
comparison = NumberUtils.compare(lhs, rhs);
return this;
}
Appends to the builder the comparison of
two doubles.
This handles NaNs, Infinities, and -0.0.
It is compatible with the hash code generated by
HashCodeBuilder.
|
public CompareToBuilder append(float lhs,
float rhs) {
if (comparison != 0) {
return this;
}
comparison = NumberUtils.compare(lhs, rhs);
return this;
}
Appends to the builder the comparison of
two floats.
This handles NaNs, Infinities, and -0.0.
It is compatible with the hash code generated by
HashCodeBuilder.
|
public CompareToBuilder append(boolean lhs,
boolean rhs) {
if (comparison != 0) {
return this;
}
if (lhs == rhs) {
return this;
}
if (lhs == false) {
comparison = -1;
} else {
comparison = +1;
}
return this;
}
Appends to the builder the comparison of
two booleanss. |
public CompareToBuilder append(Object[] lhs,
Object[] rhs) {
return append(lhs, rhs, null);
}
Appends to the builder the deep comparison of
two Object 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.
|
public CompareToBuilder append(long[] lhs,
long[] rhs) {
if (comparison != 0) {
return this;
}
if (lhs == rhs) {
return this;
}
if (lhs == null) {
comparison = -1;
return this;
}
if (rhs == null) {
comparison = +1;
return this;
}
if (lhs.length != rhs.length) {
comparison = (lhs.length < rhs.length) ? -1 : +1;
return this;
}
for (int i = 0; i < lhs.length && comparison == 0; i++) {
append(lhs[i], rhs[i]);
}
return this;
}
|
public CompareToBuilder append(int[] lhs,
int[] rhs) {
if (comparison != 0) {
return this;
}
if (lhs == rhs) {
return this;
}
if (lhs == null) {
comparison = -1;
return this;
}
if (rhs == null) {
comparison = +1;
return this;
}
if (lhs.length != rhs.length) {
comparison = (lhs.length < rhs.length) ? -1 : +1;
return this;
}
for (int i = 0; i < lhs.length && comparison == 0; i++) {
append(lhs[i], rhs[i]);
}
return this;
}
|
public CompareToBuilder append(short[] lhs,
short[] rhs) {
if (comparison != 0) {
return this;
}
if (lhs == rhs) {
return this;
}
if (lhs == null) {
comparison = -1;
return this;
}
if (rhs == null) {
comparison = +1;
return this;
}
if (lhs.length != rhs.length) {
comparison = (lhs.length < rhs.length) ? -1 : +1;
return this;
}
for (int i = 0; i < lhs.length && comparison == 0; i++) {
append(lhs[i], rhs[i]);
}
return this;
}
|
public CompareToBuilder append(char[] lhs,
char[] rhs) {
if (comparison != 0) {
return this;
}
if (lhs == rhs) {
return this;
}
if (lhs == null) {
comparison = -1;
return this;
}
if (rhs == null) {
comparison = +1;
return this;
}
if (lhs.length != rhs.length) {
comparison = (lhs.length < rhs.length) ? -1 : +1;
return this;
}
for (int i = 0; i < lhs.length && comparison == 0; i++) {
append(lhs[i], rhs[i]);
}
return this;
}
|
public CompareToBuilder append(byte[] lhs,
byte[] rhs) {
if (comparison != 0) {
return this;
}
if (lhs == rhs) {
return this;
}
if (lhs == null) {
comparison = -1;
return this;
}
if (rhs == null) {
comparison = +1;
return this;
}
if (lhs.length != rhs.length) {
comparison = (lhs.length < rhs.length) ? -1 : +1;
return this;
}
for (int i = 0; i < lhs.length && comparison == 0; i++) {
append(lhs[i], rhs[i]);
}
return this;
}
|
public CompareToBuilder append(double[] lhs,
double[] rhs) {
if (comparison != 0) {
return this;
}
if (lhs == rhs) {
return this;
}
if (lhs == null) {
comparison = -1;
return this;
}
if (rhs == null) {
comparison = +1;
return this;
}
if (lhs.length != rhs.length) {
comparison = (lhs.length < rhs.length) ? -1 : +1;
return this;
}
for (int i = 0; i < lhs.length && comparison == 0; i++) {
append(lhs[i], rhs[i]);
}
return this;
}
|
public CompareToBuilder append(float[] lhs,
float[] rhs) {
if (comparison != 0) {
return this;
}
if (lhs == rhs) {
return this;
}
if (lhs == null) {
comparison = -1;
return this;
}
if (rhs == null) {
comparison = +1;
return this;
}
if (lhs.length != rhs.length) {
comparison = (lhs.length < rhs.length) ? -1 : +1;
return this;
}
for (int i = 0; i < lhs.length && comparison == 0; i++) {
append(lhs[i], rhs[i]);
}
return this;
}
|
public CompareToBuilder append(boolean[] lhs,
boolean[] rhs) {
if (comparison != 0) {
return this;
}
if (lhs == rhs) {
return this;
}
if (lhs == null) {
comparison = -1;
return this;
}
if (rhs == null) {
comparison = +1;
return this;
}
if (lhs.length != rhs.length) {
comparison = (lhs.length < rhs.length) ? -1 : +1;
return this;
}
for (int i = 0; i < lhs.length && comparison == 0; i++) {
append(lhs[i], rhs[i]);
}
return this;
}
|
public CompareToBuilder append(Object lhs,
Object rhs,
Comparator comparator) {
if (comparison != 0) {
return this;
}
if (lhs == rhs) {
return this;
}
if (lhs == null) {
comparison = -1;
return this;
}
if (rhs == null) {
comparison = +1;
return this;
}
if (lhs.getClass().isArray()) {
// switch on type of array, to dispatch to the correct handler
// handles multi dimensional arrays
// throws a ClassCastException if rhs is not the correct array type
if (lhs instanceof long[]) {
append((long[]) lhs, (long[]) rhs);
} else if (lhs instanceof int[]) {
append((int[]) lhs, (int[]) rhs);
} else if (lhs instanceof short[]) {
append((short[]) lhs, (short[]) rhs);
} else if (lhs instanceof char[]) {
append((char[]) lhs, (char[]) rhs);
} else if (lhs instanceof byte[]) {
append((byte[]) lhs, (byte[]) rhs);
} else if (lhs instanceof double[]) {
append((double[]) lhs, (double[]) rhs);
} else if (lhs instanceof float[]) {
append((float[]) lhs, (float[]) rhs);
} else if (lhs instanceof boolean[]) {
append((boolean[]) lhs, (boolean[]) rhs);
} else {
// not an array of primitives
// throws a ClassCastException if rhs is not an array
append((Object[]) lhs, (Object[]) rhs, comparator);
}
} else {
// the simple case, not an array, just test the element
if (comparator == null) {
comparison = ((Comparable) lhs).compareTo(rhs);
} else {
comparison = comparator.compare(lhs, rhs);
}
}
return this;
}
Appends to the builder the comparison of
two Objects.
- Check if
lhs == rhs
- Check if either
lhs or rhs is null,
a null object is less than a non-null object
- Check the object contents
If 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.
|
public CompareToBuilder append(Object[] lhs,
Object[] rhs,
Comparator comparator) {
if (comparison != 0) {
return this;
}
if (lhs == rhs) {
return this;
}
if (lhs == null) {
comparison = -1;
return this;
}
if (rhs == null) {
comparison = +1;
return this;
}
if (lhs.length != rhs.length) {
comparison = (lhs.length < rhs.length) ? -1 : +1;
return this;
}
for (int i = 0; i < lhs.length && comparison == 0; i++) {
append(lhs[i], rhs[i], comparator);
}
return this;
}
Appends to the builder the deep comparison of
two Object 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.
|
public CompareToBuilder appendSuper(int superCompareTo) {
if (comparison != 0) {
return this;
}
comparison = superCompareTo;
return this;
}
|
public static int reflectionCompare(Object lhs,
Object rhs) {
return reflectionCompare(lhs, rhs, false, null, null);
}
Compares two Objects 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 and rhs are null,
they are considered equal.
|
public static int reflectionCompare(Object lhs,
Object rhs,
boolean compareTransients) {
return reflectionCompare(lhs, rhs, compareTransients, null, null);
}
Compares two Objects 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 is true,
compares transient members. Otherwise ignores them, as they
are likely derived fields.
- Superclass fields will be compared
If both lhs and rhs are null,
they are considered equal.
|
public static int reflectionCompare(Object lhs,
Object rhs,
Collection excludeFields) {
return reflectionCompare(lhs, rhs, ReflectionToStringBuilder.toNoNullStringArray(excludeFields));
}
Compares two Objects 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 is true,
compares transient members. Otherwise ignores them, as they
are likely derived fields.
- Superclass fields will be compared
If both lhs and rhs are null,
they are considered equal.
|
public static int reflectionCompare(Object lhs,
Object rhs,
String[] excludeFields) {
return reflectionCompare(lhs, rhs, false, null, excludeFields);
}
Compares two Objects 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 is true,
compares transient members. Otherwise ignores them, as they
are likely derived fields.
- Superclass fields will be compared
If both lhs and rhs are null,
they are considered equal.
|
public static int reflectionCompare(Object lhs,
Object rhs,
boolean compareTransients,
Class reflectUpToClass) {
return reflectionCompare(lhs, rhs, false, reflectUpToClass, null);
}
Compares two Objects 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 is true,
compares transient members. Otherwise ignores them, as they
are likely derived fields.
- Compares superclass fields up to and including
reflectUpToClass.
If reflectUpToClass is null, compares all superclass fields.
If both lhs and rhs are null,
they are considered equal.
|
public static int reflectionCompare(Object lhs,
Object rhs,
boolean compareTransients,
Class reflectUpToClass,
String[] excludeFields) {
if (lhs == rhs) {
return 0;
}
if (lhs == null || rhs == null) {
throw new NullPointerException();
}
Class lhsClazz = lhs.getClass();
if (!lhsClazz.isInstance(rhs)) {
throw new ClassCastException();
}
CompareToBuilder compareToBuilder = new CompareToBuilder();
reflectionAppend(lhs, rhs, lhsClazz, compareToBuilder, compareTransients, excludeFields);
while (lhsClazz.getSuperclass() != null && lhsClazz != reflectUpToClass) {
lhsClazz = lhsClazz.getSuperclass();
reflectionAppend(lhs, rhs, lhsClazz, compareToBuilder, compareTransients, excludeFields);
}
return compareToBuilder.toComparison();
}
Compares two Objects 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 is true,
compares transient members. Otherwise ignores them, as they
are likely derived fields.
- Compares superclass fields up to and including
reflectUpToClass.
If reflectUpToClass is null, compares all superclass fields.
If both lhs and rhs are null,
they are considered equal.
|
public int toComparison() {
return comparison;
}
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. |