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 400fb65  [TEXT-211] TextStringBuilder.equals whatever the capacity is 
(#281)
400fb65 is described below

commit 400fb6541a887fc6cb46cc03ff3ce93fcba16333
Author: sebx59 <[email protected]>
AuthorDate: Thu Mar 31 16:34:23 2022 +0200

    [TEXT-211] TextStringBuilder.equals whatever the capacity is (#281)
    
    * TEXT-211 - equals whatever the capacity is
    
    Used subarrays for Arrays.equals to allow equals method to be relevant in 
case the 2 TextStringBuilders have not the same capacity.
    
    Updated the test case to reflect the change
    
    * TEXT-211 - Correction to hashCode()
    
    Corrected the way hashCode is calculated, based only on the relevant 
characters of the buffer, independantly of the capacity.
    Modified also the test case with relevant test
    
    * using toString instead of subArrays
    
    * format correction
    
    * rolled back to 1.8 code for equals following TEXT-211
    
    * format correction
    
    damn tabs !
    
    * removed trailing spaces
    
    * removed trailing whitespaces
    
    * format correction
---
 .../org/apache/commons/text/TextStringBuilder.java | 22 +++++++++++++--
 .../apache/commons/text/TextStringBuilderTest.java | 31 +++++++++++++++-------
 2 files changed, 42 insertions(+), 11 deletions(-)

diff --git a/src/main/java/org/apache/commons/text/TextStringBuilder.java 
b/src/main/java/org/apache/commons/text/TextStringBuilder.java
index 1d3186f..4fafcf1 100644
--- a/src/main/java/org/apache/commons/text/TextStringBuilder.java
+++ b/src/main/java/org/apache/commons/text/TextStringBuilder.java
@@ -1858,7 +1858,25 @@ 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) {
-        return other != null && Arrays.equals(buffer, other.buffer);
+        if (this == other) {
+            return true;
+        }
+        if (other == null) {
+            return false;
+        }
+        if (this.size != other.size) {
+            return false;
+        }
+        // Be aware not to use Arrays.equals(buffer, other.buffer) for 
equals() method
+        // as length of the buffers may be different (TEXT-211)
+        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;
     }
 
     /**
@@ -1955,7 +1973,7 @@ public class TextStringBuilder implements CharSequence, 
Appendable, Serializable
      */
     @Override
     public int hashCode() {
-        return Arrays.hashCode(buffer);
+        return this.toString().hashCode();
     }
 
     /**
diff --git a/src/test/java/org/apache/commons/text/TextStringBuilderTest.java 
b/src/test/java/org/apache/commons/text/TextStringBuilderTest.java
index db169a8..581fa02 100644
--- a/src/test/java/org/apache/commons/text/TextStringBuilderTest.java
+++ b/src/test/java/org/apache/commons/text/TextStringBuilderTest.java
@@ -900,24 +900,24 @@ public class TextStringBuilderTest {
 
     @Test
     public void testEquals() {
-        final TextStringBuilder sb1 = new TextStringBuilder();
-        final TextStringBuilder sb2 = new TextStringBuilder();
+        final TextStringBuilder sb1 = new TextStringBuilder(50);
+        final TextStringBuilder sb2 = new TextStringBuilder(100);
         assertTrue(sb1.equals(sb2));
         assertTrue(sb1.equals(sb1));
         assertTrue(sb2.equals(sb2));
-        assertEquals(sb1, (Object) sb2);
+        assertEquals(sb1, sb2);
 
         sb1.append("abc");
         assertFalse(sb1.equals(sb2));
-        assertNotEquals(sb1, (Object) sb2);
+        assertNotEquals(sb1, sb2);
 
         sb2.append("ABC");
         assertFalse(sb1.equals(sb2));
-        assertNotEquals(sb1, (Object) sb2);
+        assertNotEquals(sb1, sb2);
 
         sb2.set("abc");
         assertTrue(sb1.equals(sb2));
-        assertEquals(sb1, (Object) sb2);
+        assertEquals(sb1, sb2);
 
         assertNotEquals(sb1, Integer.valueOf(1));
         assertNotEquals("abc", sb1);
@@ -1057,15 +1057,28 @@ public class TextStringBuilderTest {
         final TextStringBuilder sb = new TextStringBuilder();
         final int hc1a = sb.hashCode();
         final int hc1b = sb.hashCode();
-        final int emptyHc = Arrays.hashCode(sb.getBuffer());
-        assertEquals(emptyHc, hc1a);
         assertEquals(hc1a, hc1b);
 
+        // following TEXT-211 : the hashcode of the buffer may not be equals 
to the hashcode of the TextStringBuilder itself
+        final int emptyHc = Arrays.hashCode(sb.getBuffer());
+        assertNotEquals(emptyHc, hc1a);
+
         sb.append("abc");
         final int hc2a = sb.hashCode();
         final int hc2b = sb.hashCode();
-        assertTrue(hc2a != emptyHc);
         assertEquals(hc2a, hc2b);
+
+        final TextStringBuilder sb2 = new TextStringBuilder(100);
+        final TextStringBuilder sb3 = new TextStringBuilder(10);
+        final int hc2 = sb2.hashCode();
+        final int hc3 = sb3.hashCode();
+        assertEquals(hc2, hc3);
+
+        sb2.append("abc");
+        sb3.append("abc");
+        final int hc2b2 = sb2.hashCode();
+        final int hc3b2 = sb3.hashCode();
+        assertEquals(hc2b2, hc3b2);
     }
 
     @Test

Reply via email to