Gaffer icon indicating copy to clipboard operation
Gaffer copied to clipboard

Refactor existing hashCode() to include class for methods using the Apache Commons Lang 3 style

Open GCHQDev404 opened this issue 3 years ago • 3 comments

Append class to all HashCodeBuilders in Gaffer for the below issue to minimise hash collisions.

@Test
    void name() {
        Foo foo = new Foo();
        Bar bar = new Bar();

        assertFalse(foo.equals(bar));
        assertNotEquals(foo.hashCode(), bar.hashCode()); //fails
    }

    class Bar {
        int a = 3;

        @Override
        public int hashCode() {
            return new HashCodeBuilder(7, 13) //not unique 
                    .append(a)
                    // needs class
                    .hashCode();

        }
    }

    class Foo {
        int a = 3;

        @Override
        public int hashCode() {
            return new HashCodeBuilder(7, 13) //not unique
                    .append(a)
                    // needs class
                    .hashCode();
        }
    }

GCHQDev404 avatar Nov 03 '20 16:11 GCHQDev404

Hello, this looks like a interesting first issue to me, but could you give me some more information how sould this calss including work. Thanks

matthew-smith0 avatar Nov 12 '20 09:11 matthew-smith0

Hi @matthew-smith0

I'm a little dubious about this issue to be honest. The hashcodes only have to be implemented so that objects that are equal should have the same hashcode. There is no guarantee that other non-equal objects have different hashcodes, nor that objects which have the same hashcode should be equal. So in my opinion, hashing the class is pointless as all equal objects should have the same class anyway.

If there are instances where the hashcode hasn't been implemented with the lang3 style then it should be changed as that's part of our ways of working. But I'm not sure about hashing the class. @GCHQDev404 could you elaborate?

d47853 avatar Nov 12 '20 11:11 d47853

@d47853 It was a suggestion around minimising hash collisions. Since numbers given to Hashcode builder seem to be duplicated a lot in Gaffer.

I'm happy for this ticket to be closed if required.

GCHQDev404 avatar Dec 14 '20 15:12 GCHQDev404