]> granicus.if.org Git - icu/commitdiff
ICU-10837 Andy & Mark's review.
authorTravis Keep <keep94@gmail.com>
Fri, 13 Jun 2014 20:18:57 +0000 (20:18 +0000)
committerTravis Keep <keep94@gmail.com>
Fri, 13 Jun 2014 20:18:57 +0000 (20:18 +0000)
X-SVN-Rev: 35878

icu4j/main/classes/core/src/com/ibm/icu/text/DecimalFormatSymbols.java
icu4j/main/classes/core/src/com/ibm/icu/text/ScientificFormatHelper.java
icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/ScientificFormatHelperTest.java
icu4j/main/tests/localespi/src/com/ibm/icu/dev/test/localespi/DecimalFormatSymbolsTest.java

index fac38e17183b9a250af5e5183412c5db74235ca0..5785af96f89ad2a0fe677a08d2b8edf11ffec6e8 100644 (file)
@@ -544,6 +544,24 @@ public class DecimalFormatSymbols implements Cloneable, Serializable {
     public void setMonetaryGroupingSeparator(char sep) {
         monetaryGroupingSeparator = sep;
     }
+    
+    /**
+    * Returns the multiplication sign
+    * @draft ICU 54
+    * @provisional
+    */
+    public String getExponentMultiplicationSign() {
+        return exponentMultiplicationSign;
+    }
+    
+    /**
+    * Sets the multiplication sign
+    * @draft ICU 54
+    * @provisional
+    */
+    public void setExponentMultiplicationSign(String exponentMultiplicationSign) {
+        this.exponentMultiplicationSign = exponentMultiplicationSign;
+    }
 
     /**
      * {@icu} Returns the string used to separate the mantissa from the exponent.
@@ -803,7 +821,8 @@ public class DecimalFormatSymbols implements Cloneable, Serializable {
         plusString.equals(other.plusString) &&
         exponentSeparator.equals(other.exponentSeparator) &&
         monetarySeparator == other.monetarySeparator &&
-        monetaryGroupingSeparator == other.monetaryGroupingSeparator);
+        monetaryGroupingSeparator == other.monetaryGroupingSeparator &&
+        exponentMultiplicationSign.equals(other.exponentMultiplicationSign));
     }
 
     /**
@@ -875,7 +894,7 @@ public class DecimalFormatSymbols implements Cloneable, Serializable {
             boolean isLatn = nsName.equals("latn");
             String baseKey = "NumberElements/" + nsName + "/symbols/";
             String latnKey = "NumberElements/latn/symbols/";
-            String[] symbolKeys = { "decimal", "group", "list", "percentSign", "minusSign", "plusSign", "exponential", "perMille", "infinity", "nan", "currencyDecimal", "currencyGroup" };
+            String[] symbolKeys = { "decimal", "group", "list", "percentSign", "minusSign", "plusSign", "exponential", "perMille", "infinity", "nan", "currencyDecimal", "currencyGroup", "superscriptingExponent" };
             String[] fallbackElements = { ".", ",", ";", "%", "-", "+", "E", "\u2030", "\u221e", "NaN", null, null };
             String[] symbolsArray = new String[symbolKeys.length];
             for ( int i = 0 ; i < symbolKeys.length; i++ ) {
@@ -933,6 +952,12 @@ public class DecimalFormatSymbols implements Cloneable, Serializable {
             monetaryGroupingSeparator = groupingSeparator;
         }
         
+        if ( numberElements[12] != null) {
+            exponentMultiplicationSign = numberElements[12];
+        } else {
+            exponentMultiplicationSign = "\u00D7";
+        }
+        
         digit = DecimalFormat.PATTERN_DIGIT;  // Localized pattern character no longer in CLDR
         padEscape = DecimalFormat.PATTERN_PAD_ESCAPE;
         sigDigit  = DecimalFormat.PATTERN_SIGNIFICANT_DIGIT;
@@ -1051,6 +1076,11 @@ public class DecimalFormatSymbols implements Cloneable, Serializable {
                 plusString = new String(plusArray);
             }
         }
+        if (serialVersionOnStream < 8) {
+            if (exponentMultiplicationSign == null) {
+                exponentMultiplicationSign = "\u00D7";
+            }
+        }
         serialVersionOnStream = currentSerialVersion;
 
     // recreate
@@ -1234,6 +1264,13 @@ public class DecimalFormatSymbols implements Cloneable, Serializable {
      */
     private String minusString = null;
     private String plusString = null;
+    
+    /**
+     * Exponent multiplication sign. e.g "x"
+     * @serial
+     * @since ICU 54
+     */
+    private String exponentMultiplicationSign = null;
 
     // Proclaim JDK 1.1 FCS compatibility
     private static final long serialVersionUID = 5772796243397350300L;
@@ -1249,7 +1286,8 @@ public class DecimalFormatSymbols implements Cloneable, Serializable {
     // - 5 for ICU 3.6, which includes the monetaryGroupingSeparator field
     // - 6 for ICU 4.2, which includes the currencySpc* fields
     // - 7 for ICU 52, which includes the minusString and plusString fields
-    private static final int currentSerialVersion = 7;
+    // - 8 for ICU 54, which includes exponentMultiplicationSign field.
+    private static final int currentSerialVersion = 8;
 
     /**
      * Describes the version of <code>DecimalFormatSymbols</code> present on the stream.
index 9b326b6069bf33d4fc705c32ec7f2035813a21b1..bccf8f3f925ed03ab97fdc18eccad89bb1849b40 100644 (file)
@@ -66,8 +66,7 @@ public final class ScientificFormatHelper {
     }
     
     private static String getMultiplicationSymbol(DecimalFormatSymbols dfs) {
-        //TODO: revisit this
-        return "\u00d7";
+        return dfs.getExponentMultiplicationSign();
     }
 
     /**
@@ -86,12 +85,15 @@ public final class ScientificFormatHelper {
             CharSequence endMarkup) {
         int copyFromOffset = 0;
         StringBuilder result = new StringBuilder();
+        boolean exponentSymbolFieldPresent = false;
+        boolean exponentFieldPresent = false;
         for (
                 iterator.first();
                 iterator.current() != CharacterIterator.DONE;
             ) {
             Map<Attribute, Object> attributeSet = iterator.getAttributes();
             if (attributeSet.containsKey(NumberFormat.Field.EXPONENT_SYMBOL)) {
+                exponentSymbolFieldPresent = true;
                 append(
                         iterator,
                         copyFromOffset,
@@ -102,6 +104,7 @@ public final class ScientificFormatHelper {
                 result.append(preExponent);
                 result.append(beginMarkup);
             } else if (attributeSet.containsKey(NumberFormat.Field.EXPONENT)) {
+                exponentFieldPresent = true;
                 int limit = iterator.getRunLimit(NumberFormat.Field.EXPONENT);
                 append(
                         iterator,
@@ -114,7 +117,10 @@ public final class ScientificFormatHelper {
             } else {
                 iterator.next();
             }
-        } 
+        }
+        if (!exponentSymbolFieldPresent || !exponentFieldPresent) {
+            throw new IllegalArgumentException("Must start with standard e notation.");
+        }
         append(iterator, copyFromOffset, iterator.getEndIndex(), result);
         return result.toString();
     }
@@ -146,12 +152,15 @@ public final class ScientificFormatHelper {
     public String toSuperscriptExponentDigits(AttributedCharacterIterator iterator) {
         int copyFromOffset = 0;
         StringBuilder result = new StringBuilder();
+        boolean exponentSymbolFieldPresent = false;
+        boolean exponentFieldPresent = false;
         for (
                 iterator.first();
                 iterator.current() != CharacterIterator.DONE;
             ) {
             Map<Attribute, Object> attributeSet = iterator.getAttributes();
             if (attributeSet.containsKey(NumberFormat.Field.EXPONENT_SYMBOL)) {
+                exponentSymbolFieldPresent = true;
                 append(
                         iterator,
                         copyFromOffset,
@@ -184,6 +193,7 @@ public final class ScientificFormatHelper {
                 copyFromOffset = limit;
                 iterator.setIndex(copyFromOffset);
             } else if (attributeSet.containsKey(NumberFormat.Field.EXPONENT)) {
+                exponentFieldPresent = true;
                 int start = iterator.getRunStart(NumberFormat.Field.EXPONENT);
                 int limit = iterator.getRunLimit(NumberFormat.Field.EXPONENT);
                 append(
@@ -198,6 +208,9 @@ public final class ScientificFormatHelper {
                 iterator.next();
             }
         } 
+        if (!exponentSymbolFieldPresent || !exponentFieldPresent) {
+            throw new IllegalArgumentException("Must start with standard e notation.");
+        }
         append(iterator, copyFromOffset, iterator.getEndIndex(), result);
         return result.toString();
     }
index 3a75c80d9cbcb389463e3fbd92870c7e117573a6..22ee42a12180afca79df4eb59489f607ecf4a045 100644 (file)
@@ -35,8 +35,22 @@ public class ScientificFormatHelperTest extends TestFmwk {
                 "1.23456\u00d710\u207b\u2077\u2078",
                 helper.toSuperscriptExponentDigits(iterator));
     }
+
+    public void TestPlusSignInExponentMarkup() {
+        ULocale en = new ULocale("en");
+        DecimalFormat decfmt = (DecimalFormat) NumberFormat.getScientificInstance(en);
+        decfmt.applyPattern("0.00E+0");
+        AttributedCharacterIterator iterator = decfmt.formatToCharacterIterator(6.02e23);
+        ScientificFormatHelper helper = ScientificFormatHelper.getInstance(
+                decfmt.getDecimalFormatSymbols());
+        assertEquals(
+                "",
+                "6.02\u00d710<sup>+23</sup>",
+                helper.insertMarkup(iterator, "<sup>", "</sup>"));
+    }
+
     
-    public void TestPlusSignInExponent() {
+    public void TestPlusSignInExponentSuperscript() {
         ULocale en = new ULocale("en");
         DecimalFormat decfmt = (DecimalFormat) NumberFormat.getScientificInstance(en);
         decfmt.applyPattern("0.00E+0");
@@ -48,4 +62,32 @@ public class ScientificFormatHelperTest extends TestFmwk {
                 "6.02\u00d710\u207a\u00b2\u00b3",
                 helper.toSuperscriptExponentDigits(iterator));
     }
+    
+    public void TestFixedDecimalMarkup() {
+        ULocale en = new ULocale("en");
+        DecimalFormat decfmt = (DecimalFormat) NumberFormat.getInstance(en);
+        AttributedCharacterIterator iterator = decfmt.formatToCharacterIterator(123456.0);
+        ScientificFormatHelper helper = ScientificFormatHelper.getInstance(
+                decfmt.getDecimalFormatSymbols());
+        try {
+            helper.insertMarkup(iterator, "<sup>", "</sup>");
+            fail("expected illegal argument exception");
+        } catch (IllegalArgumentException expected) {
+            // do nothing
+        }
+    }
+    
+    public void TestFixedDecimalSuperscript() {
+        ULocale en = new ULocale("en");
+        DecimalFormat decfmt = (DecimalFormat) NumberFormat.getInstance(en);
+        AttributedCharacterIterator iterator = decfmt.formatToCharacterIterator(123456.0);
+        ScientificFormatHelper helper = ScientificFormatHelper.getInstance(
+                decfmt.getDecimalFormatSymbols());
+        try {
+            helper.toSuperscriptExponentDigits(iterator);
+            fail("expected illegal argument exception");
+        } catch (IllegalArgumentException expected) {
+            // do nothing
+        }
+    }
 }
index 5b83442b96db55e65bbe1d41076aff15ddaab938..b32a1528b06f3c872671cafbe11e46dc4940b535 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *******************************************************************************
- * Copyright (C) 2008-2012, International Business Machines Corporation and    *
+ * Copyright (C) 2008-2014, International Business Machines Corporation and    *
  * others. All Rights Reserved.                                                *
  *******************************************************************************
  */
@@ -132,6 +132,7 @@ public class DecimalFormatSymbolsTest extends TestFmwk {
         decfs.setPercent(decfsEnUS.getPercent());
         decfs.setPerMill(decfsEnUS.getPerMill());
         decfs.setZeroDigit(decfsEnUS.getZeroDigit());
+        decfs.setExponentMultiplicationSign(decfsEnUS.getExponentMultiplicationSign());
 
         // Check
         Currency cur = decfs.getCurrency();
@@ -156,6 +157,7 @@ public class DecimalFormatSymbolsTest extends TestFmwk {
         checkEquivalence(decfs.getPercent(), decfsEnUS.getPercent(), loc, "getPercent");
         checkEquivalence(decfs.getPerMill(), decfsEnUS.getPerMill(), loc, "getPerMill");
         checkEquivalence(decfs.getZeroDigit(), decfsEnUS.getZeroDigit(), loc, "getZeroDigit");
+        checkEquivalence(decfs.getExponentMultiplicationSign(), decfsEnUS.getExponentMultiplicationSign(), loc, "getExponentMultiplicationSign");
     }
 
     public void TestKeywords() {