src/share/classes/sun/util/locale/provider/CalendarNameProviderImpl.java

Print this page




  36  * Concrete implementation of the  {@link java.util.spi.CalendarDataProvider
  37  * CalendarDataProvider} class for the JRE LocaleProviderAdapter.
  38  *
  39  * @author Masayoshi Okutsu
  40  * @author Naoto Sato
  41  */
  42 public class CalendarNameProviderImpl extends CalendarNameProvider implements AvailableLanguageTags {
  43     private final LocaleProviderAdapter.Type type;
  44     private final Set<String> langtags;
  45 
  46     public CalendarNameProviderImpl(LocaleProviderAdapter.Type type, Set<String> langtags) {
  47         this.type = type;
  48         this.langtags = langtags;
  49     }
  50 
  51     @Override
  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) {
  61         String name = null;
  62         String key = getResourceKey(calendarType, field, style, cldr);
  63         if (key != null) {
  64             String[] strings = LocaleProviderAdapter.forType(type).getLocaleResources(locale).getCalendarNames(key);

  65             if (strings != null && strings.length > 0) {
  66                 if (field == DAY_OF_WEEK || field == YEAR) {
  67                     --value;
  68                 }
  69                 if (value < 0 || value >= strings.length) {
  70                     return null;
  71                 }
  72                 name = strings[value];
  73                 // If name is empty in standalone, try its `format' style.
  74                 if (name.length() == 0
  75                         && (style == SHORT_STANDALONE || style == LONG_STANDALONE
  76                             || style == NARROW_STANDALONE)) {
  77                     name = getDisplayName(calendarType, field, value,
  78                                           getBaseStyle(style),
  79                                           locale);
  80                 }
  81             }
  82         }
  83         return name;
  84     }


  87         SHORT_STANDALONE, LONG_FORMAT, LONG_STANDALONE,
  88         NARROW_FORMAT, NARROW_STANDALONE
  89     };
  90 
  91     @Override
  92     public Map<String, Integer> getDisplayNames(String calendarType, int field, int style, Locale locale) {
  93         Map<String, Integer> names;
  94         if (style == ALL_STYLES) {
  95             names = getDisplayNamesImpl(calendarType, field, SHORT_FORMAT, locale, false);
  96             for (int st : REST_OF_STYLES) {
  97                 names.putAll(getDisplayNamesImpl(calendarType, field, st, locale, false));
  98             }
  99         } else {
 100             // specific style
 101             names = getDisplayNamesImpl(calendarType, field, style, locale, false);
 102         }
 103         return names.isEmpty() ? null : names;
 104     }
 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 
 113     private Map<String, Integer> getDisplayNamesImpl(String calendarType, int field,
 114                                                      int style, Locale locale, boolean cldr) {
 115         String key = getResourceKey(calendarType, field, style, cldr);
 116         Map<String, Integer> map = new TreeMap<>(LengthBasedComparator.INSTANCE);
 117         if (key != null) {
 118             String[] strings = LocaleProviderAdapter.forType(type).getLocaleResources(locale).getCalendarNames(key);

 119             if (strings != null) {
 120                 if (!hasDuplicates(strings)) {
 121                     if (field == YEAR) {
 122                         if (strings.length > 0) {
 123                             map.put(strings[0], 1);
 124                         }
 125                     } else {
 126                         int base = (field == DAY_OF_WEEK) ? 1 : 0;
 127                         for (int i = 0; i < strings.length; i++) {
 128                             String name = strings[i];
 129                             // Ignore any empty string (some standalone month names
 130                             // are not defined)
 131                             if (name.length() == 0) {
 132                                 continue;
 133                             }
 134                             map.put(name, base + i);
 135                         }
 136                     }
 137                 }
 138             }


 203     @Override
 204     public Set<String> getAvailableLanguageTags() {
 205         return langtags;
 206     }
 207 
 208     private boolean hasDuplicates(String[] strings) {
 209         int len = strings.length;
 210         for (int i = 0; i < len - 1; i++) {
 211             String a = strings[i];
 212             if (a != null) {
 213                 for (int j = i + 1; j < len; j++) {
 214                     if (a.equals(strings[j]))  {
 215                         return true;
 216                     }
 217                 }
 218             }
 219         }
 220         return false;
 221     }
 222 
 223     private String getResourceKey(String type, int field, int style, boolean cldr) {
 224         int baseStyle = getBaseStyle(style);
 225         boolean isStandalone = (style != baseStyle);
 226 
 227         if ("gregory".equals(type)) {
 228             type = null;
 229         }
 230         boolean isNarrow = (baseStyle == NARROW_FORMAT);
 231         StringBuilder key = new StringBuilder();
 232         // If cldr is true, use prefix "cldr.".
 233         if (cldr) {
 234             key.append("cldr.");
 235         }
 236         switch (field) {
 237         case ERA:
 238             if (type != null) {
 239                 key.append(type).append('.');
 240             }
 241             if (isNarrow) {
 242                 key.append("narrow.");
 243             } else {
 244                 // JRE and CLDR use different resource key conventions
 245                 // due to historical reasons. (JRE DateFormatSymbols.getEras returns
 246                 // abbreviations while other getShort*() return abbreviations.)
 247                 if (this.type == LocaleProviderAdapter.Type.JRE) {
 248                     if (cldr) {
 249                         if (baseStyle == LONG) {
 250                             key.append("long.");
 251                         }
 252                     }
 253                     if (baseStyle == SHORT) {
 254                         key.append("short.");
 255                     }
 256                 } else { // CLDR
 257                     if (baseStyle == LONG) {
 258                         key.append("long.");
 259                     }
 260                 }
 261             }
 262             key.append("Eras");
 263             break;
 264 
 265         case YEAR:
 266             if (!isNarrow) {
 267                 key.append(type).append(".FirstYear");
 268             }
 269             break;
 270 
 271         case MONTH:
 272             if ("islamic".equals(type)) {
 273                 key.append(type).append('.');
 274             }
 275             if (isStandalone) {
 276                 key.append("standalone.");




  36  * Concrete implementation of the  {@link java.util.spi.CalendarDataProvider
  37  * CalendarDataProvider} class for the JRE LocaleProviderAdapter.
  38  *
  39  * @author Masayoshi Okutsu
  40  * @author Naoto Sato
  41  */
  42 public class CalendarNameProviderImpl extends CalendarNameProvider implements AvailableLanguageTags {
  43     private final LocaleProviderAdapter.Type type;
  44     private final Set<String> langtags;
  45 
  46     public CalendarNameProviderImpl(LocaleProviderAdapter.Type type, Set<String> langtags) {
  47         this.type = type;
  48         this.langtags = langtags;
  49     }
  50 
  51     @Override
  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 getJavaTimeDisplayName(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 javatime) {
  61         String name = null;
  62         String key = getResourceKey(calendarType, field, style, javatime);
  63         if (key != null) {
  64             LocaleResources lr = LocaleProviderAdapter.forType(type).getLocaleResources(locale);
  65             String[] strings = javatime ? lr.getJavaTimeNames(key) : lr.getCalendarNames(key);
  66             if (strings != null && strings.length > 0) {
  67                 if (field == DAY_OF_WEEK || field == YEAR) {
  68                     --value;
  69                 }
  70                 if (value < 0 || value >= strings.length) {
  71                     return null;
  72                 }
  73                 name = strings[value];
  74                 // If name is empty in standalone, try its `format' style.
  75                 if (name.length() == 0
  76                         && (style == SHORT_STANDALONE || style == LONG_STANDALONE
  77                             || style == NARROW_STANDALONE)) {
  78                     name = getDisplayName(calendarType, field, value,
  79                                           getBaseStyle(style),
  80                                           locale);
  81                 }
  82             }
  83         }
  84         return name;
  85     }


  88         SHORT_STANDALONE, LONG_FORMAT, LONG_STANDALONE,
  89         NARROW_FORMAT, NARROW_STANDALONE
  90     };
  91 
  92     @Override
  93     public Map<String, Integer> getDisplayNames(String calendarType, int field, int style, Locale locale) {
  94         Map<String, Integer> names;
  95         if (style == ALL_STYLES) {
  96             names = getDisplayNamesImpl(calendarType, field, SHORT_FORMAT, locale, false);
  97             for (int st : REST_OF_STYLES) {
  98                 names.putAll(getDisplayNamesImpl(calendarType, field, st, locale, false));
  99             }
 100         } else {
 101             // specific style
 102             names = getDisplayNamesImpl(calendarType, field, style, locale, false);
 103         }
 104         return names.isEmpty() ? null : names;
 105     }
 106 
 107     // NOTE: This method should be used ONLY BY JSR 310 classes.
 108     public Map<String, Integer> getJavaTimeDisplayNames(String calendarType, int field, int style, Locale locale) {
 109         Map<String, Integer> names;
 110         names = getDisplayNamesImpl(calendarType, field, style, locale, true);
 111         return names.isEmpty() ? null : names;
 112     }
 113 
 114     private Map<String, Integer> getDisplayNamesImpl(String calendarType, int field,
 115                                                      int style, Locale locale, boolean javatime) {
 116         String key = getResourceKey(calendarType, field, style, javatime);
 117         Map<String, Integer> map = new TreeMap<>(LengthBasedComparator.INSTANCE);
 118         if (key != null) {
 119             LocaleResources lr = LocaleProviderAdapter.forType(type).getLocaleResources(locale);
 120             String[] strings = javatime ? lr.getJavaTimeNames(key) : lr.getCalendarNames(key);
 121             if (strings != null) {
 122                 if (!hasDuplicates(strings)) {
 123                     if (field == YEAR) {
 124                         if (strings.length > 0) {
 125                             map.put(strings[0], 1);
 126                         }
 127                     } else {
 128                         int base = (field == DAY_OF_WEEK) ? 1 : 0;
 129                         for (int i = 0; i < strings.length; i++) {
 130                             String name = strings[i];
 131                             // Ignore any empty string (some standalone month names
 132                             // are not defined)
 133                             if (name.length() == 0) {
 134                                 continue;
 135                             }
 136                             map.put(name, base + i);
 137                         }
 138                     }
 139                 }
 140             }


 205     @Override
 206     public Set<String> getAvailableLanguageTags() {
 207         return langtags;
 208     }
 209 
 210     private boolean hasDuplicates(String[] strings) {
 211         int len = strings.length;
 212         for (int i = 0; i < len - 1; i++) {
 213             String a = strings[i];
 214             if (a != null) {
 215                 for (int j = i + 1; j < len; j++) {
 216                     if (a.equals(strings[j]))  {
 217                         return true;
 218                     }
 219                 }
 220             }
 221         }
 222         return false;
 223     }
 224 
 225     private String getResourceKey(String type, int field, int style, boolean javatime) {
 226         int baseStyle = getBaseStyle(style);
 227         boolean isStandalone = (style != baseStyle);
 228 
 229         if ("gregory".equals(type)) {
 230             type = null;
 231         }
 232         boolean isNarrow = (baseStyle == NARROW_FORMAT);
 233         StringBuilder key = new StringBuilder();
 234         // If javatime is true, use prefix "java.time.".
 235         if (javatime) {
 236             key.append("java.time.");
 237         }
 238         switch (field) {
 239         case ERA:
 240             if (type != null) {
 241                 key.append(type).append('.');
 242             }
 243             if (isNarrow) {
 244                 key.append("narrow.");
 245             } else {
 246                 // JRE and CLDR use different resource key conventions
 247                 // due to historical reasons. (JRE DateFormatSymbols.getEras returns
 248                 // abbreviations while other getShort*() return abbreviations.)
 249                 if (this.type == LocaleProviderAdapter.Type.JRE) {
 250                     if (javatime) {
 251                         if (baseStyle == LONG) {
 252                             key.append("long.");
 253                         }
 254                     }
 255                     if (baseStyle == SHORT) {
 256                         key.append("short.");
 257                     }
 258                 } else { // this.type == LocaleProviderAdapter.Type.CLDR
 259                     if (baseStyle == LONG) {
 260                         key.append("long.");
 261                     }
 262                 }
 263             }
 264             key.append("Eras");
 265             break;
 266 
 267         case YEAR:
 268             if (!isNarrow) {
 269                 key.append(type).append(".FirstYear");
 270             }
 271             break;
 272 
 273         case MONTH:
 274             if ("islamic".equals(type)) {
 275                 key.append(type).append('.');
 276             }
 277             if (isStandalone) {
 278                 key.append("standalone.");