You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/08/26 19:09:07 UTC

[commons-lang] 07/10: PMD: Implement equals()

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git

commit 49ef6b530c89c5f7181aa3fbdb957155a6802b05
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Aug 26 14:57:21 2022 -0400

    PMD: Implement equals()
---
 .../org/apache/commons/lang3/builder/HashCodeBuilder.java  | 14 +++++++++++++-
 .../apache/commons/lang3/builder/HashCodeBuilderTest.java  | 13 +++++++++++++
 2 files changed, 26 insertions(+), 1 deletion(-)

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<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
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());