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