From 9a011771da738322b406288d4543044ea10c2906 Mon Sep 17 00:00:00 2001 From: George Rhoten Date: Tue, 25 Aug 2015 19:33:18 +0000 Subject: [PATCH] ICU-11653 Override roundingMode API from the NumberFormat superclass so that it doesn't throw an exception. X-SVN-Rev: 37812 --- .../ibm/icu/text/RuleBasedNumberFormat.java | 52 +++++++++++++++++++ .../com/ibm/icu/dev/test/format/RbnfTest.java | 37 ++++++++++++- 2 files changed, 88 insertions(+), 1 deletion(-) 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 cab35dfbbd3..2e7820c897c 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 @@ -21,6 +21,7 @@ import com.ibm.icu.impl.ICUDebug; import com.ibm.icu.impl.ICUResourceBundle; import com.ibm.icu.impl.PatternProps; import com.ibm.icu.lang.UCharacter; +import com.ibm.icu.math.BigDecimal; import com.ibm.icu.util.ULocale; import com.ibm.icu.util.ULocale.Category; import com.ibm.icu.util.UResourceBundle; @@ -593,6 +594,12 @@ public class RuleBasedNumberFormat extends NumberFormat { */ private ULocale locale = null; + /** + * The formatter's rounding mode. + * @serial + */ + private int roundingMode = BigDecimal.ROUND_UNNECESSARY; + /** * Collator to be used in lenient parsing. This variable is lazy-evaluated: * the collator is actually created the first time the client does a parse @@ -968,6 +975,7 @@ public class RuleBasedNumberFormat extends NumberFormat { // have an implementation-independent streaming format out.writeUTF(this.toString()); out.writeObject(this.locale); + out.writeInt(this.roundingMode); } /** @@ -986,6 +994,10 @@ public class RuleBasedNumberFormat extends NumberFormat { } catch (Exception e) { loc = ULocale.getDefault(Category.FORMAT); } + try { + roundingMode = in.readInt(); + } catch (Exception ignored) { + } // build a brand-new RuleBasedNumberFormat from the description, // then steal its substructure. This object's substructure and @@ -999,6 +1011,8 @@ public class RuleBasedNumberFormat extends NumberFormat { decimalFormatSymbols = temp.decimalFormatSymbols; decimalFormat = temp.decimalFormat; locale = temp.locale; + defaultInfinityRule = temp.defaultInfinityRule; + defaultNaNRule = temp.defaultNaNRule; } @@ -1491,6 +1505,41 @@ public class RuleBasedNumberFormat extends NumberFormat { } } + /** + * Returns the rounding mode. + * + * @return A rounding mode, between BigDecimal.ROUND_UP and + * BigDecimal.ROUND_UNNECESSARY. + * @see #setRoundingMode + * @see java.math.BigDecimal + * @draft ICU 56 + */ + @Override + public int getRoundingMode() { + return roundingMode; + } + + /** + * Sets the rounding mode. This has no effect unless the rounding increment is greater + * than zero. + * + * @param roundingMode A rounding mode, between BigDecimal.ROUND_UP and + * BigDecimal.ROUND_UNNECESSARY. + * @exception IllegalArgumentException if roundingMode is unrecognized. + * @see #getRoundingMode + * @see java.math.BigDecimal + * @draft ICU 56 + */ + @Override + public void setRoundingMode(int roundingMode) { + if (roundingMode < BigDecimal.ROUND_UP || roundingMode > BigDecimal.ROUND_UNNECESSARY) { + throw new IllegalArgumentException("Invalid rounding mode: " + roundingMode); + } + + this.roundingMode = roundingMode; + } + + //----------------------------------------------------------------------- // package-internal API //----------------------------------------------------------------------- @@ -1879,6 +1928,9 @@ public class RuleBasedNumberFormat extends NumberFormat { // position of 0 and the number being formatted) to the rule set // for formatting StringBuffer result = new StringBuffer(); + if (getRoundingMode() != BigDecimal.ROUND_UNNECESSARY) { + number = new BigDecimal(number).setScale(getMaximumFractionDigits(), roundingMode).doubleValue(); + } ruleSet.format(number, result, 0, 0); postProcess(result, ruleSet); return result.toString(); 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 8159ffee05e..dfd7b177db1 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 @@ -12,6 +12,7 @@ import java.util.Locale; import java.util.Random; import com.ibm.icu.dev.test.TestFmwk; +import com.ibm.icu.math.BigDecimal; import com.ibm.icu.text.DecimalFormat; import com.ibm.icu.text.DecimalFormatSymbols; import com.ibm.icu.text.DisplayContext; @@ -1404,7 +1405,7 @@ public class RbnfTest extends TestFmwk { String[] ruleNames = rbnf.getRuleSetNames(); try{ // Test with "null" rules - rbnf.format(0.0,null); + rbnf.format(0.0, null); errln("This was suppose to return an exception for a null format"); } catch(Exception e){} for(int i=0; i