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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to