Uploaded image for project: 'Commons Lang'
  1. Commons Lang
  2. LANG-1113

Manages Comparable in EqualsBuilder

    XMLWordPrintableJSON

Details

    Description

      As explained by LANG-393 and LANG-467, EqualsBuilder fails to provide a proper way to handle BigDecimal since BigDecimal takes the scale into account when using equals() but not with compareTo().

      Would it be possible to add a method appendComparable() in EqualsBuilder to manages Comparable objects when we want to build equality on compareTo() and not equals()?

      Making clear that EqualsBuilder is not compliant with HashCodeBuilder when using this method.

      For example:

          return new EqualsBuilder()
              .append(this.name, other.name)
              .appendComparable(this.amount, other.amount)
              .isEqual();
      

      with:

      EqualsBuilder.java
      	/**
      	 * <p>Test if two <code>Comparable</code>s are equal using their
      	 * <code>compareTo</code> method.</p>
      	 * <p>This may break the <code>equals</code>/<code>hashcode</code> contract but
      	 * it is useful in some situation, specially with {@link java.math.BigDecimal}.</p>
      	 * 
      	 * @param lhs  the left hand comparable
      	 * @param rhs  the right hand comparable
      	 * @return EqualsBuilder - used to chain calls.
      	 */
      	public <T extends Comparable<? super T>> EqualsBuilder appendComparable(T lhs, T rhs) {
      		if (isEquals == false) {
      			return this;
      		}
      		if (lhs == rhs) {
      			return this;
      		}
      		if (lhs == null || rhs == null) {
      			isEquals = false;
      			return this;
      		} else if (lhs.compareTo(rhs) != 0) {
      			isEquals = false;
      			return this;
      		}
      		return this;
      	}
      	
      	/**
      	 * <p>Performs a deep comparison of two <code>Comparable</code> arrays.</p>
      	 * 
      	 * @param lhs  the left hand <code>Comparable[]</code>
      	 * @param rhs  the right hand <code>Comparable[]</code>
      	 * @return EqualsBuilder - used to chain calls.
      	 * @see #appendComparable(Comparable, Comparable)
      	 */
      	public <T extends Comparable<? super T>> EqualsBuilder appendComparable(T[] lhs, T[] rhs) {
      		if (isEquals == false) {
      			return this;
      		}
      		if (lhs == rhs) {
      			return this;
      		}
      		if (lhs == null || rhs == null) {
      			isEquals = false;
      			return this;
      		}
      		if (lhs.length != rhs.length) {
      			isEquals = false;
      			return this;
      		}
      		for (int i = 0; i < lhs.length && isEquals; ++i) {
      			appendComparable(lhs[i], rhs[i]);
      		}
      		return this;
      	}
      

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              smeyffret Simon Meyffret
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated: