]> granicus.if.org Git - icu/commitdiff
ICU-10574 Add API (getter/setter) to set capitalization context for NumberFormat, J
authorPeter Edberg <pedberg@unicode.org>
Wed, 15 Jan 2014 09:13:26 +0000 (09:13 +0000)
committerPeter Edberg <pedberg@unicode.org>
Wed, 15 Jan 2014 09:13:26 +0000 (09:13 +0000)
X-SVN-Rev: 34899

icu4j/main/classes/core/src/com/ibm/icu/text/NumberFormat.java
icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java

index 6b46271f23c5c848c7bb5a5b1688772f5e788014..57eb9bed2f3d0e95cac232c62a050346786a37b2 100644 (file)
@@ -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
index c3111d25ef62171a5a97fd8a5339e62d33f11016..611281e2cb7c4d2ea8cc2efaf17d9a7b2d246ef4 100644 (file)
@@ -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");
+        }
+    }
 }