Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package panda.lang.builder;
import java.util.Comparator;
import panda.lang.Objects;
/**
* Assists in implementing {@link java.lang.Comparable#compareTo(Object)} methods.
*
* It is consistent with equals(Object) and
* hashcode() built with {@link EqualsBuilder} and
* {@link Objects#hashCode}.
*
*
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();
* }
* }
*
*
*
* @see java.lang.Comparable
* @see java.lang.Object#equals(Object)
* @see java.lang.Object#hashCode()
* @see EqualsBuilder
*
*/
public class CompareToBuilder implements Builder {
/**
* Current state of the comparison as appended fields are checked.
*/
private int comparison;
/**
*
* 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 {@link #toComparison} to get the result.
*
* Appends to the builder the compareTo(Object) result of the
* superclass.
*
*
* @param superCompareTo result of calling super.compareTo(Object)
* @return this - used to chain append calls
*/
public CompareToBuilder appendSuper(final int superCompareTo) {
if (comparison != 0) {
return this;
}
comparison = superCompareTo;
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
*
*
* lhs must either be an array or implement {@link Comparable}.
*
*
* @param lhs left-hand object
* @param rhs right-hand object
* @return this - used to chain append calls
* @throws ClassCastException if rhs is not assignment-compatible with
* lhs
*/
public CompareToBuilder append(final Object lhs, final 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
*
*
* 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 {@link Comparable} instead.
*
*
* @param lhs left-hand object
* @param rhs right-hand object
* @param comparator Comparator used to compare the objects, null
* means treat lhs as Comparable
* @return this - used to chain append calls
* @throws ClassCastException if rhs is not assignment-compatible with
* lhs
*/
public CompareToBuilder append(final Object lhs, final Object rhs,
final 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) {
@SuppressWarnings("unchecked")
// assume this can be done; if not throw CCE as per Javadoc
final Comparable