src/share/classes/java/time/format/DateTimeTextProvider.java

Print this page




 178                     fieldValue = (int) value + 2;
 179                 }
 180             } else {
 181                 fieldValue = (int) value;
 182             }
 183         } else if (field == MONTH_OF_YEAR) {
 184             fieldIndex = Calendar.MONTH;
 185             fieldValue = (int) value - 1;
 186         } else if (field == DAY_OF_WEEK) {
 187             fieldIndex = Calendar.DAY_OF_WEEK;
 188             fieldValue = (int) value + 1;
 189             if (fieldValue > 7) {
 190                 fieldValue = Calendar.SUNDAY;
 191             }
 192         } else if (field == AMPM_OF_DAY) {
 193             fieldIndex = Calendar.AM_PM;
 194             fieldValue = (int) value;
 195         } else {
 196             return null;
 197         }
 198         return CalendarDataUtility.retrieveCldrFieldValueName(
 199                 chrono.getCalendarType(), fieldIndex, fieldValue, style.toCalendarStyle(), locale);
 200     }
 201 
 202     /**
 203      * Gets an iterator of text to field for the specified field, locale and style
 204      * for the purpose of parsing.
 205      * <p>
 206      * The iterator must be returned in order from the longest text to the shortest.
 207      * <p>
 208      * The null return value should be used if there is no applicable parsable text, or
 209      * if the text would be a numeric representation of the value.
 210      * Text can only be parsed if all the values for that field-style-locale combination are unique.
 211      *
 212      * @param field  the field to get text for, not null
 213      * @param style  the style to get text for, null for all parsable text
 214      * @param locale  the locale to get text for, not null
 215      * @return the iterator of text to field pairs, in order from longest text to shortest text,
 216      *  null if the field or style is not parsable
 217      */
 218     public Iterator<Entry<String, Long>> getTextIterator(TemporalField field, TextStyle style, Locale locale) {


 249 
 250         int fieldIndex;
 251         switch ((ChronoField)field) {
 252         case ERA:
 253             fieldIndex = Calendar.ERA;
 254             break;
 255         case MONTH_OF_YEAR:
 256             fieldIndex = Calendar.MONTH;
 257             break;
 258         case DAY_OF_WEEK:
 259             fieldIndex = Calendar.DAY_OF_WEEK;
 260             break;
 261         case AMPM_OF_DAY:
 262             fieldIndex = Calendar.AM_PM;
 263             break;
 264         default:
 265             return null;
 266         }
 267 
 268         int calendarStyle = (style == null) ? Calendar.ALL_STYLES : style.toCalendarStyle();
 269         Map<String, Integer> map = CalendarDataUtility.retrieveCldrFieldValueNames(
 270                 chrono.getCalendarType(), fieldIndex, calendarStyle, locale);
 271         if (map == null) {
 272             return null;
 273         }
 274         List<Entry<String, Long>> list = new ArrayList<>(map.size());
 275         switch (fieldIndex) {
 276         case Calendar.ERA:
 277             for (Map.Entry<String, Integer> entry : map.entrySet()) {
 278                 int era = entry.getValue();
 279                 if (chrono == JapaneseChronology.INSTANCE) {
 280                     if (era == 0) {
 281                         era = -999;
 282                     } else {
 283                         era -= 2;
 284                     }
 285                 }
 286                 list.add(createEntry(entry.getKey(), (long)era));
 287             }
 288             break;
 289         case Calendar.MONTH:


 315         }
 316         return store;
 317     }
 318 
 319     private static int toWeekDay(int calWeekDay) {
 320         if (calWeekDay == Calendar.SUNDAY) {
 321             return 7;
 322         } else {
 323             return calWeekDay - 1;
 324         }
 325     }
 326 
 327     private Object createStore(TemporalField field, Locale locale) {
 328         Map<TextStyle, Map<Long, String>> styleMap = new HashMap<>();
 329         if (field == ERA) {
 330             for (TextStyle textStyle : TextStyle.values()) {
 331                 if (textStyle.isStandalone()) {
 332                     // Stand-alone isn't applicable to era names.
 333                     continue;
 334                 }
 335                 Map<String, Integer> displayNames = CalendarDataUtility.retrieveCldrFieldValueNames(
 336                         "gregory", Calendar.ERA, textStyle.toCalendarStyle(), locale);
 337                 if (displayNames != null) {
 338                     Map<Long, String> map = new HashMap<>();
 339                     for (Entry<String, Integer> entry : displayNames.entrySet()) {
 340                         map.put((long) entry.getValue(), entry.getKey());
 341                     }
 342                     if (!map.isEmpty()) {
 343                         styleMap.put(textStyle, map);
 344                     }
 345                 }
 346             }
 347             return new LocaleStore(styleMap);
 348         }
 349 
 350         if (field == MONTH_OF_YEAR) {
 351             for (TextStyle textStyle : TextStyle.values()) {
 352                 Map<String, Integer> displayNames = CalendarDataUtility.retrieveCldrFieldValueNames(
 353                         "gregory", Calendar.MONTH, textStyle.toCalendarStyle(), locale);
 354                 Map<Long, String> map = new HashMap<>();
 355                 if (displayNames != null) {
 356                     for (Entry<String, Integer> entry : displayNames.entrySet()) {
 357                         map.put((long) (entry.getValue() + 1), entry.getKey());
 358                     }
 359 
 360                 } else {
 361                     // Narrow names may have duplicated names, such as "J" for January, Jun, July.
 362                     // Get names one by one in that case.
 363                     for (int month = Calendar.JANUARY; month <= Calendar.DECEMBER; month++) {
 364                         String name;
 365                         name = CalendarDataUtility.retrieveCldrFieldValueName(
 366                                 "gregory", Calendar.MONTH, month, textStyle.toCalendarStyle(), locale);
 367                         if (name == null) {
 368                             break;
 369                         }
 370                         map.put((long) (month + 1), name);
 371                     }
 372                 }
 373                 if (!map.isEmpty()) {
 374                     styleMap.put(textStyle, map);
 375                 }
 376             }
 377             return new LocaleStore(styleMap);
 378         }
 379 
 380         if (field == DAY_OF_WEEK) {
 381             for (TextStyle textStyle : TextStyle.values()) {
 382                 Map<String, Integer> displayNames = CalendarDataUtility.retrieveCldrFieldValueNames(
 383                         "gregory", Calendar.DAY_OF_WEEK, textStyle.toCalendarStyle(), locale);
 384                 Map<Long, String> map = new HashMap<>();
 385                 if (displayNames != null) {
 386                     for (Entry<String, Integer> entry : displayNames.entrySet()) {
 387                         map.put((long)toWeekDay(entry.getValue()), entry.getKey());
 388                     }
 389 
 390                 } else {
 391                     // Narrow names may have duplicated names, such as "S" for Sunday and Saturday.
 392                     // Get names one by one in that case.
 393                     for (int wday = Calendar.SUNDAY; wday <= Calendar.SATURDAY; wday++) {
 394                         String name;
 395                         name = CalendarDataUtility.retrieveCldrFieldValueName(
 396                             "gregory", Calendar.DAY_OF_WEEK, wday, textStyle.toCalendarStyle(), locale);
 397                         if (name == null) {
 398                             break;
 399                         }
 400                         map.put((long)toWeekDay(wday), name);
 401                     }
 402                 }
 403                 if (!map.isEmpty()) {
 404                     styleMap.put(textStyle, map);
 405                 }
 406             }
 407             return new LocaleStore(styleMap);
 408         }
 409 
 410         if (field == AMPM_OF_DAY) {
 411             for (TextStyle textStyle : TextStyle.values()) {
 412                 if (textStyle.isStandalone()) {
 413                     // Stand-alone isn't applicable to AM/PM.
 414                     continue;
 415                 }
 416                 Map<String, Integer> displayNames = CalendarDataUtility.retrieveCldrFieldValueNames(
 417                         "gregory", Calendar.AM_PM, textStyle.toCalendarStyle(), locale);
 418                 if (displayNames != null) {
 419                     Map<Long, String> map = new HashMap<>();
 420                     for (Entry<String, Integer> entry : displayNames.entrySet()) {
 421                         map.put((long) entry.getValue(), entry.getKey());
 422                     }
 423                     if (!map.isEmpty()) {
 424                         styleMap.put(textStyle, map);
 425                     }
 426                 }
 427             }
 428             return new LocaleStore(styleMap);
 429         }
 430 
 431         if (field == IsoFields.QUARTER_OF_YEAR) {
 432             // The order of keys must correspond to the TextStyle.values() order.
 433             final String[] keys = {
 434                 "QuarterNames",
 435                 "standalone.QuarterNames",
 436                 "QuarterAbbreviations",


 461      * @param field  the field, not null
 462      * @return the entry, not null
 463      */
 464     private static <A, B> Entry<A, B> createEntry(A text, B field) {
 465         return new SimpleImmutableEntry<>(text, field);
 466     }
 467 
 468     /**
 469      * Returns the localized resource of the given key and locale, or null
 470      * if no localized resource is available.
 471      *
 472      * @param key  the key of the localized resource, not null
 473      * @param locale  the locale, not null
 474      * @return the localized resource, or null if not available
 475      * @throws NullPointerException if key or locale is null
 476      */
 477     @SuppressWarnings("unchecked")
 478     static <T> T getLocalizedResource(String key, Locale locale) {
 479         LocaleResources lr = LocaleProviderAdapter.getResourceBundleBased()
 480                                     .getLocaleResources(locale);
 481         ResourceBundle rb = lr.getFormatData();
 482         return rb.containsKey(key) ? (T) rb.getObject(key) : null;
 483     }
 484 
 485     /**
 486      * Stores the text for a single locale.
 487      * <p>
 488      * Some fields have a textual representation, such as day-of-week or month-of-year.
 489      * These textual representations can be captured in this class for printing
 490      * and parsing.
 491      * <p>
 492      * This class is immutable and thread-safe.
 493      */
 494     static final class LocaleStore {
 495         /**
 496          * Map of value to text.
 497          */
 498         private final Map<TextStyle, Map<Long, String>> valueTextMap;
 499         /**
 500          * Parsable data.
 501          */




 178                     fieldValue = (int) value + 2;
 179                 }
 180             } else {
 181                 fieldValue = (int) value;
 182             }
 183         } else if (field == MONTH_OF_YEAR) {
 184             fieldIndex = Calendar.MONTH;
 185             fieldValue = (int) value - 1;
 186         } else if (field == DAY_OF_WEEK) {
 187             fieldIndex = Calendar.DAY_OF_WEEK;
 188             fieldValue = (int) value + 1;
 189             if (fieldValue > 7) {
 190                 fieldValue = Calendar.SUNDAY;
 191             }
 192         } else if (field == AMPM_OF_DAY) {
 193             fieldIndex = Calendar.AM_PM;
 194             fieldValue = (int) value;
 195         } else {
 196             return null;
 197         }
 198         return CalendarDataUtility.retrieveJavaTimeFieldValueName(
 199                 chrono.getCalendarType(), fieldIndex, fieldValue, style.toCalendarStyle(), locale);
 200     }
 201 
 202     /**
 203      * Gets an iterator of text to field for the specified field, locale and style
 204      * for the purpose of parsing.
 205      * <p>
 206      * The iterator must be returned in order from the longest text to the shortest.
 207      * <p>
 208      * The null return value should be used if there is no applicable parsable text, or
 209      * if the text would be a numeric representation of the value.
 210      * Text can only be parsed if all the values for that field-style-locale combination are unique.
 211      *
 212      * @param field  the field to get text for, not null
 213      * @param style  the style to get text for, null for all parsable text
 214      * @param locale  the locale to get text for, not null
 215      * @return the iterator of text to field pairs, in order from longest text to shortest text,
 216      *  null if the field or style is not parsable
 217      */
 218     public Iterator<Entry<String, Long>> getTextIterator(TemporalField field, TextStyle style, Locale locale) {


 249 
 250         int fieldIndex;
 251         switch ((ChronoField)field) {
 252         case ERA:
 253             fieldIndex = Calendar.ERA;
 254             break;
 255         case MONTH_OF_YEAR:
 256             fieldIndex = Calendar.MONTH;
 257             break;
 258         case DAY_OF_WEEK:
 259             fieldIndex = Calendar.DAY_OF_WEEK;
 260             break;
 261         case AMPM_OF_DAY:
 262             fieldIndex = Calendar.AM_PM;
 263             break;
 264         default:
 265             return null;
 266         }
 267 
 268         int calendarStyle = (style == null) ? Calendar.ALL_STYLES : style.toCalendarStyle();
 269         Map<String, Integer> map = CalendarDataUtility.retrieveJavaTimeFieldValueNames(
 270                 chrono.getCalendarType(), fieldIndex, calendarStyle, locale);
 271         if (map == null) {
 272             return null;
 273         }
 274         List<Entry<String, Long>> list = new ArrayList<>(map.size());
 275         switch (fieldIndex) {
 276         case Calendar.ERA:
 277             for (Map.Entry<String, Integer> entry : map.entrySet()) {
 278                 int era = entry.getValue();
 279                 if (chrono == JapaneseChronology.INSTANCE) {
 280                     if (era == 0) {
 281                         era = -999;
 282                     } else {
 283                         era -= 2;
 284                     }
 285                 }
 286                 list.add(createEntry(entry.getKey(), (long)era));
 287             }
 288             break;
 289         case Calendar.MONTH:


 315         }
 316         return store;
 317     }
 318 
 319     private static int toWeekDay(int calWeekDay) {
 320         if (calWeekDay == Calendar.SUNDAY) {
 321             return 7;
 322         } else {
 323             return calWeekDay - 1;
 324         }
 325     }
 326 
 327     private Object createStore(TemporalField field, Locale locale) {
 328         Map<TextStyle, Map<Long, String>> styleMap = new HashMap<>();
 329         if (field == ERA) {
 330             for (TextStyle textStyle : TextStyle.values()) {
 331                 if (textStyle.isStandalone()) {
 332                     // Stand-alone isn't applicable to era names.
 333                     continue;
 334                 }
 335                 Map<String, Integer> displayNames = CalendarDataUtility.retrieveJavaTimeFieldValueNames(
 336                         "gregory", Calendar.ERA, textStyle.toCalendarStyle(), locale);
 337                 if (displayNames != null) {
 338                     Map<Long, String> map = new HashMap<>();
 339                     for (Entry<String, Integer> entry : displayNames.entrySet()) {
 340                         map.put((long) entry.getValue(), entry.getKey());
 341                     }
 342                     if (!map.isEmpty()) {
 343                         styleMap.put(textStyle, map);
 344                     }
 345                 }
 346             }
 347             return new LocaleStore(styleMap);
 348         }
 349 
 350         if (field == MONTH_OF_YEAR) {
 351             for (TextStyle textStyle : TextStyle.values()) {
 352                 Map<String, Integer> displayNames = CalendarDataUtility.retrieveJavaTimeFieldValueNames(
 353                         "gregory", Calendar.MONTH, textStyle.toCalendarStyle(), locale);
 354                 Map<Long, String> map = new HashMap<>();
 355                 if (displayNames != null) {
 356                     for (Entry<String, Integer> entry : displayNames.entrySet()) {
 357                         map.put((long) (entry.getValue() + 1), entry.getKey());
 358                     }
 359 
 360                 } else {
 361                     // Narrow names may have duplicated names, such as "J" for January, Jun, July.
 362                     // Get names one by one in that case.
 363                     for (int month = Calendar.JANUARY; month <= Calendar.DECEMBER; month++) {
 364                         String name;
 365                         name = CalendarDataUtility.retrieveJavaTimeFieldValueName(
 366                                 "gregory", Calendar.MONTH, month, textStyle.toCalendarStyle(), locale);
 367                         if (name == null) {
 368                             break;
 369                         }
 370                         map.put((long) (month + 1), name);
 371                     }
 372                 }
 373                 if (!map.isEmpty()) {
 374                     styleMap.put(textStyle, map);
 375                 }
 376             }
 377             return new LocaleStore(styleMap);
 378         }
 379 
 380         if (field == DAY_OF_WEEK) {
 381             for (TextStyle textStyle : TextStyle.values()) {
 382                 Map<String, Integer> displayNames = CalendarDataUtility.retrieveJavaTimeFieldValueNames(
 383                         "gregory", Calendar.DAY_OF_WEEK, textStyle.toCalendarStyle(), locale);
 384                 Map<Long, String> map = new HashMap<>();
 385                 if (displayNames != null) {
 386                     for (Entry<String, Integer> entry : displayNames.entrySet()) {
 387                         map.put((long)toWeekDay(entry.getValue()), entry.getKey());
 388                     }
 389 
 390                 } else {
 391                     // Narrow names may have duplicated names, such as "S" for Sunday and Saturday.
 392                     // Get names one by one in that case.
 393                     for (int wday = Calendar.SUNDAY; wday <= Calendar.SATURDAY; wday++) {
 394                         String name;
 395                         name = CalendarDataUtility.retrieveJavaTimeFieldValueName(
 396                             "gregory", Calendar.DAY_OF_WEEK, wday, textStyle.toCalendarStyle(), locale);
 397                         if (name == null) {
 398                             break;
 399                         }
 400                         map.put((long)toWeekDay(wday), name);
 401                     }
 402                 }
 403                 if (!map.isEmpty()) {
 404                     styleMap.put(textStyle, map);
 405                 }
 406             }
 407             return new LocaleStore(styleMap);
 408         }
 409 
 410         if (field == AMPM_OF_DAY) {
 411             for (TextStyle textStyle : TextStyle.values()) {
 412                 if (textStyle.isStandalone()) {
 413                     // Stand-alone isn't applicable to AM/PM.
 414                     continue;
 415                 }
 416                 Map<String, Integer> displayNames = CalendarDataUtility.retrieveJavaTimeFieldValueNames(
 417                         "gregory", Calendar.AM_PM, textStyle.toCalendarStyle(), locale);
 418                 if (displayNames != null) {
 419                     Map<Long, String> map = new HashMap<>();
 420                     for (Entry<String, Integer> entry : displayNames.entrySet()) {
 421                         map.put((long) entry.getValue(), entry.getKey());
 422                     }
 423                     if (!map.isEmpty()) {
 424                         styleMap.put(textStyle, map);
 425                     }
 426                 }
 427             }
 428             return new LocaleStore(styleMap);
 429         }
 430 
 431         if (field == IsoFields.QUARTER_OF_YEAR) {
 432             // The order of keys must correspond to the TextStyle.values() order.
 433             final String[] keys = {
 434                 "QuarterNames",
 435                 "standalone.QuarterNames",
 436                 "QuarterAbbreviations",


 461      * @param field  the field, not null
 462      * @return the entry, not null
 463      */
 464     private static <A, B> Entry<A, B> createEntry(A text, B field) {
 465         return new SimpleImmutableEntry<>(text, field);
 466     }
 467 
 468     /**
 469      * Returns the localized resource of the given key and locale, or null
 470      * if no localized resource is available.
 471      *
 472      * @param key  the key of the localized resource, not null
 473      * @param locale  the locale, not null
 474      * @return the localized resource, or null if not available
 475      * @throws NullPointerException if key or locale is null
 476      */
 477     @SuppressWarnings("unchecked")
 478     static <T> T getLocalizedResource(String key, Locale locale) {
 479         LocaleResources lr = LocaleProviderAdapter.getResourceBundleBased()
 480                                     .getLocaleResources(locale);
 481         ResourceBundle rb = lr.getJavaTimeFormatData();
 482         return rb.containsKey(key) ? (T) rb.getObject(key) : null;
 483     }
 484 
 485     /**
 486      * Stores the text for a single locale.
 487      * <p>
 488      * Some fields have a textual representation, such as day-of-week or month-of-year.
 489      * These textual representations can be captured in this class for printing
 490      * and parsing.
 491      * <p>
 492      * This class is immutable and thread-safe.
 493      */
 494     static final class LocaleStore {
 495         /**
 496          * Map of value to text.
 497          */
 498         private final Map<TextStyle, Map<Long, String>> valueTextMap;
 499         /**
 500          * Parsable data.
 501          */