1 /*
   2  * Copyright (c) 1996, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
  28  * (C) Copyright IBM Corp. 1996 - All Rights Reserved
  29  *
  30  *   The original version of this source code and documentation is copyrighted
  31  * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
  32  * materials are provided under terms of a License Agreement between Taligent
  33  * and Sun. This technology is protected by multiple US and International
  34  * patents. This notice and attribution to Taligent may not be removed.
  35  *   Taligent is a registered trademark of Taligent, Inc.
  36  *
  37  */
  38 
  39 package java.text;
  40 
  41 import java.io.IOException;
  42 import java.io.ObjectOutputStream;
  43 import java.io.Serializable;
  44 import java.lang.ref.SoftReference;
  45 import java.text.spi.DateFormatSymbolsProvider;
  46 import java.util.Arrays;
  47 import java.util.Locale;
  48 import java.util.Objects;
  49 import java.util.ResourceBundle;
  50 import java.util.concurrent.ConcurrentHashMap;
  51 import java.util.concurrent.ConcurrentMap;
  52 import sun.util.locale.provider.CalendarDataUtility;
  53 import sun.util.locale.provider.LocaleProviderAdapter;
  54 import sun.util.locale.provider.LocaleServiceProviderPool;
  55 import sun.util.locale.provider.ResourceBundleBasedAdapter;
  56 import sun.util.locale.provider.TimeZoneNameUtility;
  57 
  58 /**
  59  * <code>DateFormatSymbols</code> is a public class for encapsulating
  60  * localizable date-time formatting data, such as the names of the
  61  * months, the names of the days of the week, and the time zone data.
  62  * <code>SimpleDateFormat</code> uses
  63  * <code>DateFormatSymbols</code> to encapsulate this information.
  64  *
  65  * <p>
  66  * Typically you shouldn't use <code>DateFormatSymbols</code> directly.
  67  * Rather, you are encouraged to create a date-time formatter with the
  68  * <code>DateFormat</code> class's factory methods: <code>getTimeInstance</code>,
  69  * <code>getDateInstance</code>, or <code>getDateTimeInstance</code>.
  70  * These methods automatically create a <code>DateFormatSymbols</code> for
  71  * the formatter so that you don't have to. After the
  72  * formatter is created, you may modify its format pattern using the
  73  * <code>setPattern</code> method. For more information about
  74  * creating formatters using <code>DateFormat</code>'s factory methods,
  75  * see {@link DateFormat}.
  76  *
  77  * <p>
  78  * If you decide to create a date-time formatter with a specific
  79  * format pattern for a specific locale, you can do so with:
  80  * <blockquote>
  81  * <pre>
  82  * new SimpleDateFormat(aPattern, DateFormatSymbols.getInstance(aLocale)).
  83  * </pre>
  84  * </blockquote>
  85  *
  86  * <p>If the locale contains "rg" (region override)
  87  * <a href="../util/Locale.html#def_locale_extension">Unicode extension</a>,
  88  * the symbols are overridden for the designated region.
  89  *
  90  * <p>
  91  * <code>DateFormatSymbols</code> objects are cloneable. When you obtain
  92  * a <code>DateFormatSymbols</code> object, feel free to modify the
  93  * date-time formatting data. For instance, you can replace the localized
  94  * date-time format pattern characters with the ones that you feel easy
  95  * to remember. Or you can change the representative cities
  96  * to your favorite ones.
  97  *
  98  * <p>
  99  * New <code>DateFormatSymbols</code> subclasses may be added to support
 100  * <code>SimpleDateFormat</code> for date-time formatting for additional locales.
 101 
 102  * @see          DateFormat
 103  * @see          SimpleDateFormat
 104  * @see          java.util.SimpleTimeZone
 105  * @author       Chen-Lieh Huang
 106  * @since 1.1
 107  */
 108 public class DateFormatSymbols implements Serializable, Cloneable {
 109 
 110     /**
 111      * Construct a DateFormatSymbols object by loading format data from
 112      * resources for the default {@link java.util.Locale.Category#FORMAT FORMAT}
 113      * locale. This constructor can only
 114      * construct instances for the locales supported by the Java
 115      * runtime environment, not for those supported by installed
 116      * {@link java.text.spi.DateFormatSymbolsProvider DateFormatSymbolsProvider}
 117      * implementations. For full locale coverage, use the
 118      * {@link #getInstance(Locale) getInstance} method.
 119      * <p>This is equivalent to calling
 120      * {@link #DateFormatSymbols(Locale)
 121      *     DateFormatSymbols(Locale.getDefault(Locale.Category.FORMAT))}.
 122      * @see #getInstance()
 123      * @see java.util.Locale#getDefault(java.util.Locale.Category)
 124      * @see java.util.Locale.Category#FORMAT
 125      * @exception  java.util.MissingResourceException
 126      *             if the resources for the default locale cannot be
 127      *             found or cannot be loaded.
 128      */
 129     public DateFormatSymbols()
 130     {
 131         initializeData(Locale.getDefault(Locale.Category.FORMAT));
 132     }
 133 
 134     /**
 135      * Construct a DateFormatSymbols object by loading format data from
 136      * resources for the given locale. This constructor can only
 137      * construct instances for the locales supported by the Java
 138      * runtime environment, not for those supported by installed
 139      * {@link java.text.spi.DateFormatSymbolsProvider DateFormatSymbolsProvider}
 140      * implementations. For full locale coverage, use the
 141      * {@link #getInstance(Locale) getInstance} method.
 142      *
 143      * @param locale the desired locale
 144      * @see #getInstance(Locale)
 145      * @exception  java.util.MissingResourceException
 146      *             if the resources for the specified locale cannot be
 147      *             found or cannot be loaded.
 148      */
 149     public DateFormatSymbols(Locale locale)
 150     {
 151         initializeData(locale);
 152     }
 153 
 154     /**
 155      * Constructs an uninitialized DateFormatSymbols.
 156      */
 157     private DateFormatSymbols(boolean flag) {
 158     }
 159 
 160     /**
 161      * Era strings. For example: "AD" and "BC".  An array of 2 strings,
 162      * indexed by <code>Calendar.BC</code> and <code>Calendar.AD</code>.
 163      * @serial
 164      */
 165     String eras[] = null;
 166 
 167     /**
 168      * Month strings. For example: "January", "February", etc.  An array
 169      * of 13 strings (some calendars have 13 months), indexed by
 170      * <code>Calendar.JANUARY</code>, <code>Calendar.FEBRUARY</code>, etc.
 171      * @serial
 172      */
 173     String months[] = null;
 174 
 175     /**
 176      * Short month strings. For example: "Jan", "Feb", etc.  An array of
 177      * 13 strings (some calendars have 13 months), indexed by
 178      * <code>Calendar.JANUARY</code>, <code>Calendar.FEBRUARY</code>, etc.
 179 
 180      * @serial
 181      */
 182     String shortMonths[] = null;
 183 
 184     /**
 185      * Weekday strings. For example: "Sunday", "Monday", etc.  An array
 186      * of 8 strings, indexed by <code>Calendar.SUNDAY</code>,
 187      * <code>Calendar.MONDAY</code>, etc.
 188      * The element <code>weekdays[0]</code> is ignored.
 189      * @serial
 190      */
 191     String weekdays[] = null;
 192 
 193     /**
 194      * Short weekday strings. For example: "Sun", "Mon", etc.  An array
 195      * of 8 strings, indexed by <code>Calendar.SUNDAY</code>,
 196      * <code>Calendar.MONDAY</code>, etc.
 197      * The element <code>shortWeekdays[0]</code> is ignored.
 198      * @serial
 199      */
 200     String shortWeekdays[] = null;
 201 
 202     /**
 203      * AM and PM strings. For example: "AM" and "PM".  An array of
 204      * 2 strings, indexed by <code>Calendar.AM</code> and
 205      * <code>Calendar.PM</code>.
 206      * @serial
 207      */
 208     String ampms[] = null;
 209 
 210     /**
 211      * Localized names of time zones in this locale.  This is a
 212      * two-dimensional array of strings of size <em>n</em> by <em>m</em>,
 213      * where <em>m</em> is at least 5.  Each of the <em>n</em> rows is an
 214      * entry containing the localized names for a single <code>TimeZone</code>.
 215      * Each such row contains (with <code>i</code> ranging from
 216      * 0..<em>n</em>-1):
 217      * <ul>
 218      * <li><code>zoneStrings[i][0]</code> - time zone ID</li>
 219      * <li><code>zoneStrings[i][1]</code> - long name of zone in standard
 220      * time</li>
 221      * <li><code>zoneStrings[i][2]</code> - short name of zone in
 222      * standard time</li>
 223      * <li><code>zoneStrings[i][3]</code> - long name of zone in daylight
 224      * saving time</li>
 225      * <li><code>zoneStrings[i][4]</code> - short name of zone in daylight
 226      * saving time</li>
 227      * </ul>
 228      * The zone ID is <em>not</em> localized; it's one of the valid IDs of
 229      * the {@link java.util.TimeZone TimeZone} class that are not
 230      * <a href="../util/TimeZone.html#CustomID">custom IDs</a>.
 231      * All other entries are localized names.
 232      * @see java.util.TimeZone
 233      * @serial
 234      */
 235     String zoneStrings[][] = null;
 236 
 237     /**
 238      * Indicates that zoneStrings is set externally with setZoneStrings() method.
 239      */
 240     transient boolean isZoneStringsSet = false;
 241 
 242     /**
 243      * Unlocalized date-time pattern characters. For example: 'y', 'd', etc.
 244      * All locales use the same these unlocalized pattern characters.
 245      */
 246     static final String  patternChars = "GyMdkHmsSEDFwWahKzZYuXL";
 247 
 248     static final int PATTERN_ERA                  =  0; // G
 249     static final int PATTERN_YEAR                 =  1; // y
 250     static final int PATTERN_MONTH                =  2; // M
 251     static final int PATTERN_DAY_OF_MONTH         =  3; // d
 252     static final int PATTERN_HOUR_OF_DAY1         =  4; // k
 253     static final int PATTERN_HOUR_OF_DAY0         =  5; // H
 254     static final int PATTERN_MINUTE               =  6; // m
 255     static final int PATTERN_SECOND               =  7; // s
 256     static final int PATTERN_MILLISECOND          =  8; // S
 257     static final int PATTERN_DAY_OF_WEEK          =  9; // E
 258     static final int PATTERN_DAY_OF_YEAR          = 10; // D
 259     static final int PATTERN_DAY_OF_WEEK_IN_MONTH = 11; // F
 260     static final int PATTERN_WEEK_OF_YEAR         = 12; // w
 261     static final int PATTERN_WEEK_OF_MONTH        = 13; // W
 262     static final int PATTERN_AM_PM                = 14; // a
 263     static final int PATTERN_HOUR1                = 15; // h
 264     static final int PATTERN_HOUR0                = 16; // K
 265     static final int PATTERN_ZONE_NAME            = 17; // z
 266     static final int PATTERN_ZONE_VALUE           = 18; // Z
 267     static final int PATTERN_WEEK_YEAR            = 19; // Y
 268     static final int PATTERN_ISO_DAY_OF_WEEK      = 20; // u
 269     static final int PATTERN_ISO_ZONE             = 21; // X
 270     static final int PATTERN_MONTH_STANDALONE     = 22; // L
 271 
 272     /**
 273      * Localized date-time pattern characters. For example, a locale may
 274      * wish to use 'u' rather than 'y' to represent years in its date format
 275      * pattern strings.
 276      * This string must be exactly 18 characters long, with the index of
 277      * the characters described by <code>DateFormat.ERA_FIELD</code>,
 278      * <code>DateFormat.YEAR_FIELD</code>, etc.  Thus, if the string were
 279      * "Xz...", then localized patterns would use 'X' for era and 'z' for year.
 280      * @serial
 281      */
 282     String  localPatternChars = null;
 283 
 284     /**
 285      * The locale which is used for initializing this DateFormatSymbols object.
 286      *
 287      * @since 1.6
 288      * @serial
 289      */
 290     Locale locale = null;
 291 
 292     /* use serialVersionUID from JDK 1.1.4 for interoperability */
 293     static final long serialVersionUID = -5987973545549424702L;
 294 
 295     /**
 296      * Returns an array of all locales for which the
 297      * <code>getInstance</code> methods of this class can return
 298      * localized instances.
 299      * The returned array represents the union of locales supported by the
 300      * Java runtime and by installed
 301      * {@link java.text.spi.DateFormatSymbolsProvider DateFormatSymbolsProvider}
 302      * implementations.  It must contain at least a <code>Locale</code>
 303      * instance equal to {@link java.util.Locale#US Locale.US}.
 304      *
 305      * @return An array of locales for which localized
 306      *         <code>DateFormatSymbols</code> instances are available.
 307      * @since 1.6
 308      */
 309     public static Locale[] getAvailableLocales() {
 310         LocaleServiceProviderPool pool=
 311             LocaleServiceProviderPool.getPool(DateFormatSymbolsProvider.class);
 312         return pool.getAvailableLocales();
 313     }
 314 
 315     /**
 316      * Gets the <code>DateFormatSymbols</code> instance for the default
 317      * locale.  This method provides access to <code>DateFormatSymbols</code>
 318      * instances for locales supported by the Java runtime itself as well
 319      * as for those supported by installed
 320      * {@link java.text.spi.DateFormatSymbolsProvider DateFormatSymbolsProvider}
 321      * implementations.
 322      * <p>This is equivalent to calling {@link #getInstance(Locale)
 323      *     getInstance(Locale.getDefault(Locale.Category.FORMAT))}.
 324      * @see java.util.Locale#getDefault(java.util.Locale.Category)
 325      * @see java.util.Locale.Category#FORMAT
 326      * @return a <code>DateFormatSymbols</code> instance.
 327      * @since 1.6
 328      */
 329     public static final DateFormatSymbols getInstance() {
 330         return getInstance(Locale.getDefault(Locale.Category.FORMAT));
 331     }
 332 
 333     /**
 334      * Gets the <code>DateFormatSymbols</code> instance for the specified
 335      * locale.  This method provides access to <code>DateFormatSymbols</code>
 336      * instances for locales supported by the Java runtime itself as well
 337      * as for those supported by installed
 338      * {@link java.text.spi.DateFormatSymbolsProvider DateFormatSymbolsProvider}
 339      * implementations.
 340      * @param locale the given locale.
 341      * @return a <code>DateFormatSymbols</code> instance.
 342      * @exception NullPointerException if <code>locale</code> is null
 343      * @since 1.6
 344      */
 345     public static final DateFormatSymbols getInstance(Locale locale) {
 346         DateFormatSymbols dfs = getProviderInstance(locale);
 347         if (dfs != null) {
 348             return dfs;
 349         }
 350         throw new RuntimeException("DateFormatSymbols instance creation failed.");
 351     }
 352 
 353     /**
 354      * Returns a DateFormatSymbols provided by a provider or found in
 355      * the cache. Note that this method returns a cached instance,
 356      * not its clone. Therefore, the instance should never be given to
 357      * an application.
 358      */
 359     static final DateFormatSymbols getInstanceRef(Locale locale) {
 360         DateFormatSymbols dfs = getProviderInstance(locale);
 361         if (dfs != null) {
 362             return dfs;
 363         }
 364         throw new RuntimeException("DateFormatSymbols instance creation failed.");
 365     }
 366 
 367     private static DateFormatSymbols getProviderInstance(Locale locale) {
 368         LocaleProviderAdapter adapter = LocaleProviderAdapter.getAdapter(DateFormatSymbolsProvider.class, locale);
 369         DateFormatSymbolsProvider provider = adapter.getDateFormatSymbolsProvider();
 370         DateFormatSymbols dfsyms = provider.getInstance(locale);
 371         if (dfsyms == null) {
 372             provider = LocaleProviderAdapter.forJRE().getDateFormatSymbolsProvider();
 373             dfsyms = provider.getInstance(locale);
 374         }
 375         return dfsyms;
 376     }
 377 
 378     /**
 379      * Gets era strings. For example: "AD" and "BC".
 380      * @return the era strings.
 381      */
 382     public String[] getEras() {
 383         return Arrays.copyOf(eras, eras.length);
 384     }
 385 
 386     /**
 387      * Sets era strings. For example: "AD" and "BC".
 388      * @param newEras the new era strings.
 389      */
 390     public void setEras(String[] newEras) {
 391         eras = Arrays.copyOf(newEras, newEras.length);
 392         cachedHashCode = 0;
 393     }
 394 
 395     /**
 396      * Gets month strings. For example: "January", "February", etc.
 397      *
 398      * <p>If the language requires different forms for formatting and
 399      * stand-alone usages, this method returns month names in the
 400      * formatting form. For example, the preferred month name for
 401      * January in the Czech language is <em>ledna</em> in the
 402      * formatting form, while it is <em>leden</em> in the stand-alone
 403      * form. This method returns {@code "ledna"} in this case. Refer
 404      * to the <a href="http://unicode.org/reports/tr35/#Calendar_Elements">
 405      * Calendar Elements in the Unicode Locale Data Markup Language
 406      * (LDML) specification</a> for more details.
 407      *
 408      * @return the month strings. Use
 409      * {@link java.util.Calendar#JANUARY Calendar.JANUARY},
 410      * {@link java.util.Calendar#FEBRUARY Calendar.FEBRUARY},
 411      * etc. to index the result array.
 412      */
 413     public String[] getMonths() {
 414         return Arrays.copyOf(months, months.length);
 415     }
 416 
 417     /**
 418      * Sets month strings. For example: "January", "February", etc.
 419      * @param newMonths the new month strings. The array should
 420      * be indexed by {@link java.util.Calendar#JANUARY Calendar.JANUARY},
 421      * {@link java.util.Calendar#FEBRUARY Calendar.FEBRUARY}, etc.
 422      */
 423     public void setMonths(String[] newMonths) {
 424         months = Arrays.copyOf(newMonths, newMonths.length);
 425         cachedHashCode = 0;
 426     }
 427 
 428     /**
 429      * Gets short month strings. For example: "Jan", "Feb", etc.
 430      *
 431      * <p>If the language requires different forms for formatting and
 432      * stand-alone usages, this method returns short month names in
 433      * the formatting form. For example, the preferred abbreviation
 434      * for January in the Catalan language is <em>de gen.</em> in the
 435      * formatting form, while it is <em>gen.</em> in the stand-alone
 436      * form. This method returns {@code "de gen."} in this case. Refer
 437      * to the <a href="http://unicode.org/reports/tr35/#Calendar_Elements">
 438      * Calendar Elements in the Unicode Locale Data Markup Language
 439      * (LDML) specification</a> for more details.
 440      *
 441      * @return the short month strings. Use
 442      * {@link java.util.Calendar#JANUARY Calendar.JANUARY},
 443      * {@link java.util.Calendar#FEBRUARY Calendar.FEBRUARY},
 444      * etc. to index the result array.
 445      */
 446     public String[] getShortMonths() {
 447         return Arrays.copyOf(shortMonths, shortMonths.length);
 448     }
 449 
 450     /**
 451      * Sets short month strings. For example: "Jan", "Feb", etc.
 452      * @param newShortMonths the new short month strings. The array should
 453      * be indexed by {@link java.util.Calendar#JANUARY Calendar.JANUARY},
 454      * {@link java.util.Calendar#FEBRUARY Calendar.FEBRUARY}, etc.
 455      */
 456     public void setShortMonths(String[] newShortMonths) {
 457         shortMonths = Arrays.copyOf(newShortMonths, newShortMonths.length);
 458         cachedHashCode = 0;
 459     }
 460 
 461     /**
 462      * Gets weekday strings. For example: "Sunday", "Monday", etc.
 463      * @return the weekday strings. Use
 464      * {@link java.util.Calendar#SUNDAY Calendar.SUNDAY},
 465      * {@link java.util.Calendar#MONDAY Calendar.MONDAY}, etc. to index
 466      * the result array.
 467      */
 468     public String[] getWeekdays() {
 469         return Arrays.copyOf(weekdays, weekdays.length);
 470     }
 471 
 472     /**
 473      * Sets weekday strings. For example: "Sunday", "Monday", etc.
 474      * @param newWeekdays the new weekday strings. The array should
 475      * be indexed by {@link java.util.Calendar#SUNDAY Calendar.SUNDAY},
 476      * {@link java.util.Calendar#MONDAY Calendar.MONDAY}, etc.
 477      */
 478     public void setWeekdays(String[] newWeekdays) {
 479         weekdays = Arrays.copyOf(newWeekdays, newWeekdays.length);
 480         cachedHashCode = 0;
 481     }
 482 
 483     /**
 484      * Gets short weekday strings. For example: "Sun", "Mon", etc.
 485      * @return the short weekday strings. Use
 486      * {@link java.util.Calendar#SUNDAY Calendar.SUNDAY},
 487      * {@link java.util.Calendar#MONDAY Calendar.MONDAY}, etc. to index
 488      * the result array.
 489      */
 490     public String[] getShortWeekdays() {
 491         return Arrays.copyOf(shortWeekdays, shortWeekdays.length);
 492     }
 493 
 494     /**
 495      * Sets short weekday strings. For example: "Sun", "Mon", etc.
 496      * @param newShortWeekdays the new short weekday strings. The array should
 497      * be indexed by {@link java.util.Calendar#SUNDAY Calendar.SUNDAY},
 498      * {@link java.util.Calendar#MONDAY Calendar.MONDAY}, etc.
 499      */
 500     public void setShortWeekdays(String[] newShortWeekdays) {
 501         shortWeekdays = Arrays.copyOf(newShortWeekdays, newShortWeekdays.length);
 502         cachedHashCode = 0;
 503     }
 504 
 505     /**
 506      * Gets ampm strings. For example: "AM" and "PM".
 507      * @return the ampm strings.
 508      */
 509     public String[] getAmPmStrings() {
 510         return Arrays.copyOf(ampms, ampms.length);
 511     }
 512 
 513     /**
 514      * Sets ampm strings. For example: "AM" and "PM".
 515      * @param newAmpms the new ampm strings.
 516      */
 517     public void setAmPmStrings(String[] newAmpms) {
 518         ampms = Arrays.copyOf(newAmpms, newAmpms.length);
 519         cachedHashCode = 0;
 520     }
 521 
 522     /**
 523      * Gets time zone strings.  Use of this method is discouraged; use
 524      * {@link java.util.TimeZone#getDisplayName() TimeZone.getDisplayName()}
 525      * instead.
 526      * <p>
 527      * The value returned is a
 528      * two-dimensional array of strings of size <em>n</em> by <em>m</em>,
 529      * where <em>m</em> is at least 5.  Each of the <em>n</em> rows is an
 530      * entry containing the localized names for a single <code>TimeZone</code>.
 531      * Each such row contains (with <code>i</code> ranging from
 532      * 0..<em>n</em>-1):
 533      * <ul>
 534      * <li><code>zoneStrings[i][0]</code> - time zone ID</li>
 535      * <li><code>zoneStrings[i][1]</code> - long name of zone in standard
 536      * time</li>
 537      * <li><code>zoneStrings[i][2]</code> - short name of zone in
 538      * standard time</li>
 539      * <li><code>zoneStrings[i][3]</code> - long name of zone in daylight
 540      * saving time</li>
 541      * <li><code>zoneStrings[i][4]</code> - short name of zone in daylight
 542      * saving time</li>
 543      * </ul>
 544      * The zone ID is <em>not</em> localized; it's one of the valid IDs of
 545      * the {@link java.util.TimeZone TimeZone} class that are not
 546      * <a href="../util/TimeZone.html#CustomID">custom IDs</a>.
 547      * All other entries are localized names.  If a zone does not implement
 548      * daylight saving time, the daylight saving time names should not be used.
 549      * <p>
 550      * If {@link #setZoneStrings(String[][]) setZoneStrings} has been called
 551      * on this <code>DateFormatSymbols</code> instance, then the strings
 552      * provided by that call are returned. Otherwise, the returned array
 553      * contains names provided by the Java runtime and by installed
 554      * {@link java.util.spi.TimeZoneNameProvider TimeZoneNameProvider}
 555      * implementations.
 556      *
 557      * @return the time zone strings.
 558      * @see #setZoneStrings(String[][])
 559      */
 560     public String[][] getZoneStrings() {
 561         return getZoneStringsImpl(true);
 562     }
 563 
 564     /**
 565      * Sets time zone strings.  The argument must be a
 566      * two-dimensional array of strings of size <em>n</em> by <em>m</em>,
 567      * where <em>m</em> is at least 5.  Each of the <em>n</em> rows is an
 568      * entry containing the localized names for a single <code>TimeZone</code>.
 569      * Each such row contains (with <code>i</code> ranging from
 570      * 0..<em>n</em>-1):
 571      * <ul>
 572      * <li><code>zoneStrings[i][0]</code> - time zone ID</li>
 573      * <li><code>zoneStrings[i][1]</code> - long name of zone in standard
 574      * time</li>
 575      * <li><code>zoneStrings[i][2]</code> - short name of zone in
 576      * standard time</li>
 577      * <li><code>zoneStrings[i][3]</code> - long name of zone in daylight
 578      * saving time</li>
 579      * <li><code>zoneStrings[i][4]</code> - short name of zone in daylight
 580      * saving time</li>
 581      * </ul>
 582      * The zone ID is <em>not</em> localized; it's one of the valid IDs of
 583      * the {@link java.util.TimeZone TimeZone} class that are not
 584      * <a href="../util/TimeZone.html#CustomID">custom IDs</a>.
 585      * All other entries are localized names.
 586      *
 587      * @param newZoneStrings the new time zone strings.
 588      * @exception IllegalArgumentException if the length of any row in
 589      *    <code>newZoneStrings</code> is less than 5
 590      * @exception NullPointerException if <code>newZoneStrings</code> is null
 591      * @see #getZoneStrings()
 592      */
 593     public void setZoneStrings(String[][] newZoneStrings) {
 594         String[][] aCopy = new String[newZoneStrings.length][];
 595         for (int i = 0; i < newZoneStrings.length; ++i) {
 596             int len = newZoneStrings[i].length;
 597             if (len < 5) {
 598                 throw new IllegalArgumentException();
 599             }
 600             aCopy[i] = Arrays.copyOf(newZoneStrings[i], len);
 601         }
 602         zoneStrings = aCopy;
 603         isZoneStringsSet = true;
 604         cachedHashCode = 0;
 605     }
 606 
 607     /**
 608      * Gets localized date-time pattern characters. For example: 'u', 't', etc.
 609      * @return the localized date-time pattern characters.
 610      */
 611     public String getLocalPatternChars() {
 612         return localPatternChars;
 613     }
 614 
 615     /**
 616      * Sets localized date-time pattern characters. For example: 'u', 't', etc.
 617      * @param newLocalPatternChars the new localized date-time
 618      * pattern characters.
 619      */
 620     public void setLocalPatternChars(String newLocalPatternChars) {
 621         // Call toString() to throw an NPE in case the argument is null
 622         localPatternChars = newLocalPatternChars.toString();
 623         cachedHashCode = 0;
 624     }
 625 
 626     /**
 627      * Overrides Cloneable
 628      */
 629     public Object clone()
 630     {
 631         try
 632         {
 633             DateFormatSymbols other = (DateFormatSymbols)super.clone();
 634             copyMembers(this, other);
 635             return other;
 636         } catch (CloneNotSupportedException e) {
 637             throw new InternalError(e);
 638         }
 639     }
 640 
 641     /**
 642      * Override hashCode.
 643      * Generates a hash code for the DateFormatSymbols object.
 644      */
 645     @Override
 646     public int hashCode() {
 647         int hashCode = cachedHashCode;
 648         if (hashCode == 0) {
 649             hashCode = 5;
 650             hashCode = 11 * hashCode + Arrays.hashCode(eras);
 651             hashCode = 11 * hashCode + Arrays.hashCode(months);
 652             hashCode = 11 * hashCode + Arrays.hashCode(shortMonths);
 653             hashCode = 11 * hashCode + Arrays.hashCode(weekdays);
 654             hashCode = 11 * hashCode + Arrays.hashCode(shortWeekdays);
 655             hashCode = 11 * hashCode + Arrays.hashCode(ampms);
 656             hashCode = 11 * hashCode + Arrays.deepHashCode(getZoneStringsWrapper());
 657             hashCode = 11 * hashCode + Objects.hashCode(localPatternChars);
 658             if (hashCode != 0) {
 659                 cachedHashCode = hashCode;
 660             }
 661         }
 662 
 663         return hashCode;
 664     }
 665 
 666     /**
 667      * Override equals
 668      */
 669     public boolean equals(Object obj)
 670     {
 671         if (this == obj) return true;
 672         if (obj == null || getClass() != obj.getClass()) return false;
 673         DateFormatSymbols that = (DateFormatSymbols) obj;
 674         return (Arrays.equals(eras, that.eras)
 675                 && Arrays.equals(months, that.months)
 676                 && Arrays.equals(shortMonths, that.shortMonths)
 677                 && Arrays.equals(weekdays, that.weekdays)
 678                 && Arrays.equals(shortWeekdays, that.shortWeekdays)
 679                 && Arrays.equals(ampms, that.ampms)
 680                 && Arrays.deepEquals(getZoneStringsWrapper(), that.getZoneStringsWrapper())
 681                 && ((localPatternChars != null
 682                   && localPatternChars.equals(that.localPatternChars))
 683                  || (localPatternChars == null
 684                   && that.localPatternChars == null)));
 685     }
 686 
 687     // =======================privates===============================
 688 
 689     /**
 690      * Useful constant for defining time zone offsets.
 691      */
 692     static final int millisPerHour = 60*60*1000;
 693 
 694     /**
 695      * Cache to hold DateFormatSymbols instances per Locale.
 696      */
 697     private static final ConcurrentMap<Locale, SoftReference<DateFormatSymbols>> cachedInstances
 698         = new ConcurrentHashMap<>(3);
 699 
 700     private transient int lastZoneIndex;
 701 
 702     /**
 703      * Cached hash code
 704      */
 705     transient volatile int cachedHashCode;
 706 
 707     /**
 708      * Initializes this DateFormatSymbols with the locale data. This method uses
 709      * a cached DateFormatSymbols instance for the given locale if available. If
 710      * there's no cached one, this method creates an uninitialized instance and
 711      * populates its fields from the resource bundle for the locale, and caches
 712      * the instance. Note: zoneStrings isn't initialized in this method.
 713      */
 714     private void initializeData(Locale locale) {
 715         SoftReference<DateFormatSymbols> ref = cachedInstances.get(locale);
 716         DateFormatSymbols dfs;
 717         if (ref == null || (dfs = ref.get()) == null) {
 718             if (ref != null) {
 719                 // Remove the empty SoftReference
 720                 cachedInstances.remove(locale, ref);
 721             }
 722             dfs = new DateFormatSymbols(false);
 723 
 724             // check for region override
 725             Locale override = CalendarDataUtility.findRegionOverride(locale);
 726 
 727             // Initialize the fields from the ResourceBundle for locale.
 728             LocaleProviderAdapter adapter
 729                 = LocaleProviderAdapter.getAdapter(DateFormatSymbolsProvider.class, override);
 730             // Avoid any potential recursions
 731             if (!(adapter instanceof ResourceBundleBasedAdapter)) {
 732                 adapter = LocaleProviderAdapter.getResourceBundleBased();
 733             }
 734             ResourceBundle resource
 735                 = ((ResourceBundleBasedAdapter)adapter).getLocaleData().getDateFormatData(override);
 736 
 737             dfs.locale = locale;
 738             // JRE and CLDR use different keys
 739             // JRE: Eras, short.Eras and narrow.Eras
 740             // CLDR: long.Eras, Eras and narrow.Eras
 741             if (resource.containsKey("Eras")) {
 742                 dfs.eras = resource.getStringArray("Eras");
 743             } else if (resource.containsKey("long.Eras")) {
 744                 dfs.eras = resource.getStringArray("long.Eras");
 745             } else if (resource.containsKey("short.Eras")) {
 746                 dfs.eras = resource.getStringArray("short.Eras");
 747             }
 748             dfs.months = resource.getStringArray("MonthNames");
 749             dfs.shortMonths = resource.getStringArray("MonthAbbreviations");
 750             dfs.ampms = resource.getStringArray("AmPmMarkers");
 751             dfs.localPatternChars = resource.getString("DateTimePatternChars");
 752 
 753             // Day of week names are stored in a 1-based array.
 754             dfs.weekdays = toOneBasedArray(resource.getStringArray("DayNames"));
 755             dfs.shortWeekdays = toOneBasedArray(resource.getStringArray("DayAbbreviations"));
 756 
 757             // Put dfs in the cache
 758             ref = new SoftReference<>(dfs);
 759             SoftReference<DateFormatSymbols> x = cachedInstances.putIfAbsent(locale, ref);
 760             if (x != null) {
 761                 DateFormatSymbols y = x.get();
 762                 if (y == null) {
 763                     // Replace the empty SoftReference with ref.
 764                     cachedInstances.replace(locale, x, ref);
 765                 } else {
 766                     ref = x;
 767                     dfs = y;
 768                 }
 769             }
 770         }
 771 
 772         // Copy the field values from dfs to this instance.
 773         copyMembers(dfs, this);
 774     }
 775 
 776     private static String[] toOneBasedArray(String[] src) {
 777         int len = src.length;
 778         String[] dst = new String[len + 1];
 779         dst[0] = "";
 780         for (int i = 0; i < len; i++) {
 781             dst[i + 1] = src[i];
 782         }
 783         return dst;
 784     }
 785 
 786     /**
 787      * Package private: used by SimpleDateFormat
 788      * Gets the index for the given time zone ID to obtain the time zone
 789      * strings for formatting. The time zone ID is just for programmatic
 790      * lookup. NOT LOCALIZED!!!
 791      * @param ID the given time zone ID.
 792      * @return the index of the given time zone ID.  Returns -1 if
 793      * the given time zone ID can't be located in the DateFormatSymbols object.
 794      * @see java.util.SimpleTimeZone
 795      */
 796     final int getZoneIndex(String ID) {
 797         String[][] zoneStrings = getZoneStringsWrapper();
 798 
 799         /*
 800          * getZoneIndex has been re-written for performance reasons. instead of
 801          * traversing the zoneStrings array every time, we cache the last used zone
 802          * index
 803          */
 804         if (lastZoneIndex < zoneStrings.length && ID.equals(zoneStrings[lastZoneIndex][0])) {
 805             return lastZoneIndex;
 806         }
 807 
 808         /* slow path, search entire list */
 809         for (int index = 0; index < zoneStrings.length; index++) {
 810             if (ID.equals(zoneStrings[index][0])) {
 811                 lastZoneIndex = index;
 812                 return index;
 813             }
 814         }
 815 
 816         return -1;
 817     }
 818 
 819     /**
 820      * Wrapper method to the getZoneStrings(), which is called from inside
 821      * the java.text package and not to mutate the returned arrays, so that
 822      * it does not need to create a defensive copy.
 823      */
 824     final String[][] getZoneStringsWrapper() {
 825         if (isSubclassObject()) {
 826             return getZoneStrings();
 827         } else {
 828             return getZoneStringsImpl(false);
 829         }
 830     }
 831 
 832     private String[][] getZoneStringsImpl(boolean needsCopy) {
 833         if (zoneStrings == null) {
 834             zoneStrings = TimeZoneNameUtility.getZoneStrings(locale);
 835         }
 836 
 837         if (!needsCopy) {
 838             return zoneStrings;
 839         }
 840 
 841         int len = zoneStrings.length;
 842         String[][] aCopy = new String[len][];
 843         for (int i = 0; i < len; i++) {
 844             aCopy[i] = Arrays.copyOf(zoneStrings[i], zoneStrings[i].length);
 845         }
 846         return aCopy;
 847     }
 848 
 849     private boolean isSubclassObject() {
 850         return !getClass().getName().equals("java.text.DateFormatSymbols");
 851     }
 852 
 853     /**
 854      * Clones all the data members from the source DateFormatSymbols to
 855      * the target DateFormatSymbols.
 856      *
 857      * @param src the source DateFormatSymbols.
 858      * @param dst the target DateFormatSymbols.
 859      */
 860     private void copyMembers(DateFormatSymbols src, DateFormatSymbols dst)
 861     {
 862         dst.locale = src.locale;
 863         dst.eras = Arrays.copyOf(src.eras, src.eras.length);
 864         dst.months = Arrays.copyOf(src.months, src.months.length);
 865         dst.shortMonths = Arrays.copyOf(src.shortMonths, src.shortMonths.length);
 866         dst.weekdays = Arrays.copyOf(src.weekdays, src.weekdays.length);
 867         dst.shortWeekdays = Arrays.copyOf(src.shortWeekdays, src.shortWeekdays.length);
 868         dst.ampms = Arrays.copyOf(src.ampms, src.ampms.length);
 869         if (src.zoneStrings != null) {
 870             dst.zoneStrings = src.getZoneStringsImpl(true);
 871         } else {
 872             dst.zoneStrings = null;
 873         }
 874         dst.localPatternChars = src.localPatternChars;
 875         dst.cachedHashCode = 0;
 876     }
 877 
 878     /**
 879      * Write out the default serializable data, after ensuring the
 880      * <code>zoneStrings</code> field is initialized in order to make
 881      * sure the backward compatibility.
 882      *
 883      * @since 1.6
 884      */
 885     private void writeObject(ObjectOutputStream stream) throws IOException {
 886         if (zoneStrings == null) {
 887             zoneStrings = TimeZoneNameUtility.getZoneStrings(locale);
 888         }
 889         stream.defaultWriteObject();
 890     }
 891 }