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 } 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 } 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."); 279 } 280 key.append("Month").append(toStyleName(baseStyle)); 281 break; 282 283 case DAY_OF_WEEK: 284 // support standalone narrow day names 285 if (isStandalone && isNarrow) { 286 key.append("standalone."); 287 } 288 key.append("Day").append(toStyleName(baseStyle)); 289 break; 290 291 case AM_PM: 292 if (isNarrow) { 293 key.append("narrow."); 294 } 295 key.append("AmPmMarkers"); 296 break; 297 } 298 return key.length() > 0 ? key.toString() : null; 299 } 300 301 private String toStyleName(int baseStyle) { 302 switch (baseStyle) { 303 case SHORT: 304 return "Abbreviations"; 305 case NARROW_FORMAT: | 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 67 // If standalone names are requested and no "standalone." resources are found, 68 // try the default ones instead. 69 if (strings == null && key.indexOf("standalone.") != -1) { 70 key = key.replaceFirst("standalone.", ""); 71 strings = javatime ? lr.getJavaTimeNames(key) : lr.getCalendarNames(key); 72 } 73 74 if (strings != null && strings.length > 0) { 75 if (field == DAY_OF_WEEK || field == YEAR) { 76 --value; 77 } 78 if (value < 0 || value >= strings.length) { 79 return null; 80 } 81 name = strings[value]; 82 // If name is empty in standalone, try its `format' style. 83 if (name.length() == 0 84 && (style == SHORT_STANDALONE || style == LONG_STANDALONE 85 || style == NARROW_STANDALONE)) { 86 name = getDisplayName(calendarType, field, value, 87 getBaseStyle(style), 88 locale); 89 } 90 } 91 } 92 return name; 93 } 109 // specific style 110 names = getDisplayNamesImpl(calendarType, field, style, locale, false); 111 } 112 return names.isEmpty() ? null : names; 113 } 114 115 // NOTE: This method should be used ONLY BY JSR 310 classes. 116 public Map<String, Integer> getJavaTimeDisplayNames(String calendarType, int field, int style, Locale locale) { 117 Map<String, Integer> names; 118 names = getDisplayNamesImpl(calendarType, field, style, locale, true); 119 return names.isEmpty() ? null : names; 120 } 121 122 private Map<String, Integer> getDisplayNamesImpl(String calendarType, int field, 123 int style, Locale locale, boolean javatime) { 124 String key = getResourceKey(calendarType, field, style, javatime); 125 Map<String, Integer> map = new TreeMap<>(LengthBasedComparator.INSTANCE); 126 if (key != null) { 127 LocaleResources lr = LocaleProviderAdapter.forType(type).getLocaleResources(locale); 128 String[] strings = javatime ? lr.getJavaTimeNames(key) : lr.getCalendarNames(key); 129 130 // If standalone names are requested and no "standalone." resources are found, 131 // try the default ones instead. 132 if (strings == null && key.indexOf("standalone.") != -1) { 133 key = key.replaceFirst("standalone.", ""); 134 strings = javatime ? lr.getJavaTimeNames(key) : lr.getCalendarNames(key); 135 } 136 137 if (strings != null) { 138 if (!hasDuplicates(strings)) { 139 if (field == YEAR) { 140 if (strings.length > 0) { 141 map.put(strings[0], 1); 142 } 143 } else { 144 int base = (field == DAY_OF_WEEK) ? 1 : 0; 145 for (int i = 0; i < strings.length; i++) { 146 String name = strings[i]; 147 // Ignore any empty string (some standalone month names 148 // are not defined) 149 if (name.length() == 0) { 150 continue; 151 } 152 map.put(name, base + i); 153 } 154 } 155 } 156 } 280 key.append("Eras"); 281 break; 282 283 case YEAR: 284 if (!isNarrow) { 285 key.append(type).append(".FirstYear"); 286 } 287 break; 288 289 case MONTH: 290 if ("islamic".equals(type)) { 291 key.append(type).append('.'); 292 } 293 if (isStandalone) { 294 key.append("standalone."); 295 } 296 key.append("Month").append(toStyleName(baseStyle)); 297 break; 298 299 case DAY_OF_WEEK: 300 // support standalone day names 301 if (isStandalone) { 302 key.append("standalone."); 303 } 304 key.append("Day").append(toStyleName(baseStyle)); 305 break; 306 307 case AM_PM: 308 if (isNarrow) { 309 key.append("narrow."); 310 } 311 key.append("AmPmMarkers"); 312 break; 313 } 314 return key.length() > 0 ? key.toString() : null; 315 } 316 317 private String toStyleName(int baseStyle) { 318 switch (baseStyle) { 319 case SHORT: 320 return "Abbreviations"; 321 case NARROW_FORMAT: |