From ef9fbd093ca05a507db65151eaca296b9cadd822 Mon Sep 17 00:00:00 2001 From: Travis Keep Date: Wed, 18 Dec 2013 21:55:57 +0000 Subject: [PATCH] ICU-10268 Implement final comments from ICU meeting. X-SVN-Rev: 34795 --- .../src/com/ibm/icu/text/CurrencyFormat.java | 10 -- .../src/com/ibm/icu/text/MeasureFormat.java | 132 +++++++++--------- .../src/com/ibm/icu/text/TimeUnitFormat.java | 10 -- .../src/com/ibm/icu/util/MeasureUnit.java | 6 +- 4 files changed, 66 insertions(+), 92 deletions(-) diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/CurrencyFormat.java b/icu4j/main/classes/core/src/com/ibm/icu/text/CurrencyFormat.java index 39634847e55..fbe80aaa18c 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/CurrencyFormat.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/CurrencyFormat.java @@ -90,16 +90,6 @@ class CurrencyFormat extends MeasureFormat { return mf.formatMeasures(measures); } - /** - * @draft ICU 53 - * @provisional - */ - @Override - public T formatMeasure( - Measure measure, T appendable, FieldPosition fieldPosition) { - return mf.formatMeasure(measure, appendable, fieldPosition); - } - /** * @draft ICU 53 * @provisional diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/MeasureFormat.java b/icu4j/main/classes/core/src/com/ibm/icu/text/MeasureFormat.java index 39cfd1c52a0..63e9d2444a7 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/MeasureFormat.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/MeasureFormat.java @@ -40,7 +40,7 @@ import com.ibm.icu.util.UResourceBundle; * *

To format a Measure object, first create a formatter * object using a MeasureFormat factory method. Then use that - * object's format, formatMeasure, or formatMeasures methods. + * object's format or formatMeasures methods. * * Here is sample code: *

@@ -121,7 +121,7 @@ public class MeasureFormat extends UFormat {
      */
     // Be sure to update the toFormatWidth and fromFormatWidth() functions
     // when adding an enum value.
-    public static enum FormatWidth {
+    public enum FormatWidth {
         
         /**
          * Spell out everything.
@@ -194,10 +194,10 @@ public class MeasureFormat extends UFormat {
     
     /**
      * Able to format Collection<? extends Measure>, Measure[], and Measure
-     * by delegating to formatMeasure or formatMeasures.
-     * If the pos argument identifies a field used by the format
+     * by delegating to formatMeasures.
+     * If the pos argument identifies a NumberFormat field,
      * then its indices are set to the beginning and end of the first such field
-     * encountered.
+     * encountered. MeasureFormat itself does not supply any fields.
      * 
      * @param obj must be a Collection, Measure[], or Measure object.
      * @param toAppendTo Formatted string appended here.
@@ -239,49 +239,6 @@ public class MeasureFormat extends UFormat {
     public Measure parseObject(String source, ParsePosition pos) {
         throw new UnsupportedOperationException();
     }
-
-    
-    /**
-     * Formats a single measure and adds to appendable.
-     * If the fieldPosition argument identifies a field used by the format,
-     * then its indices are set to the beginning and end of the first such
-     * field encountered.
-     * 
-     * @param measure the measure to format
-     * @param appendable the formatted string appended here.
-     * @param fieldPosition Identifies a field in the formatted text.
-     * @return appendable.
-     * @see MeasureFormat#formatMeasures(Measure...)
-     * @draft ICU 53
-     * @provisional
-     */
-    public  T formatMeasure(
-            Measure measure, T appendable, FieldPosition fieldPosition) {
-        Number n = measure.getNumber();
-        MeasureUnit unit = measure.getUnit();        
-        UFieldPosition fpos = new UFieldPosition(fieldPosition.getFieldAttribute(), fieldPosition.getField());
-        StringBuffer formattedNumber = numberFormat.format(n, new StringBuffer(), fpos);
-        String keyword = rules.select(new PluralRules.FixedDecimal(n.doubleValue(), fpos.getCountVisibleFractionDigits(), fpos.getFractionDigits()));
-
-        Map> styleToCountToFormat = unitToStyleToCountToFormat.get(unit);
-        Map countToFormat = styleToCountToFormat.get(length);
-        PatternData messagePatternData = countToFormat.get(keyword);
-        try {
-            appendable.append(messagePatternData.prefix);
-            if (messagePatternData.suffix != null) { // there is a number (may not happen with, say, Arabic dual)
-                // Fix field position
-                if (fpos.getBeginIndex() != 0 || fpos.getEndIndex() != 0) {
-                    fieldPosition.setBeginIndex(fpos.getBeginIndex() + messagePatternData.prefix.length());
-                    fieldPosition.setEndIndex(fpos.getEndIndex() + messagePatternData.prefix.length());
-                }
-                appendable.append(formattedNumber);
-                appendable.append(messagePatternData.suffix);
-            }
-            return appendable;
-        } catch (IOException e) {
-            throw new RuntimeException(e);
-        }
-    }
     
     /**
      * Format a sequence of measures. Uses the ListFormatter unit lists.
@@ -305,9 +262,10 @@ public class MeasureFormat extends UFormat {
     
     /**
      * Formats a sequence of measures and adds to appendable.
-     * If the fieldPosition argument identifies a field used by the format,
-     * then its indices are set to the beginning and end of the first such
-     * field encountered.
+     * 
+     * If the fieldPosition argument identifies a NumberFormat field,
+     * then its indices are set to the beginning and end of the first such field
+     * encountered. MeasureFormat itself does not supply any fields.
      * 
      * @param appendable the formatted string appended here.
      * @param fieldPosition Identifies a field in the formatted text.
@@ -321,6 +279,14 @@ public class MeasureFormat extends UFormat {
     public  T formatMeasures(
             T appendable, FieldPosition fieldPosition, Measure... measures) {
         
+        // fast track for trivial cases
+        if (measures.length == 0) {
+            return (T) appendable;
+        }
+        if (measures.length == 1) {
+            return formatMeasure(measures[0], appendable, fieldPosition);
+        }
+        
         // Zero out our field position so that we can tell when we find our field.
         FieldPosition fpos = new FieldPosition(fieldPosition.getFieldAttribute(), fieldPosition.getField());
         FieldPosition dummyPos = new FieldPosition(0);
@@ -566,6 +532,52 @@ public class MeasureFormat extends UFormat {
         return unitToStyleToCountToFormat;
     }
     
+    private  T formatMeasure(
+            Measure measure, T appendable, FieldPosition fieldPosition) {
+        Number n = measure.getNumber();
+        MeasureUnit unit = measure.getUnit();        
+        UFieldPosition fpos = new UFieldPosition(fieldPosition.getFieldAttribute(), fieldPosition.getField());
+        StringBuffer formattedNumber = numberFormat.format(n, new StringBuffer(), fpos);
+        String keyword = rules.select(new PluralRules.FixedDecimal(n.doubleValue(), fpos.getCountVisibleFractionDigits(), fpos.getFractionDigits()));
+
+        Map> styleToCountToFormat = unitToStyleToCountToFormat.get(unit);
+        Map countToFormat = styleToCountToFormat.get(length);
+        PatternData messagePatternData = countToFormat.get(keyword);
+        try {
+            appendable.append(messagePatternData.prefix);
+            if (messagePatternData.suffix != null) { // there is a number (may not happen with, say, Arabic dual)
+                // Fix field position
+                if (fpos.getBeginIndex() != 0 || fpos.getEndIndex() != 0) {
+                    fieldPosition.setBeginIndex(fpos.getBeginIndex() + messagePatternData.prefix.length());
+                    fieldPosition.setEndIndex(fpos.getEndIndex() + messagePatternData.prefix.length());
+                }
+                appendable.append(formattedNumber);
+                appendable.append(messagePatternData.suffix);
+            }
+            return appendable;
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+    
+    // Wrapper around NumberFormat that provides immutability and thread-safety.
+    private static final class ImmutableNumberFormat {
+        private NumberFormat nf;
+        
+        public ImmutableNumberFormat(NumberFormat nf) {
+            this.nf = (NumberFormat) nf.clone();
+        }
+        
+        public synchronized NumberFormat get() {
+            return (NumberFormat) nf.clone();
+        }
+        
+        public synchronized StringBuffer format(
+                Number n, StringBuffer buffer, FieldPosition pos) {
+            return nf.format(n, buffer, pos);
+        }
+    }
+    
     static final class PatternData {
         final String prefix;
         final String suffix;
@@ -704,22 +716,4 @@ public class MeasureFormat extends UFormat {
             throw new IllegalStateException("Unable to serialize Format Width " + fw);
         }
     }
-    
-    // Wrapper around NumberFormat that provides immutability and thread-safety.
-    private static final class ImmutableNumberFormat {
-        private NumberFormat nf;
-        
-        public ImmutableNumberFormat(NumberFormat nf) {
-            this.nf = (NumberFormat) nf.clone();
-        }
-        
-        public synchronized NumberFormat get() {
-            return (NumberFormat) nf.clone();
-        }
-        
-        public synchronized StringBuffer format(
-                Number n, StringBuffer buffer, FieldPosition pos) {
-            return nf.format(n, buffer, pos);
-        }
-    }
 }
diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/TimeUnitFormat.java b/icu4j/main/classes/core/src/com/ibm/icu/text/TimeUnitFormat.java
index 60726ebe633..9fd647175db 100644
--- a/icu4j/main/classes/core/src/com/ibm/icu/text/TimeUnitFormat.java
+++ b/icu4j/main/classes/core/src/com/ibm/icu/text/TimeUnitFormat.java
@@ -525,16 +525,6 @@ if ( searchPluralCount.equals("other") ) {
         return mf.formatMeasures(measures);
     }
     
-    /**
-     * @draft ICU 53
-     * @provisional
-     */
-    @Override
-    public  T formatMeasure(
-            Measure measure, T appendable, FieldPosition fieldPosition) {
-        return mf.formatMeasure(measure, appendable, fieldPosition);
-    }
-    
     /**
      * @draft ICU 53
      * @provisional
diff --git a/icu4j/main/classes/core/src/com/ibm/icu/util/MeasureUnit.java b/icu4j/main/classes/core/src/com/ibm/icu/util/MeasureUnit.java
index 9d26afa0ad6..f36a53f10ef 100644
--- a/icu4j/main/classes/core/src/com/ibm/icu/util/MeasureUnit.java
+++ b/icu4j/main/classes/core/src/com/ibm/icu/util/MeasureUnit.java
@@ -122,7 +122,7 @@ public class MeasureUnit implements Serializable {
     }
     
     /**
-     * Get all of the available units' types.
+     * Get all of the available units' types. Returned set is unmodifiable.
      * 
      * @draft ICU 53
      * @provisional
@@ -134,7 +134,7 @@ public class MeasureUnit implements Serializable {
     /**
      * For the given type, return the available units.
      * @param type the type
-     * @return the available units for type
+     * @return the available units for type. Returned set is unmodifiable.
      * @draft ICU 53
      * @provisional
      */
@@ -147,7 +147,7 @@ public class MeasureUnit implements Serializable {
     }
 
     /**
-     * Get all of the available units.
+     * Get all of the available units. Returned set is unmodifiable.
      *
      * @draft ICU 53
      * @provisional