DateFormatSymbols.java revision 9c853c5b9ebbb0ef60a013ae10ee411d70dfa832
1/* 2 * Copyright (c) 1996, 2010, 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 39package java.text; 40 41import java.io.IOException; 42import java.io.ObjectOutputStream; 43import java.io.Serializable; 44import java.lang.ref.SoftReference; 45import java.text.spi.DateFormatSymbolsProvider; 46import java.util.Arrays; 47import java.util.List; 48import java.util.Locale; 49import java.util.ResourceBundle; 50import java.util.TimeZone; 51import java.util.concurrent.ConcurrentHashMap; 52import java.util.concurrent.ConcurrentMap; 53import java.util.spi.LocaleServiceProvider; 54import libcore.icu.LocaleData; 55import libcore.icu.TimeZoneNames; 56import sun.util.LocaleServiceProviderPool; 57 58/** 59 * <code>DateFormatSymbols</code> is a public class for encapsulating 60 * localizable date-time formatting data, such as the names of the 61 * months, the names of the days of the week, and the time zone data. 62 * <code>DateFormat</code> and <code>SimpleDateFormat</code> both use 63 * <code>DateFormatSymbols</code> to encapsulate this information. 64 * 65 * <p> 66 * Typically you shouldn't use <code>DateFormatSymbols</code> directly. 67 * Rather, you are encouraged to create a date-time formatter with the 68 * <code>DateFormat</code> class's factory methods: <code>getTimeInstance</code>, 69 * <code>getDateInstance</code>, or <code>getDateTimeInstance</code>. 70 * These methods automatically create a <code>DateFormatSymbols</code> for 71 * the formatter so that you don't have to. After the 72 * formatter is created, you may modify its format pattern using the 73 * <code>setPattern</code> method. For more information about 74 * creating formatters using <code>DateFormat</code>'s factory methods, 75 * see {@link DateFormat}. 76 * 77 * <p> 78 * If you decide to create a date-time formatter with a specific 79 * format pattern for a specific locale, you can do so with: 80 * <blockquote> 81 * <pre> 82 * new SimpleDateFormat(aPattern, DateFormatSymbols.getInstance(aLocale)). 83 * </pre> 84 * </blockquote> 85 * 86 * <p> 87 * <code>DateFormatSymbols</code> objects are cloneable. When you obtain 88 * a <code>DateFormatSymbols</code> object, feel free to modify the 89 * date-time formatting data. For instance, you can replace the localized 90 * date-time format pattern characters with the ones that you feel easy 91 * to remember. Or you can change the representative cities 92 * to your favorite ones. 93 * 94 * <p> 95 * New <code>DateFormatSymbols</code> subclasses may be added to support 96 * <code>SimpleDateFormat</code> for date-time formatting for additional locales. 97 98 * @see DateFormat 99 * @see SimpleDateFormat 100 * @see java.util.SimpleTimeZone 101 * @author Chen-Lieh Huang 102 */ 103public class DateFormatSymbols implements Serializable, Cloneable { 104 105 /** 106 * Construct a DateFormatSymbols object by loading format data from 107 * resources for the default locale. This constructor can only 108 * construct instances for the locales supported by the Java 109 * runtime environment, not for those supported by installed 110 * {@link java.text.spi.DateFormatSymbolsProvider DateFormatSymbolsProvider} 111 * implementations. For full locale coverage, use the 112 * {@link #getInstance(Locale) getInstance} method. 113 * 114 * @see #getInstance() 115 * @exception java.util.MissingResourceException 116 * if the resources for the default locale cannot be 117 * found or cannot be loaded. 118 */ 119 public DateFormatSymbols() 120 { 121 initializeData(Locale.getDefault(Locale.Category.FORMAT)); 122 } 123 124 /** 125 * Construct a DateFormatSymbols object by loading format data from 126 * resources for the given locale. This constructor can only 127 * construct instances for the locales supported by the Java 128 * runtime environment, not for those supported by installed 129 * {@link java.text.spi.DateFormatSymbolsProvider DateFormatSymbolsProvider} 130 * implementations. For full locale coverage, use the 131 * {@link #getInstance(Locale) getInstance} method. 132 * 133 * @see #getInstance(Locale) 134 * @exception java.util.MissingResourceException 135 * if the resources for the specified locale cannot be 136 * found or cannot be loaded. 137 */ 138 public DateFormatSymbols(Locale locale) 139 { 140 initializeData(locale); 141 } 142 143 /** 144 * Era strings. For example: "AD" and "BC". An array of 2 strings, 145 * indexed by <code>Calendar.BC</code> and <code>Calendar.AD</code>. 146 * @serial 147 */ 148 String eras[] = null; 149 150 /** 151 * Month strings. For example: "January", "February", etc. An array 152 * of 13 strings (some calendars have 13 months), indexed by 153 * <code>Calendar.JANUARY</code>, <code>Calendar.FEBRUARY</code>, etc. 154 * @serial 155 */ 156 String months[] = null; 157 158 /** 159 * Short month strings. For example: "Jan", "Feb", etc. An array of 160 * 13 strings (some calendars have 13 months), indexed by 161 * <code>Calendar.JANUARY</code>, <code>Calendar.FEBRUARY</code>, etc. 162 163 * @serial 164 */ 165 String shortMonths[] = null; 166 167 /** 168 * Weekday strings. For example: "Sunday", "Monday", etc. An array 169 * of 8 strings, indexed by <code>Calendar.SUNDAY</code>, 170 * <code>Calendar.MONDAY</code>, etc. 171 * The element <code>weekdays[0]</code> is ignored. 172 * @serial 173 */ 174 String weekdays[] = null; 175 176 /** 177 * Short weekday strings. For example: "Sun", "Mon", etc. An array 178 * of 8 strings, indexed by <code>Calendar.SUNDAY</code>, 179 * <code>Calendar.MONDAY</code>, etc. 180 * The element <code>shortWeekdays[0]</code> is ignored. 181 * @serial 182 */ 183 String shortWeekdays[] = null; 184 185 /** 186 * AM and PM strings. For example: "AM" and "PM". An array of 187 * 2 strings, indexed by <code>Calendar.AM</code> and 188 * <code>Calendar.PM</code>. 189 * @serial 190 */ 191 String ampms[] = null; 192 193 /** 194 * Localized names of time zones in this locale. This is a 195 * two-dimensional array of strings of size <em>n</em> by <em>m</em>, 196 * where <em>m</em> is at least 5. Each of the <em>n</em> rows is an 197 * entry containing the localized names for a single <code>TimeZone</code>. 198 * Each such row contains (with <code>i</code> ranging from 199 * 0..<em>n</em>-1): 200 * <ul> 201 * <li><code>zoneStrings[i][0]</code> - time zone ID</li> 202 * <li><code>zoneStrings[i][1]</code> - long name of zone in standard 203 * time</li> 204 * <li><code>zoneStrings[i][2]</code> - short name of zone in 205 * standard time</li> 206 * <li><code>zoneStrings[i][3]</code> - long name of zone in daylight 207 * saving time</li> 208 * <li><code>zoneStrings[i][4]</code> - short name of zone in daylight 209 * saving time</li> 210 * </ul> 211 * The zone ID is <em>not</em> localized; it's one of the valid IDs of 212 * the {@link java.util.TimeZone TimeZone} class that are not 213 * <a href="../java/util/TimeZone.html#CustomID">custom IDs</a>. 214 * All other entries are localized names. 215 * @see java.util.TimeZone 216 * @serial 217 */ 218 String zoneStrings[][] = null; 219 220 /** 221 * Indicates that zoneStrings is set externally with setZoneStrings() method. 222 */ 223 transient boolean isZoneStringsSet = false; 224 225 /** 226 * Unlocalized date-time pattern characters. For example: 'y', 'd', etc. 227 * All locales use the same these unlocalized pattern characters. 228 */ 229 static final String patternChars = "GyMdkHmsSEDFwWahKzZYuX"; 230 231 static final int PATTERN_ERA = 0; // G 232 static final int PATTERN_YEAR = 1; // y 233 static final int PATTERN_MONTH = 2; // M 234 static final int PATTERN_DAY_OF_MONTH = 3; // d 235 static final int PATTERN_HOUR_OF_DAY1 = 4; // k 236 static final int PATTERN_HOUR_OF_DAY0 = 5; // H 237 static final int PATTERN_MINUTE = 6; // m 238 static final int PATTERN_SECOND = 7; // s 239 static final int PATTERN_MILLISECOND = 8; // S 240 static final int PATTERN_DAY_OF_WEEK = 9; // E 241 static final int PATTERN_DAY_OF_YEAR = 10; // D 242 static final int PATTERN_DAY_OF_WEEK_IN_MONTH = 11; // F 243 static final int PATTERN_WEEK_OF_YEAR = 12; // w 244 static final int PATTERN_WEEK_OF_MONTH = 13; // W 245 static final int PATTERN_AM_PM = 14; // a 246 static final int PATTERN_HOUR1 = 15; // h 247 static final int PATTERN_HOUR0 = 16; // K 248 static final int PATTERN_ZONE_NAME = 17; // z 249 static final int PATTERN_ZONE_VALUE = 18; // Z 250 static final int PATTERN_WEEK_YEAR = 19; // Y 251 static final int PATTERN_ISO_DAY_OF_WEEK = 20; // u 252 static final int PATTERN_ISO_ZONE = 21; // X 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 return (DateFormatSymbols) getCachedInstance(locale).clone(); 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 return getCachedInstance(locale); 343 } 344 345 private static DateFormatSymbols getProviderInstance(Locale locale) { 346 DateFormatSymbols providersInstance = null; 347 348 // Check whether a provider can provide an implementation that's closer 349 // to the requested locale than what the Java runtime itself can provide. 350 LocaleServiceProviderPool pool = 351 LocaleServiceProviderPool.getPool(DateFormatSymbolsProvider.class); 352 if (pool.hasProviders()) { 353 providersInstance = pool.getLocalizedObject( 354 DateFormatSymbolsGetter.INSTANCE, locale); 355 } 356 return providersInstance; 357 } 358 359 /** 360 * Returns a cached DateFormatSymbols if it's found in the 361 * cache. Otherwise, this method returns a newly cached instance 362 * for the given locale. 363 */ 364 private static DateFormatSymbols getCachedInstance(Locale locale) { 365 SoftReference<DateFormatSymbols> ref = cachedInstances.get(locale); 366 DateFormatSymbols dfs = null; 367 if (ref == null || (dfs = ref.get()) == null) { 368 dfs = new DateFormatSymbols(locale); 369 ref = new SoftReference<DateFormatSymbols>(dfs); 370 SoftReference<DateFormatSymbols> x = cachedInstances.putIfAbsent(locale, ref); 371 if (x != null) { 372 DateFormatSymbols y = x.get(); 373 if (y != null) { 374 dfs = y; 375 } else { 376 // Replace the empty SoftReference with ref. 377 cachedInstances.put(locale, ref); 378 } 379 } 380 } 381 return dfs; 382 } 383 384 /** 385 * Gets era strings. For example: "AD" and "BC". 386 * @return the era strings. 387 */ 388 public String[] getEras() { 389 return Arrays.copyOf(eras, eras.length); 390 } 391 392 /** 393 * Sets era strings. For example: "AD" and "BC". 394 * @param newEras the new era strings. 395 */ 396 public void setEras(String[] newEras) { 397 eras = Arrays.copyOf(newEras, newEras.length); 398 } 399 400 /** 401 * Gets month strings. For example: "January", "February", etc. 402 * @return the month strings. 403 */ 404 public String[] getMonths() { 405 return Arrays.copyOf(months, months.length); 406 } 407 408 /** 409 * Sets month strings. For example: "January", "February", etc. 410 * @param newMonths the new month strings. 411 */ 412 public void setMonths(String[] newMonths) { 413 months = Arrays.copyOf(newMonths, newMonths.length); 414 } 415 416 /** 417 * Gets short month strings. For example: "Jan", "Feb", etc. 418 * @return the short month strings. 419 */ 420 public String[] getShortMonths() { 421 return Arrays.copyOf(shortMonths, shortMonths.length); 422 } 423 424 /** 425 * Sets short month strings. For example: "Jan", "Feb", etc. 426 * @param newShortMonths the new short month strings. 427 */ 428 public void setShortMonths(String[] newShortMonths) { 429 shortMonths = Arrays.copyOf(newShortMonths, newShortMonths.length); 430 } 431 432 /** 433 * Gets weekday strings. For example: "Sunday", "Monday", etc. 434 * @return the weekday strings. Use <code>Calendar.SUNDAY</code>, 435 * <code>Calendar.MONDAY</code>, etc. to index the result array. 436 */ 437 public String[] getWeekdays() { 438 return Arrays.copyOf(weekdays, weekdays.length); 439 } 440 441 /** 442 * Sets weekday strings. For example: "Sunday", "Monday", etc. 443 * @param newWeekdays the new weekday strings. The array should 444 * be indexed by <code>Calendar.SUNDAY</code>, 445 * <code>Calendar.MONDAY</code>, etc. 446 */ 447 public void setWeekdays(String[] newWeekdays) { 448 weekdays = Arrays.copyOf(newWeekdays, newWeekdays.length); 449 } 450 451 /** 452 * Gets short weekday strings. For example: "Sun", "Mon", etc. 453 * @return the short weekday strings. Use <code>Calendar.SUNDAY</code>, 454 * <code>Calendar.MONDAY</code>, etc. to index the result array. 455 */ 456 public String[] getShortWeekdays() { 457 return Arrays.copyOf(shortWeekdays, shortWeekdays.length); 458 } 459 460 /** 461 * Sets short weekday strings. For example: "Sun", "Mon", etc. 462 * @param newShortWeekdays the new short weekday strings. The array should 463 * be indexed by <code>Calendar.SUNDAY</code>, 464 * <code>Calendar.MONDAY</code>, etc. 465 */ 466 public void setShortWeekdays(String[] newShortWeekdays) { 467 shortWeekdays = Arrays.copyOf(newShortWeekdays, newShortWeekdays.length); 468 } 469 470 /** 471 * Gets ampm strings. For example: "AM" and "PM". 472 * @return the ampm strings. 473 */ 474 public String[] getAmPmStrings() { 475 return Arrays.copyOf(ampms, ampms.length); 476 } 477 478 /** 479 * Sets ampm strings. For example: "AM" and "PM". 480 * @param newAmpms the new ampm strings. 481 */ 482 public void setAmPmStrings(String[] newAmpms) { 483 ampms = Arrays.copyOf(newAmpms, newAmpms.length); 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 } 569 570 /** 571 * Gets localized date-time pattern characters. For example: 'u', 't', etc. 572 * @return the localized date-time pattern characters. 573 */ 574 public String getLocalPatternChars() { 575 return localPatternChars; 576 } 577 578 /** 579 * Sets localized date-time pattern characters. For example: 'u', 't', etc. 580 * @param newLocalPatternChars the new localized date-time 581 * pattern characters. 582 */ 583 public void setLocalPatternChars(String newLocalPatternChars) { 584 // Call toString() to throw an NPE in case the argument is null 585 localPatternChars = newLocalPatternChars.toString(); 586 } 587 588 /** 589 * Overrides Cloneable 590 */ 591 public Object clone() 592 { 593 try 594 { 595 DateFormatSymbols other = (DateFormatSymbols)super.clone(); 596 copyMembers(this, other); 597 return other; 598 } catch (CloneNotSupportedException e) { 599 throw new InternalError(); 600 } 601 } 602 603 /** 604 * Override hashCode. 605 * Generates a hash code for the DateFormatSymbols object. 606 */ 607 public int hashCode() { 608 int hashcode = 0; 609 String[][] zoneStrings = getZoneStringsWrapper(); 610 for (int index = 0; index < zoneStrings[0].length; ++index) 611 hashcode ^= zoneStrings[0][index].hashCode(); 612 return hashcode; 613 } 614 615 /** 616 * Override equals 617 */ 618 public boolean equals(Object obj) 619 { 620 if (this == obj) return true; 621 if (obj == null || getClass() != obj.getClass()) return false; 622 DateFormatSymbols that = (DateFormatSymbols) obj; 623 return (Arrays.equals(eras, that.eras) 624 && Arrays.equals(months, that.months) 625 && Arrays.equals(shortMonths, that.shortMonths) 626 && Arrays.equals(weekdays, that.weekdays) 627 && Arrays.equals(shortWeekdays, that.shortWeekdays) 628 && Arrays.equals(ampms, that.ampms) 629 && Arrays.deepEquals(getZoneStringsWrapper(), that.getZoneStringsWrapper()) 630 && ((localPatternChars != null 631 && localPatternChars.equals(that.localPatternChars)) 632 || (localPatternChars == null 633 && that.localPatternChars == null))); 634 } 635 636 // =======================privates=============================== 637 638 /** 639 * Useful constant for defining time zone offsets. 640 */ 641 static final int millisPerHour = 60*60*1000; 642 643 /** 644 * Cache to hold DateFormatSymbols instances per Locale. 645 */ 646 private static final ConcurrentMap<Locale, SoftReference<DateFormatSymbols>> cachedInstances 647 = new ConcurrentHashMap<Locale, SoftReference<DateFormatSymbols>>(3); 648 649 private transient int lastZoneIndex = 0; 650 651 private void initializeData(Locale desiredLocale) { 652 locale = desiredLocale; 653 654 // Copy values of a cached instance if any. 655 SoftReference<DateFormatSymbols> ref = cachedInstances.get(locale); 656 DateFormatSymbols dfs; 657 if (ref != null && (dfs = ref.get()) != null) { 658 copyMembers(dfs, this); 659 return; 660 } 661 locale = LocaleData.mapInvalidAndNullLocales(locale); 662 LocaleData localeData = LocaleData.get(locale); 663 664 eras = localeData.eras; 665 months = localeData.longMonthNames; 666 shortMonths = localeData.shortMonthNames; 667 ampms = localeData.amPm; 668 localPatternChars = SimpleDateFormat.PATTERN_CHARS; 669 670 // Day of week names are stored in a 1-based array. 671 weekdays = localeData.longWeekdayNames; 672 shortWeekdays = localeData.shortWeekdayNames; 673 } 674 675 private static String[] toOneBasedArray(String[] src) { 676 int len = src.length; 677 String[] dst = new String[len + 1]; 678 dst[0] = ""; 679 for (int i = 0; i < len; i++) { 680 dst[i + 1] = src[i]; 681 } 682 return dst; 683 } 684 685 /** 686 * Package private: used by SimpleDateFormat 687 * Gets the index for the given time zone ID to obtain the time zone 688 * strings for formatting. The time zone ID is just for programmatic 689 * lookup. NOT LOCALIZED!!! 690 * @param ID the given time zone ID. 691 * @return the index of the given time zone ID. Returns -1 if 692 * the given time zone ID can't be located in the DateFormatSymbols object. 693 * @see java.util.SimpleTimeZone 694 */ 695 final int getZoneIndex(String ID) { 696 String[][] zoneStrings = getZoneStringsWrapper(); 697 698 /* 699 * getZoneIndex has been re-written for performance reasons. instead of 700 * traversing the zoneStrings array every time, we cache the last used zone 701 * index 702 */ 703 if (lastZoneIndex < zoneStrings.length && ID.equals(zoneStrings[lastZoneIndex][0])) { 704 return lastZoneIndex; 705 } 706 707 /* slow path, search entire list */ 708 for (int index = 0; index < zoneStrings.length; index++) { 709 if (ID.equals(zoneStrings[index][0])) { 710 lastZoneIndex = index; 711 return index; 712 } 713 } 714 715 return -1; 716 } 717 718 /** 719 * Wrapper method to the getZoneStrings(), which is called from inside 720 * the java.text package and not to mutate the returned arrays, so that 721 * it does not need to create a defensive copy. 722 */ 723 final String[][] getZoneStringsWrapper() { 724 if (isSubclassObject()) { 725 return getZoneStrings(); 726 } else { 727 return getZoneStringsImpl(false); 728 } 729 } 730 731 private final synchronized String[][] internalZoneStrings() { 732 if (zoneStrings == null) { 733 zoneStrings = TimeZoneNames.getZoneStrings(locale); 734 // If icu4c doesn't have a name, our array contains a null. TimeZone.getDisplayName 735 // knows how to format GMT offsets (and, unlike icu4c, has accurate data). http://b/8128460. 736 for (String[] zone : zoneStrings) { 737 String id = zone[0]; 738 if (zone[1] == null) { 739 zone[1] = 740 TimeZone.getTimeZone(id).getDisplayName(false, TimeZone.LONG, locale); 741 } 742 if (zone[2] == null) { 743 zone[2] = 744 TimeZone.getTimeZone(id).getDisplayName(false, TimeZone.SHORT, locale); 745 } 746 if (zone[3] == null) { 747 zone[3] = TimeZone.getTimeZone(id).getDisplayName(true, TimeZone.LONG, locale); 748 } 749 if (zone[4] == null) { 750 zone[4] = 751 TimeZone.getTimeZone(id).getDisplayName(true, TimeZone.SHORT, locale); 752 } 753 } 754 } 755 return zoneStrings; 756 } 757 758 private final String[][] getZoneStringsImpl(boolean needsCopy) { 759 String[][] zoneStrings = internalZoneStrings(); 760 761 if (!needsCopy) { 762 return zoneStrings; 763 } 764 765 int len = zoneStrings.length; 766 String[][] aCopy = new String[len][]; 767 for (int i = 0; i < len; i++) { 768 aCopy[i] = Arrays.copyOf(zoneStrings[i], zoneStrings[i].length); 769 } 770 return aCopy; 771 } 772 773 private final boolean isSubclassObject() { 774 return !getClass().getName().equals("java.text.DateFormatSymbols"); 775 } 776 777 /** 778 * Clones all the data members from the source DateFormatSymbols to 779 * the target DateFormatSymbols. This is only for subclasses. 780 * @param src the source DateFormatSymbols. 781 * @param dst the target DateFormatSymbols. 782 */ 783 private final void copyMembers(DateFormatSymbols src, DateFormatSymbols dst) 784 { 785 dst.eras = Arrays.copyOf(src.eras, src.eras.length); 786 dst.months = Arrays.copyOf(src.months, src.months.length); 787 dst.shortMonths = Arrays.copyOf(src.shortMonths, src.shortMonths.length); 788 dst.weekdays = Arrays.copyOf(src.weekdays, src.weekdays.length); 789 dst.shortWeekdays = Arrays.copyOf(src.shortWeekdays, src.shortWeekdays.length); 790 dst.ampms = Arrays.copyOf(src.ampms, src.ampms.length); 791 if (src.zoneStrings != null) { 792 dst.zoneStrings = src.getZoneStringsImpl(true); 793 } else { 794 dst.zoneStrings = null; 795 } 796 dst.localPatternChars = src.localPatternChars; 797 } 798 799 /** 800 * Write out the default serializable data, after ensuring the 801 * <code>zoneStrings</code> field is initialized in order to make 802 * sure the backward compatibility. 803 * 804 * @since 1.6 805 */ 806 private void writeObject(ObjectOutputStream stream) throws IOException { 807 internalZoneStrings(); 808 stream.defaultWriteObject(); 809 } 810 811 /** 812 * Obtains a DateFormatSymbols instance from a DateFormatSymbolsProvider 813 * implementation. 814 */ 815 private static class DateFormatSymbolsGetter 816 implements LocaleServiceProviderPool.LocalizedObjectGetter<DateFormatSymbolsProvider, 817 DateFormatSymbols> { 818 private static final DateFormatSymbolsGetter INSTANCE = 819 new DateFormatSymbolsGetter(); 820 821 public DateFormatSymbols getObject(DateFormatSymbolsProvider dateFormatSymbolsProvider, 822 Locale locale, 823 String key, 824 Object... params) { 825 assert params.length == 0; 826 return dateFormatSymbolsProvider.getInstance(locale); 827 } 828 } 829} 830