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 2020/06/30 19:07:26 UTC

[commons-text] branch master updated: Reimplement hashCode() and equals() using JRE APIs.

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-text.git


The following commit(s) were added to refs/heads/master by this push:
     new 36af8b1  Reimplement hashCode() and equals() using JRE APIs.
36af8b1 is described below

commit 36af8b1884a3ac7bf323a08a7d62e4a290336d2b
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Jun 30 15:07:19 2020 -0400

    Reimplement hashCode() and equals() using JRE APIs.
---
 .../org/apache/commons/text/TextStringBuilder.java | 25 ++--------------------
 .../apache/commons/text/TextStringBuilderTest.java |  5 +++--
 2 files changed, 5 insertions(+), 25 deletions(-)

diff --git a/src/main/java/org/apache/commons/text/TextStringBuilder.java b/src/main/java/org/apache/commons/text/TextStringBuilder.java
index a0f7480..d9de9f3 100644
--- a/src/main/java/org/apache/commons/text/TextStringBuilder.java
+++ b/src/main/java/org/apache/commons/text/TextStringBuilder.java
@@ -1957,23 +1957,7 @@ public class TextStringBuilder implements CharSequence, Appendable, Serializable
      * @return true if the builders contain the same characters in the same order
      */
     public boolean equals(final TextStringBuilder other) {
-        if (this == other) {
-            return true;
-        }
-        if (other == null) {
-            return false;
-        }
-        if (this.size != other.size) {
-            return false;
-        }
-        final char[] thisBuf = this.buffer;
-        final char[] otherBuf = other.buffer;
-        for (int i = size - 1; i >= 0; i--) {
-            if (thisBuf[i] != otherBuf[i]) {
-                return false;
-            }
-        }
-        return true;
+        return other != null && Arrays.equals(buffer, other.buffer);
     }
 
     /**
@@ -2079,12 +2063,7 @@ public class TextStringBuilder implements CharSequence, Appendable, Serializable
      */
     @Override
     public int hashCode() {
-        final char[] buf = buffer;
-        int hash = 0;
-        for (int i = size - 1; i >= 0; i--) {
-            hash = 31 * hash + buf[i];
-        }
-        return hash;
+        return Arrays.hashCode(buffer);
     }
 
     /**
diff --git a/src/test/java/org/apache/commons/text/TextStringBuilderTest.java b/src/test/java/org/apache/commons/text/TextStringBuilderTest.java
index 51161f6..1cd4106 100644
--- a/src/test/java/org/apache/commons/text/TextStringBuilderTest.java
+++ b/src/test/java/org/apache/commons/text/TextStringBuilderTest.java
@@ -1012,13 +1012,14 @@ public class TextStringBuilderTest {
         final TextStringBuilder sb = new TextStringBuilder();
         final int hc1a = sb.hashCode();
         final int hc1b = sb.hashCode();
-        assertEquals(0, hc1a);
+        final int emptyHc = Arrays.hashCode(sb.getBuffer());
+        assertEquals(emptyHc, hc1a);
         assertEquals(hc1a, hc1b);
 
         sb.append("abc");
         final int hc2a = sb.hashCode();
         final int hc2b = sb.hashCode();
-        assertTrue(hc2a != 0);
+        assertTrue(hc2a != emptyHc);
         assertEquals(hc2a, hc2b);
     }