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

Print this page
rev 6495 : imported patch 8007038


   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package sun.util.locale.provider;
  26 
  27 import static java.util.Calendar.*;

  28 import java.util.Comparator;
  29 import java.util.Locale;
  30 import java.util.Map;
  31 import java.util.Set;
  32 import java.util.TreeMap;
  33 import java.util.spi.CalendarNameProvider;
  34 
  35 /**
  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         String name = null;
  54         String key = getResourceKey(calendarType, field, style);
  55         if (key != null) {
  56             String[] strings = LocaleProviderAdapter.forType(type).getLocaleResources(locale).getCalendarNames(key);
  57             if (strings != null && strings.length > 0) {
  58                 if (field == DAY_OF_WEEK || field == YEAR) {
  59                     --value;
  60                 }
  61                 name = strings[value];
  62                 // If name is empty in standalone, try its `format' style.
  63                 if (name.length() == 0
  64                         && (style == SHORT_STANDALONE || style == LONG_STANDALONE
  65                             || style == NARROW_STANDALONE)) {
  66                     name = getDisplayName(calendarType, field, value,
  67                                           getBaseStyle(style),
  68                                           locale);
  69                 }
  70             }
  71         }
  72         return name;


 260 
 261         case AM_PM:
 262             if (isNarrow) {
 263                 key.append("narrow.");
 264             }
 265             key.append("AmPmMarkers");
 266             break;
 267         }
 268         return key.length() > 0 ? key.toString() : null;
 269     }
 270 
 271     private String toStyleName(int baseStyle) {
 272         switch (baseStyle) {
 273         case SHORT:
 274             return "Abbreviations";
 275         case NARROW_FORMAT:
 276             return "Narrows";
 277         }
 278         return "Names";
 279     }


























 280 }


   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package sun.util.locale.provider;
  26 
  27 import static java.util.Calendar.*;
  28 import static java.util.GregorianCalendar.*;
  29 import java.util.Comparator;
  30 import java.util.Locale;
  31 import java.util.Map;
  32 import java.util.Set;
  33 import java.util.TreeMap;
  34 import java.util.spi.CalendarNameProvider;
  35 
  36 /**
  37  * Concrete implementation of the  {@link java.util.spi.CalendarDataProvider
  38  * CalendarDataProvider} class for the JRE LocaleProviderAdapter.
  39  *
  40  * @author Masayoshi Okutsu
  41  * @author Naoto Sato
  42  */
  43 public class CalendarNameProviderImpl extends CalendarNameProvider implements AvailableLanguageTags {
  44     private final LocaleProviderAdapter.Type type;
  45     private final Set<String> langtags;
  46 
  47     public CalendarNameProviderImpl(LocaleProviderAdapter.Type type, Set<String> langtags) {
  48         this.type = type;
  49         this.langtags = langtags;
  50     }
  51 
  52     @Override
  53     public String getDisplayName(String calendarType, int field, int value, int style, Locale locale) {
  54         if (!valueRangeCheck(calendarType, field, value, style)) {
  55             return null;
  56         }
  57         String name = null;
  58         String key = getResourceKey(calendarType, field, style);
  59         if (key != null) {
  60             String[] strings = LocaleProviderAdapter.forType(type).getLocaleResources(locale).getCalendarNames(key);
  61             if (strings != null && strings.length > 0) {
  62                 if (field == DAY_OF_WEEK || field == YEAR) {
  63                     --value;
  64                 }
  65                 name = strings[value];
  66                 // If name is empty in standalone, try its `format' style.
  67                 if (name.length() == 0
  68                         && (style == SHORT_STANDALONE || style == LONG_STANDALONE
  69                             || style == NARROW_STANDALONE)) {
  70                     name = getDisplayName(calendarType, field, value,
  71                                           getBaseStyle(style),
  72                                           locale);
  73                 }
  74             }
  75         }
  76         return name;


 264 
 265         case AM_PM:
 266             if (isNarrow) {
 267                 key.append("narrow.");
 268             }
 269             key.append("AmPmMarkers");
 270             break;
 271         }
 272         return key.length() > 0 ? key.toString() : null;
 273     }
 274 
 275     private String toStyleName(int baseStyle) {
 276         switch (baseStyle) {
 277         case SHORT:
 278             return "Abbreviations";
 279         case NARROW_FORMAT:
 280             return "Narrows";
 281         }
 282         return "Names";
 283     }
 284 
 285     private boolean valueRangeCheck(String calType, int field, int value, int style) {
 286         switch (field) {
 287         case MONTH:
 288             return value >= JANUARY && value <= UNDECIMBER;
 289         case DAY_OF_WEEK:
 290             return value >= SUNDAY && value <= SATURDAY;
 291         case AM_PM:
 292             return value == AM || value == PM;
 293         case ERA:
 294             switch (calType) {
 295             case "gregory":
 296                 return value == BC || value == AD;
 297             case "buddhist": // BC or B.E.
 298             case "roc": // Before R.O.C. or R.O.C.
 299             case "islamic": // Before AH or AH
 300                 return value == 0 || value == 1;
 301             case "japanese":
 302                 return value >= 0 && value <= 4; // Seireki, Meiji, Taisho, Showa, or Heisei
 303             default:
 304                 return false; // unknown calendar type
 305             }
 306         default:
 307             return true; // no value range limitation
 308         }
 309     }
 310 }