]> gitweb.fperrin.net Git - Dictionary.git/blobdiff - jars/icu4j-52_1/main/classes/core/src/com/ibm/icu/text/TimeUnitFormat.java
Upgrade ICU4J.
[Dictionary.git] / jars / icu4j-52_1 / main / classes / core / src / com / ibm / icu / text / TimeUnitFormat.java
similarity index 93%
rename from jars/icu4j-4_8_1_1/main/classes/core/src/com/ibm/icu/text/TimeUnitFormat.java
rename to jars/icu4j-52_1/main/classes/core/src/com/ibm/icu/text/TimeUnitFormat.java
index 685f8965c277742760bd21edef8507456fc79da2..f8762948ae3493d4cdf7769700f71e46d6a597bc 100644 (file)
@@ -1,7 +1,7 @@
 /*
  **************************************************************************
- * Copyright (C) 2008-2011, Google, International Business Machines
- * Corporationand others. All Rights Reserved.
+ * Copyright (C) 2008-2013, Google, International Business Machines
+ * Corporation and others. All Rights Reserved.
  **************************************************************************
  */
 package com.ibm.icu.text;
@@ -11,6 +11,7 @@ import java.text.ParsePosition;
 import java.util.HashMap;
 import java.util.Locale;
 import java.util.Map;
+import java.util.Map.Entry;
 import java.util.MissingResourceException;
 import java.util.Set;
 import java.util.TreeMap;
@@ -69,7 +70,7 @@ public class TimeUnitFormat extends MeasureFormat {
      * @stable ICU 4.2
      */
     public static final int ABBREVIATED_NAME = 1;
-
+    
     private static final int TOTAL_STYLES = 2;
 
     private static final long serialVersionUID = -3707773153184971529L;
@@ -119,8 +120,7 @@ public class TimeUnitFormat extends MeasureFormat {
     }
 
     /**
-     * Create TimeUnitFormat given a ULocale and a formatting style: full or
-     * abbreviated.
+     * Create TimeUnitFormat given a ULocale and a formatting style.
      * @param locale   locale of this time unit formatter.
      * @param style    format style, either FULL_NAME or ABBREVIATED_NAME style.
      * @throws IllegalArgumentException if the style is not FULL_NAME or
@@ -137,8 +137,7 @@ public class TimeUnitFormat extends MeasureFormat {
     }
 
     /**
-     * Create TimeUnitFormat given a Locale and a formatting style: full or
-     * abbreviated.
+     * Create TimeUnitFormat given a Locale and a formatting style.
      * @stable ICU 4.2
      */
     public TimeUnitFormat(Locale locale, int style) {
@@ -170,7 +169,8 @@ public class TimeUnitFormat extends MeasureFormat {
     }
     
     /**
-     * Set the format used for formatting or parsing. If null or not available, use the getNumberInstance(locale).
+     * Set the format used for formatting or parsing. Passing null is equivalent to passing
+     * {@link NumberFormat#getNumberInstance(ULocale)}.
      * @param format   the number formatter.
      * @return this, for chaining.
      * @stable ICU 4.0
@@ -193,10 +193,8 @@ public class TimeUnitFormat extends MeasureFormat {
         if (isReady == false) {
             return this;
         }
-        for (TimeUnit timeUnit : timeUnitToCountToPatterns.keySet()) {
-            Map<String, Object[]> countToPattern = timeUnitToCountToPatterns.get(timeUnit);
-            for (String count : countToPattern.keySet()) {
-                Object[] pair = countToPattern.get(count);
+        for (Map<String, Object[]> countToPattern : timeUnitToCountToPatterns.values()) {
+            for (Object[] pair : countToPattern.values()) {
                 MessageFormat pattern = (MessageFormat)pair[FULL_NAME];
                 pattern.setFormatByArgumentIndex(0, format);
                 pattern = (MessageFormat)pair[ABBREVIATED_NAME];
@@ -215,7 +213,8 @@ public class TimeUnitFormat extends MeasureFormat {
     public StringBuffer format(Object obj, StringBuffer toAppendTo,
             FieldPosition pos) {
         if ( !(obj instanceof TimeUnitAmount) ) {
-            throw new IllegalArgumentException("can not format non TimeUnitAmount object");
+            throw new IllegalArgumentException(
+                    "cannot format a non TimeUnitAmount object");
         }
         if (!isReady) {
             setup();
@@ -227,8 +226,7 @@ public class TimeUnitFormat extends MeasureFormat {
         MessageFormat pattern = (MessageFormat)(countToPattern.get(count))[style];
         return pattern.format(new Object[]{amount.getNumber()}, toAppendTo, pos);
     }
-
-
+    
     /**
      * Parse a TimeUnitAmount.
      * @see java.text.Format#parseObject(java.lang.String, java.text.ParsePosition)
@@ -249,9 +247,10 @@ public class TimeUnitFormat extends MeasureFormat {
         // and looking for the longest match.
         for (TimeUnit timeUnit : timeUnitToCountToPatterns.keySet()) {
             Map<String, Object[]> countToPattern = timeUnitToCountToPatterns.get(timeUnit);
-            for (String count : countToPattern.keySet()) {
+            for (Entry<String, Object[]> patternEntry : countToPattern.entrySet()) {
+              String count = patternEntry.getKey();
               for (int styl = FULL_NAME; styl < TOTAL_STYLES; ++styl) {
-                MessageFormat pattern = (MessageFormat)(countToPattern.get(count))[styl];
+                MessageFormat pattern = (MessageFormat)(patternEntry.getValue())[styl];
                 pos.setErrorIndex(-1);
                 pos.setIndex(oldPos);
                 // see if we can parse
@@ -290,15 +289,15 @@ public class TimeUnitFormat extends MeasureFormat {
         if (resultNumber == null && longestParseDistance != 0) {
             // set the number using plurrual count
             if ( countOfLongestMatch.equals("zero") ) {
-                resultNumber = new Integer(0);
+                resultNumber = Integer.valueOf(0);
             } else if ( countOfLongestMatch.equals("one") ) {
-                resultNumber = new Integer(1);
+                resultNumber = Integer.valueOf(1);
             } else if ( countOfLongestMatch.equals("two") ) {
-                resultNumber = new Integer(2);
+                resultNumber = Integer.valueOf(2);
             } else {
                 // should not happen.
                 // TODO: how to handle?
-                resultNumber = new Integer(3);
+                resultNumber = Integer.valueOf(3);
             }
         }
         if (longestParseDistance == 0) {
@@ -332,15 +331,14 @@ public class TimeUnitFormat extends MeasureFormat {
         }
         pluralRules = PluralRules.forLocale(locale);
         timeUnitToCountToPatterns = new HashMap<TimeUnit, Map<String, Object[]>>();
-
-        setup("units", timeUnitToCountToPatterns, FULL_NAME);
-        setup("unitsShort", timeUnitToCountToPatterns, ABBREVIATED_NAME);
+        Set<String> pluralKeywords = pluralRules.getKeywords();
+        setup("units/duration", timeUnitToCountToPatterns, FULL_NAME, pluralKeywords);
+        setup("unitsShort/duration", timeUnitToCountToPatterns, ABBREVIATED_NAME, pluralKeywords);
         isReady = true;
     }
-
-
+    
     private void setup(String resourceKey, Map<TimeUnit, Map<String, Object[]>> timeUnitToCountToPatterns,
-                       int style) {
+                       int style, Set<String> pluralKeywords) {
         // fill timeUnitToCountToPatterns from resource file
         try {
             ICUResourceBundle resource = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, locale);
@@ -375,6 +373,8 @@ public class TimeUnitFormat extends MeasureFormat {
                 } 
                 for ( int pluralIndex = 0; pluralIndex < count; ++pluralIndex) {
                     String pluralCount = oneUnitRes.get(pluralIndex).getKey();
+                    if (!pluralKeywords.contains(pluralCount))
+                        continue;
                     String pattern = oneUnitRes.get(pluralIndex).getString();
                     final MessageFormat messageFormat = new MessageFormat(pattern, locale);
                     if (format != null) {