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 import sun.util.resources.LocaleData; 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 return provider.getInstance(locale); 349 } 350 351 /** 352 * Gets era strings. For example: "AD" and "BC". 353 * @return the era strings. 354 */ 355 public String[] getEras() { 356 return Arrays.copyOf(eras, eras.length); 357 } 358 359 /** 360 * Sets era strings. For example: "AD" and "BC". 361 * @param newEras the new era strings. 362 */ 363 public void setEras(String[] newEras) { 364 eras = Arrays.copyOf(newEras, newEras.length); 365 } 366 367 /** 368 * Gets month strings. For example: "January", "February", etc. 369 * 370 * <p>If the language requires different forms for formatting and 371 * stand-alone usages, this method returns month names in the 372 * formatting form. For example, the preferred month name for 373 * January in the Czech language is <em>ledna</em> in the 374 * formatting form, while it is <em>leden</em> in the stand-alone 375 * form. This method returns {@code "ledna"} in this case. Refer 376 * to the <a href="http://unicode.org/reports/tr35/#Calendar_Elements"> 377 * Calendar Elements in the Unicode Locale Data Markup Language 378 * (LDML) specification</a> for more details. 379 * 380 * @return the month strings. 381 */ 382 public String[] getMonths() { 383 return Arrays.copyOf(months, months.length); 384 } 385 386 /** 387 * Sets month strings. For example: "January", "February", etc. 388 * @param newMonths the new month strings. 389 */ 390 public void setMonths(String[] newMonths) { 391 months = Arrays.copyOf(newMonths, newMonths.length); 392 } 393 394 /** 395 * Gets short month strings. For example: "Jan", "Feb", etc. 396 * 397 * <p>If the language requires different forms for formatting and 398 * stand-alone usages, This method returns short month names in 399 * the formatting form. For example, the preferred abbreviation 400 * for January in the Catalan language is <em>de gen.</em> in the 401 * formatting form, while it is <em>gen.</em> in the stand-alone 402 * form. This method returns {@code "de gen."} in this case. Refer 403 * to the <a href="http://unicode.org/reports/tr35/#Calendar_Elements"> 404 * Calendar Elements in the Unicode Locale Data Markup Language 405 * (LDML) specification</a> for more details. 406 * 407 * @return the short month strings. 408 */ 409 public String[] getShortMonths() { 410 return Arrays.copyOf(shortMonths, shortMonths.length); 411 } 412 413 /** 414 * Sets short month strings. For example: "Jan", "Feb", etc. 415 * @param newShortMonths the new short month strings. 416 */ 417 public void setShortMonths(String[] newShortMonths) { 418 shortMonths = Arrays.copyOf(newShortMonths, newShortMonths.length); 419 } 420 421 /** 422 * Gets weekday strings. For example: "Sunday", "Monday", etc. 423 * @return the weekday strings. Use <code>Calendar.SUNDAY</code>, 424 * <code>Calendar.MONDAY</code>, etc. to index the result array. 425 */ 426 public String[] getWeekdays() { 427 return Arrays.copyOf(weekdays, weekdays.length); 428 } 429 430 /** 431 * Sets weekday strings. For example: "Sunday", "Monday", etc. 432 * @param newWeekdays the new weekday strings. The array should 433 * be indexed by <code>Calendar.SUNDAY</code>, 434 * <code>Calendar.MONDAY</code>, etc. 435 */ 436 public void setWeekdays(String[] newWeekdays) { 437 weekdays = Arrays.copyOf(newWeekdays, newWeekdays.length); 438 } 439 440 /** 441 * Gets short weekday strings. For example: "Sun", "Mon", etc. 442 * @return the short weekday strings. Use <code>Calendar.SUNDAY</code>, 443 * <code>Calendar.MONDAY</code>, etc. to index the result array. 444 */ 445 public String[] getShortWeekdays() { 446 return Arrays.copyOf(shortWeekdays, shortWeekdays.length); 447 } 448 449 /** 450 * Sets short weekday strings. For example: "Sun", "Mon", etc. 451 * @param newShortWeekdays the new short weekday strings. The array should 452 * be indexed by <code>Calendar.SUNDAY</code>, 453 * <code>Calendar.MONDAY</code>, etc. 454 */ 455 public void setShortWeekdays(String[] newShortWeekdays) { 456 shortWeekdays = Arrays.copyOf(newShortWeekdays, newShortWeekdays.length); 457 } 458 459 /** 460 * Gets ampm strings. For example: "AM" and "PM". 461 * @return the ampm strings. 462 */ 463 public String[] getAmPmStrings() { 464 return Arrays.copyOf(ampms, ampms.length); 465 } 466 467 /** 468 * Sets ampm strings. For example: "AM" and "PM". 469 * @param newAmpms the new ampm strings. 470 */ 471 public void setAmPmStrings(String[] newAmpms) { 472 ampms = Arrays.copyOf(newAmpms, newAmpms.length); 473 } 474 475 /** 476 * Gets time zone strings. Use of this method is discouraged; use 477 * {@link java.util.TimeZone#getDisplayName() TimeZone.getDisplayName()} 478 * instead. 479 * <p> 480 * The value returned is a 481 * two-dimensional array of strings of size <em>n</em> by <em>m</em>, 482 * where <em>m</em> is at least 5. Each of the <em>n</em> rows is an 483 * entry containing the localized names for a single <code>TimeZone</code>. 484 * Each such row contains (with <code>i</code> ranging from 485 * 0..<em>n</em>-1): 486 * <ul> 487 * <li><code>zoneStrings[i][0]</code> - time zone ID</li> 488 * <li><code>zoneStrings[i][1]</code> - long name of zone in standard 489 * time</li> 490 * <li><code>zoneStrings[i][2]</code> - short name of zone in 491 * standard time</li> 492 * <li><code>zoneStrings[i][3]</code> - long name of zone in daylight 493 * saving time</li> 494 * <li><code>zoneStrings[i][4]</code> - short name of zone in daylight 495 * saving time</li> 496 * </ul> 497 * The zone ID is <em>not</em> localized; it's one of the valid IDs of 498 * the {@link java.util.TimeZone TimeZone} class that are not 499 * <a href="../util/TimeZone.html#CustomID">custom IDs</a>. 500 * All other entries are localized names. If a zone does not implement 501 * daylight saving time, the daylight saving time names should not be used. 502 * <p> 503 * If {@link #setZoneStrings(String[][]) setZoneStrings} has been called 504 * on this <code>DateFormatSymbols</code> instance, then the strings 505 * provided by that call are returned. Otherwise, the returned array 506 * contains names provided by the Java runtime and by installed 507 * {@link java.util.spi.TimeZoneNameProvider TimeZoneNameProvider} 508 * implementations. 509 * 510 * @return the time zone strings. 511 * @see #setZoneStrings(String[][]) 512 */ 513 public String[][] getZoneStrings() { 514 return getZoneStringsImpl(true); 515 } 516 517 /** 518 * Sets time zone strings. The argument must be a 519 * two-dimensional array of strings of size <em>n</em> by <em>m</em>, 520 * where <em>m</em> is at least 5. Each of the <em>n</em> rows is an 521 * entry containing the localized names for a single <code>TimeZone</code>. 522 * Each such row contains (with <code>i</code> ranging from 523 * 0..<em>n</em>-1): 524 * <ul> 525 * <li><code>zoneStrings[i][0]</code> - time zone ID</li> 526 * <li><code>zoneStrings[i][1]</code> - long name of zone in standard 527 * time</li> 528 * <li><code>zoneStrings[i][2]</code> - short name of zone in 529 * standard time</li> 530 * <li><code>zoneStrings[i][3]</code> - long name of zone in daylight 531 * saving time</li> 532 * <li><code>zoneStrings[i][4]</code> - short name of zone in daylight 533 * saving time</li> 534 * </ul> 535 * The zone ID is <em>not</em> localized; it's one of the valid IDs of 536 * the {@link java.util.TimeZone TimeZone} class that are not 537 * <a href="../util/TimeZone.html#CustomID">custom IDs</a>. 538 * All other entries are localized names. 539 * 540 * @param newZoneStrings the new time zone strings. 541 * @exception IllegalArgumentException if the length of any row in 542 * <code>newZoneStrings</code> is less than 5 543 * @exception NullPointerException if <code>newZoneStrings</code> is null 544 * @see #getZoneStrings() 545 */ 546 public void setZoneStrings(String[][] newZoneStrings) { 547 String[][] aCopy = new String[newZoneStrings.length][]; 548 for (int i = 0; i < newZoneStrings.length; ++i) { 549 int len = newZoneStrings[i].length; 550 if (len < 5) { 551 throw new IllegalArgumentException(); 552 } 553 aCopy[i] = Arrays.copyOf(newZoneStrings[i], len); 554 } 555 zoneStrings = aCopy; 556 isZoneStringsSet = true; 557 } 558 559 /** 560 * Gets localized date-time pattern characters. For example: 'u', 't', etc. 561 * @return the localized date-time pattern characters. 562 */ 563 public String getLocalPatternChars() { 564 return localPatternChars; 565 } 566 567 /** 568 * Sets localized date-time pattern characters. For example: 'u', 't', etc. 569 * @param newLocalPatternChars the new localized date-time 570 * pattern characters. 571 */ 572 public void setLocalPatternChars(String newLocalPatternChars) { 573 // Call toString() to throw an NPE in case the argument is null 574 localPatternChars = newLocalPatternChars.toString(); 575 } 576 577 /** 578 * Overrides Cloneable 579 */ 580 public Object clone() 581 { 582 try 583 { 584 DateFormatSymbols other = (DateFormatSymbols)super.clone(); 585 copyMembers(this, other); 586 return other; 587 } catch (CloneNotSupportedException e) { 588 throw new InternalError(e); 589 } 590 } 591 592 /** 593 * Override hashCode. 594 * Generates a hash code for the DateFormatSymbols object. 595 */ 596 public int hashCode() { 597 int hashcode = 0; 598 String[][] zoneStrings = getZoneStringsWrapper(); 599 for (int index = 0; index < zoneStrings[0].length; ++index) 600 hashcode ^= zoneStrings[0][index].hashCode(); 601 return hashcode; 602 } 603 604 /** 605 * Override equals 606 */ 607 public boolean equals(Object obj) 608 { 609 if (this == obj) return true; 610 if (obj == null || getClass() != obj.getClass()) return false; 611 DateFormatSymbols that = (DateFormatSymbols) obj; 612 return (Arrays.equals(eras, that.eras) 613 && Arrays.equals(months, that.months) 614 && Arrays.equals(shortMonths, that.shortMonths) 615 && Arrays.equals(weekdays, that.weekdays) 616 && Arrays.equals(shortWeekdays, that.shortWeekdays) 617 && Arrays.equals(ampms, that.ampms) 618 && Arrays.deepEquals(getZoneStringsWrapper(), that.getZoneStringsWrapper()) 619 && ((localPatternChars != null 620 && localPatternChars.equals(that.localPatternChars)) 621 || (localPatternChars == null 622 && that.localPatternChars == null))); 623 } 624 625 // =======================privates=============================== 626 627 /** 628 * Useful constant for defining time zone offsets. 629 */ 630 static final int millisPerHour = 60*60*1000; 631 632 /** 633 * Cache to hold DateFormatSymbols instances per Locale. 634 */ 635 private static final ConcurrentMap<Locale, SoftReference<DateFormatSymbols>> cachedInstances 636 = new ConcurrentHashMap<>(3); 637 638 private transient int lastZoneIndex = 0; 639 640 private void initializeData(Locale desiredLocale) { 641 locale = desiredLocale; 642 643 // Copy values of a cached instance if any. 644 SoftReference<DateFormatSymbols> ref = cachedInstances.get(locale); 645 DateFormatSymbols dfs; 646 if (ref != null && (dfs = ref.get()) != null) { 647 copyMembers(dfs, this); 648 return; 649 } 650 651 // Initialize the fields from the ResourceBundle for locale. 652 LocaleProviderAdapter adapter = LocaleProviderAdapter.getAdapter(DateFormatSymbolsProvider.class, locale); 653 // Avaoid any potential recursions 654 switch (adapter.getAdapterType()) { 655 case HOST: 656 case SPI: 657 adapter = LocaleProviderAdapter.getResourceBundleBased(); 658 break; 659 } 660 ResourceBundle resource = adapter.getLocaleData().getDateFormatData(locale); 661 662 eras = resource.getStringArray("Eras"); 663 months = resource.getStringArray("MonthNames"); 664 shortMonths = resource.getStringArray("MonthAbbreviations"); 665 ampms = resource.getStringArray("AmPmMarkers"); 666 localPatternChars = resource.getString("DateTimePatternChars"); 667 668 // Day of week names are stored in a 1-based array. 669 weekdays = toOneBasedArray(resource.getStringArray("DayNames")); 670 shortWeekdays = toOneBasedArray(resource.getStringArray("DayAbbreviations")); 671 672 // Put a clone in the cache 673 ref = new SoftReference<>((DateFormatSymbols)this.clone()); 674 SoftReference<DateFormatSymbols> x = cachedInstances.putIfAbsent(locale, ref); 675 if (x != null) { 676 DateFormatSymbols y = x.get(); 677 if (y == null) { 678 // Replace the empty SoftReference with ref. 679 cachedInstances.put(locale, ref); 680 } 681 } 682 } 683 684 private static String[] toOneBasedArray(String[] src) { 685 int len = src.length; 686 String[] dst = new String[len + 1]; 687 dst[0] = ""; 688 for (int i = 0; i < len; i++) { 689 dst[i + 1] = src[i]; 690 } 691 return dst; 692 } 693 694 /** 695 * Package private: used by SimpleDateFormat 696 * Gets the index for the given time zone ID to obtain the time zone 697 * strings for formatting. The time zone ID is just for programmatic 698 * lookup. NOT LOCALIZED!!! 699 * @param ID the given time zone ID. 700 * @return the index of the given time zone ID. Returns -1 if 701 * the given time zone ID can't be located in the DateFormatSymbols object. 702 * @see java.util.SimpleTimeZone 703 */ 704 final int getZoneIndex(String ID) { 705 String[][] zoneStrings = getZoneStringsWrapper(); 706 707 /* 708 * getZoneIndex has been re-written for performance reasons. instead of 709 * traversing the zoneStrings array every time, we cache the last used zone 710 * index 711 */ 712 if (lastZoneIndex < zoneStrings.length && ID.equals(zoneStrings[lastZoneIndex][0])) { 713 return lastZoneIndex; 714 } 715 716 /* slow path, search entire list */ 717 for (int index = 0; index < zoneStrings.length; index++) { 718 if (ID.equals(zoneStrings[index][0])) { 719 lastZoneIndex = index; 720 return index; 721 } 722 } 723 724 return -1; 725 } 726 727 /** 728 * Wrapper method to the getZoneStrings(), which is called from inside 729 * the java.text package and not to mutate the returned arrays, so that 730 * it does not need to create a defensive copy. 731 */ 732 final String[][] getZoneStringsWrapper() { 733 if (isSubclassObject()) { 734 return getZoneStrings(); 735 } else { 736 return getZoneStringsImpl(false); 737 } 738 } 739 740 private String[][] getZoneStringsImpl(boolean needsCopy) { 741 if (zoneStrings == null) { 742 zoneStrings = TimeZoneNameUtility.getZoneStrings(locale); 743 } 744 745 if (!needsCopy) { 746 return zoneStrings; 747 } 748 749 int len = zoneStrings.length; 750 String[][] aCopy = new String[len][]; 751 for (int i = 0; i < len; i++) { 752 aCopy[i] = Arrays.copyOf(zoneStrings[i], zoneStrings[i].length); 753 } 754 return aCopy; 755 } 756 757 private boolean isSubclassObject() { 758 return !getClass().getName().equals("java.text.DateFormatSymbols"); 759 } 760 761 /** 762 * Clones all the data members from the source DateFormatSymbols to 763 * the target DateFormatSymbols. This is only for subclasses. 764 * @param src the source DateFormatSymbols. 765 * @param dst the target DateFormatSymbols. 766 */ 767 private void copyMembers(DateFormatSymbols src, DateFormatSymbols dst) 768 { 769 dst.eras = Arrays.copyOf(src.eras, src.eras.length); 770 dst.months = Arrays.copyOf(src.months, src.months.length); 771 dst.shortMonths = Arrays.copyOf(src.shortMonths, src.shortMonths.length); 772 dst.weekdays = Arrays.copyOf(src.weekdays, src.weekdays.length); 773 dst.shortWeekdays = Arrays.copyOf(src.shortWeekdays, src.shortWeekdays.length); 774 dst.ampms = Arrays.copyOf(src.ampms, src.ampms.length); 775 if (src.zoneStrings != null) { 776 dst.zoneStrings = src.getZoneStringsImpl(true); 777 } else { 778 dst.zoneStrings = null; 779 } 780 dst.localPatternChars = src.localPatternChars; 781 } 782 783 /** 784 * Write out the default serializable data, after ensuring the 785 * <code>zoneStrings</code> field is initialized in order to make 786 * sure the backward compatibility. 787 * 788 * @since 1.6 789 */ 790 private void writeObject(ObjectOutputStream stream) throws IOException { 791 if (zoneStrings == null) { 792 zoneStrings = TimeZoneNameUtility.getZoneStrings(locale); 793 } 794 stream.defaultWriteObject(); 795 } 796 }