25 |
25 |
26 package java.util; |
26 package java.util; |
27 |
27 |
28 import java.io.IOException; |
28 import java.io.IOException; |
29 import java.io.ObjectInputStream; |
29 import java.io.ObjectInputStream; |
|
30 import sun.util.locale.provider.CalendarDataUtility; |
30 import sun.util.calendar.BaseCalendar; |
31 import sun.util.calendar.BaseCalendar; |
31 import sun.util.calendar.CalendarDate; |
32 import sun.util.calendar.CalendarDate; |
32 import sun.util.calendar.CalendarSystem; |
33 import sun.util.calendar.CalendarSystem; |
33 import sun.util.calendar.CalendarUtils; |
34 import sun.util.calendar.CalendarUtils; |
34 import sun.util.calendar.Era; |
35 import sun.util.calendar.Era; |
35 import sun.util.calendar.Gregorian; |
36 import sun.util.calendar.Gregorian; |
36 import sun.util.calendar.LocalGregorianCalendar; |
37 import sun.util.calendar.LocalGregorianCalendar; |
37 import sun.util.calendar.ZoneInfo; |
38 import sun.util.calendar.ZoneInfo; |
38 import sun.util.resources.LocaleData; |
|
39 |
39 |
40 /** |
40 /** |
41 * <code>JapaneseImperialCalendar</code> implements a Japanese |
41 * <code>JapaneseImperialCalendar</code> implements a Japanese |
42 * calendar system in which the imperial era-based year numbering is |
42 * calendar system in which the imperial era-based year numbering is |
43 * supported from the Meiji era. The following are the eras supported |
43 * supported from the Meiji era. The following are the eras supported |
939 if (!checkDisplayNameParams(field, style, SHORT, LONG, locale, |
950 if (!checkDisplayNameParams(field, style, SHORT, LONG, locale, |
940 ERA_MASK|YEAR_MASK|MONTH_MASK|DAY_OF_WEEK_MASK|AM_PM_MASK)) { |
951 ERA_MASK|YEAR_MASK|MONTH_MASK|DAY_OF_WEEK_MASK|AM_PM_MASK)) { |
941 return null; |
952 return null; |
942 } |
953 } |
943 |
954 |
|
955 int fieldValue = get(field); |
|
956 |
944 // "GanNen" is supported only in the LONG style. |
957 // "GanNen" is supported only in the LONG style. |
945 if (field == YEAR |
958 if (field == YEAR |
946 && (style == SHORT || get(YEAR) != 1 || get(ERA) == 0)) { |
959 && (getBaseStyle(style) == SHORT || fieldValue != 1 || get(ERA) == 0)) { |
947 return null; |
960 return null; |
948 } |
961 } |
949 |
962 |
950 ResourceBundle rb = LocaleData.getDateFormatData(locale); |
963 String name = CalendarDataUtility.retrieveFieldValueName("japanese", field, fieldValue, style, locale); |
951 String name = null; |
964 // If the ERA value is null, then |
952 String key = getKey(field, style); |
965 // try to get its name or abbreviation from the Era instance. |
953 if (key != null) { |
966 if (name == null && field == ERA && fieldValue < eras.length) { |
954 String[] strings = rb.getStringArray(key); |
967 Era era = eras[fieldValue]; |
955 if (field == YEAR) { |
968 name = (style == SHORT) ? era.getAbbreviation() : era.getName(); |
956 if (strings.length > 0) { |
|
957 name = strings[0]; |
|
958 } |
|
959 } else { |
|
960 int index = get(field); |
|
961 // If the ERA value is out of range for strings, then |
|
962 // try to get its name or abbreviation from the Era instance. |
|
963 if (field == ERA && index >= strings.length && index < eras.length) { |
|
964 Era era = eras[index]; |
|
965 name = (style == SHORT) ? era.getAbbreviation() : era.getName(); |
|
966 } else { |
|
967 if (field == DAY_OF_WEEK) { |
|
968 --index; |
|
969 } |
|
970 name = strings[index]; |
|
971 } |
|
972 } |
|
973 } |
969 } |
974 return name; |
970 return name; |
975 } |
971 } |
976 |
972 |
977 public Map<String,Integer> getDisplayNames(int field, int style, Locale locale) { |
973 public Map<String,Integer> getDisplayNames(int field, int style, Locale locale) { |
978 if (!checkDisplayNameParams(field, style, ALL_STYLES, LONG, locale, |
974 if (!checkDisplayNameParams(field, style, ALL_STYLES, LONG, locale, |
979 ERA_MASK|YEAR_MASK|MONTH_MASK|DAY_OF_WEEK_MASK|AM_PM_MASK)) { |
975 ERA_MASK|YEAR_MASK|MONTH_MASK|DAY_OF_WEEK_MASK|AM_PM_MASK)) { |
980 return null; |
976 return null; |
981 } |
977 } |
982 |
978 Map<String, Integer> names = CalendarDataUtility.retrieveFieldValueNames("japanese", field, style, locale); |
983 if (style == ALL_STYLES) { |
979 // If strings[] has fewer than eras[], get more names from eras[]. |
984 Map<String,Integer> shortNames = getDisplayNamesImpl(field, SHORT, locale); |
980 if (field == ERA) { |
985 if (field == AM_PM) { |
981 int size = names.size(); |
986 return shortNames; |
982 if (style == ALL_STYLES) { |
987 } |
983 size /= 2; // SHORT and LONG |
988 Map<String,Integer> longNames = getDisplayNamesImpl(field, LONG, locale); |
984 } |
989 if (shortNames == null) { |
985 if (size < eras.length) { |
990 return longNames; |
986 int baseStyle = getBaseStyle(style); |
991 } |
987 for (int i = size; i < eras.length; i++) { |
992 if (longNames != null) { |
988 Era era = eras[i]; |
993 shortNames.putAll(longNames); |
989 if (baseStyle == ALL_STYLES || baseStyle == SHORT) { |
994 } |
990 names.put(era.getAbbreviation(), i); |
995 return shortNames; |
991 } |
996 } |
992 if (baseStyle == ALL_STYLES || baseStyle == LONG) { |
997 |
993 names.put(era.getName(), i); |
998 // SHORT or LONG |
994 } |
999 return getDisplayNamesImpl(field, style, locale); |
995 } |
1000 } |
996 } |
1001 |
997 } |
1002 private Map<String,Integer> getDisplayNamesImpl(int field, int style, Locale locale) { |
998 return names; |
1003 ResourceBundle rb = LocaleData.getDateFormatData(locale); |
|
1004 String key = getKey(field, style); |
|
1005 Map<String,Integer> map = new HashMap<>(); |
|
1006 if (key != null) { |
|
1007 String[] strings = rb.getStringArray(key); |
|
1008 if (field == YEAR) { |
|
1009 if (strings.length > 0) { |
|
1010 map.put(strings[0], 1); |
|
1011 } |
|
1012 } else { |
|
1013 int base = (field == DAY_OF_WEEK) ? 1 : 0; |
|
1014 for (int i = 0; i < strings.length; i++) { |
|
1015 map.put(strings[i], base + i); |
|
1016 } |
|
1017 // If strings[] has fewer than eras[], get more names from eras[]. |
|
1018 if (field == ERA && strings.length < eras.length) { |
|
1019 for (int i = strings.length; i < eras.length; i++) { |
|
1020 Era era = eras[i]; |
|
1021 String name = (style == SHORT) ? era.getAbbreviation() : era.getName(); |
|
1022 map.put(name, i); |
|
1023 } |
|
1024 } |
|
1025 } |
|
1026 } |
|
1027 return map.size() > 0 ? map : null; |
|
1028 } |
|
1029 |
|
1030 private String getKey(int field, int style) { |
|
1031 String className = JapaneseImperialCalendar.class.getName(); |
|
1032 StringBuilder key = new StringBuilder(); |
|
1033 switch (field) { |
|
1034 case ERA: |
|
1035 key.append(className); |
|
1036 if (style == SHORT) { |
|
1037 key.append(".short"); |
|
1038 } |
|
1039 key.append(".Eras"); |
|
1040 break; |
|
1041 |
|
1042 case YEAR: |
|
1043 key.append(className).append(".FirstYear"); |
|
1044 break; |
|
1045 |
|
1046 case MONTH: |
|
1047 key.append(style == SHORT ? "MonthAbbreviations" : "MonthNames"); |
|
1048 break; |
|
1049 |
|
1050 case DAY_OF_WEEK: |
|
1051 key.append(style == SHORT ? "DayAbbreviations" : "DayNames"); |
|
1052 break; |
|
1053 |
|
1054 case AM_PM: |
|
1055 key.append("AmPmMarkers"); |
|
1056 break; |
|
1057 } |
|
1058 return key.length() > 0 ? key.toString() : null; |
|
1059 } |
999 } |
1060 |
1000 |
1061 /** |
1001 /** |
1062 * Returns the minimum value for the given calendar field of this |
1002 * Returns the minimum value for the given calendar field of this |
1063 * <code>Calendar</code> instance. The minimum value is |
1003 * <code>Calendar</code> instance. The minimum value is |