Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/util/locale/provider/CalendarNameProviderImpl.java
38918 views
/*1* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package sun.util.locale.provider;2526import static java.util.Calendar.*;27import java.util.Comparator;28import java.util.Locale;29import java.util.Map;30import java.util.Set;31import java.util.TreeMap;32import java.util.spi.CalendarNameProvider;33import sun.util.calendar.CalendarSystem;34import sun.util.calendar.Era;3536/**37* Concrete implementation of the {@link java.util.spi.CalendarNameProvider38* CalendarNameProvider} class for the JRE LocaleProviderAdapter.39*40* @author Masayoshi Okutsu41* @author Naoto Sato42*/43public class CalendarNameProviderImpl extends CalendarNameProvider implements AvailableLanguageTags {44private final LocaleProviderAdapter.Type type;45private final Set<String> langtags;4647public CalendarNameProviderImpl(LocaleProviderAdapter.Type type, Set<String> langtags) {48this.type = type;49this.langtags = langtags;50}5152@Override53public String getDisplayName(String calendarType, int field, int value, int style, Locale locale) {54return getDisplayNameImpl(calendarType, field, value, style, locale, false);55}5657public String getJavaTimeDisplayName(String calendarType, int field, int value, int style, Locale locale) {58return getDisplayNameImpl(calendarType, field, value, style, locale, true);59}6061public String getDisplayNameImpl(String calendarType, int field, int value, int style, Locale locale, boolean javatime) {62String name = null;63String key = getResourceKey(calendarType, field, style, javatime);64if (key != null) {65LocaleResources lr = LocaleProviderAdapter.forType(type).getLocaleResources(locale);66String[] strings = javatime ? lr.getJavaTimeNames(key) : lr.getCalendarNames(key);67if (strings != null && strings.length > 0) {68if (field == DAY_OF_WEEK || field == YEAR) {69--value;70}71if (value < 0) {72return null;73} else if (value >= strings.length) {74if (field == ERA && "japanese".equals(calendarType)) {75Era[] jeras = CalendarSystem.forName("japanese").getEras();76if (value <= jeras.length) {77// Localized era name could not be retrieved from this provider.78// This can occur either for Reiwa or SupEra.79//80// If it's CLDR provider, try COMPAT first, which is guaranteed to have81// the name for Reiwa.82if (type == LocaleProviderAdapter.Type.CLDR) {83lr = LocaleProviderAdapter.forJRE().getLocaleResources(locale);84key = getResourceKeyFor(LocaleProviderAdapter.Type.JRE,85calendarType, field, style, javatime);86strings =87javatime ? lr.getJavaTimeNames(key) : lr.getCalendarNames(key);88}89if (strings == null || value >= strings.length) {90// Get the default name for SupEra91Era supEra = jeras[value - 1]; // 0-based index92if (javatime) {93return getBaseStyle(style) == NARROW_FORMAT ?94supEra.getAbbreviation() :95supEra.getName();96} else {97return (style & LONG) != 0 ?98supEra.getName() :99supEra.getAbbreviation();100}101}102} else {103return null;104}105} else {106return null;107}108}109name = strings[value];110// If name is empty in standalone, try its `format' style.111if (name.length() == 0112&& (style == SHORT_STANDALONE || style == LONG_STANDALONE113|| style == NARROW_STANDALONE)) {114name = getDisplayName(calendarType, field, value,115getBaseStyle(style),116locale);117}118}119}120return name;121}122123private static int[] REST_OF_STYLES = {124SHORT_STANDALONE, LONG_FORMAT, LONG_STANDALONE,125NARROW_FORMAT, NARROW_STANDALONE126};127128@Override129public Map<String, Integer> getDisplayNames(String calendarType, int field, int style, Locale locale) {130Map<String, Integer> names;131if (style == ALL_STYLES) {132names = getDisplayNamesImpl(calendarType, field, SHORT_FORMAT, locale, false);133for (int st : REST_OF_STYLES) {134names.putAll(getDisplayNamesImpl(calendarType, field, st, locale, false));135}136} else {137// specific style138names = getDisplayNamesImpl(calendarType, field, style, locale, false);139}140return names.isEmpty() ? null : names;141}142143// NOTE: This method should be used ONLY BY JSR 310 classes.144public Map<String, Integer> getJavaTimeDisplayNames(String calendarType, int field, int style, Locale locale) {145Map<String, Integer> names;146names = getDisplayNamesImpl(calendarType, field, style, locale, true);147return names.isEmpty() ? null : names;148}149150private Map<String, Integer> getDisplayNamesImpl(String calendarType, int field,151int style, Locale locale, boolean javatime) {152String key = getResourceKey(calendarType, field, style, javatime);153Map<String, Integer> map = new TreeMap<>(LengthBasedComparator.INSTANCE);154if (key != null) {155LocaleResources lr = LocaleProviderAdapter.forType(type).getLocaleResources(locale);156String[] strings = javatime ? lr.getJavaTimeNames(key) : lr.getCalendarNames(key);157if (strings != null) {158if (!hasDuplicates(strings)) {159if (field == YEAR) {160if (strings.length > 0) {161map.put(strings[0], 1);162}163} else {164int base = (field == DAY_OF_WEEK) ? 1 : 0;165for (int i = 0; i < strings.length; i++) {166String name = strings[i];167// Ignore any empty string (some standalone month names168// are not defined)169if (name.length() == 0) {170continue;171}172map.put(name, base + i);173}174}175}176}177}178return map;179}180181private static int getBaseStyle(int style) {182return style & ~(SHORT_STANDALONE - SHORT_FORMAT);183}184185/**186* Comparator implementation for TreeMap which iterates keys from longest187* to shortest.188*/189private static class LengthBasedComparator implements Comparator<String> {190private static final LengthBasedComparator INSTANCE = new LengthBasedComparator();191192private LengthBasedComparator() {193}194195@Override196public int compare(String o1, String o2) {197int n = o2.length() - o1.length();198return (n == 0) ? o1.compareTo(o2) : n;199}200}201202@Override203public Locale[] getAvailableLocales() {204return LocaleProviderAdapter.toLocaleArray(langtags);205}206207@Override208public boolean isSupportedLocale(Locale locale) {209if (Locale.ROOT.equals(locale)) {210return true;211}212String calendarType = null;213if (locale.hasExtensions()) {214calendarType = locale.getUnicodeLocaleType("ca");215locale = locale.stripExtensions();216}217218if (calendarType != null) {219switch (calendarType) {220case "buddhist":221case "japanese":222case "gregory":223case "islamic":224case "roc":225break;226default:227// Unknown calendar type228return false;229}230}231if (langtags.contains(locale.toLanguageTag())) {232return true;233}234if (type == LocaleProviderAdapter.Type.JRE) {235String oldname = locale.toString().replace('_', '-');236return langtags.contains(oldname);237}238return false;239}240241@Override242public Set<String> getAvailableLanguageTags() {243return langtags;244}245246private boolean hasDuplicates(String[] strings) {247int len = strings.length;248for (int i = 0; i < len - 1; i++) {249String a = strings[i];250if (a != null) {251for (int j = i + 1; j < len; j++) {252if (a.equals(strings[j])) {253return true;254}255}256}257}258return false;259}260261private String getResourceKey(String type, int field, int style, boolean javatime) {262return getResourceKeyFor(this.type, type, field, style, javatime);263}264265private static String getResourceKeyFor(LocaleProviderAdapter.Type adapterType,266String type, int field, int style, boolean javatime) {267int baseStyle = getBaseStyle(style);268boolean isStandalone = (style != baseStyle);269270if ("gregory".equals(type)) {271type = null;272}273boolean isNarrow = (baseStyle == NARROW_FORMAT);274StringBuilder key = new StringBuilder();275// If javatime is true, use prefix "java.time.".276if (javatime) {277key.append("java.time.");278}279switch (field) {280case ERA:281if (type != null) {282key.append(type).append('.');283}284if (isNarrow) {285key.append("narrow.");286} else {287// JRE and CLDR use different resource key conventions288// due to historical reasons. (JRE DateFormatSymbols.getEras returns289// abbreviations while other getShort*() return abbreviations.)290if (adapterType == LocaleProviderAdapter.Type.JRE) {291if (javatime) {292if (baseStyle == LONG) {293key.append("long.");294}295}296if (baseStyle == SHORT) {297key.append("short.");298}299} else { // this.type == LocaleProviderAdapter.Type.CLDR300if (baseStyle == LONG) {301key.append("long.");302}303}304}305key.append("Eras");306break;307308case YEAR:309if (!isNarrow) {310key.append(type).append(".FirstYear");311}312break;313314case MONTH:315if ("islamic".equals(type)) {316key.append(type).append('.');317}318if (isStandalone) {319key.append("standalone.");320}321key.append("Month").append(toStyleName(baseStyle));322break;323324case DAY_OF_WEEK:325// support standalone narrow day names326if (isStandalone && isNarrow) {327key.append("standalone.");328}329key.append("Day").append(toStyleName(baseStyle));330break;331332case AM_PM:333if (isNarrow) {334key.append("narrow.");335}336key.append("AmPmMarkers");337break;338}339return key.length() > 0 ? key.toString() : null;340}341342private static String toStyleName(int baseStyle) {343switch (baseStyle) {344case SHORT:345return "Abbreviations";346case NARROW_FORMAT:347return "Narrows";348}349return "Names";350}351}352353354