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