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