< prev index next >

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

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -33,12 +33,12 @@
 import java.util.spi.CalendarNameProvider;
 import sun.util.calendar.CalendarSystem;
 import sun.util.calendar.Era;
 
 /**
- * Concrete implementation of the  {@link java.util.spi.CalendarDataProvider
- * CalendarDataProvider} class for the JRE LocaleProviderAdapter.
+ * Concrete implementation of the  {@link java.util.spi.CalendarNameProvider
+ * CalendarNameProvider} class for the JRE LocaleProviderAdapter.
  *
  * @author Masayoshi Okutsu
  * @author Naoto Sato
  */
 public class CalendarNameProviderImpl extends CalendarNameProvider implements AvailableLanguageTags {

@@ -67,27 +67,48 @@
             String[] strings = javatime ? lr.getJavaTimeNames(key) : lr.getCalendarNames(key);
             if (strings != null && strings.length > 0) {
                 if (field == DAY_OF_WEEK || field == YEAR) {
                     --value;
                 }
-                if (value < 0 || value > strings.length) {
+                if (value < 0) {
                     return null;
-                } else if (value == strings.length) {
+                } else if (value >= strings.length) {
                     if (field == ERA && "japanese".equals(calendarType)) {
-                        // get the supplemental era, if any, specified through
-                        // the property "jdk.calendar.japanese.supplemental.era"
-                        // which is always the last element.
                         Era[] jeras = CalendarSystem.forName("japanese").getEras();
-                        if (jeras.length == value) {
+                        if (value <= jeras.length) {
+                            // Localized era name could not be retrieved from this provider.
+                            // This can occur either for NewEra or SupEra.
+                            //
+                            // If it's CLDR provider, try COMPAT first, which is guaranteed to have
+                            // the name for NewEra.
+                            if (type == LocaleProviderAdapter.Type.CLDR) {
+                                lr = LocaleProviderAdapter.forJRE().getLocaleResources(locale);
+                                key = getResourceKeyFor(LocaleProviderAdapter.Type.JRE,
+                                                calendarType, field, style, javatime);
+                                strings =
+                                    javatime ? lr.getJavaTimeNames(key) : lr.getCalendarNames(key);
+                            }
+                            if (strings == null || value >= strings.length) {
+                                // Get the default name for SupEra
                             Era supEra = jeras[value - 1]; // 0-based index
-                            return style == LONG ?
+                                if (javatime) {
+                                    return getBaseStyle(style) == NARROW_FORMAT ?
+                                        supEra.getAbbreviation() :
+                                        supEra.getName();
+                                } else {
+                                    return (style & LONG) != 0 ?
                                 supEra.getName() :
                                 supEra.getAbbreviation();
                         }
                     }
+                        } else {
+                    return null;
+                }
+                    } else {
                     return null;
                 }
+                }
                 name = strings[value];
                 // If name is empty in standalone, try its `format' style.
                 if (name.length() == 0
                         && (style == SHORT_STANDALONE || style == LONG_STANDALONE
                             || style == NARROW_STANDALONE)) {

@@ -156,11 +177,11 @@
             }
         }
         return map;
     }
 
-    private int getBaseStyle(int style) {
+    private static int getBaseStyle(int style) {
         return style & ~(SHORT_STANDALONE - SHORT_FORMAT);
     }
 
     /**
      * Comparator implementation for TreeMap which iterates keys from longest

@@ -237,10 +258,15 @@
         }
         return false;
     }
 
     private String getResourceKey(String type, int field, int style, boolean javatime) {
+        return getResourceKeyFor(this.type, type, field, style, javatime);
+    }
+
+    private static String getResourceKeyFor(LocaleProviderAdapter.Type adapterType,
+                            String type, int field, int style, boolean javatime) {
         int baseStyle = getBaseStyle(style);
         boolean isStandalone = (style != baseStyle);
 
         if ("gregory".equals(type)) {
             type = null;

@@ -260,11 +286,11 @@
                 key.append("narrow.");
             } else {
                 // JRE and CLDR use different resource key conventions
                 // due to historical reasons. (JRE DateFormatSymbols.getEras returns
                 // abbreviations while other getShort*() return abbreviations.)
-                if (this.type == LocaleProviderAdapter.Type.JRE) {
+                if (adapterType == LocaleProviderAdapter.Type.JRE) {
                     if (javatime) {
                         if (baseStyle == LONG) {
                             key.append("long.");
                         }
                     }

@@ -312,11 +338,11 @@
             break;
         }
         return key.length() > 0 ? key.toString() : null;
     }
 
-    private String toStyleName(int baseStyle) {
+    private static String toStyleName(int baseStyle) {
         switch (baseStyle) {
         case SHORT:
             return "Abbreviations";
         case NARROW_FORMAT:
             return "Narrows";
< prev index next >