commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Benjamin Manes (JIRA)" <j...@apache.org>
Subject [jira] Created: (LANG-342) HashCodeBuilder.append(long) is incorrect
Date Fri, 22 Jun 2007 07:43:26 GMT
HashCodeBuilder.append(long) is incorrect
-----------------------------------------

                 Key: LANG-342
                 URL: https://issues.apache.org/jira/browse/LANG-342
             Project: Commons Lang
          Issue Type: Bug
            Reporter: Benjamin Manes
            Priority: Minor


I was looking at using HashCodeBuilder rather than always writing out the strategy by hand,
and I noticed one potential mistake:
    /**
     * Append a hashCode for a long.
     *
     * @param value  the long to add to the hashCode
     * @return this
     */
    public HashCodeBuilder append(long value)
    {
        iTotal = iTotal * iConstant + ((int) (value ^ (value >> 32))); 
        return this;
    }
 
whereas Effective Java and Long.hashCode() use:
    /**
     * Returns a hash code for this <code>Long</code>. The result is
     * the exclusive OR of the two halves of the primitive
     * <code>long</code> value held by this <code>Long</code> 
     * object. That is, the hashcode is the value of the expression:
     * <blockquote><pre>
     * (int)(this.longValue()^(this.longValue()&gt;&gt;&gt;32))
     * </pre></blockquote> 
     *
     * @return  a hash code value for this object.
     */
    public int hashCode() {
      return (int)(value ^ (value >>> 32));
    }

So the author accidentally used a signed right-shift rather than an unsigned.

----

Stephen Colebourne noted that while this is a bug, it is minor and could have backward compatability
issues.  I would simply recommend that a non-JavaDoc comment be added noting this method doesn't
follow "Effective Java" correctly.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message