]> granicus.if.org Git - icu/commitdiff
ICU-21561 rename StringSegment.equals() to contentEquals()
authorMarkus Scherer <markus.icu@gmail.com>
Mon, 5 Apr 2021 21:47:42 +0000 (14:47 -0700)
committerMarkus Scherer <markus.icu@gmail.com>
Tue, 6 Apr 2021 00:00:31 +0000 (17:00 -0700)
and remove hashCode() because of now-missing equals() and because
StringSegment is mutable and documented as not suitable for HashMaps

icu4j/main/classes/core/src/com/ibm/icu/impl/StringSegment.java
icu4j/main/classes/core/src/com/ibm/icu/number/NumberSkeletonImpl.java
icu4j/main/tests/core/src/com/ibm/icu/dev/test/impl/StringSegmentTest.java

index b8f5baf0b2b65fa659e5e649fd72593c55b3bc1f..19d9eff9205490ecffda28761ad8985eafa31006 100644 (file)
@@ -202,23 +202,13 @@ public class StringSegment implements CharSequence {
     }
 
     /**
-     * Equals any CharSequence with the same chars as this segment.
+     * Returns true if this segment contains the same characters as the other CharSequence.
      *
-     * <p>
-     * This method does not perform case folding; if you want case-insensitive equality, use
+     * <p>This method does not perform case folding; if you want case-insensitive equality, use
      * {@link #getCommonPrefixLength}.
      */
-    @Override
-    public boolean equals(Object other) {
-        if (!(other instanceof CharSequence))
-            return false;
-        return Utility.charSequenceEquals(this, (CharSequence) other);
-    }
-
-    /** Returns a hash code equivalent to calling .toString().hashCode() */
-    @Override
-    public int hashCode() {
-        return Utility.charSequenceHashCode(this);
+    public boolean contentEquals(CharSequence other) {
+        return Utility.charSequenceEquals(this, other);
     }
 
     /** Returns a string representation useful for debugging. */
index f36a98a2e11437959ec6eac5e06e8526b4a8504e..54555d88c24e79583cee84f5fa247f9017869da9 100644 (file)
@@ -1368,7 +1368,7 @@ class NumberSkeletonImpl {
 
         /** @return Whether we successfully found and parsed a trailing zero option. */
         private static boolean parseTrailingZeroOption(StringSegment segment, MacroProps macros) {
-            if (segment.equals("w")) {
+            if (segment.contentEquals("w")) {
                 macros.precision = macros.precision.trailingZeroDisplay(TrailingZeroDisplay.HIDE_IF_WHOLE);
                 return true;
             }
index 1a2baf6a05ace17258989a36c58304a033c5b602..028f2f9edab605afe8351237f5e47d9913bfdbbb 100644 (file)
@@ -3,6 +3,8 @@
 package com.ibm.icu.dev.test.impl;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
 
 import org.junit.Test;
 
@@ -47,10 +49,15 @@ public class StringSegmentTest {
     public void testCharAt() {
         StringSegment segment = new StringSegment(SAMPLE_STRING, false);
         assertCharSequenceEquals(SAMPLE_STRING, segment);
+        assertTrue(segment.contentEquals(SAMPLE_STRING));
         segment.adjustOffset(3);
         assertCharSequenceEquals("radio ðŸ“»", segment);
+        assertTrue(segment.contentEquals("radio ðŸ“»"));
+        assertFalse(segment.contentEquals(SAMPLE_STRING));
         segment.setLength(5);
         assertCharSequenceEquals("radio", segment);
+        assertTrue(segment.contentEquals("radio"));
+        assertFalse(segment.contentEquals(SAMPLE_STRING));
     }
 
     @Test