Gaffer
Gaffer copied to clipboard
Refactor existing hashCode() to include class for methods using the Apache Commons Lang 3 style
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();
}
}
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
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 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.