From d894ab3a4f01646fa579bf09550620d46a7869f7 Mon Sep 17 00:00:00 2001 From: Peter Edberg Date: Wed, 15 Jan 2014 09:13:26 +0000 Subject: [PATCH] ICU-10574 Add API (getter/setter) to set capitalization context for NumberFormat, J X-SVN-Rev: 34899 --- .../src/com/ibm/icu/text/NumberFormat.java | 38 ++++++++++++++++++- .../icu/dev/test/format/NumberFormatTest.java | 17 ++++++++- 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/NumberFormat.java b/icu4j/main/classes/core/src/com/ibm/icu/text/NumberFormat.java index 6b46271f23c..57eb9bed2f3 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/NumberFormat.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/NumberFormat.java @@ -1,6 +1,6 @@ /* ******************************************************************************* - * Copyright (C) 1996-2013, International Business Machines Corporation and * + * Copyright (C) 1996-2014, International Business Machines Corporation and * * others. All Rights Reserved. * ******************************************************************************* */ @@ -22,6 +22,7 @@ import java.util.MissingResourceException; import java.util.Set; import com.ibm.icu.impl.ICUResourceBundle; +import com.ibm.icu.text.DisplayContext; import com.ibm.icu.util.Currency; import com.ibm.icu.util.CurrencyAmount; import com.ibm.icu.util.ULocale; @@ -498,6 +499,34 @@ public abstract class NumberFormat extends UFormat { return parseStrict; } + /** + * {@icu} Set a particular DisplayContext value in the formatter, + * such as CAPITALIZATION_FOR_STANDALONE. + * + * @param context The DisplayContext value to set. + * @draft ICU 53 + * @provisional This API might change or be removed in a future release. + */ + public void setContext(DisplayContext context) { + if (context.type() == DisplayContext.Type.CAPITALIZATION) { + capitalizationSetting = context; + } + } + + /** + * {@icu} Get the formatter's DisplayContext value for the specified DisplayContext.Type, + * such as CAPITALIZATION. + * + * @param type the DisplayContext.Type whose value to return + * @return the current DisplayContext setting for the specified type + * @draft ICU 53 + * @provisional This API might change or be removed in a future release. + */ + public DisplayContext getContext(DisplayContext.Type type) { + return (type == DisplayContext.Type.CAPITALIZATION && capitalizationSetting != null)? + capitalizationSetting: DisplayContext.CAPITALIZATION_NONE; + } + //============== Locale Stuff ===================== /** @@ -1686,7 +1715,7 @@ public abstract class NumberFormat extends UFormat { private static final long serialVersionUID = -2308460125733713944L; /** - * Empty constructor. Public for compatibily with JDK which lets the + * Empty constructor. Public for compatibility with JDK which lets the * compiler generate a default public constructor even though this is * an abstract class. * @stable ICU 2.6 @@ -1697,6 +1726,11 @@ public abstract class NumberFormat extends UFormat { // new in ICU4J 3.6 private boolean parseStrict; + /* + * Capitalization setting, new in ICU 53 + */ + private transient DisplayContext capitalizationSetting = DisplayContext.CAPITALIZATION_NONE; + /** * The instances of this inner class are used as attribute keys and values * in AttributedCharacterIterator that diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java index c3111d25ef6..611281e2cb7 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java @@ -1,6 +1,6 @@ /* ******************************************************************************* - * Copyright (C) 2001-2013, International Business Machines Corporation and * + * Copyright (C) 2001-2014, International Business Machines Corporation and * * others. All Rights Reserved. * ******************************************************************************* */ @@ -29,6 +29,7 @@ import com.ibm.icu.math.BigDecimal; import com.ibm.icu.math.MathContext; import com.ibm.icu.text.DecimalFormat; import com.ibm.icu.text.DecimalFormatSymbols; +import com.ibm.icu.text.DisplayContext; import com.ibm.icu.text.MeasureFormat; import com.ibm.icu.text.NumberFormat; import com.ibm.icu.text.NumberFormat.NumberFormatFactory; @@ -3569,4 +3570,18 @@ public class NumberFormatTest extends com.ibm.icu.dev.test.TestFmwk { } } } + + public void TestContext() { + // just a minimal sanity check for now + NumberFormat nfmt = NumberFormat.getInstance(); + DisplayContext context = nfmt.getContext(DisplayContext.Type.CAPITALIZATION); + if (context != DisplayContext.CAPITALIZATION_NONE) { + errln("FAIL: Initial NumberFormat.getContext() is not CAPITALIZATION_NONE"); + } + nfmt.setContext(DisplayContext.CAPITALIZATION_FOR_STANDALONE); + context = nfmt.getContext(DisplayContext.Type.CAPITALIZATION); + if (context != DisplayContext.CAPITALIZATION_FOR_STANDALONE) { + errln("FAIL: NumberFormat.getContext() does not return the value set, CAPITALIZATION_FOR_STANDALONE"); + } + } } -- 2.40.0