You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Henri Yandell (JIRA)" <ji...@apache.org> on 2007/08/09 02:30:59 UTC

[jira] Commented: (LANG-342) HashCodeBuilder.append(long) is incorrect

    [ https://issues.apache.org/jira/browse/LANG-342?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12518586 ] 

Henri Yandell commented on LANG-342:
------------------------------------

Non-javadoc note added to HashCodeBuilder.append.

> 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
>             Fix For: 3.0
>
>
> 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.