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