]> granicus.if.org Git - icu/commitdiff
ICU-9795 Fixed the internals and javadoc to have predictable behavior.
authorMark Davis <mark@macchiato.com>
Fri, 29 Aug 2014 15:51:22 +0000 (15:51 +0000)
committerMark Davis <mark@macchiato.com>
Fri, 29 Aug 2014 15:51:22 +0000 (15:51 +0000)
X-SVN-Rev: 36289

icu4j/main/classes/core/src/com/ibm/icu/util/LocaleData.java

index b9930f202767791853e9dea42bfdb5aafaa94ddc..f4fc795ea62f9241d3e143d8c56fa53d14b59766 100644 (file)
@@ -6,6 +6,7 @@
  */
 package com.ibm.icu.util;
 
+import java.io.ObjectInputStream.GetField;
 import java.util.MissingResourceException;
 
 import com.ibm.icu.impl.ICUResourceBundle;
@@ -18,8 +19,8 @@ import com.ibm.icu.util.ULocale.Category;
  * @stable ICU 2.8
  */
 public final class LocaleData {
-    
-//    private static final String EXEMPLAR_CHARS      = "ExemplarCharacters";
+
+    //    private static final String EXEMPLAR_CHARS      = "ExemplarCharacters";
     private static final String MEASUREMENT_SYSTEM  = "MeasurementSystem";
     private static final String PAPER_SIZE          = "PaperSize";
     private static final String LOCALE_DISPLAY_PATTERN  = "localeDisplayPattern";
@@ -31,42 +32,52 @@ public final class LocaleData {
 
     /**
      * EXType for {@link #getExemplarSet(int, int)}.
+     * Corresponds to the 'main' (aka 'standard') CLDR exemplars in 
+     * {@link "http://www.unicode.org/reports/tr35/tr35-general.html#Character_Elements"}.
      * @stable ICU 3.4
      */
     public static final int ES_STANDARD = 0;
 
     /**
      * EXType for {@link #getExemplarSet(int, int)}.
+     * Corresponds to the 'auxiliary' CLDR exemplars in 
+     * {@link "http://www.unicode.org/reports/tr35/tr35-general.html#Character_Elements"}.
      * @stable ICU 3.4
      */
     public static final int ES_AUXILIARY = 1;
 
     /**
      * EXType for {@link #getExemplarSet(int, int)}.
+     * Corresponds to the 'index' CLDR exemplars in 
+     * {@link "http://www.unicode.org/reports/tr35/tr35-general.html#Character_Elements"}.
      * @stable ICU 4.4
      */
-   public static final int ES_INDEX = 2;
+    public static final int ES_INDEX = 2;
 
-   /**
-    * EXType for {@link #getExemplarSet(int, int)}.
-    * Note: This type is no longer supported.
-    * @deprecated ICU 51
-    */
+    /**
+     * EXType for {@link #getExemplarSet(int, int)}.
+     * Corresponds to the 'currencySymbol' CLDR exemplars in 
+     * {@link "http://www.unicode.org/reports/tr35/tr35-general.html#Character_Elements"}.
+     * Note: This type is no longer supported.
+     * @deprecated ICU 51
+     */
     @Deprecated
     public static final int ES_CURRENCY = 3;
 
     /**
+     * Corresponds to the 'punctuation' CLDR exemplars in 
+     * {@link "http://www.unicode.org/reports/tr35/tr35-general.html#Character_Elements"}.
      * EXType for {@link #getExemplarSet(int, int)}.
      * @stable ICU 49
      */
-     public static final int ES_PUNCTUATION = 4;
+    public static final int ES_PUNCTUATION = 4;
 
     /**
      * Count of EXTypes for {@link #getExemplarSet(int, int)}.
      * @stable ICU 3.4
      */
     public static final int ES_COUNT = 5;
-    
+
     /**
      * Delimiter type for {@link #getDelimiter(int)}.
      * @stable ICU 3.4
@@ -103,7 +114,8 @@ public final class LocaleData {
     ///CLOVER:ON
 
     /**
-     * Returns the set of exemplar characters for a locale.
+     * Returns the set of exemplar characters for a locale. Equivalent to calling {@link #getExemplarSet(ULocale, int, int)} with
+     * the extype == {@link #ES_STANDARD}.
      *
      * @param locale    Locale for which the exemplar character set
      *                  is to be retrieved.
@@ -120,10 +132,11 @@ public final class LocaleData {
      */
     public static UnicodeSet getExemplarSet(ULocale locale, int options) {
         return LocaleData.getInstance(locale).getExemplarSet(options, ES_STANDARD);
-     }
-    
+    }
+
     /**
-     * Returns the set of exemplar characters for a locale.
+     * Returns the set of exemplar characters for a locale. 
+     * Equivalent to calling new LocaleData(locale).{@link #getExemplarSet(int, int)}.
      *
      * @param locale    Locale for which the exemplar character set
      *                  is to be retrieved.
@@ -141,8 +154,8 @@ public final class LocaleData {
      */
     public static UnicodeSet getExemplarSet(ULocale locale, int options, int extype) {
         return LocaleData.getInstance(locale).getExemplarSet(options, extype);
-     }
-    
+    }
+
     /**
      * Returns the set of exemplar characters for a locale.
      *
@@ -157,42 +170,38 @@ public final class LocaleData {
      * @param extype    The type of exemplar set to be retrieved,
      *                  ES_STANDARD, ES_INDEX, ES_AUXILIARY, or ES_PUNCTUATION
      * @return          The set of exemplar characters for the given locale.
+     *                  If there is nothing available for the locale,
+     *                  then null is returned if {@link #getNoSubstitute()} is true, otherwise the 
+     *                  root value is returned (which may be UnicodeSet.EMPTY).
+     * @exception       RuntimeException if the extype is invalid.
      * @stable ICU 3.4
      */
     public UnicodeSet getExemplarSet(int options, int extype) {
         String [] exemplarSetTypes = { 
-            "ExemplarCharacters", "AuxExemplarCharacters",
-            "ExemplarCharactersIndex", "ExemplarCharactersCurrency",
-            "ExemplarCharactersPunctuation"
+                "ExemplarCharacters", 
+                "AuxExemplarCharacters",
+                "ExemplarCharactersIndex", 
+                "ExemplarCharactersCurrency",
+                "ExemplarCharactersPunctuation"
         };
 
         if (extype == ES_CURRENCY) {
             // currency symbol exemplar is no longer available
-            return new UnicodeSet();
+            return noSubstitute ? null : UnicodeSet.EMPTY;
         }
+        final String aKey = exemplarSetTypes[extype]; // will throw an out-of-bounds exception
 
         try{
-            ICUResourceBundle stringBundle = (ICUResourceBundle) bundle.get(exemplarSetTypes[extype]);
-    
-            if ( noSubstitute && (stringBundle.getLoadingStatus() == ICUResourceBundle.FROM_ROOT) )
-               return null;
-    
-            String unicodeSetPattern = stringBundle.getString();
-            if (extype == ES_PUNCTUATION) {
-                try {
-                    return new UnicodeSet(unicodeSetPattern, UnicodeSet.IGNORE_SPACE | options);
-                } catch (IllegalArgumentException e) {
-                    throw new IllegalArgumentException("Can't create exemplars for " + exemplarSetTypes[extype] + " in " + bundle.getLocale(), e);
-                }
-            }
-            return new UnicodeSet(unicodeSetPattern, UnicodeSet.IGNORE_SPACE | options);
-        }catch(MissingResourceException ex){
-            if(extype==LocaleData.ES_AUXILIARY){
-                return new UnicodeSet();
-            } else if (extype==LocaleData.ES_INDEX){
+            ICUResourceBundle stringBundle = (ICUResourceBundle) bundle.get(aKey);
+
+            if ( noSubstitute && (stringBundle.getLoadingStatus() == ICUResourceBundle.FROM_ROOT) ) {
                 return null;
             }
-            throw ex;
+            String unicodeSetPattern = stringBundle.getString();
+            return new UnicodeSet(unicodeSetPattern, UnicodeSet.IGNORE_SPACE | options);
+            
+        }catch (Exception ex){
+            return noSubstitute ? null : UnicodeSet.EMPTY;
         }
     }
 
@@ -204,13 +213,13 @@ public final class LocaleData {
      * @stable ICU 3.4
      */
     public static final LocaleData getInstance(ULocale locale) {
-       LocaleData ld = new LocaleData();
-       ld.bundle = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, locale);
-       ld.langBundle = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_LANG_BASE_NAME, locale);
-       ld.noSubstitute = false;
-       return ld;
+        LocaleData ld = new LocaleData();
+        ld.bundle = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, locale);
+        ld.langBundle = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_LANG_BASE_NAME, locale);
+        ld.noSubstitute = false;
+        return ld;
     }
-    
+
     /**
      * Gets the LocaleData object associated with the default <code>FORMAT</code> locale
      *
@@ -219,7 +228,7 @@ public final class LocaleData {
      * @stable ICU 3.4
      */
     public static final LocaleData getInstance() {
-       return LocaleData.getInstance(ULocale.getDefault(Category.FORMAT));
+        return LocaleData.getInstance(ULocale.getDefault(Category.FORMAT));
     }
 
     /**
@@ -232,7 +241,7 @@ public final class LocaleData {
      * @stable ICU 3.4
      */
     public void setNoSubstitute(boolean setting) {
-       noSubstitute = setting;
+        noSubstitute = setting;
     }
 
     /**
@@ -245,14 +254,14 @@ public final class LocaleData {
      * @stable ICU 3.4
      */
     public boolean getNoSubstitute() {
-       return noSubstitute;
+        return noSubstitute;
     }
 
     private static final String [] DELIMITER_TYPES = {
-                                 "quotationStart", 
-                                 "quotationEnd", 
-                                 "alternateQuotationStart", 
-                                 "alternateQuotationEnd"
+        "quotationStart", 
+        "quotationEnd", 
+        "alternateQuotationStart", 
+        "alternateQuotationEnd"
     };
 
     /**
@@ -270,7 +279,7 @@ public final class LocaleData {
         ICUResourceBundle stringBundle = delimitersBundle.getWithFallback(DELIMITER_TYPES[type]);
 
         if ( noSubstitute && (stringBundle.getLoadingStatus() == ICUResourceBundle.FROM_ROOT) )
-           return null;
+            return null;
 
         return stringBundle.getString();
     }
@@ -281,27 +290,27 @@ public final class LocaleData {
     private static UResourceBundle measurementTypeBundleForLocale(ULocale locale, String measurementType){
         // Much of this is taken from getCalendarType in impl/CalendarUtil.java
         UResourceBundle measTypeBundle = null;
-       ULocale fullLoc = ULocale.addLikelySubtags(locale);
-       String region = fullLoc.getCountry();
-               try {
-                       UResourceBundle rb = UResourceBundle.getBundleInstance(
-                                                                       ICUResourceBundle.ICU_BASE_NAME,
-                                                                       "supplementalData",
-                                                                       ICUResourceBundle.ICU_DATA_CLASS_LOADER);
-                       UResourceBundle measurementData = rb.get("measurementData");
-                       UResourceBundle measDataBundle = null;
-                       try {
-                               measDataBundle = measurementData.get(region);
-                               measTypeBundle = measDataBundle.get(measurementType);
-                       } catch (MissingResourceException mre) {
-                               // use "001" as fallback
-                               measDataBundle = measurementData.get("001");
-                               measTypeBundle = measDataBundle.get(measurementType);
-                       }
-               } catch (MissingResourceException mre) {
-                       // fall through
-               }
-               return measTypeBundle;
+        ULocale fullLoc = ULocale.addLikelySubtags(locale);
+        String region = fullLoc.getCountry();
+        try {
+            UResourceBundle rb = UResourceBundle.getBundleInstance(
+                    ICUResourceBundle.ICU_BASE_NAME,
+                    "supplementalData",
+                    ICUResourceBundle.ICU_DATA_CLASS_LOADER);
+            UResourceBundle measurementData = rb.get("measurementData");
+            UResourceBundle measDataBundle = null;
+            try {
+                measDataBundle = measurementData.get(region);
+                measTypeBundle = measDataBundle.get(measurementType);
+            } catch (MissingResourceException mre) {
+                // use "001" as fallback
+                measDataBundle = measurementData.get("001");
+                measTypeBundle = measDataBundle.get(measurementType);
+            }
+        } catch (MissingResourceException mre) {
+            // fall through
+        }
+        return measTypeBundle;
     }
 
 
@@ -316,13 +325,13 @@ public final class LocaleData {
          * @stable ICU 2.8
          */
         public static final MeasurementSystem SI = new MeasurementSystem(0);
+
         /** 
          * Measurement system followed in the United States of America. 
          * @stable ICU 2.8
          */ 
         public static final MeasurementSystem US = new MeasurementSystem(1);
-    
+
         private int systemID;
         private MeasurementSystem(int id){
             systemID = id;
@@ -332,7 +341,7 @@ public final class LocaleData {
             return systemID == id;
         }
     }
-   
+
     /**
      * Returns the measurement system used in the locale specified by the locale.
      *
@@ -342,7 +351,7 @@ public final class LocaleData {
      */
     public static final MeasurementSystem getMeasurementSystem(ULocale locale){
         UResourceBundle sysBundle = measurementTypeBundleForLocale(locale, MEASUREMENT_SYSTEM);
-        
+
         int system = sysBundle.getInt();
         if(MeasurementSystem.US.equals(system)){
             return MeasurementSystem.US;
@@ -354,7 +363,7 @@ public final class LocaleData {
         // of integer indicating an error
         return null;
     }
-    
+
     /**
      * A class that represents the size of letter head 
      * used in the country
@@ -363,7 +372,7 @@ public final class LocaleData {
     public static final class PaperSize{
         private int height;
         private int width;
-        
+
         private PaperSize(int h, int w){
             height = h;
             width = w;
@@ -385,7 +394,7 @@ public final class LocaleData {
             return width;
         }
     }
-    
+
     /**
      * Returns the size of paper used in the locale. The paper sizes returned are always in 
      * <em> milli-meters<em>.
@@ -398,38 +407,38 @@ public final class LocaleData {
         int[] size = obj.getIntVector();
         return new PaperSize(size[0], size[1]);
     }
-    
+
     /**
      * Returns LocaleDisplayPattern for this locale, e.g., {0}({1})
      * @return locale display pattern as a String.
      * @stable ICU 4.2
      */ 
     public String getLocaleDisplayPattern() {
-      ICUResourceBundle locDispBundle = (ICUResourceBundle) langBundle.get(LOCALE_DISPLAY_PATTERN);
-      String localeDisplayPattern = locDispBundle.getStringWithFallback(PATTERN);
-      return localeDisplayPattern;
+        ICUResourceBundle locDispBundle = (ICUResourceBundle) langBundle.get(LOCALE_DISPLAY_PATTERN);
+        String localeDisplayPattern = locDispBundle.getStringWithFallback(PATTERN);
+        return localeDisplayPattern;
     }
-    
+
     /**
      * Returns LocaleDisplaySeparator for this locale.
      * @return locale display separator as a char.
      * @stable ICU 4.2
      */ 
     public String getLocaleSeparator() {
-      String sub0 = "{0}";
-      String sub1 = "{1}";
-      ICUResourceBundle locDispBundle = (ICUResourceBundle) langBundle.get(LOCALE_DISPLAY_PATTERN);
-      String  localeSeparator = locDispBundle.getStringWithFallback(SEPARATOR);
-      int index0 = localeSeparator.indexOf(sub0);
-      int index1 = localeSeparator.indexOf(sub1);
-      if (index0 >= 0 && index1 >= 0 && index0 <= index1) {
-          return localeSeparator.substring(index0 + sub0.length(), index1);
-      }
-      return localeSeparator;
+        String sub0 = "{0}";
+        String sub1 = "{1}";
+        ICUResourceBundle locDispBundle = (ICUResourceBundle) langBundle.get(LOCALE_DISPLAY_PATTERN);
+        String  localeSeparator = locDispBundle.getStringWithFallback(SEPARATOR);
+        int index0 = localeSeparator.indexOf(sub0);
+        int index1 = localeSeparator.indexOf(sub1);
+        if (index0 >= 0 && index1 >= 0 && index0 <= index1) {
+            return localeSeparator.substring(index0 + sub0.length(), index1);
+        }
+        return localeSeparator;
     }
-    
+
     private static VersionInfo gCLDRVersion = null;
-    
+
     /**
      * Returns the current CLDR version
      * @stable ICU 4.2