1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * Copyright (c) 1996, 2016, 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.util.Arrays; 48 import java.util.Locale; 49 import java.util.Objects; 50 import java.util.concurrent.ConcurrentHashMap; 51 import java.util.concurrent.ConcurrentMap; 52 53 import libcore.icu.ICU; 54 import libcore.icu.LocaleData; 55 import libcore.icu.TimeZoneNames; 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>SimpleDateFormat</code> uses 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 // Android-changed: Removed reference to DateFormatSymbolsProvider but suggested getInstance() 105 // be used instead in case Android supports it in future. 106 /** 107 * Construct a DateFormatSymbols object by loading format data from 108 * resources for the default {@link java.util.Locale.Category#FORMAT FORMAT} 109 * locale. It is recommended that the {@link #getInstance(Locale) getInstance} method is used 110 * instead. 111 * <p>This is equivalent to calling 112 * {@link #DateFormatSymbols(Locale) 113 * DateFormatSymbols(Locale.getDefault(Locale.Category.FORMAT))}. 114 * @see #getInstance() 115 * @see java.util.Locale#getDefault(java.util.Locale.Category) 116 * @see java.util.Locale.Category#FORMAT 117 * @exception java.util.MissingResourceException 118 * if the resources for the default locale cannot be 119 * found or cannot be loaded. 120 */ 121 public DateFormatSymbols() 122 { 123 initializeData(Locale.getDefault(Locale.Category.FORMAT)); 124 } 125 126 // Android-changed: Removed reference to DateFormatSymbolsProvider but suggested getInstance() 127 // be used instead in case Android supports it in future. 128 /** 129 * Construct a DateFormatSymbols object by loading format data from 130 * resources for the given locale. It is recommended that the 131 * {@link #getInstance(Locale) getInstance} method is used instead. 132 * 133 * @param locale the desired locale 134 * @see #getInstance(Locale) 135 * @exception java.util.MissingResourceException 136 * if the resources for the specified locale cannot be 137 * found or cannot be loaded. 138 */ 139 public DateFormatSymbols(Locale locale) 140 { 141 initializeData(locale); 142 } 143 144 // Android-removed: unused private DateFormatSymbols(boolean) constructor. 145 146 /** 147 * Era strings. For example: "AD" and "BC". An array of 2 strings, 148 * indexed by <code>Calendar.BC</code> and <code>Calendar.AD</code>. 149 * @serial 150 */ 151 String eras[] = null; 152 153 /** 154 * Month strings. For example: "January", "February", etc. An array 155 * of 13 strings (some calendars have 13 months), indexed by 156 * <code>Calendar.JANUARY</code>, <code>Calendar.FEBRUARY</code>, etc. 157 * @serial 158 */ 159 String months[] = null; 160 161 /** 162 * Short month strings. For example: "Jan", "Feb", etc. An array of 163 * 13 strings (some calendars have 13 months), indexed by 164 * <code>Calendar.JANUARY</code>, <code>Calendar.FEBRUARY</code>, etc. 165 166 * @serial 167 */ 168 String shortMonths[] = null; 169 170 /** 171 * Weekday strings. For example: "Sunday", "Monday", etc. An array 172 * of 8 strings, indexed by <code>Calendar.SUNDAY</code>, 173 * <code>Calendar.MONDAY</code>, etc. 174 * The element <code>weekdays[0]</code> is ignored. 175 * @serial 176 */ 177 String weekdays[] = null; 178 179 /** 180 * Short weekday strings. For example: "Sun", "Mon", etc. An array 181 * of 8 strings, indexed by <code>Calendar.SUNDAY</code>, 182 * <code>Calendar.MONDAY</code>, etc. 183 * The element <code>shortWeekdays[0]</code> is ignored. 184 * @serial 185 */ 186 String shortWeekdays[] = null; 187 188 /** 189 * AM and PM strings. For example: "AM" and "PM". An array of 190 * 2 strings, indexed by <code>Calendar.AM</code> and 191 * <code>Calendar.PM</code>. 192 * @serial 193 */ 194 String ampms[] = null; 195 196 /** 197 * Localized names of time zones in this locale. This is a 198 * two-dimensional array of strings of size <em>n</em> by <em>m</em>, 199 * where <em>m</em> is at least 5. Each of the <em>n</em> rows is an 200 * entry containing the localized names for a single <code>TimeZone</code>. 201 * Each such row contains (with <code>i</code> ranging from 202 * 0..<em>n</em>-1): 203 * <ul> 204 * <li><code>zoneStrings[i][0]</code> - time zone ID</li> 205 * <li><code>zoneStrings[i][1]</code> - long name of zone in standard 206 * time</li> 207 * <li><code>zoneStrings[i][2]</code> - short name of zone in 208 * standard time</li> 209 * <li><code>zoneStrings[i][3]</code> - long name of zone in daylight 210 * saving time</li> 211 * <li><code>zoneStrings[i][4]</code> - short name of zone in daylight 212 * saving time</li> 213 * </ul> 214 * The zone ID is <em>not</em> localized; it's one of the valid IDs of 215 * the {@link java.util.TimeZone TimeZone} class that are not 216 * <a href="../java/util/TimeZone.html#CustomID">custom IDs</a>. 217 * All other entries are localized names. 218 * @see java.util.TimeZone 219 * @serial 220 */ 221 String zoneStrings[][] = null; 222 223 /** 224 * Indicates that zoneStrings is set externally with setZoneStrings() method. 225 */ 226 transient boolean isZoneStringsSet = false; 227 228 /** 229 * Unlocalized date-time pattern characters. For example: 'y', 'd', etc. 230 * All locales use the same these unlocalized pattern characters. 231 */ 232 // Android-changed: Add 'c' (standalone day of week), 'b' (day period), 233 // 'B' (flexible day period) 234 static final String patternChars = "GyMdkHmsSEDFwWahKzZYuXLcbB"; 235 236 static final int PATTERN_ERA = 0; // G 237 static final int PATTERN_YEAR = 1; // y 238 static final int PATTERN_MONTH = 2; // M 239 static final int PATTERN_DAY_OF_MONTH = 3; // d 240 static final int PATTERN_HOUR_OF_DAY1 = 4; // k 241 static final int PATTERN_HOUR_OF_DAY0 = 5; // H 242 static final int PATTERN_MINUTE = 6; // m 243 static final int PATTERN_SECOND = 7; // s 244 static final int PATTERN_MILLISECOND = 8; // S 245 static final int PATTERN_DAY_OF_WEEK = 9; // E 246 static final int PATTERN_DAY_OF_YEAR = 10; // D 247 static final int PATTERN_DAY_OF_WEEK_IN_MONTH = 11; // F 248 static final int PATTERN_WEEK_OF_YEAR = 12; // w 249 static final int PATTERN_WEEK_OF_MONTH = 13; // W 250 static final int PATTERN_AM_PM = 14; // a 251 static final int PATTERN_HOUR1 = 15; // h 252 static final int PATTERN_HOUR0 = 16; // K 253 static final int PATTERN_ZONE_NAME = 17; // z 254 static final int PATTERN_ZONE_VALUE = 18; // Z 255 static final int PATTERN_WEEK_YEAR = 19; // Y 256 static final int PATTERN_ISO_DAY_OF_WEEK = 20; // u 257 static final int PATTERN_ISO_ZONE = 21; // X 258 static final int PATTERN_MONTH_STANDALONE = 22; // L 259 // Android-added: Constant for standalone day of week. 260 static final int PATTERN_STANDALONE_DAY_OF_WEEK = 23; // c 261 // Android-added: Constant for pattern letter 'b', 'B' 262 static final int PATTERN_DAY_PERIOD = 24; // b 263 static final int PATTERN_FLEXIBLE_DAY_PERIOD = 25; // B 264 265 /** 266 * Localized date-time pattern characters. For example, a locale may 267 * wish to use 'u' rather than 'y' to represent years in its date format 268 * pattern strings. 269 * This string must be exactly 18 characters long, with the index of 270 * the characters described by <code>DateFormat.ERA_FIELD</code>, 271 * <code>DateFormat.YEAR_FIELD</code>, etc. Thus, if the string were 272 * "Xz...", then localized patterns would use 'X' for era and 'z' for year. 273 * @serial 274 */ 275 String localPatternChars = null; 276 277 /** 278 * The locale which is used for initializing this DateFormatSymbols object. 279 * 280 * @since 1.6 281 * @serial 282 */ 283 Locale locale = null; 284 285 /* use serialVersionUID from JDK 1.1.4 for interoperability */ 286 static final long serialVersionUID = -5987973545549424702L; 287 288 // BEGIN Android-added: Android specific serialization code. 289 // the internal serial version which says which version was written 290 // - 0 (default) for version up to JDK 1.1.4 291 // - 1 Android version that contains a whole bunch of new fields. 292 static final int currentSerialVersion = 1; 293 294 /** 295 * The version of the serialized data on the stream. Possible values: 296 * <ul> 297 * <li><b>0</b> or not present on stream: JDK 1.1.4. 298 * <li><b>1</b> Android: 299 * </ul> 300 * When streaming out this class, the most recent format 301 * and the highest allowable <code>serialVersionOnStream</code> 302 * is written. 303 * @serial 304 * @since JDK1.1.4 305 */ 306 private int serialVersionOnStream = currentSerialVersion; 307 // END Android-added: Android specific serialization code. 308 309 // BEGIN Android-added: Support for tiny and standalone field names. 310 /** 311 * Tiny month strings; "J", "F", "M" etc. 312 * 313 * @serial 314 */ 315 private String[] tinyMonths; 316 317 /** 318 * Tiny weekday strings: "M", "F", "W" etc. 319 * 320 * @serial 321 */ 322 private String[] tinyWeekdays; 323 324 /** 325 * Standalone month strings; "January", "February", "March" etc. 326 * 327 * @serial 328 */ 329 private String[] standAloneMonths; 330 331 /** 332 * Short standalone month strings: "Jan", "Feb", "Mar" etc. 333 * 334 * @serial 335 */ 336 private String[] shortStandAloneMonths; 337 338 /** 339 * Tiny standalone month strings: "J", "F", "M" etc. 340 * 341 * @serial 342 */ 343 private String[] tinyStandAloneMonths; 344 345 /** 346 * Standalone weekday strings; "Monday", "Tuesday", "Wednesday" etc. 347 * 348 * @serial 349 */ 350 private String[] standAloneWeekdays; 351 352 /** 353 * Short standalone weekday strings; "Mon", "Tue", "Wed" etc. 354 * 355 * @serial 356 */ 357 private String[] shortStandAloneWeekdays; 358 359 /** 360 * Tiny standalone weekday strings; "M", "T", "W" etc. 361 * 362 * @serial 363 */ 364 private String[] tinyStandAloneWeekdays; 365 // END Android-added: Support for tiny and standalone field names. 366 367 // Android-changed: Removed reference to DateFormatSymbolsProvider. 368 /** 369 * Returns an array of all locales for which the 370 * <code>getInstance</code> methods of this class can return 371 * localized instances. 372 * 373 * @return An array of locales for which localized 374 * <code>DateFormatSymbols</code> instances are available. 375 * @since 1.6 376 */ 377 public static Locale[] getAvailableLocales() { 378 // Android-changed: No support for DateFormatSymbolsProvider. 379 return ICU.getAvailableLocales(); 380 } 381 382 // Android-changed: Removed reference to DateFormatSymbolsProvider. 383 /** 384 * Gets the <code>DateFormatSymbols</code> instance for the default 385 * locale. 386 * <p>This is equivalent to calling {@link #getInstance(Locale) 387 * getInstance(Locale.getDefault(Locale.Category.FORMAT))}. 388 * @see java.util.Locale#getDefault(java.util.Locale.Category) 389 * @see java.util.Locale.Category#FORMAT 390 * @return a <code>DateFormatSymbols</code> instance. 391 * @since 1.6 392 */ 393 public static final DateFormatSymbols getInstance() { 394 return getInstance(Locale.getDefault(Locale.Category.FORMAT)); 395 } 396 397 // Android-changed: Removed reference to DateFormatSymbolsProvider. 398 /** 399 * Gets the <code>DateFormatSymbols</code> instance for the specified 400 * locale. 401 * @param locale the given locale. 402 * @return a <code>DateFormatSymbols</code> instance. 403 * @exception NullPointerException if <code>locale</code> is null 404 * @since 1.6 405 */ 406 public static final DateFormatSymbols getInstance(Locale locale) { 407 // Android-changed: Removed used of DateFormatSymbolsProvider. 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 */ 417 static final DateFormatSymbols getInstanceRef(Locale locale) { 418 // Android-changed: Removed used of DateFormatSymbolsProvider. 419 return getCachedInstance(locale); 420 } 421 422 // BEGIN Android-changed: Replace getProviderInstance() with getCachedInstance(). 423 // Android removed support for DateFormatSymbolsProviders, but still caches DFS. 424 /** 425 * Returns a cached DateFormatSymbols if it's found in the 426 * cache. Otherwise, this method returns a newly cached instance 427 * for the given locale. 428 */ 429 private static DateFormatSymbols getCachedInstance(Locale locale) { 430 SoftReference<DateFormatSymbols> ref = cachedInstances.get(locale); 431 DateFormatSymbols dfs; 432 if (ref == null || (dfs = ref.get()) == null) { 433 dfs = new DateFormatSymbols(locale); 434 ref = new SoftReference<>(dfs); 435 SoftReference<DateFormatSymbols> x = cachedInstances.putIfAbsent(locale, ref); 436 if (x != null) { 437 DateFormatSymbols y = x.get(); 438 if (y != null) { 439 dfs = y; 440 } else { 441 // Replace the empty SoftReference with ref. 442 cachedInstances.put(locale, ref); 443 } 444 } 445 } 446 return dfs; 447 } 448 // END Android-changed: Replace getProviderInstance() with getCachedInstance(). 449 450 /** 451 * Gets era strings. For example: "AD" and "BC". 452 * @return the era strings. 453 */ 454 public String[] getEras() { 455 return Arrays.copyOf(eras, eras.length); 456 } 457 458 /** 459 * Sets era strings. For example: "AD" and "BC". 460 * @param newEras the new era strings. 461 */ 462 public void setEras(String[] newEras) { 463 eras = Arrays.copyOf(newEras, newEras.length); 464 cachedHashCode = 0; 465 } 466 467 /** 468 * Gets month strings. For example: "January", "February", etc. 469 * 470 * <p>If the language requires different forms for formatting and 471 * stand-alone usages, this method returns month names in the 472 * formatting form. For example, the preferred month name for 473 * January in the Czech language is <em>ledna</em> in the 474 * formatting form, while it is <em>leden</em> in the stand-alone 475 * form. This method returns {@code "ledna"} in this case. Refer 476 * to the <a href="http://unicode.org/reports/tr35/#Calendar_Elements"> 477 * Calendar Elements in the Unicode Locale Data Markup Language 478 * (LDML) specification</a> for more details. 479 * 480 * @return the month strings. 481 */ 482 public String[] getMonths() { 483 return Arrays.copyOf(months, months.length); 484 } 485 486 /** 487 * Sets month strings. For example: "January", "February", etc. 488 * @param newMonths the new month strings. 489 */ 490 public void setMonths(String[] newMonths) { 491 months = Arrays.copyOf(newMonths, newMonths.length); 492 cachedHashCode = 0; 493 } 494 495 /** 496 * Gets short month strings. For example: "Jan", "Feb", etc. 497 * 498 * <p>If the language requires different forms for formatting and 499 * stand-alone usages, This method returns short month names in 500 * the formatting form. For example, the preferred abbreviation 501 * for January in the Catalan language is <em>de gen.</em> in the 502 * formatting form, while it is <em>gen.</em> in the stand-alone 503 * form. This method returns {@code "de gen."} in this case. Refer 504 * to the <a href="http://unicode.org/reports/tr35/#Calendar_Elements"> 505 * Calendar Elements in the Unicode Locale Data Markup Language 506 * (LDML) specification</a> for more details. 507 * 508 * @return the short month strings. 509 */ 510 public String[] getShortMonths() { 511 return Arrays.copyOf(shortMonths, shortMonths.length); 512 } 513 514 /** 515 * Sets short month strings. For example: "Jan", "Feb", etc. 516 * @param newShortMonths the new short month strings. 517 */ 518 public void setShortMonths(String[] newShortMonths) { 519 shortMonths = Arrays.copyOf(newShortMonths, newShortMonths.length); 520 cachedHashCode = 0; 521 } 522 523 /** 524 * Gets weekday strings. For example: "Sunday", "Monday", etc. 525 * @return the weekday strings. Use <code>Calendar.SUNDAY</code>, 526 * <code>Calendar.MONDAY</code>, etc. to index the result array. 527 */ 528 public String[] getWeekdays() { 529 return Arrays.copyOf(weekdays, weekdays.length); 530 } 531 532 /** 533 * Sets weekday strings. For example: "Sunday", "Monday", etc. 534 * @param newWeekdays the new weekday strings. The array should 535 * be indexed by <code>Calendar.SUNDAY</code>, 536 * <code>Calendar.MONDAY</code>, etc. 537 */ 538 public void setWeekdays(String[] newWeekdays) { 539 weekdays = Arrays.copyOf(newWeekdays, newWeekdays.length); 540 cachedHashCode = 0; 541 } 542 543 /** 544 * Gets short weekday strings. For example: "Sun", "Mon", etc. 545 * @return the short weekday strings. Use <code>Calendar.SUNDAY</code>, 546 * <code>Calendar.MONDAY</code>, etc. to index the result array. 547 */ 548 public String[] getShortWeekdays() { 549 return Arrays.copyOf(shortWeekdays, shortWeekdays.length); 550 } 551 552 /** 553 * Sets short weekday strings. For example: "Sun", "Mon", etc. 554 * @param newShortWeekdays the new short weekday strings. The array should 555 * be indexed by <code>Calendar.SUNDAY</code>, 556 * <code>Calendar.MONDAY</code>, etc. 557 */ 558 public void setShortWeekdays(String[] newShortWeekdays) { 559 shortWeekdays = Arrays.copyOf(newShortWeekdays, newShortWeekdays.length); 560 cachedHashCode = 0; 561 } 562 563 /** 564 * Gets ampm strings. For example: "AM" and "PM". 565 * @return the ampm strings. 566 */ 567 public String[] getAmPmStrings() { 568 return Arrays.copyOf(ampms, ampms.length); 569 } 570 571 /** 572 * Sets ampm strings. For example: "AM" and "PM". 573 * @param newAmpms the new ampm strings. 574 */ 575 public void setAmPmStrings(String[] newAmpms) { 576 ampms = Arrays.copyOf(newAmpms, newAmpms.length); 577 cachedHashCode = 0; 578 } 579 580 // Android-changed: Removed reference to TimeZoneNameProvider. 581 /** 582 * Gets time zone strings. Use of this method is discouraged; use 583 * {@link java.util.TimeZone#getDisplayName() TimeZone.getDisplayName()} 584 * instead. 585 * <p> 586 * The value returned is a 587 * two-dimensional array of strings of size <em>n</em> by <em>m</em>, 588 * where <em>m</em> is at least 5. Each of the <em>n</em> rows is an 589 * entry containing the localized names for a single <code>TimeZone</code>. 590 * Each such row contains (with <code>i</code> ranging from 591 * 0..<em>n</em>-1): 592 * <ul> 593 * <li><code>zoneStrings[i][0]</code> - time zone ID</li> 594 * <li><code>zoneStrings[i][1]</code> - long name of zone in standard 595 * time</li> 596 * <li><code>zoneStrings[i][2]</code> - short name of zone in 597 * standard time</li> 598 * <li><code>zoneStrings[i][3]</code> - long name of zone in daylight 599 * saving time</li> 600 * <li><code>zoneStrings[i][4]</code> - short name of zone in daylight 601 * saving time</li> 602 * </ul> 603 * The zone ID is <em>not</em> localized; it's one of the valid IDs of 604 * the {@link java.util.TimeZone TimeZone} class that are not 605 * <a href="../util/TimeZone.html#CustomID">custom IDs</a>. 606 * All other entries are localized names. If a zone does not implement 607 * daylight saving time, the daylight saving time names should not be used. 608 * <p> 609 * If {@link #setZoneStrings(String[][]) setZoneStrings} has been called 610 * on this <code>DateFormatSymbols</code> instance, then the strings 611 * provided by that call are returned. Otherwise, the returned array 612 * contains names provided by the runtime. 613 * 614 * @return the time zone strings. 615 * @see #setZoneStrings(String[][]) 616 */ 617 public String[][] getZoneStrings() { 618 return getZoneStringsImpl(true); 619 } 620 621 /** 622 * Sets time zone strings. The argument must be a 623 * two-dimensional array of strings of size <em>n</em> by <em>m</em>, 624 * where <em>m</em> is at least 5. Each of the <em>n</em> rows is an 625 * entry containing the localized names for a single <code>TimeZone</code>. 626 * Each such row contains (with <code>i</code> ranging from 627 * 0..<em>n</em>-1): 628 * <ul> 629 * <li><code>zoneStrings[i][0]</code> - time zone ID</li> 630 * <li><code>zoneStrings[i][1]</code> - long name of zone in standard 631 * time</li> 632 * <li><code>zoneStrings[i][2]</code> - short name of zone in 633 * standard time</li> 634 * <li><code>zoneStrings[i][3]</code> - long name of zone in daylight 635 * saving time</li> 636 * <li><code>zoneStrings[i][4]</code> - short name of zone in daylight 637 * saving time</li> 638 * </ul> 639 * The zone ID is <em>not</em> localized; it's one of the valid IDs of 640 * the {@link java.util.TimeZone TimeZone} class that are not 641 * <a href="../util/TimeZone.html#CustomID">custom IDs</a>. 642 * All other entries are localized names. 643 * 644 * @param newZoneStrings the new time zone strings. 645 * @exception IllegalArgumentException if the length of any row in 646 * <code>newZoneStrings</code> is less than 5 647 * @exception NullPointerException if <code>newZoneStrings</code> is null 648 * @see #getZoneStrings() 649 */ 650 public void setZoneStrings(String[][] newZoneStrings) { 651 String[][] aCopy = new String[newZoneStrings.length][]; 652 for (int i = 0; i < newZoneStrings.length; ++i) { 653 int len = newZoneStrings[i].length; 654 if (len < 5) { 655 throw new IllegalArgumentException(); 656 } 657 aCopy[i] = Arrays.copyOf(newZoneStrings[i], len); 658 } 659 zoneStrings = aCopy; 660 isZoneStringsSet = true; 661 // Android-changed: don't include zone strings in hashCode to avoid populating it. 662 // cachedHashCode = 0; 663 } 664 665 /** 666 * Gets localized date-time pattern characters. For example: 'u', 't', etc. 667 * @return the localized date-time pattern characters. 668 */ 669 public String getLocalPatternChars() { 670 return localPatternChars; 671 } 672 673 /** 674 * Sets localized date-time pattern characters. For example: 'u', 't', etc. 675 * @param newLocalPatternChars the new localized date-time 676 * pattern characters. 677 */ 678 public void setLocalPatternChars(String newLocalPatternChars) { 679 // Call toString() to throw an NPE in case the argument is null 680 localPatternChars = newLocalPatternChars.toString(); 681 cachedHashCode = 0; 682 } 683 684 // BEGIN Android-added: Support for tiny and standalone field names. 685 String[] getTinyMonths() { 686 return tinyMonths; 687 } 688 689 String[] getStandAloneMonths() { 690 return standAloneMonths; 691 } 692 693 String[] getShortStandAloneMonths() { 694 return shortStandAloneMonths; 695 } 696 697 String[] getTinyStandAloneMonths() { 698 return tinyStandAloneMonths; 699 } 700 701 String[] getTinyWeekdays() { 702 return tinyWeekdays; 703 } 704 705 String[] getStandAloneWeekdays() { 706 return standAloneWeekdays; 707 } 708 709 String[] getShortStandAloneWeekdays() { 710 return shortStandAloneWeekdays; 711 } 712 713 String[] getTinyStandAloneWeekdays() { 714 return tinyStandAloneWeekdays; 715 } 716 // END Android-added: Support for tiny and standalone field names. 717 718 /** 719 * Overrides Cloneable 720 */ 721 public Object clone() 722 { 723 try 724 { 725 DateFormatSymbols other = (DateFormatSymbols)super.clone(); 726 copyMembers(this, other); 727 return other; 728 } catch (CloneNotSupportedException e) { 729 throw new InternalError(e); 730 } 731 } 732 733 /** 734 * Override hashCode. 735 * Generates a hash code for the DateFormatSymbols object. 736 */ 737 @Override 738 public int hashCode() { 739 int hashCode = cachedHashCode; 740 if (hashCode == 0) { 741 hashCode = 5; 742 hashCode = 11 * hashCode + Arrays.hashCode(eras); 743 hashCode = 11 * hashCode + Arrays.hashCode(months); 744 hashCode = 11 * hashCode + Arrays.hashCode(shortMonths); 745 hashCode = 11 * hashCode + Arrays.hashCode(weekdays); 746 hashCode = 11 * hashCode + Arrays.hashCode(shortWeekdays); 747 hashCode = 11 * hashCode + Arrays.hashCode(ampms); 748 // Android-changed: Don't include zone strings in hashCode to avoid populating it. 749 // hashCode = 11 * hashCode + Arrays.deepHashCode(getZoneStringsWrapper()); 750 hashCode = 11 * hashCode + Objects.hashCode(localPatternChars); 751 cachedHashCode = hashCode; 752 } 753 754 return hashCode; 755 } 756 757 /** 758 * Override equals 759 */ 760 public boolean equals(Object obj) 761 { 762 if (this == obj) return true; 763 if (obj == null || getClass() != obj.getClass()) return false; 764 DateFormatSymbols that = (DateFormatSymbols) obj; 765 // BEGIN Android-changed: Avoid populating zoneStrings just for the comparison, add fields. 766 if (!(Arrays.equals(eras, that.eras) 767 && Arrays.equals(months, that.months) 768 && Arrays.equals(shortMonths, that.shortMonths) 769 && Arrays.equals(tinyMonths, that.tinyMonths) 770 && Arrays.equals(weekdays, that.weekdays) 771 && Arrays.equals(shortWeekdays, that.shortWeekdays) 772 && Arrays.equals(tinyWeekdays, that.tinyWeekdays) 773 && Arrays.equals(standAloneMonths, that.standAloneMonths) 774 && Arrays.equals(shortStandAloneMonths, that.shortStandAloneMonths) 775 && Arrays.equals(tinyStandAloneMonths, that.tinyStandAloneMonths) 776 && Arrays.equals(standAloneWeekdays, that.standAloneWeekdays) 777 && Arrays.equals(shortStandAloneWeekdays, that.shortStandAloneWeekdays) 778 && Arrays.equals(tinyStandAloneWeekdays, that.tinyStandAloneWeekdays) 779 && Arrays.equals(ampms, that.ampms) 780 && ((localPatternChars != null 781 && localPatternChars.equals(that.localPatternChars)) 782 || (localPatternChars == null 783 && that.localPatternChars == null)))) { 784 return false; 785 } 786 if (!isZoneStringsSet && !that.isZoneStringsSet && Objects.equals(locale, that.locale)) { 787 return true; 788 } 789 return Arrays.deepEquals(getZoneStringsWrapper(), that.getZoneStringsWrapper()); 790 // END Android-changed: Avoid populating zoneStrings just for the comparison. 791 } 792 793 // =======================privates=============================== 794 795 /** 796 * Useful constant for defining time zone offsets. 797 */ 798 static final int millisPerHour = 60*60*1000; 799 800 /** 801 * Cache to hold DateFormatSymbols instances per Locale. 802 */ 803 private static final ConcurrentMap<Locale, SoftReference<DateFormatSymbols>> cachedInstances 804 = new ConcurrentHashMap<>(3); 805 806 private transient int lastZoneIndex = 0; 807 808 /** 809 * Cached hash code 810 */ 811 transient volatile int cachedHashCode = 0; 812 813 // Android-changed: update comment to describe local modification. 814 /** 815 * Initializes this DateFormatSymbols with the locale data. This method uses 816 * a cached DateFormatSymbols instance for the given locale if available. If 817 * there's no cached one, this method populates this objects fields from an 818 * appropriate LocaleData object. Note: zoneStrings isn't initialized in this method. 819 */ 820 private void initializeData(Locale locale) { 821 SoftReference<DateFormatSymbols> ref = cachedInstances.get(locale); 822 DateFormatSymbols dfs; 823 // Android-changed: invert cache presence check to simplify code flow. 824 if (ref != null && (dfs = ref.get()) != null) { 825 copyMembers(dfs, this); 826 return; 827 } 828 829 // BEGIN Android-changed: Use ICU data and move cache handling to getCachedInstance(). 830 locale = LocaleData.mapInvalidAndNullLocales(locale); 831 LocaleData localeData = LocaleData.get(locale); 832 833 this.locale = locale; 834 eras = localeData.eras; 835 months = localeData.longMonthNames; 836 shortMonths = localeData.shortMonthNames; 837 ampms = localeData.amPm; 838 localPatternChars = patternChars; 839 840 weekdays = localeData.longWeekdayNames; 841 shortWeekdays = localeData.shortWeekdayNames; 842 843 initializeSupplementaryData(localeData); 844 // END Android-changed: Use ICU data and move cache handling to getCachedInstance(). 845 } 846 847 // Android-removed: toOneBasedArray(String[]) 848 849 // BEGIN Android-added: initializeSupplementaryData(LocaleData) for tiny and standalone fields. 850 private void initializeSupplementaryData(LocaleData localeData) { 851 // Tiny weekdays and months. 852 tinyMonths = localeData.tinyMonthNames; 853 tinyWeekdays = localeData.tinyWeekdayNames; 854 855 // Standalone month names. 856 standAloneMonths = localeData.longStandAloneMonthNames; 857 shortStandAloneMonths = localeData.shortStandAloneMonthNames; 858 tinyStandAloneMonths = localeData.tinyStandAloneMonthNames; 859 860 // Standalone weekdays. 861 standAloneWeekdays = localeData.longStandAloneWeekdayNames; 862 shortStandAloneWeekdays = localeData.shortStandAloneWeekdayNames; 863 tinyStandAloneWeekdays = localeData.tinyStandAloneWeekdayNames; 864 } 865 // END Android-added: initializeSupplementaryData(LocaleData) for tiny and standalone fields. 866 867 /** 868 * Package private: used by SimpleDateFormat 869 * Gets the index for the given time zone ID to obtain the time zone 870 * strings for formatting. The time zone ID is just for programmatic 871 * lookup. NOT LOCALIZED!!! 872 * @param ID the given time zone ID. 873 * @return the index of the given time zone ID. Returns -1 if 874 * the given time zone ID can't be located in the DateFormatSymbols object. 875 * @see java.util.SimpleTimeZone 876 */ 877 final int getZoneIndex(String ID) { 878 String[][] zoneStrings = getZoneStringsWrapper(); 879 880 /* 881 * getZoneIndex has been re-written for performance reasons. instead of 882 * traversing the zoneStrings array every time, we cache the last used zone 883 * index 884 */ 885 if (lastZoneIndex < zoneStrings.length && ID.equals(zoneStrings[lastZoneIndex][0])) { 886 return lastZoneIndex; 887 } 888 889 /* slow path, search entire list */ 890 for (int index = 0; index < zoneStrings.length; index++) { 891 if (ID.equals(zoneStrings[index][0])) { 892 lastZoneIndex = index; 893 return index; 894 } 895 } 896 897 return -1; 898 } 899 900 /** 901 * Wrapper method to the getZoneStrings(), which is called from inside 902 * the java.text package and not to mutate the returned arrays, so that 903 * it does not need to create a defensive copy. 904 */ 905 final String[][] getZoneStringsWrapper() { 906 if (isSubclassObject()) { 907 return getZoneStrings(); 908 } else { 909 return getZoneStringsImpl(false); 910 } 911 } 912 913 // BEGIN Android-changed: extract initialization of zoneStrings to separate method. 914 private synchronized String[][] internalZoneStrings() { 915 if (zoneStrings == null) { 916 zoneStrings = TimeZoneNames.getZoneStrings(locale); 917 } 918 return zoneStrings; 919 } 920 921 private String[][] getZoneStringsImpl(boolean needsCopy) { 922 String[][] zoneStrings = internalZoneStrings(); 923 // END Android-changed: extract initialization of zoneStrings to separate method. 924 925 if (!needsCopy) { 926 return zoneStrings; 927 } 928 929 int len = zoneStrings.length; 930 String[][] aCopy = new String[len][]; 931 for (int i = 0; i < len; i++) { 932 aCopy[i] = Arrays.copyOf(zoneStrings[i], zoneStrings[i].length); 933 } 934 return aCopy; 935 } 936 937 private boolean isSubclassObject() { 938 return !getClass().getName().equals("java.text.DateFormatSymbols"); 939 } 940 941 /** 942 * Clones all the data members from the source DateFormatSymbols to 943 * the target DateFormatSymbols. 944 * 945 * @param src the source DateFormatSymbols. 946 * @param dst the target DateFormatSymbols. 947 */ 948 private void copyMembers(DateFormatSymbols src, DateFormatSymbols dst) 949 { 950 dst.locale = src.locale; 951 dst.eras = Arrays.copyOf(src.eras, src.eras.length); 952 dst.months = Arrays.copyOf(src.months, src.months.length); 953 dst.shortMonths = Arrays.copyOf(src.shortMonths, src.shortMonths.length); 954 dst.weekdays = Arrays.copyOf(src.weekdays, src.weekdays.length); 955 dst.shortWeekdays = Arrays.copyOf(src.shortWeekdays, src.shortWeekdays.length); 956 dst.ampms = Arrays.copyOf(src.ampms, src.ampms.length); 957 if (src.zoneStrings != null) { 958 dst.zoneStrings = src.getZoneStringsImpl(true); 959 } else { 960 dst.zoneStrings = null; 961 } 962 dst.localPatternChars = src.localPatternChars; 963 dst.cachedHashCode = 0; 964 965 // BEGIN Android-added: Support for tiny and standalone field names. 966 dst.tinyMonths = src.tinyMonths; 967 dst.tinyWeekdays = src.tinyWeekdays; 968 969 dst.standAloneMonths = src.standAloneMonths; 970 dst.shortStandAloneMonths = src.shortStandAloneMonths; 971 dst.tinyStandAloneMonths = src.tinyStandAloneMonths; 972 973 dst.standAloneWeekdays = src.standAloneWeekdays; 974 dst.shortStandAloneWeekdays = src.shortStandAloneWeekdays; 975 dst.tinyStandAloneWeekdays = src.tinyStandAloneWeekdays; 976 // END Android-added: Support for tiny and standalone field names. 977 } 978 979 // BEGIN Android-added: support reading non-Android serialized DFS. 980 private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { 981 stream.defaultReadObject(); 982 983 if (serialVersionOnStream < 1) { 984 LocaleData localeData = LocaleData.get(locale); 985 initializeSupplementaryData(localeData); 986 } 987 988 serialVersionOnStream = currentSerialVersion; 989 } 990 // END Android-added: support reading non-Android serialized DFS. 991 992 /** 993 * Write out the default serializable data, after ensuring the 994 * <code>zoneStrings</code> field is initialized in order to make 995 * sure the backward compatibility. 996 * 997 * @since 1.6 998 */ 999 private void writeObject(ObjectOutputStream stream) throws IOException { 1000 // Android-changed: extract initialization of zoneStrings to separate method. 1001 internalZoneStrings(); 1002 stream.defaultWriteObject(); 1003 } 1004 } 1005