ustramooner / gwt-geom

gwt-geom is a port of the OpenJDK java.awt.geom package to work on GWT
GNU General Public License v2.0
13 stars 4 forks source link

HashCode wrong #1

Open murkle opened 8 years ago

murkle commented 8 years ago

The logic in HashCode is wrong - this will give the same answer if you send the numbers in a different order. hashcode += (value.hashCode() * 32);

http://stackoverflow.com/questions/892618/create-a-hashcode-of-two-numbers suggests hashcode += hashcode * 31 + value.hashCode();

haumacher commented 7 years ago

Why were the hashCode() implementations changed in comparison to the OpenJDK implementations anyway? GWT seems to support the required Double.doubleToLongBits(double) method, see http://www.gwtproject.org/doc/latest/RefJreEmulation.html

Good hashCode functions use prime numbers as multipliers - 32 is "not fully" prime. To build a hashCode from multiple numbers, there is no point in multiplying each number with the same prime before adding them. Instead, the current result must be multiplied with a prime before adding the next number.

Instead of

    public void append(Double value) {
        hashcode += (value.hashCode() * 32);
    }

something like that should be used:

    public void append(Double value) {
        hashcode = hashcode * 31 + value.hashCode();
    }
murkle commented 7 years ago

This project is from 7 years ago - I guess GWT didn't support hashCode() then :)