From 74eb9e621bfd368a2cea41b2448e1f296cf92961 Mon Sep 17 00:00:00 2001 From: "Shane F. Carr" Date: Tue, 9 Mar 2021 01:33:30 -0600 Subject: [PATCH] ICU-21397 Allow null to unset options in Java NumberRangeFormatter --- .../number/NumberRangeFormatterSettings.java | 32 ++++++++----------- .../test/number/NumberRangeFormatterTest.java | 20 ++++++++++++ 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/icu4j/main/classes/core/src/com/ibm/icu/number/NumberRangeFormatterSettings.java b/icu4j/main/classes/core/src/com/ibm/icu/number/NumberRangeFormatterSettings.java index 13734e199b6..6843f5e9224 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/number/NumberRangeFormatterSettings.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/number/NumberRangeFormatterSettings.java @@ -149,41 +149,37 @@ public abstract class NumberRangeFormatterSettings current = this; while (current != null) { + long keyBitmask = (1L << current.key); + if (0 != (seen & keyBitmask)) { + current = current.parent; + continue; + } + seen |= keyBitmask; switch (current.key) { case KEY_MACROS: // ignored for now break; case KEY_LOCALE: - if (macros.loc == null) { - macros.loc = (ULocale) current.value; - } + macros.loc = (ULocale) current.value; break; case KEY_FORMATTER_1: - if (macros.formatter1 == null) { - macros.formatter1 = (UnlocalizedNumberFormatter) current.value; - } + macros.formatter1 = (UnlocalizedNumberFormatter) current.value; break; case KEY_FORMATTER_2: - if (macros.formatter2 == null) { - macros.formatter2 = (UnlocalizedNumberFormatter) current.value; - } + macros.formatter2 = (UnlocalizedNumberFormatter) current.value; break; case KEY_SAME_FORMATTERS: - if (macros.sameFormatters == -1) { - macros.sameFormatters = (boolean) current.value ? 1 : 0; - } + macros.sameFormatters = (boolean) current.value ? 1 : 0; break; case KEY_COLLAPSE: - if (macros.collapse == null) { - macros.collapse = (RangeCollapse) current.value; - } + macros.collapse = (RangeCollapse) current.value; break; case KEY_IDENTITY_FALLBACK: - if (macros.identityFallback == null) { - macros.identityFallback = (RangeIdentityFallback) current.value; - } + macros.identityFallback = (RangeIdentityFallback) current.value; break; default: throw new AssertionError("Unknown key: " + current.key); diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/number/NumberRangeFormatterTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/number/NumberRangeFormatterTest.java index 0788513337a..02ffbfcee84 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/number/NumberRangeFormatterTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/number/NumberRangeFormatterTest.java @@ -664,6 +664,26 @@ public class NumberRangeFormatterTest extends TestFmwk { "5,000–5,000,000"); } + @Test + public void test21397_UnsetNull() { + assertFormatRange( + "Unset identity fallback", + NumberRangeFormatter.with() + .identityFallback(RangeIdentityFallback.RANGE) + .identityFallback(null), + new ULocale("en-us"), + "1–5", + "~5", + "~5", + "0–3", + "~0", + "3–3,000", + "3,000–5,000", + "4,999–5,001", + "~5,000", + "5,000–5,000,000"); + } + @Test public void testPlurals() { // Locale sl has interesting plural forms: