1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. Oracle designates this 9 * particular file as subject to the "Classpath" exception as provided 10 * by Oracle in the LICENSE file that accompanied this code. 11 * 12 * This code is distributed in the hope that it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15 * version 2 for more details (a copy is included in the LICENSE file that 16 * accompanied this code). 17 * 18 * You should have received a copy of the GNU General Public License version 19 * 2 along with this work; if not, write to the Free Software Foundation, 20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 21 * 22 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 23 * or visit www.oracle.com if you need additional information or have any 24 * questions. 25 */ 26 27 /* 28 * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved 29 * (C) Copyright IBM Corp. 1996 - All Rights Reserved 30 * 31 * The original version of this source code and documentation is copyrighted 32 * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These 33 * materials are provided under terms of a License Agreement between Taligent 34 * and Sun. This technology is protected by multiple US and International 35 * patents. This notice and attribution to Taligent may not be removed. 36 * Taligent is a registered trademark of Taligent, Inc. 37 * 38 */ 39 40 package java.text; 41 42 import java.io.IOException; 43 import java.io.ObjectInputStream; 44 import java.io.ObjectOutputStream; 45 import java.io.Serializable; 46 import java.lang.ref.SoftReference; 47 import java.text.spi.DateFormatSymbolsProvider; 48 import java.util.Arrays; 49 import java.util.Locale; 50 import java.util.TimeZone; 51 import java.util.concurrent.ConcurrentHashMap; 52 import java.util.concurrent.ConcurrentMap; 53 54 import libcore.icu.LocaleData; 55 import libcore.icu.TimeZoneNames; 56 import 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 */ 103 public 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 */ DateFormatSymbols()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 */ DateFormatSymbols(Locale locale)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 * Pretend to support 'L' and 'c' for now. It's meant for standalone weekday and 230 * month names, but we just use the non-standalone versions for now. 231 */ 232 static final String patternChars = "GyMdkHmsSEDFwWahKzZYuXLc"; 233 234 static final int PATTERN_ERA = 0; // G 235 static final int PATTERN_YEAR = 1; // y 236 static final int PATTERN_MONTH = 2; // M 237 static final int PATTERN_DAY_OF_MONTH = 3; // d 238 static final int PATTERN_HOUR_OF_DAY1 = 4; // k 239 static final int PATTERN_HOUR_OF_DAY0 = 5; // H 240 static final int PATTERN_MINUTE = 6; // m 241 static final int PATTERN_SECOND = 7; // s 242 static final int PATTERN_MILLISECOND = 8; // S 243 static final int PATTERN_DAY_OF_WEEK = 9; // E 244 static final int PATTERN_DAY_OF_YEAR = 10; // D 245 static final int PATTERN_DAY_OF_WEEK_IN_MONTH = 11; // F 246 static final int PATTERN_WEEK_OF_YEAR = 12; // w 247 static final int PATTERN_WEEK_OF_MONTH = 13; // W 248 static final int PATTERN_AM_PM = 14; // a 249 static final int PATTERN_HOUR1 = 15; // h 250 static final int PATTERN_HOUR0 = 16; // K 251 static final int PATTERN_ZONE_NAME = 17; // z 252 static final int PATTERN_ZONE_VALUE = 18; // Z 253 static final int PATTERN_WEEK_YEAR = 19; // Y 254 static final int PATTERN_ISO_DAY_OF_WEEK = 20; // u 255 static final int PATTERN_ISO_ZONE = 21; // X 256 static final int PATTERN_STANDALONE_MONTH = 22; // L 257 static final int PATTERN_STANDALONE_DAY_OF_WEEK = 23; // c 258 259 /** 260 * Localized date-time pattern characters. For example, a locale may 261 * wish to use 'u' rather than 'y' to represent years in its date format 262 * pattern strings. 263 * This string must be exactly 18 characters long, with the index of 264 * the characters described by <code>DateFormat.ERA_FIELD</code>, 265 * <code>DateFormat.YEAR_FIELD</code>, etc. Thus, if the string were 266 * "Xz...", then localized patterns would use 'X' for era and 'z' for year. 267 * @serial 268 */ 269 String localPatternChars = null; 270 271 /** 272 * The locale which is used for initializing this DateFormatSymbols object. 273 * 274 * @since 1.6 275 * @serial 276 */ 277 Locale locale = null; 278 279 /* use serialVersionUID from JDK 1.1.4 for interoperability */ 280 static final long serialVersionUID = -5987973545549424702L; 281 282 // the internal serial version which says which version was written 283 // - 0 (default) for version up to JDK 1.1.4 284 // - 1 Android version that contains a whole bunch of new fields. 285 static final int currentSerialVersion = 1; 286 287 /** 288 * The version of the serialized data on the stream. Possible values: 289 * <ul> 290 * <li><b>0</b> or not present on stream: JDK 1.1.4. 291 * <li><b>1</b> Android: 292 * </ul> 293 * When streaming out this class, the most recent format 294 * and the highest allowable <code>serialVersionOnStream</code> 295 * is written. 296 * @serial 297 * @since JDK1.1.4 298 */ 299 private int serialVersionOnStream = currentSerialVersion; 300 301 /** 302 * Tiny month strings; "J", "F", "M" etc. 303 * 304 * @serial 305 */ 306 private String[] tinyMonths; 307 308 /** 309 * Tiny weekday strings: "M", "F", "W" etc. 310 * 311 * @serial 312 */ 313 private String[] tinyWeekdays; 314 315 /** 316 * Standalone month strings; "January", "February", "March" etc. 317 * 318 * @serial 319 */ 320 private String[] standAloneMonths; 321 322 /** 323 * Short standalone month strings: "Jan", "Feb", "Mar" etc. 324 * 325 * @serial 326 */ 327 private String[] shortStandAloneMonths; 328 329 /** 330 * Tiny standalone month strings: "J", "F", "M" etc. 331 * 332 * @serial 333 */ 334 private String[] tinyStandAloneMonths; 335 336 /** 337 * Standalone weekday strings; "Monday", "Tuesday", "Wednesday" etc. 338 * 339 * @serial 340 */ 341 private String[] standAloneWeekdays; 342 343 /** 344 * Short standalone weekday strings; "Mon", "Tue", "Wed" etc. 345 * 346 * @serial 347 */ 348 private String[] shortStandAloneWeekdays; 349 350 /** 351 * Tiny standalone weekday strings; "M", "T", "W" etc. 352 * 353 * @serial 354 */ 355 private String[] tinyStandAloneWeekdays; 356 357 /** 358 * Returns an array of all locales for which the 359 * <code>getInstance</code> methods of this class can return 360 * localized instances. 361 * The returned array represents the union of locales supported by the 362 * Java runtime and by installed 363 * {@link java.text.spi.DateFormatSymbolsProvider DateFormatSymbolsProvider} 364 * implementations. It must contain at least a <code>Locale</code> 365 * instance equal to {@link java.util.Locale#US Locale.US}. 366 * 367 * @return An array of locales for which localized 368 * <code>DateFormatSymbols</code> instances are available. 369 * @since 1.6 370 */ getAvailableLocales()371 public static Locale[] getAvailableLocales() { 372 LocaleServiceProviderPool pool= 373 LocaleServiceProviderPool.getPool(DateFormatSymbolsProvider.class); 374 return pool.getAvailableLocales(); 375 } 376 377 /** 378 * Gets the <code>DateFormatSymbols</code> instance for the default 379 * locale. This method provides access to <code>DateFormatSymbols</code> 380 * instances for locales supported by the Java runtime itself as well 381 * as for those supported by installed 382 * {@link java.text.spi.DateFormatSymbolsProvider DateFormatSymbolsProvider} 383 * implementations. 384 * @return a <code>DateFormatSymbols</code> instance. 385 * @since 1.6 386 */ getInstance()387 public static final DateFormatSymbols getInstance() { 388 return getInstance(Locale.getDefault(Locale.Category.FORMAT)); 389 } 390 391 /** 392 * Gets the <code>DateFormatSymbols</code> instance for the specified 393 * locale. This method provides access to <code>DateFormatSymbols</code> 394 * instances for locales supported by the Java runtime itself as well 395 * as for those supported by installed 396 * {@link java.text.spi.DateFormatSymbolsProvider DateFormatSymbolsProvider} 397 * implementations. 398 * @param locale the given locale. 399 * @return a <code>DateFormatSymbols</code> instance. 400 * @exception NullPointerException if <code>locale</code> is null 401 * @since 1.6 402 */ getInstance(Locale locale)403 public static final DateFormatSymbols getInstance(Locale locale) { 404 DateFormatSymbols dfs = getProviderInstance(locale); 405 if (dfs != null) { 406 return dfs; 407 } 408 return (DateFormatSymbols) getCachedInstance(locale).clone(); 409 } 410 411 /** 412 * Returns a DateFormatSymbols provided by a provider or found in 413 * the cache. Note that this method returns a cached instance, 414 * not its clone. Therefore, the instance should never be given to 415 * an application. 416 */ getInstanceRef(Locale locale)417 static final DateFormatSymbols getInstanceRef(Locale locale) { 418 DateFormatSymbols dfs = getProviderInstance(locale); 419 if (dfs != null) { 420 return dfs; 421 } 422 return getCachedInstance(locale); 423 } 424 getProviderInstance(Locale locale)425 private static DateFormatSymbols getProviderInstance(Locale locale) { 426 DateFormatSymbols providersInstance = null; 427 428 // Check whether a provider can provide an implementation that's closer 429 // to the requested locale than what the Java runtime itself can provide. 430 LocaleServiceProviderPool pool = 431 LocaleServiceProviderPool.getPool(DateFormatSymbolsProvider.class); 432 if (pool.hasProviders()) { 433 providersInstance = pool.getLocalizedObject( 434 DateFormatSymbolsGetter.INSTANCE, locale); 435 } 436 return providersInstance; 437 } 438 439 /** 440 * Returns a cached DateFormatSymbols if it's found in the 441 * cache. Otherwise, this method returns a newly cached instance 442 * for the given locale. 443 */ getCachedInstance(Locale locale)444 private static DateFormatSymbols getCachedInstance(Locale locale) { 445 SoftReference<DateFormatSymbols> ref = cachedInstances.get(locale); 446 DateFormatSymbols dfs = null; 447 if (ref == null || (dfs = ref.get()) == null) { 448 dfs = new DateFormatSymbols(locale); 449 ref = new SoftReference<DateFormatSymbols>(dfs); 450 SoftReference<DateFormatSymbols> x = cachedInstances.putIfAbsent(locale, ref); 451 if (x != null) { 452 DateFormatSymbols y = x.get(); 453 if (y != null) { 454 dfs = y; 455 } else { 456 // Replace the empty SoftReference with ref. 457 cachedInstances.put(locale, ref); 458 } 459 } 460 } 461 return dfs; 462 } 463 464 /** 465 * Gets era strings. For example: "AD" and "BC". 466 * @return the era strings. 467 */ getEras()468 public String[] getEras() { 469 return Arrays.copyOf(eras, eras.length); 470 } 471 472 /** 473 * Sets era strings. For example: "AD" and "BC". 474 * @param newEras the new era strings. 475 */ setEras(String[] newEras)476 public void setEras(String[] newEras) { 477 eras = Arrays.copyOf(newEras, newEras.length); 478 } 479 480 /** 481 * Gets month strings. For example: "January", "February", etc. 482 * @return the month strings. 483 */ getMonths()484 public String[] getMonths() { 485 return Arrays.copyOf(months, months.length); 486 } 487 488 /** 489 * Sets month strings. For example: "January", "February", etc. 490 * @param newMonths the new month strings. 491 */ setMonths(String[] newMonths)492 public void setMonths(String[] newMonths) { 493 months = Arrays.copyOf(newMonths, newMonths.length); 494 } 495 496 /** 497 * Gets short month strings. For example: "Jan", "Feb", etc. 498 * @return the short month strings. 499 */ getShortMonths()500 public String[] getShortMonths() { 501 return Arrays.copyOf(shortMonths, shortMonths.length); 502 } 503 504 /** 505 * Sets short month strings. For example: "Jan", "Feb", etc. 506 * @param newShortMonths the new short month strings. 507 */ setShortMonths(String[] newShortMonths)508 public void setShortMonths(String[] newShortMonths) { 509 shortMonths = Arrays.copyOf(newShortMonths, newShortMonths.length); 510 } 511 512 /** 513 * Gets weekday strings. For example: "Sunday", "Monday", etc. 514 * @return the weekday strings. Use <code>Calendar.SUNDAY</code>, 515 * <code>Calendar.MONDAY</code>, etc. to index the result array. 516 */ getWeekdays()517 public String[] getWeekdays() { 518 return Arrays.copyOf(weekdays, weekdays.length); 519 } 520 521 /** 522 * Sets weekday strings. For example: "Sunday", "Monday", etc. 523 * @param newWeekdays the new weekday strings. The array should 524 * be indexed by <code>Calendar.SUNDAY</code>, 525 * <code>Calendar.MONDAY</code>, etc. 526 */ setWeekdays(String[] newWeekdays)527 public void setWeekdays(String[] newWeekdays) { 528 weekdays = Arrays.copyOf(newWeekdays, newWeekdays.length); 529 } 530 531 /** 532 * Gets short weekday strings. For example: "Sun", "Mon", etc. 533 * @return the short weekday strings. Use <code>Calendar.SUNDAY</code>, 534 * <code>Calendar.MONDAY</code>, etc. to index the result array. 535 */ getShortWeekdays()536 public String[] getShortWeekdays() { 537 return Arrays.copyOf(shortWeekdays, shortWeekdays.length); 538 } 539 540 /** 541 * Sets short weekday strings. For example: "Sun", "Mon", etc. 542 * @param newShortWeekdays the new short weekday strings. The array should 543 * be indexed by <code>Calendar.SUNDAY</code>, 544 * <code>Calendar.MONDAY</code>, etc. 545 */ setShortWeekdays(String[] newShortWeekdays)546 public void setShortWeekdays(String[] newShortWeekdays) { 547 shortWeekdays = Arrays.copyOf(newShortWeekdays, newShortWeekdays.length); 548 } 549 550 /** 551 * Gets ampm strings. For example: "AM" and "PM". 552 * @return the ampm strings. 553 */ getAmPmStrings()554 public String[] getAmPmStrings() { 555 return Arrays.copyOf(ampms, ampms.length); 556 } 557 558 /** 559 * Sets ampm strings. For example: "AM" and "PM". 560 * @param newAmpms the new ampm strings. 561 */ setAmPmStrings(String[] newAmpms)562 public void setAmPmStrings(String[] newAmpms) { 563 ampms = Arrays.copyOf(newAmpms, newAmpms.length); 564 } 565 566 /** 567 * Gets time zone strings. Use of this method is discouraged; use 568 * {@link java.util.TimeZone#getDisplayName() TimeZone.getDisplayName()} 569 * instead. 570 * <p> 571 * The value returned is a 572 * two-dimensional array of strings of size <em>n</em> by <em>m</em>, 573 * where <em>m</em> is at least 5. Each of the <em>n</em> rows is an 574 * entry containing the localized names for a single <code>TimeZone</code>. 575 * Each such row contains (with <code>i</code> ranging from 576 * 0..<em>n</em>-1): 577 * <ul> 578 * <li><code>zoneStrings[i][0]</code> - time zone ID</li> 579 * <li><code>zoneStrings[i][1]</code> - long name of zone in standard 580 * time</li> 581 * <li><code>zoneStrings[i][2]</code> - short name of zone in 582 * standard time</li> 583 * <li><code>zoneStrings[i][3]</code> - long name of zone in daylight 584 * saving time</li> 585 * <li><code>zoneStrings[i][4]</code> - short name of zone in daylight 586 * saving time</li> 587 * </ul> 588 * The zone ID is <em>not</em> localized; it's one of the valid IDs of 589 * the {@link java.util.TimeZone TimeZone} class that are not 590 * <a href="../util/TimeZone.html#CustomID">custom IDs</a>. 591 * All other entries are localized names. If a zone does not implement 592 * daylight saving time, the daylight saving time names should not be used. 593 * <p> 594 * If {@link #setZoneStrings(String[][]) setZoneStrings} has been called 595 * on this <code>DateFormatSymbols</code> instance, then the strings 596 * provided by that call are returned. Otherwise, the returned array 597 * contains names provided by the Java runtime and by installed 598 * {@link java.util.spi.TimeZoneNameProvider TimeZoneNameProvider} 599 * implementations. 600 * 601 * @return the time zone strings. 602 * @see #setZoneStrings(String[][]) 603 */ getZoneStrings()604 public String[][] getZoneStrings() { 605 return getZoneStringsImpl(true); 606 } 607 608 /** 609 * Sets time zone strings. The argument must be a 610 * two-dimensional array of strings of size <em>n</em> by <em>m</em>, 611 * where <em>m</em> is at least 5. Each of the <em>n</em> rows is an 612 * entry containing the localized names for a single <code>TimeZone</code>. 613 * Each such row contains (with <code>i</code> ranging from 614 * 0..<em>n</em>-1): 615 * <ul> 616 * <li><code>zoneStrings[i][0]</code> - time zone ID</li> 617 * <li><code>zoneStrings[i][1]</code> - long name of zone in standard 618 * time</li> 619 * <li><code>zoneStrings[i][2]</code> - short name of zone in 620 * standard time</li> 621 * <li><code>zoneStrings[i][3]</code> - long name of zone in daylight 622 * saving time</li> 623 * <li><code>zoneStrings[i][4]</code> - short name of zone in daylight 624 * saving time</li> 625 * </ul> 626 * The zone ID is <em>not</em> localized; it's one of the valid IDs of 627 * the {@link java.util.TimeZone TimeZone} class that are not 628 * <a href="../util/TimeZone.html#CustomID">custom IDs</a>. 629 * All other entries are localized names. 630 * 631 * @param newZoneStrings the new time zone strings. 632 * @exception IllegalArgumentException if the length of any row in 633 * <code>newZoneStrings</code> is less than 5 634 * @exception NullPointerException if <code>newZoneStrings</code> is null 635 * @see #getZoneStrings() 636 */ setZoneStrings(String[][] newZoneStrings)637 public void setZoneStrings(String[][] newZoneStrings) { 638 String[][] aCopy = new String[newZoneStrings.length][]; 639 for (int i = 0; i < newZoneStrings.length; ++i) { 640 int len = newZoneStrings[i].length; 641 if (len < 5) { 642 throw new IllegalArgumentException(); 643 } 644 aCopy[i] = Arrays.copyOf(newZoneStrings[i], len); 645 } 646 zoneStrings = aCopy; 647 isZoneStringsSet = true; 648 } 649 650 /** 651 * Gets localized date-time pattern characters. For example: 'u', 't', etc. 652 * @return the localized date-time pattern characters. 653 */ getLocalPatternChars()654 public String getLocalPatternChars() { 655 return localPatternChars; 656 } 657 658 /** 659 * Sets localized date-time pattern characters. For example: 'u', 't', etc. 660 * @param newLocalPatternChars the new localized date-time 661 * pattern characters. 662 */ setLocalPatternChars(String newLocalPatternChars)663 public void setLocalPatternChars(String newLocalPatternChars) { 664 // Call toString() to throw an NPE in case the argument is null 665 localPatternChars = newLocalPatternChars.toString(); 666 } 667 getTinyMonths()668 String[] getTinyMonths() { 669 return tinyMonths; 670 } 671 getStandAloneMonths()672 String[] getStandAloneMonths() { 673 return standAloneMonths; 674 } 675 getShortStandAloneMonths()676 String[] getShortStandAloneMonths() { 677 return shortStandAloneMonths; 678 } 679 getTinyStandAloneMonths()680 String[] getTinyStandAloneMonths() { 681 return tinyStandAloneMonths; 682 } 683 getTinyWeekdays()684 String[] getTinyWeekdays() { 685 return tinyWeekdays; 686 } 687 getStandAloneWeekdays()688 String[] getStandAloneWeekdays() { 689 return standAloneWeekdays; 690 } 691 getShortStandAloneWeekdays()692 String[] getShortStandAloneWeekdays() { 693 return shortStandAloneWeekdays; 694 } 695 getTinyStandAloneWeekdays()696 String[] getTinyStandAloneWeekdays() { 697 return tinyStandAloneWeekdays; 698 } 699 700 /** 701 * Overrides Cloneable 702 */ clone()703 public Object clone() 704 { 705 try 706 { 707 DateFormatSymbols other = (DateFormatSymbols)super.clone(); 708 copyMembers(this, other); 709 return other; 710 } catch (CloneNotSupportedException e) { 711 throw new InternalError(); 712 } 713 } 714 715 /** 716 * Override hashCode. 717 * Generates a hash code for the DateFormatSymbols object. 718 */ hashCode()719 public int hashCode() { 720 int hashcode = 0; 721 String[][] zoneStrings = getZoneStringsWrapper(); 722 for (int index = 0; index < zoneStrings[0].length; ++index) 723 hashcode ^= zoneStrings[0][index].hashCode(); 724 return hashcode; 725 } 726 727 /** 728 * Override equals 729 */ equals(Object obj)730 public boolean equals(Object obj) 731 { 732 if (this == obj) return true; 733 if (obj == null || getClass() != obj.getClass()) return false; 734 DateFormatSymbols that = (DateFormatSymbols) obj; 735 return (Arrays.equals(eras, that.eras) 736 && Arrays.equals(months, that.months) 737 && Arrays.equals(shortMonths, that.shortMonths) 738 && Arrays.equals(tinyMonths, that.tinyMonths) 739 && Arrays.equals(weekdays, that.weekdays) 740 && Arrays.equals(shortWeekdays, that.shortWeekdays) 741 && Arrays.equals(tinyWeekdays, that.tinyWeekdays) 742 && Arrays.equals(standAloneMonths, that.standAloneMonths) 743 && Arrays.equals(shortStandAloneMonths, that.shortStandAloneMonths) 744 && Arrays.equals(tinyStandAloneMonths, that.tinyStandAloneMonths) 745 && Arrays.equals(standAloneWeekdays, that.standAloneWeekdays) 746 && Arrays.equals(shortStandAloneWeekdays, that.shortStandAloneWeekdays) 747 && Arrays.equals(tinyStandAloneWeekdays, that.tinyStandAloneWeekdays) 748 && Arrays.equals(ampms, that.ampms) 749 && Arrays.deepEquals(getZoneStringsWrapper(), that.getZoneStringsWrapper()) 750 && ((localPatternChars != null 751 && localPatternChars.equals(that.localPatternChars)) 752 || (localPatternChars == null 753 && that.localPatternChars == null))); 754 } 755 756 // =======================privates=============================== 757 758 /** 759 * Useful constant for defining time zone offsets. 760 */ 761 static final int millisPerHour = 60*60*1000; 762 763 /** 764 * Cache to hold DateFormatSymbols instances per Locale. 765 */ 766 private static final ConcurrentMap<Locale, SoftReference<DateFormatSymbols>> cachedInstances 767 = new ConcurrentHashMap<Locale, SoftReference<DateFormatSymbols>>(3); 768 769 private transient int lastZoneIndex = 0; 770 initializeData(Locale desiredLocale)771 private void initializeData(Locale desiredLocale) { 772 locale = desiredLocale; 773 774 // Copy values of a cached instance if any. 775 SoftReference<DateFormatSymbols> ref = cachedInstances.get(locale); 776 DateFormatSymbols dfs; 777 if (ref != null && (dfs = ref.get()) != null) { 778 copyMembers(dfs, this); 779 return; 780 } 781 locale = LocaleData.mapInvalidAndNullLocales(locale); 782 LocaleData localeData = LocaleData.get(locale); 783 784 eras = localeData.eras; 785 786 // Month names. 787 months = localeData.longMonthNames; 788 shortMonths = localeData.shortMonthNames; 789 790 ampms = localeData.amPm; 791 localPatternChars = patternChars; 792 793 // Weekdays. 794 weekdays = localeData.longWeekdayNames; 795 shortWeekdays = localeData.shortWeekdayNames; 796 797 initializeSupplementaryData(localeData); 798 } 799 initializeSupplementaryData(LocaleData localeData)800 private void initializeSupplementaryData(LocaleData localeData) { 801 // Tiny weekdays and months. 802 tinyMonths = localeData.tinyMonthNames; 803 tinyWeekdays = localeData.tinyWeekdayNames; 804 805 // Standalone month names. 806 standAloneMonths = localeData.longStandAloneMonthNames; 807 shortStandAloneMonths = localeData.shortStandAloneMonthNames; 808 tinyStandAloneMonths = localeData.tinyStandAloneMonthNames; 809 810 // Standalone weekdays. 811 standAloneWeekdays = localeData.longStandAloneWeekdayNames; 812 shortStandAloneWeekdays = localeData.shortStandAloneWeekdayNames; 813 tinyStandAloneWeekdays = localeData.tinyStandAloneWeekdayNames; 814 } 815 816 /** 817 * Package private: used by SimpleDateFormat 818 * Gets the index for the given time zone ID to obtain the time zone 819 * strings for formatting. The time zone ID is just for programmatic 820 * lookup. NOT LOCALIZED!!! 821 * @param ID the given time zone ID. 822 * @return the index of the given time zone ID. Returns -1 if 823 * the given time zone ID can't be located in the DateFormatSymbols object. 824 * @see java.util.SimpleTimeZone 825 */ getZoneIndex(String ID)826 final int getZoneIndex(String ID) { 827 String[][] zoneStrings = getZoneStringsWrapper(); 828 829 /* 830 * getZoneIndex has been re-written for performance reasons. instead of 831 * traversing the zoneStrings array every time, we cache the last used zone 832 * index 833 */ 834 if (lastZoneIndex < zoneStrings.length && ID.equals(zoneStrings[lastZoneIndex][0])) { 835 return lastZoneIndex; 836 } 837 838 /* slow path, search entire list */ 839 for (int index = 0; index < zoneStrings.length; index++) { 840 if (ID.equals(zoneStrings[index][0])) { 841 lastZoneIndex = index; 842 return index; 843 } 844 } 845 846 return -1; 847 } 848 849 /** 850 * Wrapper method to the getZoneStrings(), which is called from inside 851 * the java.text package and not to mutate the returned arrays, so that 852 * it does not need to create a defensive copy. 853 */ getZoneStringsWrapper()854 final String[][] getZoneStringsWrapper() { 855 if (isSubclassObject()) { 856 return getZoneStrings(); 857 } else { 858 return getZoneStringsImpl(false); 859 } 860 } 861 internalZoneStrings()862 private final synchronized String[][] internalZoneStrings() { 863 if (zoneStrings == null) { 864 zoneStrings = TimeZoneNames.getZoneStrings(locale); 865 // If icu4c doesn't have a name, our array contains a null. TimeZone.getDisplayName 866 // knows how to format GMT offsets (and, unlike icu4c, has accurate data). http://b/8128460. 867 for (String[] zone : zoneStrings) { 868 String id = zone[0]; 869 if (zone[1] == null) { 870 zone[1] = 871 TimeZone.getTimeZone(id).getDisplayName(false, TimeZone.LONG, locale); 872 } 873 if (zone[2] == null) { 874 zone[2] = 875 TimeZone.getTimeZone(id).getDisplayName(false, TimeZone.SHORT, locale); 876 } 877 if (zone[3] == null) { 878 zone[3] = TimeZone.getTimeZone(id).getDisplayName(true, TimeZone.LONG, locale); 879 } 880 if (zone[4] == null) { 881 zone[4] = 882 TimeZone.getTimeZone(id).getDisplayName(true, TimeZone.SHORT, locale); 883 } 884 } 885 } 886 return zoneStrings; 887 } 888 getZoneStringsImpl(boolean needsCopy)889 private final String[][] getZoneStringsImpl(boolean needsCopy) { 890 String[][] zoneStrings = internalZoneStrings(); 891 892 if (!needsCopy) { 893 return zoneStrings; 894 } 895 896 int len = zoneStrings.length; 897 String[][] aCopy = new String[len][]; 898 for (int i = 0; i < len; i++) { 899 aCopy[i] = Arrays.copyOf(zoneStrings[i], zoneStrings[i].length); 900 } 901 return aCopy; 902 } 903 isSubclassObject()904 private final boolean isSubclassObject() { 905 return !getClass().getName().equals("java.text.DateFormatSymbols"); 906 } 907 908 /** 909 * Clones all the data members from the source DateFormatSymbols to 910 * the target DateFormatSymbols. This is only for subclasses. 911 * @param src the source DateFormatSymbols. 912 * @param dst the target DateFormatSymbols. 913 */ copyMembers(DateFormatSymbols src, DateFormatSymbols dst)914 private final void copyMembers(DateFormatSymbols src, DateFormatSymbols dst) 915 { 916 dst.eras = Arrays.copyOf(src.eras, src.eras.length); 917 dst.months = Arrays.copyOf(src.months, src.months.length); 918 dst.shortMonths = Arrays.copyOf(src.shortMonths, src.shortMonths.length); 919 dst.weekdays = Arrays.copyOf(src.weekdays, src.weekdays.length); 920 dst.shortWeekdays = Arrays.copyOf(src.shortWeekdays, src.shortWeekdays.length); 921 dst.ampms = Arrays.copyOf(src.ampms, src.ampms.length); 922 if (src.zoneStrings != null) { 923 dst.zoneStrings = src.getZoneStringsImpl(true); 924 } else { 925 dst.zoneStrings = null; 926 } 927 dst.localPatternChars = src.localPatternChars; 928 929 dst.tinyMonths = src.tinyMonths; 930 dst.tinyWeekdays = src.tinyWeekdays; 931 932 dst.standAloneMonths = src.standAloneMonths; 933 dst.shortStandAloneMonths = src.shortStandAloneMonths; 934 dst.tinyStandAloneMonths = src.tinyStandAloneMonths; 935 936 dst.standAloneWeekdays = src.standAloneWeekdays; 937 dst.shortStandAloneWeekdays = src.shortStandAloneWeekdays; 938 dst.tinyStandAloneWeekdays = src.tinyStandAloneWeekdays; 939 } 940 readObject(ObjectInputStream stream)941 private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { 942 stream.defaultReadObject(); 943 944 if (serialVersionOnStream < 1) { 945 LocaleData localeData = LocaleData.get(locale); 946 initializeSupplementaryData(localeData); 947 } 948 949 serialVersionOnStream = currentSerialVersion; 950 } 951 952 /** 953 * Write out the default serializable data, after ensuring the 954 * <code>zoneStrings</code> field is initialized in order to make 955 * sure the backward compatibility. 956 * 957 * @since 1.6 958 */ writeObject(ObjectOutputStream stream)959 private void writeObject(ObjectOutputStream stream) throws IOException { 960 internalZoneStrings(); 961 stream.defaultWriteObject(); 962 } 963 964 /** 965 * Obtains a DateFormatSymbols instance from a DateFormatSymbolsProvider 966 * implementation. 967 */ 968 private static class DateFormatSymbolsGetter 969 implements LocaleServiceProviderPool.LocalizedObjectGetter<DateFormatSymbolsProvider, 970 DateFormatSymbols> { 971 private static final DateFormatSymbolsGetter INSTANCE = 972 new DateFormatSymbolsGetter(); 973 getObject(DateFormatSymbolsProvider dateFormatSymbolsProvider, Locale locale, String key, Object... params)974 public DateFormatSymbols getObject(DateFormatSymbolsProvider dateFormatSymbolsProvider, 975 Locale locale, 976 String key, 977 Object... params) { 978 assert params.length == 0; 979 return dateFormatSymbolsProvider.getInstance(locale); 980 } 981 } 982 } 983