PMD: Implement equals()

This commit is contained in:
Gary Gregory 2022-08-26 14:57:21 -04:00
parent ce40e5bdf2
commit 49ef6b530c
2 changed files with 26 additions and 1 deletions

View File

@ -746,7 +746,7 @@ public class HashCodeBuilder implements Builder<Integer> {
// some stage. There are backwards compat issues, so
// that will have to wait for the time being. cf LANG-342.
public HashCodeBuilder append(final long value) {
iTotal = iTotal * iConstant + ((int) (value ^ (value >> 32)));
iTotal = iTotal * iConstant + (int) (value ^ value >> 32);
return this;
}
@ -893,6 +893,18 @@ public class HashCodeBuilder implements Builder<Integer> {
return Integer.valueOf(toHashCode());
}
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof HashCodeBuilder)) {
return false;
}
final HashCodeBuilder other = (HashCodeBuilder) obj;
return iTotal == other.iTotal;
}
/**
* The computed {@code hashCode} from toHashCode() is returned due to the likelihood
* of bugs in mis-calling toHashCode() and the unlikeliness of it mattering what the hashCode for

View File

@ -18,6 +18,7 @@
package org.apache.commons.lang3.builder;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
@ -331,6 +332,18 @@ public class HashCodeBuilderTest extends AbstractLangTest {
assertEquals((17 * 37 + h1) * 37 + h2, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
}
@Test
public void testEquals() {
final HashCodeBuilder hcb1 = new HashCodeBuilder(17, 37).append(1).append('a');
final HashCodeBuilder hcb2 = new HashCodeBuilder(17, 37).append(1).append('a');
final HashCodeBuilder hcb3 = new HashCodeBuilder(17, 37).append(2).append('c');
assertEquals(hcb1, hcb1);
assertEquals(hcb1, hcb2);
assertEquals(hcb2, hcb1);
assertNotEquals(hcb1, hcb3);
assertNotEquals(hcb2, hcb3);
}
@Test
public void testFloat() {
assertEquals(17 * 37, new HashCodeBuilder(17, 37).append(0f).toHashCode());