Path: blob/master/src/java.base/share/classes/java/text/DateFormatSymbols.java
41152 views
/*1* Copyright (c) 1996, 2020, 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*/2425/*26* (C) Copyright Taligent, Inc. 1996 - All Rights Reserved27* (C) Copyright IBM Corp. 1996 - All Rights Reserved28*29* The original version of this source code and documentation is copyrighted30* and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These31* materials are provided under terms of a License Agreement between Taligent32* and Sun. This technology is protected by multiple US and International33* patents. This notice and attribution to Taligent may not be removed.34* Taligent is a registered trademark of Taligent, Inc.35*36*/3738package java.text;3940import java.io.IOException;41import java.io.ObjectOutputStream;42import java.io.Serializable;43import java.lang.ref.SoftReference;44import java.text.spi.DateFormatSymbolsProvider;45import java.util.Arrays;46import java.util.Locale;47import java.util.Objects;48import java.util.ResourceBundle;49import java.util.concurrent.ConcurrentHashMap;50import java.util.concurrent.ConcurrentMap;51import sun.util.locale.provider.CalendarDataUtility;52import sun.util.locale.provider.LocaleProviderAdapter;53import sun.util.locale.provider.LocaleServiceProviderPool;54import sun.util.locale.provider.ResourceBundleBasedAdapter;55import sun.util.locale.provider.TimeZoneNameUtility;5657/**58* {@code DateFormatSymbols} is a public class for encapsulating59* localizable date-time formatting data, such as the names of the60* months, the names of the days of the week, and the time zone data.61* {@code SimpleDateFormat} uses62* {@code DateFormatSymbols} to encapsulate this information.63*64* <p>65* Typically you shouldn't use {@code DateFormatSymbols} directly.66* Rather, you are encouraged to create a date-time formatter with the67* {@code DateFormat} class's factory methods: {@code getTimeInstance},68* {@code getDateInstance}, or {@code getDateTimeInstance}.69* These methods automatically create a {@code DateFormatSymbols} for70* the formatter so that you don't have to. After the71* formatter is created, you may modify its format pattern using the72* {@code setPattern} method. For more information about73* creating formatters using {@code DateFormat}'s factory methods,74* see {@link DateFormat}.75*76* <p>77* If you decide to create a date-time formatter with a specific78* format pattern for a specific locale, you can do so with:79* <blockquote>80* <pre>81* new SimpleDateFormat(aPattern, DateFormatSymbols.getInstance(aLocale)).82* </pre>83* </blockquote>84*85* <p>If the locale contains "rg" (region override)86* <a href="../util/Locale.html#def_locale_extension">Unicode extension</a>,87* the symbols are overridden for the designated region.88*89* <p>90* {@code DateFormatSymbols} objects are cloneable. When you obtain91* a {@code DateFormatSymbols} object, feel free to modify the92* date-time formatting data. For instance, you can replace the localized93* date-time format pattern characters with the ones that you feel easy94* to remember. Or you can change the representative cities95* to your favorite ones.96*97* <p>98* New {@code DateFormatSymbols} subclasses may be added to support99* {@code SimpleDateFormat} for date-time formatting for additional locales.100*101* @see DateFormat102* @see SimpleDateFormat103* @see java.util.SimpleTimeZone104* @author Chen-Lieh Huang105* @since 1.1106*/107public class DateFormatSymbols implements Serializable, Cloneable {108109/**110* Construct a DateFormatSymbols object by loading format data from111* resources for the default {@link java.util.Locale.Category#FORMAT FORMAT}112* locale. This constructor can only113* construct instances for the locales supported by the Java114* runtime environment, not for those supported by installed115* {@link java.text.spi.DateFormatSymbolsProvider DateFormatSymbolsProvider}116* implementations. For full locale coverage, use the117* {@link #getInstance(Locale) getInstance} method.118* <p>This is equivalent to calling119* {@link #DateFormatSymbols(Locale)120* DateFormatSymbols(Locale.getDefault(Locale.Category.FORMAT))}.121* @see #getInstance()122* @see java.util.Locale#getDefault(java.util.Locale.Category)123* @see java.util.Locale.Category#FORMAT124* @throws java.util.MissingResourceException125* if the resources for the default locale cannot be126* found or cannot be loaded.127*/128public DateFormatSymbols()129{130initializeData(Locale.getDefault(Locale.Category.FORMAT));131}132133/**134* Construct a DateFormatSymbols object by loading format data from135* resources for the given locale. This constructor can only136* construct instances for the locales supported by the Java137* runtime environment, not for those supported by installed138* {@link java.text.spi.DateFormatSymbolsProvider DateFormatSymbolsProvider}139* implementations. For full locale coverage, use the140* {@link #getInstance(Locale) getInstance} method.141*142* @param locale the desired locale143* @see #getInstance(Locale)144* @throws java.util.MissingResourceException145* if the resources for the specified locale cannot be146* found or cannot be loaded.147*/148public DateFormatSymbols(Locale locale)149{150initializeData(locale);151}152153/**154* Constructs an uninitialized DateFormatSymbols.155*/156private DateFormatSymbols(boolean flag) {157}158159/**160* Era strings. For example: "AD" and "BC". An array of 2 strings,161* indexed by {@code Calendar.BC} and {@code Calendar.AD}.162* @serial163*/164String eras[] = null;165166/**167* Month strings. For example: "January", "February", etc. An array168* of 13 strings (some calendars have 13 months), indexed by169* {@code Calendar.JANUARY}, {@code Calendar.FEBRUARY}, etc.170* @serial171*/172String months[] = null;173174/**175* Short month strings. For example: "Jan", "Feb", etc. An array of176* 13 strings (some calendars have 13 months), indexed by177* {@code Calendar.JANUARY}, {@code Calendar.FEBRUARY}, etc.178* @serial179*/180String shortMonths[] = null;181182/**183* Weekday strings. For example: "Sunday", "Monday", etc. An array184* of 8 strings, indexed by {@code Calendar.SUNDAY},185* {@code Calendar.MONDAY}, etc.186* The element {@code weekdays[0]} is ignored.187* @serial188*/189String weekdays[] = null;190191/**192* Short weekday strings. For example: "Sun", "Mon", etc. An array193* of 8 strings, indexed by {@code Calendar.SUNDAY},194* {@code Calendar.MONDAY}, etc.195* The element {@code shortWeekdays[0]} is ignored.196* @serial197*/198String shortWeekdays[] = null;199200/**201* AM and PM strings. For example: "AM" and "PM". An array of202* 2 strings, indexed by {@code Calendar.AM} and203* {@code Calendar.PM}.204* @serial205*/206String ampms[] = null;207208/**209* Localized names of time zones in this locale. This is a210* two-dimensional array of strings of size <em>n</em> by <em>m</em>,211* where <em>m</em> is at least 5. Each of the <em>n</em> rows is an212* entry containing the localized names for a single {@code TimeZone}.213* Each such row contains (with {@code i} ranging from214* 0..<em>n</em>-1):215* <ul>216* <li>{@code zoneStrings[i][0]} - time zone ID</li>217* <li>{@code zoneStrings[i][1]} - long name of zone in standard218* time</li>219* <li>{@code zoneStrings[i][2]} - short name of zone in220* standard time</li>221* <li>{@code zoneStrings[i][3]} - long name of zone in daylight222* saving time</li>223* <li>{@code zoneStrings[i][4]} - short name of zone in daylight224* saving time</li>225* </ul>226* The zone ID is <em>not</em> localized; it's one of the valid IDs of227* the {@link java.util.TimeZone TimeZone} class that are not228* <a href="../util/TimeZone.html#CustomID">custom IDs</a>.229* All other entries are localized names.230* @see java.util.TimeZone231* @serial232*/233String zoneStrings[][] = null;234235/**236* Indicates that zoneStrings is set externally with setZoneStrings() method.237*/238transient boolean isZoneStringsSet = false;239240/**241* Unlocalized date-time pattern characters. For example: 'y', 'd', etc.242* All locales use the same these unlocalized pattern characters.243*/244static final String patternChars = "GyMdkHmsSEDFwWahKzZYuXL";245246static final int PATTERN_ERA = 0; // G247static final int PATTERN_YEAR = 1; // y248static final int PATTERN_MONTH = 2; // M249static final int PATTERN_DAY_OF_MONTH = 3; // d250static final int PATTERN_HOUR_OF_DAY1 = 4; // k251static final int PATTERN_HOUR_OF_DAY0 = 5; // H252static final int PATTERN_MINUTE = 6; // m253static final int PATTERN_SECOND = 7; // s254static final int PATTERN_MILLISECOND = 8; // S255static final int PATTERN_DAY_OF_WEEK = 9; // E256static final int PATTERN_DAY_OF_YEAR = 10; // D257static final int PATTERN_DAY_OF_WEEK_IN_MONTH = 11; // F258static final int PATTERN_WEEK_OF_YEAR = 12; // w259static final int PATTERN_WEEK_OF_MONTH = 13; // W260static final int PATTERN_AM_PM = 14; // a261static final int PATTERN_HOUR1 = 15; // h262static final int PATTERN_HOUR0 = 16; // K263static final int PATTERN_ZONE_NAME = 17; // z264static final int PATTERN_ZONE_VALUE = 18; // Z265static final int PATTERN_WEEK_YEAR = 19; // Y266static final int PATTERN_ISO_DAY_OF_WEEK = 20; // u267static final int PATTERN_ISO_ZONE = 21; // X268static final int PATTERN_MONTH_STANDALONE = 22; // L269270/**271* Localized date-time pattern characters. For example, a locale may272* wish to use 'u' rather than 'y' to represent years in its date format273* pattern strings.274* This string must be exactly 18 characters long, with the index of275* the characters described by {@code DateFormat.ERA_FIELD},276* {@code DateFormat.YEAR_FIELD}, etc. Thus, if the string were277* "Xz...", then localized patterns would use 'X' for era and 'z' for year.278* @serial279*/280String localPatternChars = null;281282/**283* The locale which is used for initializing this DateFormatSymbols object.284*285* @since 1.6286* @serial287*/288Locale locale = null;289290/* use serialVersionUID from JDK 1.1.4 for interoperability */291@java.io.Serial292static final long serialVersionUID = -5987973545549424702L;293294/**295* Returns an array of all locales for which the296* {@code getInstance} methods of this class can return297* localized instances.298* The returned array represents the union of locales supported by the299* Java runtime and by installed300* {@link java.text.spi.DateFormatSymbolsProvider DateFormatSymbolsProvider}301* implementations. It must contain at least a {@code Locale}302* instance equal to {@link java.util.Locale#US Locale.US}.303*304* @return An array of locales for which localized305* {@code DateFormatSymbols} instances are available.306* @since 1.6307*/308public static Locale[] getAvailableLocales() {309LocaleServiceProviderPool pool=310LocaleServiceProviderPool.getPool(DateFormatSymbolsProvider.class);311return pool.getAvailableLocales();312}313314/**315* Gets the {@code DateFormatSymbols} instance for the default316* locale. This method provides access to {@code DateFormatSymbols}317* instances for locales supported by the Java runtime itself as well318* as for those supported by installed319* {@link java.text.spi.DateFormatSymbolsProvider DateFormatSymbolsProvider}320* implementations.321* <p>This is equivalent to calling {@link #getInstance(Locale)322* getInstance(Locale.getDefault(Locale.Category.FORMAT))}.323* @see java.util.Locale#getDefault(java.util.Locale.Category)324* @see java.util.Locale.Category#FORMAT325* @return a {@code DateFormatSymbols} instance.326* @since 1.6327*/328public static final DateFormatSymbols getInstance() {329return getInstance(Locale.getDefault(Locale.Category.FORMAT));330}331332/**333* Gets the {@code DateFormatSymbols} instance for the specified334* locale. This method provides access to {@code DateFormatSymbols}335* instances for locales supported by the Java runtime itself as well336* as for those supported by installed337* {@link java.text.spi.DateFormatSymbolsProvider DateFormatSymbolsProvider}338* implementations.339* @param locale the given locale.340* @return a {@code DateFormatSymbols} instance.341* @throws NullPointerException if {@code locale} is null342* @since 1.6343*/344public static final DateFormatSymbols getInstance(Locale locale) {345DateFormatSymbols dfs = getProviderInstance(locale);346if (dfs != null) {347return dfs;348}349throw new RuntimeException("DateFormatSymbols instance creation failed.");350}351352/**353* Returns a DateFormatSymbols provided by a provider or found in354* the cache. Note that this method returns a cached instance,355* not its clone. Therefore, the instance should never be given to356* an application.357*/358static final DateFormatSymbols getInstanceRef(Locale locale) {359DateFormatSymbols dfs = getProviderInstance(locale);360if (dfs != null) {361return dfs;362}363throw new RuntimeException("DateFormatSymbols instance creation failed.");364}365366private static DateFormatSymbols getProviderInstance(Locale locale) {367LocaleProviderAdapter adapter = LocaleProviderAdapter.getAdapter(DateFormatSymbolsProvider.class, locale);368DateFormatSymbolsProvider provider = adapter.getDateFormatSymbolsProvider();369DateFormatSymbols dfsyms = provider.getInstance(locale);370if (dfsyms == null) {371provider = LocaleProviderAdapter.forJRE().getDateFormatSymbolsProvider();372dfsyms = provider.getInstance(locale);373}374return dfsyms;375}376377/**378* Gets era strings. For example: "AD" and "BC".379* @return the era strings.380*/381public String[] getEras() {382return Arrays.copyOf(eras, eras.length);383}384385/**386* Sets era strings. For example: "AD" and "BC".387* @param newEras the new era strings.388*/389public void setEras(String[] newEras) {390eras = Arrays.copyOf(newEras, newEras.length);391cachedHashCode = 0;392}393394/**395* Gets month strings. For example: "January", "February", etc.396* An array with either 12 or 13 elements will be returned depending397* on whether or not {@link java.util.Calendar#UNDECIMBER Calendar.UNDECIMBER}398* is supported. Use399* {@link java.util.Calendar#JANUARY Calendar.JANUARY},400* {@link java.util.Calendar#FEBRUARY Calendar.FEBRUARY},401* etc. to index the result array.402*403* <p>If the language requires different forms for formatting and404* stand-alone usages, this method returns month names in the405* formatting form. For example, the preferred month name for406* January in the Czech language is <em>ledna</em> in the407* formatting form, while it is <em>leden</em> in the stand-alone408* form. This method returns {@code "ledna"} in this case. Refer409* to the <a href="http://unicode.org/reports/tr35/#Calendar_Elements">410* Calendar Elements in the Unicode Locale Data Markup Language411* (LDML) specification</a> for more details.412*413* @implSpec This method returns 13 elements since414* {@link java.util.Calendar#UNDECIMBER Calendar.UNDECIMBER} is supported.415* @return the month strings.416*/417public String[] getMonths() {418return Arrays.copyOf(months, months.length);419}420421/**422* Sets month strings. For example: "January", "February", etc.423* @param newMonths the new month strings. The array should424* be indexed by {@link java.util.Calendar#JANUARY Calendar.JANUARY},425* {@link java.util.Calendar#FEBRUARY Calendar.FEBRUARY}, etc.426*/427public void setMonths(String[] newMonths) {428months = Arrays.copyOf(newMonths, newMonths.length);429cachedHashCode = 0;430}431432/**433* Gets short month strings. For example: "Jan", "Feb", etc.434* An array with either 12 or 13 elements will be returned depending435* on whether or not {@link java.util.Calendar#UNDECIMBER Calendar.UNDECIMBER}436* is supported. Use437* {@link java.util.Calendar#JANUARY Calendar.JANUARY},438* {@link java.util.Calendar#FEBRUARY Calendar.FEBRUARY},439* etc. to index the result array.440*441* <p>If the language requires different forms for formatting and442* stand-alone usages, this method returns short month names in443* the formatting form. For example, the preferred abbreviation444* for January in the Catalan language is <em>de gen.</em> in the445* formatting form, while it is <em>gen.</em> in the stand-alone446* form. This method returns {@code "de gen."} in this case. Refer447* to the <a href="http://unicode.org/reports/tr35/#Calendar_Elements">448* Calendar Elements in the Unicode Locale Data Markup Language449* (LDML) specification</a> for more details.450*451* @implSpec This method returns 13 elements since452* {@link java.util.Calendar#UNDECIMBER Calendar.UNDECIMBER} is supported.453* @return the short month strings.454*/455public String[] getShortMonths() {456return Arrays.copyOf(shortMonths, shortMonths.length);457}458459/**460* Sets short month strings. For example: "Jan", "Feb", etc.461* @param newShortMonths the new short month strings. The array should462* be indexed by {@link java.util.Calendar#JANUARY Calendar.JANUARY},463* {@link java.util.Calendar#FEBRUARY Calendar.FEBRUARY}, etc.464*/465public void setShortMonths(String[] newShortMonths) {466shortMonths = Arrays.copyOf(newShortMonths, newShortMonths.length);467cachedHashCode = 0;468}469470/**471* Gets weekday strings. For example: "Sunday", "Monday", etc.472* @return the weekday strings. Use473* {@link java.util.Calendar#SUNDAY Calendar.SUNDAY},474* {@link java.util.Calendar#MONDAY Calendar.MONDAY}, etc. to index475* the result array.476*/477public String[] getWeekdays() {478return Arrays.copyOf(weekdays, weekdays.length);479}480481/**482* Sets weekday strings. For example: "Sunday", "Monday", etc.483* @param newWeekdays the new weekday strings. The array should484* be indexed by {@link java.util.Calendar#SUNDAY Calendar.SUNDAY},485* {@link java.util.Calendar#MONDAY Calendar.MONDAY}, etc.486*/487public void setWeekdays(String[] newWeekdays) {488weekdays = Arrays.copyOf(newWeekdays, newWeekdays.length);489cachedHashCode = 0;490}491492/**493* Gets short weekday strings. For example: "Sun", "Mon", etc.494* @return the short weekday strings. Use495* {@link java.util.Calendar#SUNDAY Calendar.SUNDAY},496* {@link java.util.Calendar#MONDAY Calendar.MONDAY}, etc. to index497* the result array.498*/499public String[] getShortWeekdays() {500return Arrays.copyOf(shortWeekdays, shortWeekdays.length);501}502503/**504* Sets short weekday strings. For example: "Sun", "Mon", etc.505* @param newShortWeekdays the new short weekday strings. The array should506* be indexed by {@link java.util.Calendar#SUNDAY Calendar.SUNDAY},507* {@link java.util.Calendar#MONDAY Calendar.MONDAY}, etc.508*/509public void setShortWeekdays(String[] newShortWeekdays) {510shortWeekdays = Arrays.copyOf(newShortWeekdays, newShortWeekdays.length);511cachedHashCode = 0;512}513514/**515* Gets ampm strings. For example: "AM" and "PM".516* @return the ampm strings.517*/518public String[] getAmPmStrings() {519return Arrays.copyOf(ampms, ampms.length);520}521522/**523* Sets ampm strings. For example: "AM" and "PM".524* @param newAmpms the new ampm strings.525*/526public void setAmPmStrings(String[] newAmpms) {527ampms = Arrays.copyOf(newAmpms, newAmpms.length);528cachedHashCode = 0;529}530531/**532* Gets time zone strings. Use of this method is discouraged; use533* {@link java.util.TimeZone#getDisplayName() TimeZone.getDisplayName()}534* instead.535* <p>536* The value returned is a537* two-dimensional array of strings of size <em>n</em> by <em>m</em>,538* where <em>m</em> is at least 5. Each of the <em>n</em> rows is an539* entry containing the localized names for a single {@code TimeZone}.540* Each such row contains (with {@code i} ranging from541* 0..<em>n</em>-1):542* <ul>543* <li>{@code zoneStrings[i][0]} - time zone ID</li>544* <li>{@code zoneStrings[i][1]} - long name of zone in standard545* time</li>546* <li>{@code zoneStrings[i][2]} - short name of zone in547* standard time</li>548* <li>{@code zoneStrings[i][3]} - long name of zone in daylight549* saving time</li>550* <li>{@code zoneStrings[i][4]} - short name of zone in daylight551* saving time</li>552* </ul>553* The zone ID is <em>not</em> localized; it's one of the valid IDs of554* the {@link java.util.TimeZone TimeZone} class that are not555* <a href="../util/TimeZone.html#CustomID">custom IDs</a>.556* All other entries are localized names. If a zone does not implement557* daylight saving time, the daylight saving time names should not be used.558* <p>559* If {@link #setZoneStrings(String[][]) setZoneStrings} has been called560* on this {@code DateFormatSymbols} instance, then the strings561* provided by that call are returned. Otherwise, the returned array562* contains names provided by the Java runtime and by installed563* {@link java.util.spi.TimeZoneNameProvider TimeZoneNameProvider}564* implementations.565*566* @return the time zone strings.567* @see #setZoneStrings(String[][])568*/569public String[][] getZoneStrings() {570return getZoneStringsImpl(true);571}572573/**574* Sets time zone strings. The argument must be a575* two-dimensional array of strings of size <em>n</em> by <em>m</em>,576* where <em>m</em> is at least 5. Each of the <em>n</em> rows is an577* entry containing the localized names for a single {@code TimeZone}.578* Each such row contains (with {@code i} ranging from579* 0..<em>n</em>-1):580* <ul>581* <li>{@code zoneStrings[i][0]} - time zone ID</li>582* <li>{@code zoneStrings[i][1]} - long name of zone in standard583* time</li>584* <li>{@code zoneStrings[i][2]} - short name of zone in585* standard time</li>586* <li>{@code zoneStrings[i][3]} - long name of zone in daylight587* saving time</li>588* <li>{@code zoneStrings[i][4]} - short name of zone in daylight589* saving time</li>590* </ul>591* The zone ID is <em>not</em> localized; it's one of the valid IDs of592* the {@link java.util.TimeZone TimeZone} class that are not593* <a href="../util/TimeZone.html#CustomID">custom IDs</a>.594* All other entries are localized names.595*596* @param newZoneStrings the new time zone strings.597* @throws IllegalArgumentException if the length of any row in598* {@code newZoneStrings} is less than 5599* @throws NullPointerException if {@code newZoneStrings} is null600* @see #getZoneStrings()601*/602public void setZoneStrings(String[][] newZoneStrings) {603String[][] aCopy = new String[newZoneStrings.length][];604for (int i = 0; i < newZoneStrings.length; ++i) {605int len = newZoneStrings[i].length;606if (len < 5) {607throw new IllegalArgumentException();608}609aCopy[i] = Arrays.copyOf(newZoneStrings[i], len);610}611zoneStrings = aCopy;612isZoneStringsSet = true;613cachedHashCode = 0;614}615616/**617* Gets localized date-time pattern characters. For example: 'u', 't', etc.618* @return the localized date-time pattern characters.619*/620public String getLocalPatternChars() {621return localPatternChars;622}623624/**625* Sets localized date-time pattern characters. For example: 'u', 't', etc.626* @param newLocalPatternChars the new localized date-time627* pattern characters.628*/629public void setLocalPatternChars(String newLocalPatternChars) {630// Call toString() to throw an NPE in case the argument is null631localPatternChars = newLocalPatternChars.toString();632cachedHashCode = 0;633}634635/**636* Overrides Cloneable637*/638public Object clone()639{640try641{642DateFormatSymbols other = (DateFormatSymbols)super.clone();643copyMembers(this, other);644return other;645} catch (CloneNotSupportedException e) {646throw new InternalError(e);647}648}649650/**651* Override hashCode.652* Generates a hash code for the DateFormatSymbols object.653*/654@Override655public int hashCode() {656int hashCode = cachedHashCode;657if (hashCode == 0) {658hashCode = 5;659hashCode = 11 * hashCode + Arrays.hashCode(eras);660hashCode = 11 * hashCode + Arrays.hashCode(months);661hashCode = 11 * hashCode + Arrays.hashCode(shortMonths);662hashCode = 11 * hashCode + Arrays.hashCode(weekdays);663hashCode = 11 * hashCode + Arrays.hashCode(shortWeekdays);664hashCode = 11 * hashCode + Arrays.hashCode(ampms);665hashCode = 11 * hashCode + Arrays.deepHashCode(getZoneStringsWrapper());666hashCode = 11 * hashCode + Objects.hashCode(localPatternChars);667if (hashCode != 0) {668cachedHashCode = hashCode;669}670}671672return hashCode;673}674675/**676* Override equals677*/678public boolean equals(Object obj)679{680if (this == obj) return true;681if (obj == null || getClass() != obj.getClass()) return false;682DateFormatSymbols that = (DateFormatSymbols) obj;683return (Arrays.equals(eras, that.eras)684&& Arrays.equals(months, that.months)685&& Arrays.equals(shortMonths, that.shortMonths)686&& Arrays.equals(weekdays, that.weekdays)687&& Arrays.equals(shortWeekdays, that.shortWeekdays)688&& Arrays.equals(ampms, that.ampms)689&& Arrays.deepEquals(getZoneStringsWrapper(), that.getZoneStringsWrapper())690&& ((localPatternChars != null691&& localPatternChars.equals(that.localPatternChars))692|| (localPatternChars == null693&& that.localPatternChars == null)));694}695696// =======================privates===============================697698/**699* Useful constant for defining time zone offsets.700*/701static final int millisPerHour = 60*60*1000;702703/**704* Cache to hold DateFormatSymbols instances per Locale.705*/706private static final ConcurrentMap<Locale, SoftReference<DateFormatSymbols>> cachedInstances707= new ConcurrentHashMap<>(3);708709private transient int lastZoneIndex;710711/**712* Cached hash code713*/714transient volatile int cachedHashCode;715716/**717* Initializes this DateFormatSymbols with the locale data. This method uses718* a cached DateFormatSymbols instance for the given locale if available. If719* there's no cached one, this method creates an uninitialized instance and720* populates its fields from the resource bundle for the locale, and caches721* the instance. Note: zoneStrings isn't initialized in this method.722*/723private void initializeData(Locale locale) {724SoftReference<DateFormatSymbols> ref = cachedInstances.get(locale);725DateFormatSymbols dfs;726if (ref == null || (dfs = ref.get()) == null) {727if (ref != null) {728// Remove the empty SoftReference729cachedInstances.remove(locale, ref);730}731dfs = new DateFormatSymbols(false);732733// check for region override734Locale override = CalendarDataUtility.findRegionOverride(locale);735736// Initialize the fields from the ResourceBundle for locale.737LocaleProviderAdapter adapter738= LocaleProviderAdapter.getAdapter(DateFormatSymbolsProvider.class, override);739// Avoid any potential recursions740if (!(adapter instanceof ResourceBundleBasedAdapter)) {741adapter = LocaleProviderAdapter.getResourceBundleBased();742}743ResourceBundle resource744= ((ResourceBundleBasedAdapter)adapter).getLocaleData().getDateFormatData(override);745746dfs.locale = locale;747// JRE and CLDR use different keys748// JRE: Eras, short.Eras and narrow.Eras749// CLDR: long.Eras, Eras and narrow.Eras750if (resource.containsKey("Eras")) {751dfs.eras = resource.getStringArray("Eras");752} else if (resource.containsKey("long.Eras")) {753dfs.eras = resource.getStringArray("long.Eras");754} else if (resource.containsKey("short.Eras")) {755dfs.eras = resource.getStringArray("short.Eras");756}757dfs.months = resource.getStringArray("MonthNames");758dfs.shortMonths = resource.getStringArray("MonthAbbreviations");759dfs.ampms = resource.getStringArray("AmPmMarkers");760// the array in the resource bundle may contain more elements for day periods.761// Extract only am/pm.762if (dfs.ampms.length > 2) {763dfs.ampms = Arrays.copyOf(dfs.ampms, 2);764}765dfs.localPatternChars = resource.getString("DateTimePatternChars");766767// Day of week names are stored in a 1-based array.768dfs.weekdays = toOneBasedArray(resource.getStringArray("DayNames"));769dfs.shortWeekdays = toOneBasedArray(resource.getStringArray("DayAbbreviations"));770771// Put dfs in the cache772ref = new SoftReference<>(dfs);773SoftReference<DateFormatSymbols> x = cachedInstances.putIfAbsent(locale, ref);774if (x != null) {775DateFormatSymbols y = x.get();776if (y == null) {777// Replace the empty SoftReference with ref.778cachedInstances.replace(locale, x, ref);779} else {780ref = x;781dfs = y;782}783}784}785786// Copy the field values from dfs to this instance.787copyMembers(dfs, this);788}789790private static String[] toOneBasedArray(String[] src) {791int len = src.length;792String[] dst = new String[len + 1];793dst[0] = "";794for (int i = 0; i < len; i++) {795dst[i + 1] = src[i];796}797return dst;798}799800/**801* Package private: used by SimpleDateFormat802* Gets the index for the given time zone ID to obtain the time zone803* strings for formatting. The time zone ID is just for programmatic804* lookup. NOT LOCALIZED!!!805* @param ID the given time zone ID.806* @return the index of the given time zone ID. Returns -1 if807* the given time zone ID can't be located in the DateFormatSymbols object.808* @see java.util.SimpleTimeZone809*/810final int getZoneIndex(String ID) {811String[][] zoneStrings = getZoneStringsWrapper();812813/*814* getZoneIndex has been re-written for performance reasons. instead of815* traversing the zoneStrings array every time, we cache the last used zone816* index817*/818if (lastZoneIndex < zoneStrings.length && ID.equals(zoneStrings[lastZoneIndex][0])) {819return lastZoneIndex;820}821822/* slow path, search entire list */823for (int index = 0; index < zoneStrings.length; index++) {824if (ID.equals(zoneStrings[index][0])) {825lastZoneIndex = index;826return index;827}828}829830return -1;831}832833/**834* Wrapper method to the getZoneStrings(), which is called from inside835* the java.text package and not to mutate the returned arrays, so that836* it does not need to create a defensive copy.837*/838final String[][] getZoneStringsWrapper() {839if (isSubclassObject()) {840return getZoneStrings();841} else {842return getZoneStringsImpl(false);843}844}845846private String[][] getZoneStringsImpl(boolean needsCopy) {847if (zoneStrings == null) {848zoneStrings = TimeZoneNameUtility.getZoneStrings(locale);849}850851if (!needsCopy) {852return zoneStrings;853}854855int len = zoneStrings.length;856String[][] aCopy = new String[len][];857for (int i = 0; i < len; i++) {858aCopy[i] = Arrays.copyOf(zoneStrings[i], zoneStrings[i].length);859}860return aCopy;861}862863private boolean isSubclassObject() {864return !getClass().getName().equals("java.text.DateFormatSymbols");865}866867/**868* Clones all the data members from the source DateFormatSymbols to869* the target DateFormatSymbols.870*871* @param src the source DateFormatSymbols.872* @param dst the target DateFormatSymbols.873*/874private void copyMembers(DateFormatSymbols src, DateFormatSymbols dst)875{876dst.locale = src.locale;877dst.eras = Arrays.copyOf(src.eras, src.eras.length);878dst.months = Arrays.copyOf(src.months, src.months.length);879dst.shortMonths = Arrays.copyOf(src.shortMonths, src.shortMonths.length);880dst.weekdays = Arrays.copyOf(src.weekdays, src.weekdays.length);881dst.shortWeekdays = Arrays.copyOf(src.shortWeekdays, src.shortWeekdays.length);882dst.ampms = Arrays.copyOf(src.ampms, src.ampms.length);883if (src.zoneStrings != null) {884dst.zoneStrings = src.getZoneStringsImpl(true);885} else {886dst.zoneStrings = null;887}888dst.localPatternChars = src.localPatternChars;889dst.cachedHashCode = 0;890}891892/**893* Write out the default serializable data, after ensuring the894* {@code zoneStrings} field is initialized in order to make895* sure the backward compatibility.896*897* @since 1.6898*/899@java.io.Serial900private void writeObject(ObjectOutputStream stream) throws IOException {901if (zoneStrings == null) {902zoneStrings = TimeZoneNameUtility.getZoneStrings(locale);903}904stream.defaultWriteObject();905}906}907908909