diff --git a/src/main/java/org/apache/commons/lang3/builder/HashCodeBuilder.java b/src/main/java/org/apache/commons/lang3/builder/HashCodeBuilder.java index e8e603998..cc67e86aa 100644 --- a/src/main/java/org/apache/commons/lang3/builder/HashCodeBuilder.java +++ b/src/main/java/org/apache/commons/lang3/builder/HashCodeBuilder.java @@ -746,7 +746,7 @@ public class HashCodeBuilder implements Builder { // 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 { 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 diff --git a/src/test/java/org/apache/commons/lang3/builder/HashCodeBuilderTest.java b/src/test/java/org/apache/commons/lang3/builder/HashCodeBuilderTest.java index 8741f2104..cf045e946 100644 --- a/src/test/java/org/apache/commons/lang3/builder/HashCodeBuilderTest.java +++ b/src/test/java/org/apache/commons/lang3/builder/HashCodeBuilderTest.java @@ -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());