Path: blob/master/src/java.base/share/classes/sun/util/locale/provider/CalendarNameProviderImpl.java
41161 views
/*1* Copyright (c) 2012, 2021, 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 {44protected final LocaleProviderAdapter.Type type;45protected 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);6768// If standalone names are requested and no "standalone." resources are found,69// try the default ones instead.70if (strings == null && key.contains("standalone.")) {71key = key.replaceFirst("standalone.", "");72strings = javatime ? lr.getJavaTimeNames(key) : lr.getCalendarNames(key);73}7475if (strings != null && strings.length > 0) {76if (field == DAY_OF_WEEK || field == YEAR) {77--value;78}79if (value < 0) {80return null;81} else if (value >= strings.length) {82if (field == ERA && "japanese".equals(calendarType)) {83Era[] jeras = CalendarSystem.forName("japanese").getEras();84if (value <= jeras.length) {85// Localized era name could not be retrieved from this provider.86// This can occur either for Reiwa or SupEra.87//88// If it's CLDR provider, try COMPAT first, which is guaranteed to have89// the name for Reiwa.90if (type == LocaleProviderAdapter.Type.CLDR) {91lr = LocaleProviderAdapter.forJRE().getLocaleResources(locale);92key = getResourceKeyFor(LocaleProviderAdapter.Type.JRE,93calendarType, field, style, javatime);94strings =95javatime ? lr.getJavaTimeNames(key) : lr.getCalendarNames(key);96}97if (strings == null || value >= strings.length) {98// Get the default name for SupEra99Era supEra = jeras[value - 1]; // 0-based index100if (javatime) {101return getBaseStyle(style) == NARROW_FORMAT ?102supEra.getAbbreviation() :103supEra.getName();104} else {105return (style & LONG) != 0 ?106supEra.getName() :107supEra.getAbbreviation();108}109}110} else {111return null;112}113} else {114return null;115}116}117name = strings[value];118// If name is empty in standalone, try its `format' style.119if (name.isEmpty()120&& (style == SHORT_STANDALONE || style == LONG_STANDALONE121|| style == NARROW_STANDALONE)) {122name = getDisplayName(calendarType, field, value,123getBaseStyle(style),124locale);125}126}127}128return name;129}130131private static final int[] REST_OF_STYLES = {132SHORT_STANDALONE, LONG_FORMAT, LONG_STANDALONE,133NARROW_FORMAT, NARROW_STANDALONE134};135136@Override137public Map<String, Integer> getDisplayNames(String calendarType, int field, int style, Locale locale) {138Map<String, Integer> names;139if (style == ALL_STYLES) {140names = getDisplayNamesImpl(calendarType, field, SHORT_FORMAT, locale, false);141for (int st : REST_OF_STYLES) {142names.putAll(getDisplayNamesImpl(calendarType, field, st, locale, false));143}144} else {145// specific style146names = getDisplayNamesImpl(calendarType, field, style, locale, false);147}148return names.isEmpty() ? null : names;149}150151// NOTE: This method should be used ONLY BY JSR 310 classes.152public Map<String, Integer> getJavaTimeDisplayNames(String calendarType, int field, int style, Locale locale) {153Map<String, Integer> names;154names = getDisplayNamesImpl(calendarType, field, style, locale, true);155return names.isEmpty() ? null : names;156}157158private Map<String, Integer> getDisplayNamesImpl(String calendarType, int field,159int style, Locale locale, boolean javatime) {160String key = getResourceKey(calendarType, field, style, javatime);161Map<String, Integer> map = new TreeMap<>(LengthBasedComparator.INSTANCE);162if (key != null) {163LocaleResources lr = LocaleProviderAdapter.forType(type).getLocaleResources(locale);164String[] strings = javatime ? lr.getJavaTimeNames(key) : lr.getCalendarNames(key);165166// If standalone names are requested and no "standalone." resources are found,167// try the default ones instead.168if (strings == null && key.contains("standalone.")) {169key = key.replaceFirst("standalone.", "");170strings = javatime ? lr.getJavaTimeNames(key) : lr.getCalendarNames(key);171}172173if (strings != null) {174if (!hasDuplicates(strings) || field == AM_PM) {175if (field == YEAR) {176if (strings.length > 0) {177map.put(strings[0], 1);178}179} else {180int base = (field == DAY_OF_WEEK) ? 1 : 0;181// Duplicates can happen with AM_PM field. In such a case,182// am/pm (index 0 and 1) have precedence over day183// periods.184for (int i = strings.length - 1; i >= 0; i--) {185String name = strings[i];186// Ignore any empty string (some standalone month names187// or flexible day periods are not defined)188if (name.isEmpty()) {189continue;190}191if (field == AM_PM && !javatime && i > PM) {192// Unlike in the case of java.time.format.DateTimeFormatter(Builder),193// when dealing with java.util.Calendar, don't set AM_PM field value194// to anything that isn't either AM or PM (this can happen when195// day periods are involved)196continue;197} else {198map.put(name, base + i);199}200}201}202}203}204}205return map;206}207208private static int getBaseStyle(int style) {209return style & ~(SHORT_STANDALONE - SHORT_FORMAT);210}211212/**213* Comparator implementation for TreeMap which iterates keys from longest214* to shortest.215*/216private static class LengthBasedComparator implements Comparator<String> {217private static final LengthBasedComparator INSTANCE = new LengthBasedComparator();218219private LengthBasedComparator() {220}221222@Override223public int compare(String o1, String o2) {224int n = o2.length() - o1.length();225return (n == 0) ? o1.compareTo(o2) : n;226}227}228229@Override230public Locale[] getAvailableLocales() {231return LocaleProviderAdapter.toLocaleArray(langtags);232}233234@Override235public boolean isSupportedLocale(Locale locale) {236if (Locale.ROOT.equals(locale)) {237return true;238}239String calendarType = null;240if (locale.hasExtensions()) {241calendarType = locale.getUnicodeLocaleType("ca");242locale = locale.stripExtensions();243}244245if (calendarType != null) {246switch (calendarType) {247case "buddhist":248case "japanese":249case "gregory":250case "islamic":251case "roc":252break;253default:254// Unknown calendar type255return false;256}257}258if (langtags.contains(locale.toLanguageTag())) {259return true;260}261String oldname = locale.toString().replace('_', '-');262return langtags.contains(oldname);263}264265@Override266public Set<String> getAvailableLanguageTags() {267return langtags;268}269270// Check if each string is unique, except null or empty strings,271// as these strings are used for keys in the name-to-value map.272private boolean hasDuplicates(String[] strings) {273int len = strings.length;274for (int i = 0; i < len - 1; i++) {275String a = strings[i];276if (a != null && !a.isEmpty()) {277for (int j = i + 1; j < len; j++) {278if (a.equals(strings[j])) {279return true;280}281}282}283}284return false;285}286287private String getResourceKey(String type, int field, int style, boolean javatime) {288return getResourceKeyFor(this.type, type, field, style, javatime);289}290291private static String getResourceKeyFor(LocaleProviderAdapter.Type adapterType,292String type, int field, int style, boolean javatime) {293int baseStyle = getBaseStyle(style);294boolean isStandalone = (style != baseStyle);295296if ("gregory".equals(type)) {297type = null;298}299boolean isNarrow = (baseStyle == NARROW_FORMAT);300StringBuilder key = new StringBuilder();301// If javatime is true, use prefix "java.time.".302if (javatime) {303key.append("java.time.");304}305switch (field) {306case ERA:307if (type != null) {308key.append(type).append('.');309}310if (isNarrow) {311key.append("narrow.");312} else {313// JRE and CLDR use different resource key conventions314// due to historical reasons. (JRE DateFormatSymbols.getEras returns315// abbreviations while other getShort*() return abbreviations.)316if (adapterType == LocaleProviderAdapter.Type.JRE) {317if (javatime) {318if (baseStyle == LONG) {319key.append("long.");320}321}322if (baseStyle == SHORT) {323key.append("short.");324}325} else { // this.type == LocaleProviderAdapter.Type.CLDR326if (baseStyle == LONG) {327key.append("long.");328}329}330}331key.append("Eras");332break;333334case YEAR:335if (!isNarrow) {336key.append(type).append(".FirstYear");337}338break;339340case MONTH:341if ("islamic".equals(type)) {342key.append(type).append('.');343}344if (isStandalone) {345key.append("standalone.");346}347key.append("Month").append(toStyleName(baseStyle));348break;349350case DAY_OF_WEEK:351// support standalone day names352if (isStandalone) {353key.append("standalone.");354}355key.append("Day").append(toStyleName(baseStyle));356break;357358case AM_PM:359if (isNarrow) {360key.append("narrow.");361}362key.append("AmPmMarkers");363break;364}365return key.length() > 0 ? key.toString() : null;366}367368private static String toStyleName(int baseStyle) {369switch (baseStyle) {370case SHORT:371return "Abbreviations";372case NARROW_FORMAT:373return "Narrows";374}375return "Names";376}377}378379380