jdk/src/share/classes/java/util/JapaneseImperialCalendar.java
changeset 13583 dc0017b1a452
parent 11130 c7093e306a34
child 14014 da3648e13e67
equal deleted inserted replaced
13582:16f0af616ea0 13583:dc0017b1a452
     1 /*
     1 /*
     2  * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    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
   297      */
   297      */
   298     JapaneseImperialCalendar(TimeZone zone, Locale aLocale) {
   298     JapaneseImperialCalendar(TimeZone zone, Locale aLocale) {
   299         super(zone, aLocale);
   299         super(zone, aLocale);
   300         jdate = jcal.newCalendarDate(zone);
   300         jdate = jcal.newCalendarDate(zone);
   301         setTimeInMillis(System.currentTimeMillis());
   301         setTimeInMillis(System.currentTimeMillis());
       
   302     }
       
   303 
       
   304     /**
       
   305      * Returns {@code "japanese"} as the calendar type of this {@code
       
   306      * JapaneseImperialCalendar}.
       
   307      *
       
   308      * @return {@code "japanese"}
       
   309      */
       
   310     @Override
       
   311     public String getCalendarType() {
       
   312         return "japanese";
   302     }
   313     }
   303 
   314 
   304     /**
   315     /**
   305      * Compares this <code>JapaneseImperialCalendar</code> to the specified
   316      * Compares this <code>JapaneseImperialCalendar</code> to the specified
   306      * <code>Object</code>. The result is <code>true</code> if and
   317      * <code>Object</code>. The result is <code>true</code> if and
   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