Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/text/DateFormatSymbols.java
38829 views
/*1* Copyright (c) 1996, 2016, 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.LocaleProviderAdapter;52import sun.util.locale.provider.LocaleServiceProviderPool;53import sun.util.locale.provider.ResourceBundleBasedAdapter;54import sun.util.locale.provider.TimeZoneNameUtility;5556/**57* <code>DateFormatSymbols</code> is a public class for encapsulating58* localizable date-time formatting data, such as the names of the59* months, the names of the days of the week, and the time zone data.60* <code>SimpleDateFormat</code> uses61* <code>DateFormatSymbols</code> to encapsulate this information.62*63* <p>64* Typically you shouldn't use <code>DateFormatSymbols</code> directly.65* Rather, you are encouraged to create a date-time formatter with the66* <code>DateFormat</code> class's factory methods: <code>getTimeInstance</code>,67* <code>getDateInstance</code>, or <code>getDateTimeInstance</code>.68* These methods automatically create a <code>DateFormatSymbols</code> for69* the formatter so that you don't have to. After the70* formatter is created, you may modify its format pattern using the71* <code>setPattern</code> method. For more information about72* creating formatters using <code>DateFormat</code>'s factory methods,73* see {@link DateFormat}.74*75* <p>76* If you decide to create a date-time formatter with a specific77* format pattern for a specific locale, you can do so with:78* <blockquote>79* <pre>80* new SimpleDateFormat(aPattern, DateFormatSymbols.getInstance(aLocale)).81* </pre>82* </blockquote>83*84* <p>85* <code>DateFormatSymbols</code> objects are cloneable. When you obtain86* a <code>DateFormatSymbols</code> object, feel free to modify the87* date-time formatting data. For instance, you can replace the localized88* date-time format pattern characters with the ones that you feel easy89* to remember. Or you can change the representative cities90* to your favorite ones.91*92* <p>93* New <code>DateFormatSymbols</code> subclasses may be added to support94* <code>SimpleDateFormat</code> for date-time formatting for additional locales.9596* @see DateFormat97* @see SimpleDateFormat98* @see java.util.SimpleTimeZone99* @author Chen-Lieh Huang100*/101public class DateFormatSymbols implements Serializable, Cloneable {102103/**104* Construct a DateFormatSymbols object by loading format data from105* resources for the default {@link java.util.Locale.Category#FORMAT FORMAT}106* locale. This constructor can only107* construct instances for the locales supported by the Java108* runtime environment, not for those supported by installed109* {@link java.text.spi.DateFormatSymbolsProvider DateFormatSymbolsProvider}110* implementations. For full locale coverage, use the111* {@link #getInstance(Locale) getInstance} method.112* <p>This is equivalent to calling113* {@link #DateFormatSymbols(Locale)114* DateFormatSymbols(Locale.getDefault(Locale.Category.FORMAT))}.115* @see #getInstance()116* @see java.util.Locale#getDefault(java.util.Locale.Category)117* @see java.util.Locale.Category#FORMAT118* @exception java.util.MissingResourceException119* if the resources for the default locale cannot be120* found or cannot be loaded.121*/122public DateFormatSymbols()123{124initializeData(Locale.getDefault(Locale.Category.FORMAT));125}126127/**128* Construct a DateFormatSymbols object by loading format data from129* resources for the given locale. This constructor can only130* construct instances for the locales supported by the Java131* runtime environment, not for those supported by installed132* {@link java.text.spi.DateFormatSymbolsProvider DateFormatSymbolsProvider}133* implementations. For full locale coverage, use the134* {@link #getInstance(Locale) getInstance} method.135*136* @param locale the desired locale137* @see #getInstance(Locale)138* @exception java.util.MissingResourceException139* if the resources for the specified locale cannot be140* found or cannot be loaded.141*/142public DateFormatSymbols(Locale locale)143{144initializeData(locale);145}146147/**148* Constructs an uninitialized DateFormatSymbols.149*/150private DateFormatSymbols(boolean flag) {151}152153/**154* Era strings. For example: "AD" and "BC". An array of 2 strings,155* indexed by <code>Calendar.BC</code> and <code>Calendar.AD</code>.156* @serial157*/158String eras[] = null;159160/**161* Month strings. For example: "January", "February", etc. An array162* of 13 strings (some calendars have 13 months), indexed by163* <code>Calendar.JANUARY</code>, <code>Calendar.FEBRUARY</code>, etc.164* @serial165*/166String months[] = null;167168/**169* Short month strings. For example: "Jan", "Feb", etc. An array of170* 13 strings (some calendars have 13 months), indexed by171* <code>Calendar.JANUARY</code>, <code>Calendar.FEBRUARY</code>, etc.172173* @serial174*/175String shortMonths[] = null;176177/**178* Weekday strings. For example: "Sunday", "Monday", etc. An array179* of 8 strings, indexed by <code>Calendar.SUNDAY</code>,180* <code>Calendar.MONDAY</code>, etc.181* The element <code>weekdays[0]</code> is ignored.182* @serial183*/184String weekdays[] = null;185186/**187* Short weekday strings. For example: "Sun", "Mon", etc. An array188* of 8 strings, indexed by <code>Calendar.SUNDAY</code>,189* <code>Calendar.MONDAY</code>, etc.190* The element <code>shortWeekdays[0]</code> is ignored.191* @serial192*/193String shortWeekdays[] = null;194195/**196* AM and PM strings. For example: "AM" and "PM". An array of197* 2 strings, indexed by <code>Calendar.AM</code> and198* <code>Calendar.PM</code>.199* @serial200*/201String ampms[] = null;202203/**204* Localized names of time zones in this locale. This is a205* two-dimensional array of strings of size <em>n</em> by <em>m</em>,206* where <em>m</em> is at least 5. Each of the <em>n</em> rows is an207* entry containing the localized names for a single <code>TimeZone</code>.208* Each such row contains (with <code>i</code> ranging from209* 0..<em>n</em>-1):210* <ul>211* <li><code>zoneStrings[i][0]</code> - time zone ID</li>212* <li><code>zoneStrings[i][1]</code> - long name of zone in standard213* time</li>214* <li><code>zoneStrings[i][2]</code> - short name of zone in215* standard time</li>216* <li><code>zoneStrings[i][3]</code> - long name of zone in daylight217* saving time</li>218* <li><code>zoneStrings[i][4]</code> - short name of zone in daylight219* saving time</li>220* </ul>221* The zone ID is <em>not</em> localized; it's one of the valid IDs of222* the {@link java.util.TimeZone TimeZone} class that are not223* <a href="../java/util/TimeZone.html#CustomID">custom IDs</a>.224* All other entries are localized names.225* @see java.util.TimeZone226* @serial227*/228String zoneStrings[][] = null;229230/**231* Indicates that zoneStrings is set externally with setZoneStrings() method.232*/233transient boolean isZoneStringsSet = false;234235/**236* Unlocalized date-time pattern characters. For example: 'y', 'd', etc.237* All locales use the same these unlocalized pattern characters.238*/239static final String patternChars = "GyMdkHmsSEDFwWahKzZYuXL";240241static final int PATTERN_ERA = 0; // G242static final int PATTERN_YEAR = 1; // y243static final int PATTERN_MONTH = 2; // M244static final int PATTERN_DAY_OF_MONTH = 3; // d245static final int PATTERN_HOUR_OF_DAY1 = 4; // k246static final int PATTERN_HOUR_OF_DAY0 = 5; // H247static final int PATTERN_MINUTE = 6; // m248static final int PATTERN_SECOND = 7; // s249static final int PATTERN_MILLISECOND = 8; // S250static final int PATTERN_DAY_OF_WEEK = 9; // E251static final int PATTERN_DAY_OF_YEAR = 10; // D252static final int PATTERN_DAY_OF_WEEK_IN_MONTH = 11; // F253static final int PATTERN_WEEK_OF_YEAR = 12; // w254static final int PATTERN_WEEK_OF_MONTH = 13; // W255static final int PATTERN_AM_PM = 14; // a256static final int PATTERN_HOUR1 = 15; // h257static final int PATTERN_HOUR0 = 16; // K258static final int PATTERN_ZONE_NAME = 17; // z259static final int PATTERN_ZONE_VALUE = 18; // Z260static final int PATTERN_WEEK_YEAR = 19; // Y261static final int PATTERN_ISO_DAY_OF_WEEK = 20; // u262static final int PATTERN_ISO_ZONE = 21; // X263static final int PATTERN_MONTH_STANDALONE = 22; // L264265/**266* Localized date-time pattern characters. For example, a locale may267* wish to use 'u' rather than 'y' to represent years in its date format268* pattern strings.269* This string must be exactly 18 characters long, with the index of270* the characters described by <code>DateFormat.ERA_FIELD</code>,271* <code>DateFormat.YEAR_FIELD</code>, etc. Thus, if the string were272* "Xz...", then localized patterns would use 'X' for era and 'z' for year.273* @serial274*/275String localPatternChars = null;276277/**278* The locale which is used for initializing this DateFormatSymbols object.279*280* @since 1.6281* @serial282*/283Locale locale = null;284285/* use serialVersionUID from JDK 1.1.4 for interoperability */286static final long serialVersionUID = -5987973545549424702L;287288/**289* Returns an array of all locales for which the290* <code>getInstance</code> methods of this class can return291* localized instances.292* The returned array represents the union of locales supported by the293* Java runtime and by installed294* {@link java.text.spi.DateFormatSymbolsProvider DateFormatSymbolsProvider}295* implementations. It must contain at least a <code>Locale</code>296* instance equal to {@link java.util.Locale#US Locale.US}.297*298* @return An array of locales for which localized299* <code>DateFormatSymbols</code> instances are available.300* @since 1.6301*/302public static Locale[] getAvailableLocales() {303LocaleServiceProviderPool pool=304LocaleServiceProviderPool.getPool(DateFormatSymbolsProvider.class);305return pool.getAvailableLocales();306}307308/**309* Gets the <code>DateFormatSymbols</code> instance for the default310* locale. This method provides access to <code>DateFormatSymbols</code>311* instances for locales supported by the Java runtime itself as well312* as for those supported by installed313* {@link java.text.spi.DateFormatSymbolsProvider DateFormatSymbolsProvider}314* implementations.315* <p>This is equivalent to calling {@link #getInstance(Locale)316* getInstance(Locale.getDefault(Locale.Category.FORMAT))}.317* @see java.util.Locale#getDefault(java.util.Locale.Category)318* @see java.util.Locale.Category#FORMAT319* @return a <code>DateFormatSymbols</code> instance.320* @since 1.6321*/322public static final DateFormatSymbols getInstance() {323return getInstance(Locale.getDefault(Locale.Category.FORMAT));324}325326/**327* Gets the <code>DateFormatSymbols</code> instance for the specified328* locale. This method provides access to <code>DateFormatSymbols</code>329* instances for locales supported by the Java runtime itself as well330* as for those supported by installed331* {@link java.text.spi.DateFormatSymbolsProvider DateFormatSymbolsProvider}332* implementations.333* @param locale the given locale.334* @return a <code>DateFormatSymbols</code> instance.335* @exception NullPointerException if <code>locale</code> is null336* @since 1.6337*/338public static final DateFormatSymbols getInstance(Locale locale) {339DateFormatSymbols dfs = getProviderInstance(locale);340if (dfs != null) {341return dfs;342}343throw new RuntimeException("DateFormatSymbols instance creation failed.");344}345346/**347* Returns a DateFormatSymbols provided by a provider or found in348* the cache. Note that this method returns a cached instance,349* not its clone. Therefore, the instance should never be given to350* an application.351*/352static final DateFormatSymbols getInstanceRef(Locale locale) {353DateFormatSymbols dfs = getProviderInstance(locale);354if (dfs != null) {355return dfs;356}357throw new RuntimeException("DateFormatSymbols instance creation failed.");358}359360private static DateFormatSymbols getProviderInstance(Locale locale) {361LocaleProviderAdapter adapter = LocaleProviderAdapter.getAdapter(DateFormatSymbolsProvider.class, locale);362DateFormatSymbolsProvider provider = adapter.getDateFormatSymbolsProvider();363DateFormatSymbols dfsyms = provider.getInstance(locale);364if (dfsyms == null) {365provider = LocaleProviderAdapter.forJRE().getDateFormatSymbolsProvider();366dfsyms = provider.getInstance(locale);367}368return dfsyms;369}370371/**372* Gets era strings. For example: "AD" and "BC".373* @return the era strings.374*/375public String[] getEras() {376return Arrays.copyOf(eras, eras.length);377}378379/**380* Sets era strings. For example: "AD" and "BC".381* @param newEras the new era strings.382*/383public void setEras(String[] newEras) {384eras = Arrays.copyOf(newEras, newEras.length);385cachedHashCode = 0;386}387388/**389* Gets month strings. For example: "January", "February", etc.390*391* <p>If the language requires different forms for formatting and392* stand-alone usages, this method returns month names in the393* formatting form. For example, the preferred month name for394* January in the Czech language is <em>ledna</em> in the395* formatting form, while it is <em>leden</em> in the stand-alone396* form. This method returns {@code "ledna"} in this case. Refer397* to the <a href="http://unicode.org/reports/tr35/#Calendar_Elements">398* Calendar Elements in the Unicode Locale Data Markup Language399* (LDML) specification</a> for more details.400*401* @return the month strings.402*/403public String[] getMonths() {404return Arrays.copyOf(months, months.length);405}406407/**408* Sets month strings. For example: "January", "February", etc.409* @param newMonths the new month strings.410*/411public void setMonths(String[] newMonths) {412months = Arrays.copyOf(newMonths, newMonths.length);413cachedHashCode = 0;414}415416/**417* Gets short month strings. For example: "Jan", "Feb", etc.418*419* <p>If the language requires different forms for formatting and420* stand-alone usages, This method returns short month names in421* the formatting form. For example, the preferred abbreviation422* for January in the Catalan language is <em>de gen.</em> in the423* formatting form, while it is <em>gen.</em> in the stand-alone424* form. This method returns {@code "de gen."} in this case. Refer425* to the <a href="http://unicode.org/reports/tr35/#Calendar_Elements">426* Calendar Elements in the Unicode Locale Data Markup Language427* (LDML) specification</a> for more details.428*429* @return the short month strings.430*/431public String[] getShortMonths() {432return Arrays.copyOf(shortMonths, shortMonths.length);433}434435/**436* Sets short month strings. For example: "Jan", "Feb", etc.437* @param newShortMonths the new short month strings.438*/439public void setShortMonths(String[] newShortMonths) {440shortMonths = Arrays.copyOf(newShortMonths, newShortMonths.length);441cachedHashCode = 0;442}443444/**445* Gets weekday strings. For example: "Sunday", "Monday", etc.446* @return the weekday strings. Use <code>Calendar.SUNDAY</code>,447* <code>Calendar.MONDAY</code>, etc. to index the result array.448*/449public String[] getWeekdays() {450return Arrays.copyOf(weekdays, weekdays.length);451}452453/**454* Sets weekday strings. For example: "Sunday", "Monday", etc.455* @param newWeekdays the new weekday strings. The array should456* be indexed by <code>Calendar.SUNDAY</code>,457* <code>Calendar.MONDAY</code>, etc.458*/459public void setWeekdays(String[] newWeekdays) {460weekdays = Arrays.copyOf(newWeekdays, newWeekdays.length);461cachedHashCode = 0;462}463464/**465* Gets short weekday strings. For example: "Sun", "Mon", etc.466* @return the short weekday strings. Use <code>Calendar.SUNDAY</code>,467* <code>Calendar.MONDAY</code>, etc. to index the result array.468*/469public String[] getShortWeekdays() {470return Arrays.copyOf(shortWeekdays, shortWeekdays.length);471}472473/**474* Sets short weekday strings. For example: "Sun", "Mon", etc.475* @param newShortWeekdays the new short weekday strings. The array should476* be indexed by <code>Calendar.SUNDAY</code>,477* <code>Calendar.MONDAY</code>, etc.478*/479public void setShortWeekdays(String[] newShortWeekdays) {480shortWeekdays = Arrays.copyOf(newShortWeekdays, newShortWeekdays.length);481cachedHashCode = 0;482}483484/**485* Gets ampm strings. For example: "AM" and "PM".486* @return the ampm strings.487*/488public String[] getAmPmStrings() {489return Arrays.copyOf(ampms, ampms.length);490}491492/**493* Sets ampm strings. For example: "AM" and "PM".494* @param newAmpms the new ampm strings.495*/496public void setAmPmStrings(String[] newAmpms) {497ampms = Arrays.copyOf(newAmpms, newAmpms.length);498cachedHashCode = 0;499}500501/**502* Gets time zone strings. Use of this method is discouraged; use503* {@link java.util.TimeZone#getDisplayName() TimeZone.getDisplayName()}504* instead.505* <p>506* The value returned is a507* two-dimensional array of strings of size <em>n</em> by <em>m</em>,508* where <em>m</em> is at least 5. Each of the <em>n</em> rows is an509* entry containing the localized names for a single <code>TimeZone</code>.510* Each such row contains (with <code>i</code> ranging from511* 0..<em>n</em>-1):512* <ul>513* <li><code>zoneStrings[i][0]</code> - time zone ID</li>514* <li><code>zoneStrings[i][1]</code> - long name of zone in standard515* time</li>516* <li><code>zoneStrings[i][2]</code> - short name of zone in517* standard time</li>518* <li><code>zoneStrings[i][3]</code> - long name of zone in daylight519* saving time</li>520* <li><code>zoneStrings[i][4]</code> - short name of zone in daylight521* saving time</li>522* </ul>523* The zone ID is <em>not</em> localized; it's one of the valid IDs of524* the {@link java.util.TimeZone TimeZone} class that are not525* <a href="../util/TimeZone.html#CustomID">custom IDs</a>.526* All other entries are localized names. If a zone does not implement527* daylight saving time, the daylight saving time names should not be used.528* <p>529* If {@link #setZoneStrings(String[][]) setZoneStrings} has been called530* on this <code>DateFormatSymbols</code> instance, then the strings531* provided by that call are returned. Otherwise, the returned array532* contains names provided by the Java runtime and by installed533* {@link java.util.spi.TimeZoneNameProvider TimeZoneNameProvider}534* implementations.535*536* @return the time zone strings.537* @see #setZoneStrings(String[][])538*/539public String[][] getZoneStrings() {540return getZoneStringsImpl(true);541}542543/**544* Sets time zone strings. The argument must be a545* two-dimensional array of strings of size <em>n</em> by <em>m</em>,546* where <em>m</em> is at least 5. Each of the <em>n</em> rows is an547* entry containing the localized names for a single <code>TimeZone</code>.548* Each such row contains (with <code>i</code> ranging from549* 0..<em>n</em>-1):550* <ul>551* <li><code>zoneStrings[i][0]</code> - time zone ID</li>552* <li><code>zoneStrings[i][1]</code> - long name of zone in standard553* time</li>554* <li><code>zoneStrings[i][2]</code> - short name of zone in555* standard time</li>556* <li><code>zoneStrings[i][3]</code> - long name of zone in daylight557* saving time</li>558* <li><code>zoneStrings[i][4]</code> - short name of zone in daylight559* saving time</li>560* </ul>561* The zone ID is <em>not</em> localized; it's one of the valid IDs of562* the {@link java.util.TimeZone TimeZone} class that are not563* <a href="../util/TimeZone.html#CustomID">custom IDs</a>.564* All other entries are localized names.565*566* @param newZoneStrings the new time zone strings.567* @exception IllegalArgumentException if the length of any row in568* <code>newZoneStrings</code> is less than 5569* @exception NullPointerException if <code>newZoneStrings</code> is null570* @see #getZoneStrings()571*/572public void setZoneStrings(String[][] newZoneStrings) {573String[][] aCopy = new String[newZoneStrings.length][];574for (int i = 0; i < newZoneStrings.length; ++i) {575int len = newZoneStrings[i].length;576if (len < 5) {577throw new IllegalArgumentException();578}579aCopy[i] = Arrays.copyOf(newZoneStrings[i], len);580}581zoneStrings = aCopy;582isZoneStringsSet = true;583cachedHashCode = 0;584}585586/**587* Gets localized date-time pattern characters. For example: 'u', 't', etc.588* @return the localized date-time pattern characters.589*/590public String getLocalPatternChars() {591return localPatternChars;592}593594/**595* Sets localized date-time pattern characters. For example: 'u', 't', etc.596* @param newLocalPatternChars the new localized date-time597* pattern characters.598*/599public void setLocalPatternChars(String newLocalPatternChars) {600// Call toString() to throw an NPE in case the argument is null601localPatternChars = newLocalPatternChars.toString();602cachedHashCode = 0;603}604605/**606* Overrides Cloneable607*/608public Object clone()609{610try611{612DateFormatSymbols other = (DateFormatSymbols)super.clone();613copyMembers(this, other);614return other;615} catch (CloneNotSupportedException e) {616throw new InternalError(e);617}618}619620/**621* Override hashCode.622* Generates a hash code for the DateFormatSymbols object.623*/624@Override625public int hashCode() {626int hashCode = cachedHashCode;627if (hashCode == 0) {628hashCode = 5;629hashCode = 11 * hashCode + Arrays.hashCode(eras);630hashCode = 11 * hashCode + Arrays.hashCode(months);631hashCode = 11 * hashCode + Arrays.hashCode(shortMonths);632hashCode = 11 * hashCode + Arrays.hashCode(weekdays);633hashCode = 11 * hashCode + Arrays.hashCode(shortWeekdays);634hashCode = 11 * hashCode + Arrays.hashCode(ampms);635hashCode = 11 * hashCode + Arrays.deepHashCode(getZoneStringsWrapper());636hashCode = 11 * hashCode + Objects.hashCode(localPatternChars);637cachedHashCode = hashCode;638}639640return hashCode;641}642643/**644* Override equals645*/646public boolean equals(Object obj)647{648if (this == obj) return true;649if (obj == null || getClass() != obj.getClass()) return false;650DateFormatSymbols that = (DateFormatSymbols) obj;651return (Arrays.equals(eras, that.eras)652&& Arrays.equals(months, that.months)653&& Arrays.equals(shortMonths, that.shortMonths)654&& Arrays.equals(weekdays, that.weekdays)655&& Arrays.equals(shortWeekdays, that.shortWeekdays)656&& Arrays.equals(ampms, that.ampms)657&& Arrays.deepEquals(getZoneStringsWrapper(), that.getZoneStringsWrapper())658&& ((localPatternChars != null659&& localPatternChars.equals(that.localPatternChars))660|| (localPatternChars == null661&& that.localPatternChars == null)));662}663664// =======================privates===============================665666/**667* Useful constant for defining time zone offsets.668*/669static final int millisPerHour = 60*60*1000;670671/**672* Cache to hold DateFormatSymbols instances per Locale.673*/674private static final ConcurrentMap<Locale, SoftReference<DateFormatSymbols>> cachedInstances675= new ConcurrentHashMap<>(3);676677private transient int lastZoneIndex = 0;678679/**680* Cached hash code681*/682transient volatile int cachedHashCode = 0;683684/**685* Initializes this DateFormatSymbols with the locale data. This method uses686* a cached DateFormatSymbols instance for the given locale if available. If687* there's no cached one, this method creates an uninitialized instance and688* populates its fields from the resource bundle for the locale, and caches689* the instance. Note: zoneStrings isn't initialized in this method.690*/691private void initializeData(Locale locale) {692SoftReference<DateFormatSymbols> ref = cachedInstances.get(locale);693DateFormatSymbols dfs;694if (ref == null || (dfs = ref.get()) == null) {695if (ref != null) {696// Remove the empty SoftReference697cachedInstances.remove(locale, ref);698}699dfs = new DateFormatSymbols(false);700701// Initialize the fields from the ResourceBundle for locale.702LocaleProviderAdapter adapter703= LocaleProviderAdapter.getAdapter(DateFormatSymbolsProvider.class, locale);704// Avoid any potential recursions705if (!(adapter instanceof ResourceBundleBasedAdapter)) {706adapter = LocaleProviderAdapter.getResourceBundleBased();707}708ResourceBundle resource709= ((ResourceBundleBasedAdapter)adapter).getLocaleData().getDateFormatData(locale);710711dfs.locale = locale;712// JRE and CLDR use different keys713// JRE: Eras, short.Eras and narrow.Eras714// CLDR: long.Eras, Eras and narrow.Eras715if (resource.containsKey("Eras")) {716dfs.eras = resource.getStringArray("Eras");717} else if (resource.containsKey("long.Eras")) {718dfs.eras = resource.getStringArray("long.Eras");719} else if (resource.containsKey("short.Eras")) {720dfs.eras = resource.getStringArray("short.Eras");721}722dfs.months = resource.getStringArray("MonthNames");723dfs.shortMonths = resource.getStringArray("MonthAbbreviations");724dfs.ampms = resource.getStringArray("AmPmMarkers");725dfs.localPatternChars = resource.getString("DateTimePatternChars");726727// Day of week names are stored in a 1-based array.728dfs.weekdays = toOneBasedArray(resource.getStringArray("DayNames"));729dfs.shortWeekdays = toOneBasedArray(resource.getStringArray("DayAbbreviations"));730731// Put dfs in the cache732ref = new SoftReference<>(dfs);733SoftReference<DateFormatSymbols> x = cachedInstances.putIfAbsent(locale, ref);734if (x != null) {735DateFormatSymbols y = x.get();736if (y == null) {737// Replace the empty SoftReference with ref.738cachedInstances.replace(locale, x, ref);739} else {740ref = x;741dfs = y;742}743}744// If the bundle's locale isn't the target locale, put another cache745// entry for the bundle's locale.746Locale bundleLocale = resource.getLocale();747if (!bundleLocale.equals(locale)) {748SoftReference<DateFormatSymbols> z749= cachedInstances.putIfAbsent(bundleLocale, ref);750if (z != null && z.get() == null) {751cachedInstances.replace(bundleLocale, z, ref);752}753}754}755756// Copy the field values from dfs to this instance.757copyMembers(dfs, this);758}759760private static String[] toOneBasedArray(String[] src) {761int len = src.length;762String[] dst = new String[len + 1];763dst[0] = "";764for (int i = 0; i < len; i++) {765dst[i + 1] = src[i];766}767return dst;768}769770/**771* Package private: used by SimpleDateFormat772* Gets the index for the given time zone ID to obtain the time zone773* strings for formatting. The time zone ID is just for programmatic774* lookup. NOT LOCALIZED!!!775* @param ID the given time zone ID.776* @return the index of the given time zone ID. Returns -1 if777* the given time zone ID can't be located in the DateFormatSymbols object.778* @see java.util.SimpleTimeZone779*/780final int getZoneIndex(String ID) {781String[][] zoneStrings = getZoneStringsWrapper();782783/*784* getZoneIndex has been re-written for performance reasons. instead of785* traversing the zoneStrings array every time, we cache the last used zone786* index787*/788if (lastZoneIndex < zoneStrings.length && ID.equals(zoneStrings[lastZoneIndex][0])) {789return lastZoneIndex;790}791792/* slow path, search entire list */793for (int index = 0; index < zoneStrings.length; index++) {794if (ID.equals(zoneStrings[index][0])) {795lastZoneIndex = index;796return index;797}798}799800return -1;801}802803/**804* Wrapper method to the getZoneStrings(), which is called from inside805* the java.text package and not to mutate the returned arrays, so that806* it does not need to create a defensive copy.807*/808final String[][] getZoneStringsWrapper() {809if (isSubclassObject()) {810return getZoneStrings();811} else {812return getZoneStringsImpl(false);813}814}815816private String[][] getZoneStringsImpl(boolean needsCopy) {817if (zoneStrings == null) {818zoneStrings = TimeZoneNameUtility.getZoneStrings(locale);819}820821if (!needsCopy) {822return zoneStrings;823}824825int len = zoneStrings.length;826String[][] aCopy = new String[len][];827for (int i = 0; i < len; i++) {828aCopy[i] = Arrays.copyOf(zoneStrings[i], zoneStrings[i].length);829}830return aCopy;831}832833private boolean isSubclassObject() {834return !getClass().getName().equals("java.text.DateFormatSymbols");835}836837/**838* Clones all the data members from the source DateFormatSymbols to839* the target DateFormatSymbols.840*841* @param src the source DateFormatSymbols.842* @param dst the target DateFormatSymbols.843*/844private void copyMembers(DateFormatSymbols src, DateFormatSymbols dst)845{846dst.locale = src.locale;847dst.eras = Arrays.copyOf(src.eras, src.eras.length);848dst.months = Arrays.copyOf(src.months, src.months.length);849dst.shortMonths = Arrays.copyOf(src.shortMonths, src.shortMonths.length);850dst.weekdays = Arrays.copyOf(src.weekdays, src.weekdays.length);851dst.shortWeekdays = Arrays.copyOf(src.shortWeekdays, src.shortWeekdays.length);852dst.ampms = Arrays.copyOf(src.ampms, src.ampms.length);853if (src.zoneStrings != null) {854dst.zoneStrings = src.getZoneStringsImpl(true);855} else {856dst.zoneStrings = null;857}858dst.localPatternChars = src.localPatternChars;859dst.cachedHashCode = 0;860}861862/**863* Write out the default serializable data, after ensuring the864* <code>zoneStrings</code> field is initialized in order to make865* sure the backward compatibility.866*867* @since 1.6868*/869private void writeObject(ObjectOutputStream stream) throws IOException {870if (zoneStrings == null) {871zoneStrings = TimeZoneNameUtility.getZoneStrings(locale);872}873stream.defaultWriteObject();874}875}876877878