From 993e58a2c973474ef0529984fdb89f78258c85e9 Mon Sep 17 00:00:00 2001 From: "Shane F. Carr" Date: Wed, 3 Feb 2021 21:48:19 -0600 Subject: [PATCH] ICU-20888 Hide or remove old list formatter methods --- icu4c/source/i18n/measfmt.cpp | 8 +- icu4c/source/i18n/unicode/listformatter.h | 25 +++--- .../test/intltest/listformattertest.cpp | 47 +++++------ .../source/test/intltest/listformattertest.h | 6 +- .../src/com/ibm/icu/text/ListFormatter.java | 78 +------------------ .../src/com/ibm/icu/text/MeasureFormat.java | 26 +++---- .../dev/test/format/ListFormatterTest.java | 16 ++-- 7 files changed, 62 insertions(+), 144 deletions(-) diff --git a/icu4c/source/i18n/measfmt.cpp b/icu4c/source/i18n/measfmt.cpp index 08e6f663912..ec8b47a503a 100644 --- a/icu4c/source/i18n/measfmt.cpp +++ b/icu4c/source/i18n/measfmt.cpp @@ -581,7 +581,10 @@ void MeasureFormat::initMeasureFormat( UMeasureFormatWidth w, NumberFormat *nfToAdopt, UErrorCode &status) { - static const char *listStyles[] = {"unit", "unit-short", "unit-narrow"}; + static const UListFormatterWidth listWidths[] = { + ULISTFMT_WIDTH_WIDE, + ULISTFMT_WIDTH_SHORT, + ULISTFMT_WIDTH_NARROW}; LocalPointer nf(nfToAdopt); if (U_FAILURE(status)) { return; @@ -620,7 +623,8 @@ void MeasureFormat::initMeasureFormat( delete listFormatter; listFormatter = ListFormatter::createInstance( locale, - listStyles[getRegularWidth(fWidth)], + ULISTFMT_TYPE_UNITS, + listWidths[getRegularWidth(fWidth)], status); } diff --git a/icu4c/source/i18n/unicode/listformatter.h b/icu4c/source/i18n/unicode/listformatter.h index 76a728942d9..d7ab1d20865 100644 --- a/icu4c/source/i18n/unicode/listformatter.h +++ b/icu4c/source/i18n/unicode/listformatter.h @@ -197,22 +197,6 @@ class U_I18N_API ListFormatter : public UObject{ */ static ListFormatter* createInstance( const Locale& locale, UListFormatterType type, UListFormatterWidth width, UErrorCode& errorCode); - -#ifndef U_HIDE_INTERNAL_API - /** - * Creates a ListFormatter appropriate for a locale and style. - * - * TODO(ICU-20888): Remove this in ICU 68. - * - * @param locale The locale. - * @param style the style, either "standard", "or", "unit", "unit-narrow", or "unit-short" - * @param errorCode ICU error code, set if no data available for the given locale. - * @return A ListFormatter object created from internal data derived from - * CLDR data. - * @internal - */ - static ListFormatter* createInstance(const Locale& locale, const char* style, UErrorCode& errorCode); -#endif /* U_HIDE_INTERNAL_API */ /** * Destructor. @@ -273,6 +257,15 @@ class U_I18N_API ListFormatter : public UObject{ #endif /* U_HIDE_INTERNAL_API */ private: + + /** + * Creates a ListFormatter appropriate for a locale and style. + * + * @param locale The locale. + * @param style the style, either "standard", "or", "unit", "unit-narrow", or "unit-short" + */ + static ListFormatter* createInstance(const Locale& locale, const char* style, UErrorCode& errorCode); + static void initializeHash(UErrorCode& errorCode); static const ListFormatInternal* getListFormatInternal(const Locale& locale, const char *style, UErrorCode& errorCode); struct ListPatternsSink; diff --git a/icu4c/source/test/intltest/listformattertest.cpp b/icu4c/source/test/intltest/listformattertest.cpp index 34536e6e7cb..831642241e6 100644 --- a/icu4c/source/test/intltest/listformattertest.cpp +++ b/icu4c/source/test/intltest/listformattertest.cpp @@ -45,7 +45,6 @@ void ListFormatterTest::runIndexedTest(int32_t index, UBool exec, TESTCASE_AUTO(TestFieldPositionIteratorWith3ItemsPatternShift); TESTCASE_AUTO(TestFormattedValue); TESTCASE_AUTO(TestDifferentStyles); - TESTCASE_AUTO(TestBadStylesFail); TESTCASE_AUTO(TestCreateStyled); TESTCASE_AUTO(TestContextual); TESTCASE_AUTO(TestNextPosition); @@ -276,8 +275,11 @@ void ListFormatterTest::RunTestFieldPositionIteratorWithNItemsPatternShift( const char16_t *expectedFormatted, const char* testName) { IcuTestErrorCode errorCode(*this, testName); - LocalPointer formatter( - ListFormatter::createInstance(Locale("ur", "IN"), "unit-narrow", errorCode)); + LocalPointer formatter(ListFormatter::createInstance( + Locale("ur", "IN"), + ULISTFMT_TYPE_UNITS, + ULISTFMT_WIDTH_NARROW, + errorCode)); if (U_FAILURE(errorCode)) { dataerrln( "ListFormatter::createInstance(Locale(\"ur\", \"IN\"), \"unit-narrow\", errorCode) failed in " @@ -608,16 +610,22 @@ void ListFormatterTest::TestFormattedValue() { } } -void ListFormatterTest::DoTheRealListStyleTesting(Locale locale, - UnicodeString items[], int itemCount, - const char* style, const char* expected, IcuTestErrorCode status) { +void ListFormatterTest::DoTheRealListStyleTesting( + Locale locale, + UnicodeString items[], + int itemCount, + UListFormatterType type, + UListFormatterWidth width, + const char* expected, + IcuTestErrorCode status) { LocalPointer formatter( - ListFormatter::createInstance(locale, style, status)); + ListFormatter::createInstance(locale, type, width, status)); UnicodeString actualResult; formatter->format(items, itemCount, actualResult, status); - assertEquals(style, UnicodeString(expected), actualResult); + assertEquals(Int64ToUnicodeString(type) + "-" + Int64ToUnicodeString(width), + UnicodeString(expected), actualResult); } void ListFormatterTest::TestDifferentStyles() { @@ -625,24 +633,11 @@ void ListFormatterTest::TestDifferentStyles() { UnicodeString input[4] = { u"rouge", u"jaune", u"bleu", u"vert" }; IcuTestErrorCode status(*this, "TestDifferentStyles()"); - DoTheRealListStyleTesting(locale, input, 4, "standard", "rouge, jaune, bleu et vert", status); - DoTheRealListStyleTesting(locale, input, 4, "or", "rouge, jaune, bleu ou vert", status); - DoTheRealListStyleTesting(locale, input, 4, "unit", "rouge, jaune, bleu et vert", status); - DoTheRealListStyleTesting(locale, input, 4, "unit-narrow", "rouge jaune bleu vert", status); - DoTheRealListStyleTesting(locale, input, 4, "unit-short", "rouge, jaune, bleu et vert", status); -} - -void ListFormatterTest::TestBadStylesFail() { - Locale locale("fr"); - const char * badStyles[4] = { "", "duration", "duration-short", "something-clearly-wrong" }; - IcuTestErrorCode status(*this, "TestBadStylesFail()"); - - for (int i = 0; i < 4; ++i) { - LocalPointer formatter(ListFormatter::createInstance(locale, badStyles[i], status)); - if (!status.expectErrorAndReset(U_MISSING_RESOURCE_ERROR, "style \"%s\"", badStyles[i])) { - // Do nothing, expectErrorAndReset already reports the error - } - } + DoTheRealListStyleTesting(locale, input, 4, ULISTFMT_TYPE_AND, ULISTFMT_WIDTH_WIDE, "rouge, jaune, bleu et vert", status); + DoTheRealListStyleTesting(locale, input, 4, ULISTFMT_TYPE_OR, ULISTFMT_WIDTH_WIDE, "rouge, jaune, bleu ou vert", status); + DoTheRealListStyleTesting(locale, input, 4, ULISTFMT_TYPE_UNITS, ULISTFMT_WIDTH_WIDE, "rouge, jaune, bleu et vert", status); + DoTheRealListStyleTesting(locale, input, 4, ULISTFMT_TYPE_UNITS, ULISTFMT_WIDTH_NARROW, "rouge jaune bleu vert", status); + DoTheRealListStyleTesting(locale, input, 4, ULISTFMT_TYPE_UNITS, ULISTFMT_WIDTH_SHORT, "rouge, jaune, bleu et vert", status); } void ListFormatterTest::TestCreateStyled() { diff --git a/icu4c/source/test/intltest/listformattertest.h b/icu4c/source/test/intltest/listformattertest.h index aeba483285a..0805e7be945 100644 --- a/icu4c/source/test/intltest/listformattertest.h +++ b/icu4c/source/test/intltest/listformattertest.h @@ -52,7 +52,6 @@ class ListFormatterTest : public IntlTestWithFieldPosition { void TestFieldPositionIteratorWith3ItemsPatternShift(); void TestFormattedValue(); void TestDifferentStyles(); - void TestBadStylesFail(); void TestCreateStyled(); void TestContextual(); void TestNextPosition(); @@ -111,8 +110,9 @@ class ListFormatterTest : public IntlTestWithFieldPosition { void DoTheRealListStyleTesting( Locale locale, UnicodeString items[], - int32_t itemCount, - const char* style, + int itemCount, + UListFormatterType type, + UListFormatterWidth width, const char* expected, IcuTestErrorCode status); diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/ListFormatter.java b/icu4j/main/classes/core/src/com/ibm/icu/text/ListFormatter.java index e746e8f707a..ebde0457a3e 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/ListFormatter.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/ListFormatter.java @@ -50,66 +50,6 @@ final public class ListFormatter { } private final PatternHandler patternHandler; - /** - * Indicates the style of Listformatter - * TODO(ICU-20888): Remove this in ICU 68. - * @internal - * @deprecated This API is ICU internal only. - */ - @Deprecated - public enum Style { - /** - * Standard, conjunction style. - * @internal - * @deprecated This API is ICU internal only. - */ - @Deprecated - STANDARD("standard"), - /** - * Disjunction style. - * @internal - * @deprecated This API is ICU internal only. - */ - @Deprecated - OR("or"), - /** - * Style for full units - * @internal - * @deprecated This API is ICU internal only. - */ - @Deprecated - UNIT("unit"), - /** - * Style for units in abbreviated form - * @internal - * @deprecated This API is ICU internal only. - */ - @Deprecated - UNIT_SHORT("unit-short"), - /** - * Style for units in narrow form - * @internal - * @deprecated This API is ICU internal only. - */ - @Deprecated - UNIT_NARROW("unit-narrow"); - - private final String name; - - Style(String name) { - this.name = name; - } - /** - * @internal - * @deprecated This API is ICU internal only. - */ - @Deprecated - public String getName() { - return name; - } - - } - /** * Type of meaning expressed by the list. * @@ -416,20 +356,6 @@ final public class ListFormatter { return getInstance(ULocale.forLocale(locale), type, width); } - /** - * Create a list formatter that is appropriate for a locale and style. - * - * @param locale the locale in question. - * @param style the style - * @return ListFormatter - * @internal - * @deprecated This API is ICU internal only. - */ - @Deprecated - public static ListFormatter getInstance(ULocale locale, Style style) { - return cache.get(locale, style.getName()); - } - /** * Create a list formatter that is appropriate for a locale. * @@ -439,7 +365,7 @@ final public class ListFormatter { * @stable ICU 50 */ public static ListFormatter getInstance(ULocale locale) { - return getInstance(locale, Style.STANDARD); + return getInstance(locale, Type.AND, Width.WIDE); } /** @@ -451,7 +377,7 @@ final public class ListFormatter { * @stable ICU 50 */ public static ListFormatter getInstance(Locale locale) { - return getInstance(ULocale.forLocale(locale), Style.STANDARD); + return getInstance(ULocale.forLocale(locale), Type.AND, Width.WIDE); } /** diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/MeasureFormat.java b/icu4j/main/classes/core/src/com/ibm/icu/text/MeasureFormat.java index e0665f09582..b9f072a3a18 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/MeasureFormat.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/MeasureFormat.java @@ -160,21 +160,21 @@ public class MeasureFormat extends UFormat { * * @stable ICU 53 */ - WIDE(ListFormatter.Style.UNIT, UnitWidth.FULL_NAME, UnitWidth.FULL_NAME), + WIDE(ListFormatter.Width.WIDE, UnitWidth.FULL_NAME, UnitWidth.FULL_NAME), /** * Abbreviate when possible. * * @stable ICU 53 */ - SHORT(ListFormatter.Style.UNIT_SHORT, UnitWidth.SHORT, UnitWidth.ISO_CODE), + SHORT(ListFormatter.Width.SHORT, UnitWidth.SHORT, UnitWidth.ISO_CODE), /** * Brief. Use only a symbol for the unit when possible. * * @stable ICU 53 */ - NARROW(ListFormatter.Style.UNIT_NARROW, UnitWidth.NARROW, UnitWidth.SHORT), + NARROW(ListFormatter.Width.NARROW, UnitWidth.NARROW, UnitWidth.SHORT), /** * Identical to NARROW except when formatMeasures is called with an hour and minute; minute and @@ -183,7 +183,7 @@ public class MeasureFormat extends UFormat { * * @stable ICU 53 */ - NUMERIC(ListFormatter.Style.UNIT_NARROW, UnitWidth.NARROW, UnitWidth.SHORT), + NUMERIC(ListFormatter.Width.NARROW, UnitWidth.NARROW, UnitWidth.SHORT), /** * The default format width for getCurrencyFormat(), which is to show the symbol for currency @@ -193,9 +193,9 @@ public class MeasureFormat extends UFormat { * @deprecated ICU 61 This API is ICU internal only. */ @Deprecated - DEFAULT_CURRENCY(ListFormatter.Style.UNIT, UnitWidth.FULL_NAME, UnitWidth.SHORT); + DEFAULT_CURRENCY(ListFormatter.Width.SHORT, UnitWidth.FULL_NAME, UnitWidth.SHORT); - private final ListFormatter.Style listFormatterStyle; + final ListFormatter.Width listWidth; /** * The {@link UnitWidth} (used for newer NumberFormatter API) that corresponds to this @@ -209,15 +209,14 @@ public class MeasureFormat extends UFormat { */ final UnitWidth currencyWidth; - private FormatWidth(ListFormatter.Style style, UnitWidth unitWidth, UnitWidth currencyWidth) { - this.listFormatterStyle = style; + private FormatWidth( + ListFormatter.Width listWidth, + UnitWidth unitWidth, + UnitWidth currencyWidth) { + this.listWidth = listWidth; this.unitWidth = unitWidth; this.currencyWidth = currencyWidth; } - - ListFormatter.Style getListFormatterStyle() { - return listFormatterStyle; - } } /** @@ -457,7 +456,8 @@ public class MeasureFormat extends UFormat { } ListFormatter listFormatter = ListFormatter.getInstance(getLocale(), - formatWidth.getListFormatterStyle()); + ListFormatter.Type.UNITS, + formatWidth.listWidth); if (fieldPosition != DontCareFieldPosition.INSTANCE) { formatMeasuresSlowTrack(listFormatter, appendTo, fieldPosition, measures); return; diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/ListFormatterTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/ListFormatterTest.java index 9407bd5f85a..6bfa5551e6c 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/ListFormatterTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/ListFormatterTest.java @@ -193,9 +193,9 @@ public class ListFormatterTest extends TestFmwk { } - void DoTheRealListStyleTesting(ULocale locale, String items[], ListFormatter.Style style, String expected) { - ListFormatter listFormatter = ListFormatter.getInstance(locale, style); - assertEquals("Style \"" + style + "\"", expected, listFormatter.format((Object[])items)); + void DoTheRealListStyleTesting(ULocale locale, String items[], ListFormatter.Type type, ListFormatter.Width width, String expected) { + ListFormatter listFormatter = ListFormatter.getInstance(locale, type, width); + assertEquals("Style \"" + type + "/" + width + "\"", expected, listFormatter.format((Object[])items)); } @Test @@ -203,11 +203,11 @@ public class ListFormatterTest extends TestFmwk { ULocale locale = ULocale.FRENCH; String[] input = { "rouge", "jaune", "bleu", "vert" }; - DoTheRealListStyleTesting(locale, input, ListFormatter.Style.STANDARD, "rouge, jaune, bleu et vert"); - DoTheRealListStyleTesting(locale, input, ListFormatter.Style.OR, "rouge, jaune, bleu ou vert"); - DoTheRealListStyleTesting(locale, input, ListFormatter.Style.UNIT, "rouge, jaune, bleu et vert"); - DoTheRealListStyleTesting(locale, input, ListFormatter.Style.UNIT_NARROW, "rouge jaune bleu vert"); - DoTheRealListStyleTesting(locale, input, ListFormatter.Style.UNIT_SHORT, "rouge, jaune, bleu et vert"); + DoTheRealListStyleTesting(locale, input, ListFormatter.Type.AND, ListFormatter.Width.WIDE, "rouge, jaune, bleu et vert"); + DoTheRealListStyleTesting(locale, input, ListFormatter.Type.OR, ListFormatter.Width.WIDE, "rouge, jaune, bleu ou vert"); + DoTheRealListStyleTesting(locale, input, ListFormatter.Type.UNITS, ListFormatter.Width.WIDE, "rouge, jaune, bleu et vert"); + DoTheRealListStyleTesting(locale, input, ListFormatter.Type.UNITS, ListFormatter.Width.NARROW, "rouge jaune bleu vert"); + DoTheRealListStyleTesting(locale, input, ListFormatter.Type.UNITS, ListFormatter.Width.SHORT, "rouge, jaune, bleu et vert"); } private boolean isDefaultLocaleEnglishLike() {