jdk/src/share/classes/sun/util/locale/provider/CalendarNameProviderImpl.java
changeset 15658 55b829ca2334
parent 15646 b065aeb8e26b
child 16852 60207b2b4b42
equal deleted inserted replaced
15657:c588664d547e 15658:55b829ca2334
    48         this.langtags = langtags;
    48         this.langtags = langtags;
    49     }
    49     }
    50 
    50 
    51     @Override
    51     @Override
    52     public String getDisplayName(String calendarType, int field, int value, int style, Locale locale) {
    52     public String getDisplayName(String calendarType, int field, int value, int style, Locale locale) {
       
    53         return getDisplayNameImpl(calendarType, field, value, style, locale, false);
       
    54     }
       
    55 
       
    56     public String getCldrDisplayName(String calendarType, int field, int value, int style, Locale locale) {
       
    57         return getDisplayNameImpl(calendarType, field, value, style, locale, true);
       
    58     }
       
    59 
       
    60     public String getDisplayNameImpl(String calendarType, int field, int value, int style, Locale locale, boolean cldr) {
    53         String name = null;
    61         String name = null;
    54         String key = getResourceKey(calendarType, field, style);
    62         String key = getResourceKey(calendarType, field, style, cldr);
    55         if (key != null) {
    63         if (key != null) {
    56             String[] strings = LocaleProviderAdapter.forType(type).getLocaleResources(locale).getCalendarNames(key);
    64             String[] strings = LocaleProviderAdapter.forType(type).getLocaleResources(locale).getCalendarNames(key);
    57             if (strings != null && strings.length > 0) {
    65             if (strings != null && strings.length > 0) {
    58                 if (field == DAY_OF_WEEK || field == YEAR) {
    66                 if (field == DAY_OF_WEEK || field == YEAR) {
    59                     --value;
    67                     --value;
    77 
    85 
    78     private static int[] REST_OF_STYLES = {
    86     private static int[] REST_OF_STYLES = {
    79         SHORT_STANDALONE, LONG_FORMAT, LONG_STANDALONE,
    87         SHORT_STANDALONE, LONG_FORMAT, LONG_STANDALONE,
    80         NARROW_FORMAT, NARROW_STANDALONE
    88         NARROW_FORMAT, NARROW_STANDALONE
    81     };
    89     };
       
    90 
    82     @Override
    91     @Override
    83     public Map<String, Integer> getDisplayNames(String calendarType, int field, int style, Locale locale) {
    92     public Map<String, Integer> getDisplayNames(String calendarType, int field, int style, Locale locale) {
    84         Map<String, Integer> names;
    93         Map<String, Integer> names;
    85         if (style == ALL_STYLES) {
    94         if (style == ALL_STYLES) {
    86             names = getDisplayNamesImpl(calendarType, field, SHORT_FORMAT, locale);
    95             names = getDisplayNamesImpl(calendarType, field, SHORT_FORMAT, locale, false);
    87             for (int st : REST_OF_STYLES) {
    96             for (int st : REST_OF_STYLES) {
    88                 names.putAll(getDisplayNamesImpl(calendarType, field, st, locale));
    97                 names.putAll(getDisplayNamesImpl(calendarType, field, st, locale, false));
    89             }
    98             }
    90         } else {
    99         } else {
    91             // specific style
   100             // specific style
    92             names = getDisplayNamesImpl(calendarType, field, style, locale);
   101             names = getDisplayNamesImpl(calendarType, field, style, locale, false);
    93         }
   102         }
    94         return names.isEmpty() ? null : names;
   103         return names.isEmpty() ? null : names;
    95     }
   104     }
    96 
   105 
       
   106     // NOTE: This method should be used ONLY BY JSR 310 classes.
       
   107     public Map<String, Integer> getCldrDisplayNames(String calendarType, int field, int style, Locale locale) {
       
   108         Map<String, Integer> names;
       
   109         names = getDisplayNamesImpl(calendarType, field, style, locale, true);
       
   110         return names.isEmpty() ? null : names;
       
   111     }
       
   112 
    97     private Map<String, Integer> getDisplayNamesImpl(String calendarType, int field,
   113     private Map<String, Integer> getDisplayNamesImpl(String calendarType, int field,
    98                                                      int style, Locale locale) {
   114                                                      int style, Locale locale, boolean cldr) {
    99         String key = getResourceKey(calendarType, field, style);
   115         String key = getResourceKey(calendarType, field, style, cldr);
   100         Map<String, Integer> map = new TreeMap<>(LengthBasedComparator.INSTANCE);
   116         Map<String, Integer> map = new TreeMap<>(LengthBasedComparator.INSTANCE);
   101         if (key != null) {
   117         if (key != null) {
   102             String[] strings = LocaleProviderAdapter.forType(type).getLocaleResources(locale).getCalendarNames(key);
   118             String[] strings = LocaleProviderAdapter.forType(type).getLocaleResources(locale).getCalendarNames(key);
   103             if (strings != null) {
   119             if (strings != null) {
   104                 if (!hasDuplicates(strings)) {
   120                 if (!hasDuplicates(strings)) {
   202             }
   218             }
   203         }
   219         }
   204         return false;
   220         return false;
   205     }
   221     }
   206 
   222 
   207     private String getResourceKey(String type, int field, int style) {
   223     private String getResourceKey(String type, int field, int style, boolean cldr) {
   208         int baseStyle = getBaseStyle(style);
   224         int baseStyle = getBaseStyle(style);
   209         boolean isStandalone = (style != baseStyle);
   225         boolean isStandalone = (style != baseStyle);
   210 
   226 
   211         if ("gregory".equals(type)) {
   227         if ("gregory".equals(type)) {
   212             type = null;
   228             type = null;
   213         }
   229         }
   214         boolean isNarrow = (baseStyle == NARROW_FORMAT);
   230         boolean isNarrow = (baseStyle == NARROW_FORMAT);
   215         StringBuilder key = new StringBuilder();
   231         StringBuilder key = new StringBuilder();
       
   232         // If cldr is true, use prefix "cldr.".
       
   233         if (cldr) {
       
   234             key.append("cldr.");
       
   235         }
   216         switch (field) {
   236         switch (field) {
   217         case ERA:
   237         case ERA:
   218             if (type != null) {
   238             if (type != null) {
   219                 key.append(type).append('.');
   239                 key.append(type).append('.');
   220             }
   240             }
   223             } else {
   243             } else {
   224                 // JRE and CLDR use different resource key conventions
   244                 // JRE and CLDR use different resource key conventions
   225                 // due to historical reasons. (JRE DateFormatSymbols.getEras returns
   245                 // due to historical reasons. (JRE DateFormatSymbols.getEras returns
   226                 // abbreviations while other getShort*() return abbreviations.)
   246                 // abbreviations while other getShort*() return abbreviations.)
   227                 if (this.type == LocaleProviderAdapter.Type.JRE) {
   247                 if (this.type == LocaleProviderAdapter.Type.JRE) {
       
   248                     if (cldr) {
       
   249                         if (baseStyle == LONG) {
       
   250                             key.append("long.");
       
   251                         }
       
   252                     }
   228                     if (baseStyle == SHORT) {
   253                     if (baseStyle == SHORT) {
   229                         key.append("short.");
   254                         key.append("short.");
   230                     }
   255                     }
   231                 } else { // CLDR
   256                 } else { // CLDR
   232                     if (baseStyle == LONG) {
   257                     if (baseStyle == LONG) {