From 928febc7057e8e97780443ed19f0529c153126d3 Mon Sep 17 00:00:00 2001 From: Markus Scherer Date: Mon, 5 Apr 2021 14:47:42 -0700 Subject: [PATCH] ICU-21561 rename StringSegment.equals() to contentEquals() and remove hashCode() because of now-missing equals() and because StringSegment is mutable and documented as not suitable for HashMaps --- .../src/com/ibm/icu/impl/StringSegment.java | 18 ++++-------------- .../com/ibm/icu/number/NumberSkeletonImpl.java | 2 +- .../icu/dev/test/impl/StringSegmentTest.java | 7 +++++++ 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/icu4j/main/classes/core/src/com/ibm/icu/impl/StringSegment.java b/icu4j/main/classes/core/src/com/ibm/icu/impl/StringSegment.java index b8f5baf0b2b..19d9eff9205 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/impl/StringSegment.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/impl/StringSegment.java @@ -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. * - *

- * This method does not perform case folding; if you want case-insensitive equality, use + *

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. */ diff --git a/icu4j/main/classes/core/src/com/ibm/icu/number/NumberSkeletonImpl.java b/icu4j/main/classes/core/src/com/ibm/icu/number/NumberSkeletonImpl.java index f36a98a2e11..54555d88c24 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/number/NumberSkeletonImpl.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/number/NumberSkeletonImpl.java @@ -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; } diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/impl/StringSegmentTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/impl/StringSegmentTest.java index 1a2baf6a05a..028f2f9edab 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/impl/StringSegmentTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/impl/StringSegmentTest.java @@ -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 -- 2.40.0