From e4aee6892f25c81621b1a6302ce3082899f06ad4 Mon Sep 17 00:00:00 2001 From: Michael Ow Date: Fri, 17 Feb 2012 23:14:06 +0000 Subject: [PATCH] ICU-8940 Ensure the new DecimalFormatSymbols is propagated properly in ICU4J RBNF X-SVN-Rev: 31418 --- .../ibm/icu/text/RuleBasedNumberFormat.java | 29 +++++++++++++++---- .../com/ibm/icu/dev/test/format/RbnfTest.java | 28 +++++++++++++++++- 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/RuleBasedNumberFormat.java b/icu4j/main/classes/core/src/com/ibm/icu/text/RuleBasedNumberFormat.java index 1e2786210d8..2a20da78026 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/RuleBasedNumberFormat.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/RuleBasedNumberFormat.java @@ -522,7 +522,12 @@ public class RuleBasedNumberFormat extends NumberFormat { * The formatter's rule sets. */ private transient NFRuleSet[] ruleSets = null; - + + /** + * The formatter's rule sets' descriptions. + */ + private transient String[] ruleSetDescriptions = null; + /** * A pointer to the formatter's default rule set. This is always included * in ruleSets. @@ -1384,7 +1389,17 @@ public class RuleBasedNumberFormat extends NumberFormat { * @draft ICU 49 */ public void setDecimalFormatSymbols(DecimalFormatSymbols newSymbols) { - decimalFormatSymbols = (DecimalFormatSymbols) newSymbols.clone(); + if (newSymbols != null) { + decimalFormatSymbols = (DecimalFormatSymbols) newSymbols.clone(); + if (decimalFormat != null) { + decimalFormat.setDecimalFormatSymbols(decimalFormatSymbols); + } + + // Apply the new decimalFormatSymbols by reparsing the rulesets + for (int i = 0; i < ruleSets.length; i++) { + ruleSets[i].parseRules(ruleSetDescriptions[i], this); + } + } } //----------------------------------------------------------------------- @@ -1437,6 +1452,10 @@ public class RuleBasedNumberFormat extends NumberFormat { DecimalFormat getDecimalFormat() { if (decimalFormat == null) { decimalFormat = (DecimalFormat)NumberFormat.getInstance(locale); + + if (decimalFormatSymbols != null) { + decimalFormat.setDecimalFormatSymbols(decimalFormatSymbols); + } } return decimalFormat; } @@ -1534,7 +1553,7 @@ public class RuleBasedNumberFormat extends NumberFormat { // the rest of the descriptions and finish initializing everything // because we have to know the names and locations of all the rule // sets before we can actually set everything up - String[] ruleSetDescriptions = new String[numRuleSets]; + ruleSetDescriptions = new String[numRuleSets]; int curRuleSet = 0; int start = 0; @@ -1583,11 +1602,9 @@ public class RuleBasedNumberFormat extends NumberFormat { } // finally, we can go back through the temporary descriptions - // list and finish seting up the substructure (and we throw - // away the temporary descriptions as we go) + // list and finish seting up the substructure for (int i = 0; i < ruleSets.length; i++) { ruleSets[i].parseRules(ruleSetDescriptions[i], this); - ruleSetDescriptions[i] = null; } // Now that the rules are initialized, the 'real' default rule diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/RbnfTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/RbnfTest.java index e57000eba5a..baab8486a73 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/RbnfTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/RbnfTest.java @@ -1,6 +1,6 @@ /* ******************************************************************************* - * Copyright (C) 1996-2011, International Business Machines Corporation and * + * Copyright (C) 1996-2012, International Business Machines Corporation and * * others. All Rights Reserved. * ******************************************************************************* */ @@ -16,6 +16,7 @@ import java.util.Random; import com.ibm.icu.dev.test.TestFmwk; import com.ibm.icu.text.RuleBasedNumberFormat; import com.ibm.icu.util.ULocale; +import com.ibm.icu.text.DecimalFormatSymbols; /** * This does not test lenient parse mode, since testing the default implementation @@ -1277,4 +1278,29 @@ public class RbnfTest extends TestFmwk { } } } + + public void TestSetDecimalFormatSymbols() { + RuleBasedNumberFormat rbnf = new RuleBasedNumberFormat(Locale.ENGLISH, RuleBasedNumberFormat.ORDINAL); + + DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.ENGLISH); + + double number = 1001; + + char newSeparator = '&'; + String[] expected = { "1,001st", "1&001st" }; + + String result = rbnf.format(number); + if (!result.equals(expected[0])) { + errln("Format Error - Got: " + result + " Expected: " + expected[0]); + } + + /* Set new symbol for testing */ + dfs.setGroupingSeparator('&'); + rbnf.setDecimalFormatSymbols(dfs); + + result = rbnf.format(number); + if (!result.equals(expected[1])) { + errln("Format Error - Got: " + result + " Expected: " + expected[1]); + } + } } -- 2.40.0