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