Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/text/DateFormatSymbols.java
38829 views
1
/*
2
* Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
/*
27
* (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
28
* (C) Copyright IBM Corp. 1996 - All Rights Reserved
29
*
30
* The original version of this source code and documentation is copyrighted
31
* and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
32
* materials are provided under terms of a License Agreement between Taligent
33
* and Sun. This technology is protected by multiple US and International
34
* patents. This notice and attribution to Taligent may not be removed.
35
* Taligent is a registered trademark of Taligent, Inc.
36
*
37
*/
38
39
package java.text;
40
41
import java.io.IOException;
42
import java.io.ObjectOutputStream;
43
import java.io.Serializable;
44
import java.lang.ref.SoftReference;
45
import java.text.spi.DateFormatSymbolsProvider;
46
import java.util.Arrays;
47
import java.util.Locale;
48
import java.util.Objects;
49
import java.util.ResourceBundle;
50
import java.util.concurrent.ConcurrentHashMap;
51
import java.util.concurrent.ConcurrentMap;
52
import sun.util.locale.provider.LocaleProviderAdapter;
53
import sun.util.locale.provider.LocaleServiceProviderPool;
54
import sun.util.locale.provider.ResourceBundleBasedAdapter;
55
import sun.util.locale.provider.TimeZoneNameUtility;
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
/**
105
* Construct a DateFormatSymbols object by loading format data from
106
* resources for the default {@link java.util.Locale.Category#FORMAT FORMAT}
107
* 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
* <p>This is equivalent to calling
114
* {@link #DateFormatSymbols(Locale)
115
* DateFormatSymbols(Locale.getDefault(Locale.Category.FORMAT))}.
116
* @see #getInstance()
117
* @see java.util.Locale#getDefault(java.util.Locale.Category)
118
* @see java.util.Locale.Category#FORMAT
119
* @exception java.util.MissingResourceException
120
* if the resources for the default locale cannot be
121
* found or cannot be loaded.
122
*/
123
public DateFormatSymbols()
124
{
125
initializeData(Locale.getDefault(Locale.Category.FORMAT));
126
}
127
128
/**
129
* Construct a DateFormatSymbols object by loading format data from
130
* resources for the given locale. This constructor can only
131
* construct instances for the locales supported by the Java
132
* runtime environment, not for those supported by installed
133
* {@link java.text.spi.DateFormatSymbolsProvider DateFormatSymbolsProvider}
134
* implementations. For full locale coverage, use the
135
* {@link #getInstance(Locale) getInstance} method.
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
*/
143
public DateFormatSymbols(Locale locale)
144
{
145
initializeData(locale);
146
}
147
148
/**
149
* Constructs an uninitialized DateFormatSymbols.
150
*/
151
private DateFormatSymbols(boolean flag) {
152
}
153
154
/**
155
* Era strings. For example: "AD" and "BC". An array of 2 strings,
156
* indexed by <code>Calendar.BC</code> and <code>Calendar.AD</code>.
157
* @serial
158
*/
159
String eras[] = null;
160
161
/**
162
* Month strings. For example: "January", "February", etc. An array
163
* of 13 strings (some calendars have 13 months), indexed by
164
* <code>Calendar.JANUARY</code>, <code>Calendar.FEBRUARY</code>, etc.
165
* @serial
166
*/
167
String months[] = null;
168
169
/**
170
* Short month strings. For example: "Jan", "Feb", etc. An array of
171
* 13 strings (some calendars have 13 months), indexed by
172
* <code>Calendar.JANUARY</code>, <code>Calendar.FEBRUARY</code>, etc.
173
174
* @serial
175
*/
176
String shortMonths[] = null;
177
178
/**
179
* Weekday strings. For example: "Sunday", "Monday", etc. An array
180
* of 8 strings, indexed by <code>Calendar.SUNDAY</code>,
181
* <code>Calendar.MONDAY</code>, etc.
182
* The element <code>weekdays[0]</code> is ignored.
183
* @serial
184
*/
185
String weekdays[] = null;
186
187
/**
188
* Short weekday strings. For example: "Sun", "Mon", etc. An array
189
* of 8 strings, indexed by <code>Calendar.SUNDAY</code>,
190
* <code>Calendar.MONDAY</code>, etc.
191
* The element <code>shortWeekdays[0]</code> is ignored.
192
* @serial
193
*/
194
String shortWeekdays[] = null;
195
196
/**
197
* AM and PM strings. For example: "AM" and "PM". An array of
198
* 2 strings, indexed by <code>Calendar.AM</code> and
199
* <code>Calendar.PM</code>.
200
* @serial
201
*/
202
String ampms[] = null;
203
204
/**
205
* Localized names of time zones in this locale. This is a
206
* two-dimensional array of strings of size <em>n</em> by <em>m</em>,
207
* where <em>m</em> is at least 5. Each of the <em>n</em> rows is an
208
* entry containing the localized names for a single <code>TimeZone</code>.
209
* Each such row contains (with <code>i</code> ranging from
210
* 0..<em>n</em>-1):
211
* <ul>
212
* <li><code>zoneStrings[i][0]</code> - time zone ID</li>
213
* <li><code>zoneStrings[i][1]</code> - long name of zone in standard
214
* time</li>
215
* <li><code>zoneStrings[i][2]</code> - short name of zone in
216
* standard time</li>
217
* <li><code>zoneStrings[i][3]</code> - long name of zone in daylight
218
* saving time</li>
219
* <li><code>zoneStrings[i][4]</code> - short name of zone in daylight
220
* saving time</li>
221
* </ul>
222
* The zone ID is <em>not</em> localized; it's one of the valid IDs of
223
* the {@link java.util.TimeZone TimeZone} class that are not
224
* <a href="../java/util/TimeZone.html#CustomID">custom IDs</a>.
225
* All other entries are localized names.
226
* @see java.util.TimeZone
227
* @serial
228
*/
229
String zoneStrings[][] = null;
230
231
/**
232
* Indicates that zoneStrings is set externally with setZoneStrings() method.
233
*/
234
transient boolean isZoneStringsSet = false;
235
236
/**
237
* Unlocalized date-time pattern characters. For example: 'y', 'd', etc.
238
* All locales use the same these unlocalized pattern characters.
239
*/
240
static final String patternChars = "GyMdkHmsSEDFwWahKzZYuXL";
241
242
static final int PATTERN_ERA = 0; // G
243
static final int PATTERN_YEAR = 1; // y
244
static final int PATTERN_MONTH = 2; // M
245
static final int PATTERN_DAY_OF_MONTH = 3; // d
246
static final int PATTERN_HOUR_OF_DAY1 = 4; // k
247
static final int PATTERN_HOUR_OF_DAY0 = 5; // H
248
static final int PATTERN_MINUTE = 6; // m
249
static final int PATTERN_SECOND = 7; // s
250
static final int PATTERN_MILLISECOND = 8; // S
251
static final int PATTERN_DAY_OF_WEEK = 9; // E
252
static final int PATTERN_DAY_OF_YEAR = 10; // D
253
static final int PATTERN_DAY_OF_WEEK_IN_MONTH = 11; // F
254
static final int PATTERN_WEEK_OF_YEAR = 12; // w
255
static final int PATTERN_WEEK_OF_MONTH = 13; // W
256
static final int PATTERN_AM_PM = 14; // a
257
static final int PATTERN_HOUR1 = 15; // h
258
static final int PATTERN_HOUR0 = 16; // K
259
static final int PATTERN_ZONE_NAME = 17; // z
260
static final int PATTERN_ZONE_VALUE = 18; // Z
261
static final int PATTERN_WEEK_YEAR = 19; // Y
262
static final int PATTERN_ISO_DAY_OF_WEEK = 20; // u
263
static final int PATTERN_ISO_ZONE = 21; // X
264
static final int PATTERN_MONTH_STANDALONE = 22; // L
265
266
/**
267
* Localized date-time pattern characters. For example, a locale may
268
* wish to use 'u' rather than 'y' to represent years in its date format
269
* pattern strings.
270
* This string must be exactly 18 characters long, with the index of
271
* the characters described by <code>DateFormat.ERA_FIELD</code>,
272
* <code>DateFormat.YEAR_FIELD</code>, etc. Thus, if the string were
273
* "Xz...", then localized patterns would use 'X' for era and 'z' for year.
274
* @serial
275
*/
276
String localPatternChars = null;
277
278
/**
279
* The locale which is used for initializing this DateFormatSymbols object.
280
*
281
* @since 1.6
282
* @serial
283
*/
284
Locale locale = null;
285
286
/* use serialVersionUID from JDK 1.1.4 for interoperability */
287
static final long serialVersionUID = -5987973545549424702L;
288
289
/**
290
* Returns an array of all locales for which the
291
* <code>getInstance</code> methods of this class can return
292
* localized instances.
293
* The returned array represents the union of locales supported by the
294
* Java runtime and by installed
295
* {@link java.text.spi.DateFormatSymbolsProvider DateFormatSymbolsProvider}
296
* implementations. It must contain at least a <code>Locale</code>
297
* instance equal to {@link java.util.Locale#US Locale.US}.
298
*
299
* @return An array of locales for which localized
300
* <code>DateFormatSymbols</code> instances are available.
301
* @since 1.6
302
*/
303
public static Locale[] getAvailableLocales() {
304
LocaleServiceProviderPool pool=
305
LocaleServiceProviderPool.getPool(DateFormatSymbolsProvider.class);
306
return pool.getAvailableLocales();
307
}
308
309
/**
310
* Gets the <code>DateFormatSymbols</code> instance for the default
311
* locale. This method provides access to <code>DateFormatSymbols</code>
312
* instances for locales supported by the Java runtime itself as well
313
* as for those supported by installed
314
* {@link java.text.spi.DateFormatSymbolsProvider DateFormatSymbolsProvider}
315
* implementations.
316
* <p>This is equivalent to calling {@link #getInstance(Locale)
317
* getInstance(Locale.getDefault(Locale.Category.FORMAT))}.
318
* @see java.util.Locale#getDefault(java.util.Locale.Category)
319
* @see java.util.Locale.Category#FORMAT
320
* @return a <code>DateFormatSymbols</code> instance.
321
* @since 1.6
322
*/
323
public static final DateFormatSymbols getInstance() {
324
return getInstance(Locale.getDefault(Locale.Category.FORMAT));
325
}
326
327
/**
328
* Gets the <code>DateFormatSymbols</code> instance for the specified
329
* locale. This method provides access to <code>DateFormatSymbols</code>
330
* instances for locales supported by the Java runtime itself as well
331
* as for those supported by installed
332
* {@link java.text.spi.DateFormatSymbolsProvider DateFormatSymbolsProvider}
333
* implementations.
334
* @param locale the given locale.
335
* @return a <code>DateFormatSymbols</code> instance.
336
* @exception NullPointerException if <code>locale</code> is null
337
* @since 1.6
338
*/
339
public static final DateFormatSymbols getInstance(Locale locale) {
340
DateFormatSymbols dfs = getProviderInstance(locale);
341
if (dfs != null) {
342
return dfs;
343
}
344
throw new RuntimeException("DateFormatSymbols instance creation failed.");
345
}
346
347
/**
348
* Returns a DateFormatSymbols provided by a provider or found in
349
* the cache. Note that this method returns a cached instance,
350
* not its clone. Therefore, the instance should never be given to
351
* an application.
352
*/
353
static final DateFormatSymbols getInstanceRef(Locale locale) {
354
DateFormatSymbols dfs = getProviderInstance(locale);
355
if (dfs != null) {
356
return dfs;
357
}
358
throw new RuntimeException("DateFormatSymbols instance creation failed.");
359
}
360
361
private static DateFormatSymbols getProviderInstance(Locale locale) {
362
LocaleProviderAdapter adapter = LocaleProviderAdapter.getAdapter(DateFormatSymbolsProvider.class, locale);
363
DateFormatSymbolsProvider provider = adapter.getDateFormatSymbolsProvider();
364
DateFormatSymbols dfsyms = provider.getInstance(locale);
365
if (dfsyms == null) {
366
provider = LocaleProviderAdapter.forJRE().getDateFormatSymbolsProvider();
367
dfsyms = provider.getInstance(locale);
368
}
369
return dfsyms;
370
}
371
372
/**
373
* Gets era strings. For example: "AD" and "BC".
374
* @return the era strings.
375
*/
376
public String[] getEras() {
377
return Arrays.copyOf(eras, eras.length);
378
}
379
380
/**
381
* Sets era strings. For example: "AD" and "BC".
382
* @param newEras the new era strings.
383
*/
384
public void setEras(String[] newEras) {
385
eras = Arrays.copyOf(newEras, newEras.length);
386
cachedHashCode = 0;
387
}
388
389
/**
390
* Gets month strings. For example: "January", "February", etc.
391
*
392
* <p>If the language requires different forms for formatting and
393
* stand-alone usages, this method returns month names in the
394
* formatting form. For example, the preferred month name for
395
* January in the Czech language is <em>ledna</em> in the
396
* formatting form, while it is <em>leden</em> in the stand-alone
397
* form. This method returns {@code "ledna"} in this case. Refer
398
* to the <a href="http://unicode.org/reports/tr35/#Calendar_Elements">
399
* Calendar Elements in the Unicode Locale Data Markup Language
400
* (LDML) specification</a> for more details.
401
*
402
* @return the month strings.
403
*/
404
public String[] getMonths() {
405
return Arrays.copyOf(months, months.length);
406
}
407
408
/**
409
* Sets month strings. For example: "January", "February", etc.
410
* @param newMonths the new month strings.
411
*/
412
public void setMonths(String[] newMonths) {
413
months = Arrays.copyOf(newMonths, newMonths.length);
414
cachedHashCode = 0;
415
}
416
417
/**
418
* Gets short month strings. For example: "Jan", "Feb", etc.
419
*
420
* <p>If the language requires different forms for formatting and
421
* stand-alone usages, This method returns short month names in
422
* the formatting form. For example, the preferred abbreviation
423
* for January in the Catalan language is <em>de gen.</em> in the
424
* formatting form, while it is <em>gen.</em> in the stand-alone
425
* form. This method returns {@code "de gen."} in this case. Refer
426
* to the <a href="http://unicode.org/reports/tr35/#Calendar_Elements">
427
* Calendar Elements in the Unicode Locale Data Markup Language
428
* (LDML) specification</a> for more details.
429
*
430
* @return the short month strings.
431
*/
432
public String[] getShortMonths() {
433
return Arrays.copyOf(shortMonths, shortMonths.length);
434
}
435
436
/**
437
* Sets short month strings. For example: "Jan", "Feb", etc.
438
* @param newShortMonths the new short month strings.
439
*/
440
public void setShortMonths(String[] newShortMonths) {
441
shortMonths = Arrays.copyOf(newShortMonths, newShortMonths.length);
442
cachedHashCode = 0;
443
}
444
445
/**
446
* Gets weekday strings. For example: "Sunday", "Monday", etc.
447
* @return the weekday strings. Use <code>Calendar.SUNDAY</code>,
448
* <code>Calendar.MONDAY</code>, etc. to index the result array.
449
*/
450
public String[] getWeekdays() {
451
return Arrays.copyOf(weekdays, weekdays.length);
452
}
453
454
/**
455
* Sets weekday strings. For example: "Sunday", "Monday", etc.
456
* @param newWeekdays the new weekday strings. The array should
457
* be indexed by <code>Calendar.SUNDAY</code>,
458
* <code>Calendar.MONDAY</code>, etc.
459
*/
460
public void setWeekdays(String[] newWeekdays) {
461
weekdays = Arrays.copyOf(newWeekdays, newWeekdays.length);
462
cachedHashCode = 0;
463
}
464
465
/**
466
* Gets short weekday strings. For example: "Sun", "Mon", etc.
467
* @return the short weekday strings. Use <code>Calendar.SUNDAY</code>,
468
* <code>Calendar.MONDAY</code>, etc. to index the result array.
469
*/
470
public String[] getShortWeekdays() {
471
return Arrays.copyOf(shortWeekdays, shortWeekdays.length);
472
}
473
474
/**
475
* Sets short weekday strings. For example: "Sun", "Mon", etc.
476
* @param newShortWeekdays the new short weekday strings. The array should
477
* be indexed by <code>Calendar.SUNDAY</code>,
478
* <code>Calendar.MONDAY</code>, etc.
479
*/
480
public void setShortWeekdays(String[] newShortWeekdays) {
481
shortWeekdays = Arrays.copyOf(newShortWeekdays, newShortWeekdays.length);
482
cachedHashCode = 0;
483
}
484
485
/**
486
* Gets ampm strings. For example: "AM" and "PM".
487
* @return the ampm strings.
488
*/
489
public String[] getAmPmStrings() {
490
return Arrays.copyOf(ampms, ampms.length);
491
}
492
493
/**
494
* Sets ampm strings. For example: "AM" and "PM".
495
* @param newAmpms the new ampm strings.
496
*/
497
public void setAmPmStrings(String[] newAmpms) {
498
ampms = Arrays.copyOf(newAmpms, newAmpms.length);
499
cachedHashCode = 0;
500
}
501
502
/**
503
* Gets time zone strings. Use of this method is discouraged; use
504
* {@link java.util.TimeZone#getDisplayName() TimeZone.getDisplayName()}
505
* instead.
506
* <p>
507
* The value returned is a
508
* two-dimensional array of strings of size <em>n</em> by <em>m</em>,
509
* where <em>m</em> is at least 5. Each of the <em>n</em> rows is an
510
* entry containing the localized names for a single <code>TimeZone</code>.
511
* Each such row contains (with <code>i</code> ranging from
512
* 0..<em>n</em>-1):
513
* <ul>
514
* <li><code>zoneStrings[i][0]</code> - time zone ID</li>
515
* <li><code>zoneStrings[i][1]</code> - long name of zone in standard
516
* time</li>
517
* <li><code>zoneStrings[i][2]</code> - short name of zone in
518
* standard time</li>
519
* <li><code>zoneStrings[i][3]</code> - long name of zone in daylight
520
* saving time</li>
521
* <li><code>zoneStrings[i][4]</code> - short name of zone in daylight
522
* saving time</li>
523
* </ul>
524
* The zone ID is <em>not</em> localized; it's one of the valid IDs of
525
* the {@link java.util.TimeZone TimeZone} class that are not
526
* <a href="../util/TimeZone.html#CustomID">custom IDs</a>.
527
* All other entries are localized names. If a zone does not implement
528
* daylight saving time, the daylight saving time names should not be used.
529
* <p>
530
* If {@link #setZoneStrings(String[][]) setZoneStrings} has been called
531
* on this <code>DateFormatSymbols</code> instance, then the strings
532
* provided by that call are returned. Otherwise, the returned array
533
* contains names provided by the Java runtime and by installed
534
* {@link java.util.spi.TimeZoneNameProvider TimeZoneNameProvider}
535
* implementations.
536
*
537
* @return the time zone strings.
538
* @see #setZoneStrings(String[][])
539
*/
540
public String[][] getZoneStrings() {
541
return getZoneStringsImpl(true);
542
}
543
544
/**
545
* Sets time zone strings. The argument must be a
546
* two-dimensional array of strings of size <em>n</em> by <em>m</em>,
547
* where <em>m</em> is at least 5. Each of the <em>n</em> rows is an
548
* entry containing the localized names for a single <code>TimeZone</code>.
549
* Each such row contains (with <code>i</code> ranging from
550
* 0..<em>n</em>-1):
551
* <ul>
552
* <li><code>zoneStrings[i][0]</code> - time zone ID</li>
553
* <li><code>zoneStrings[i][1]</code> - long name of zone in standard
554
* time</li>
555
* <li><code>zoneStrings[i][2]</code> - short name of zone in
556
* standard time</li>
557
* <li><code>zoneStrings[i][3]</code> - long name of zone in daylight
558
* saving time</li>
559
* <li><code>zoneStrings[i][4]</code> - short name of zone in daylight
560
* saving time</li>
561
* </ul>
562
* The zone ID is <em>not</em> localized; it's one of the valid IDs of
563
* the {@link java.util.TimeZone TimeZone} class that are not
564
* <a href="../util/TimeZone.html#CustomID">custom IDs</a>.
565
* All other entries are localized names.
566
*
567
* @param newZoneStrings the new time zone strings.
568
* @exception IllegalArgumentException if the length of any row in
569
* <code>newZoneStrings</code> is less than 5
570
* @exception NullPointerException if <code>newZoneStrings</code> is null
571
* @see #getZoneStrings()
572
*/
573
public void setZoneStrings(String[][] newZoneStrings) {
574
String[][] aCopy = new String[newZoneStrings.length][];
575
for (int i = 0; i < newZoneStrings.length; ++i) {
576
int len = newZoneStrings[i].length;
577
if (len < 5) {
578
throw new IllegalArgumentException();
579
}
580
aCopy[i] = Arrays.copyOf(newZoneStrings[i], len);
581
}
582
zoneStrings = aCopy;
583
isZoneStringsSet = true;
584
cachedHashCode = 0;
585
}
586
587
/**
588
* Gets localized date-time pattern characters. For example: 'u', 't', etc.
589
* @return the localized date-time pattern characters.
590
*/
591
public String getLocalPatternChars() {
592
return localPatternChars;
593
}
594
595
/**
596
* Sets localized date-time pattern characters. For example: 'u', 't', etc.
597
* @param newLocalPatternChars the new localized date-time
598
* pattern characters.
599
*/
600
public void setLocalPatternChars(String newLocalPatternChars) {
601
// Call toString() to throw an NPE in case the argument is null
602
localPatternChars = newLocalPatternChars.toString();
603
cachedHashCode = 0;
604
}
605
606
/**
607
* Overrides Cloneable
608
*/
609
public Object clone()
610
{
611
try
612
{
613
DateFormatSymbols other = (DateFormatSymbols)super.clone();
614
copyMembers(this, other);
615
return other;
616
} catch (CloneNotSupportedException e) {
617
throw new InternalError(e);
618
}
619
}
620
621
/**
622
* Override hashCode.
623
* Generates a hash code for the DateFormatSymbols object.
624
*/
625
@Override
626
public int hashCode() {
627
int hashCode = cachedHashCode;
628
if (hashCode == 0) {
629
hashCode = 5;
630
hashCode = 11 * hashCode + Arrays.hashCode(eras);
631
hashCode = 11 * hashCode + Arrays.hashCode(months);
632
hashCode = 11 * hashCode + Arrays.hashCode(shortMonths);
633
hashCode = 11 * hashCode + Arrays.hashCode(weekdays);
634
hashCode = 11 * hashCode + Arrays.hashCode(shortWeekdays);
635
hashCode = 11 * hashCode + Arrays.hashCode(ampms);
636
hashCode = 11 * hashCode + Arrays.deepHashCode(getZoneStringsWrapper());
637
hashCode = 11 * hashCode + Objects.hashCode(localPatternChars);
638
cachedHashCode = hashCode;
639
}
640
641
return hashCode;
642
}
643
644
/**
645
* Override equals
646
*/
647
public boolean equals(Object obj)
648
{
649
if (this == obj) return true;
650
if (obj == null || getClass() != obj.getClass()) return false;
651
DateFormatSymbols that = (DateFormatSymbols) obj;
652
return (Arrays.equals(eras, that.eras)
653
&& Arrays.equals(months, that.months)
654
&& Arrays.equals(shortMonths, that.shortMonths)
655
&& Arrays.equals(weekdays, that.weekdays)
656
&& Arrays.equals(shortWeekdays, that.shortWeekdays)
657
&& Arrays.equals(ampms, that.ampms)
658
&& Arrays.deepEquals(getZoneStringsWrapper(), that.getZoneStringsWrapper())
659
&& ((localPatternChars != null
660
&& localPatternChars.equals(that.localPatternChars))
661
|| (localPatternChars == null
662
&& that.localPatternChars == null)));
663
}
664
665
// =======================privates===============================
666
667
/**
668
* Useful constant for defining time zone offsets.
669
*/
670
static final int millisPerHour = 60*60*1000;
671
672
/**
673
* Cache to hold DateFormatSymbols instances per Locale.
674
*/
675
private static final ConcurrentMap<Locale, SoftReference<DateFormatSymbols>> cachedInstances
676
= new ConcurrentHashMap<>(3);
677
678
private transient int lastZoneIndex = 0;
679
680
/**
681
* Cached hash code
682
*/
683
transient volatile int cachedHashCode = 0;
684
685
/**
686
* Initializes this DateFormatSymbols with the locale data. This method uses
687
* a cached DateFormatSymbols instance for the given locale if available. If
688
* there's no cached one, this method creates an uninitialized instance and
689
* populates its fields from the resource bundle for the locale, and caches
690
* the instance. Note: zoneStrings isn't initialized in this method.
691
*/
692
private void initializeData(Locale locale) {
693
SoftReference<DateFormatSymbols> ref = cachedInstances.get(locale);
694
DateFormatSymbols dfs;
695
if (ref == null || (dfs = ref.get()) == null) {
696
if (ref != null) {
697
// Remove the empty SoftReference
698
cachedInstances.remove(locale, ref);
699
}
700
dfs = new DateFormatSymbols(false);
701
702
// Initialize the fields from the ResourceBundle for locale.
703
LocaleProviderAdapter adapter
704
= LocaleProviderAdapter.getAdapter(DateFormatSymbolsProvider.class, locale);
705
// Avoid any potential recursions
706
if (!(adapter instanceof ResourceBundleBasedAdapter)) {
707
adapter = LocaleProviderAdapter.getResourceBundleBased();
708
}
709
ResourceBundle resource
710
= ((ResourceBundleBasedAdapter)adapter).getLocaleData().getDateFormatData(locale);
711
712
dfs.locale = locale;
713
// JRE and CLDR use different keys
714
// JRE: Eras, short.Eras and narrow.Eras
715
// CLDR: long.Eras, Eras and narrow.Eras
716
if (resource.containsKey("Eras")) {
717
dfs.eras = resource.getStringArray("Eras");
718
} else if (resource.containsKey("long.Eras")) {
719
dfs.eras = resource.getStringArray("long.Eras");
720
} else if (resource.containsKey("short.Eras")) {
721
dfs.eras = resource.getStringArray("short.Eras");
722
}
723
dfs.months = resource.getStringArray("MonthNames");
724
dfs.shortMonths = resource.getStringArray("MonthAbbreviations");
725
dfs.ampms = resource.getStringArray("AmPmMarkers");
726
dfs.localPatternChars = resource.getString("DateTimePatternChars");
727
728
// Day of week names are stored in a 1-based array.
729
dfs.weekdays = toOneBasedArray(resource.getStringArray("DayNames"));
730
dfs.shortWeekdays = toOneBasedArray(resource.getStringArray("DayAbbreviations"));
731
732
// Put dfs in the cache
733
ref = new SoftReference<>(dfs);
734
SoftReference<DateFormatSymbols> x = cachedInstances.putIfAbsent(locale, ref);
735
if (x != null) {
736
DateFormatSymbols y = x.get();
737
if (y == null) {
738
// Replace the empty SoftReference with ref.
739
cachedInstances.replace(locale, x, ref);
740
} else {
741
ref = x;
742
dfs = y;
743
}
744
}
745
// If the bundle's locale isn't the target locale, put another cache
746
// entry for the bundle's locale.
747
Locale bundleLocale = resource.getLocale();
748
if (!bundleLocale.equals(locale)) {
749
SoftReference<DateFormatSymbols> z
750
= cachedInstances.putIfAbsent(bundleLocale, ref);
751
if (z != null && z.get() == null) {
752
cachedInstances.replace(bundleLocale, z, ref);
753
}
754
}
755
}
756
757
// Copy the field values from dfs to this instance.
758
copyMembers(dfs, this);
759
}
760
761
private static String[] toOneBasedArray(String[] src) {
762
int len = src.length;
763
String[] dst = new String[len + 1];
764
dst[0] = "";
765
for (int i = 0; i < len; i++) {
766
dst[i + 1] = src[i];
767
}
768
return dst;
769
}
770
771
/**
772
* Package private: used by SimpleDateFormat
773
* Gets the index for the given time zone ID to obtain the time zone
774
* strings for formatting. The time zone ID is just for programmatic
775
* lookup. NOT LOCALIZED!!!
776
* @param ID the given time zone ID.
777
* @return the index of the given time zone ID. Returns -1 if
778
* the given time zone ID can't be located in the DateFormatSymbols object.
779
* @see java.util.SimpleTimeZone
780
*/
781
final int getZoneIndex(String ID) {
782
String[][] zoneStrings = getZoneStringsWrapper();
783
784
/*
785
* getZoneIndex has been re-written for performance reasons. instead of
786
* traversing the zoneStrings array every time, we cache the last used zone
787
* index
788
*/
789
if (lastZoneIndex < zoneStrings.length && ID.equals(zoneStrings[lastZoneIndex][0])) {
790
return lastZoneIndex;
791
}
792
793
/* slow path, search entire list */
794
for (int index = 0; index < zoneStrings.length; index++) {
795
if (ID.equals(zoneStrings[index][0])) {
796
lastZoneIndex = index;
797
return index;
798
}
799
}
800
801
return -1;
802
}
803
804
/**
805
* Wrapper method to the getZoneStrings(), which is called from inside
806
* the java.text package and not to mutate the returned arrays, so that
807
* it does not need to create a defensive copy.
808
*/
809
final String[][] getZoneStringsWrapper() {
810
if (isSubclassObject()) {
811
return getZoneStrings();
812
} else {
813
return getZoneStringsImpl(false);
814
}
815
}
816
817
private String[][] getZoneStringsImpl(boolean needsCopy) {
818
if (zoneStrings == null) {
819
zoneStrings = TimeZoneNameUtility.getZoneStrings(locale);
820
}
821
822
if (!needsCopy) {
823
return zoneStrings;
824
}
825
826
int len = zoneStrings.length;
827
String[][] aCopy = new String[len][];
828
for (int i = 0; i < len; i++) {
829
aCopy[i] = Arrays.copyOf(zoneStrings[i], zoneStrings[i].length);
830
}
831
return aCopy;
832
}
833
834
private boolean isSubclassObject() {
835
return !getClass().getName().equals("java.text.DateFormatSymbols");
836
}
837
838
/**
839
* Clones all the data members from the source DateFormatSymbols to
840
* the target DateFormatSymbols.
841
*
842
* @param src the source DateFormatSymbols.
843
* @param dst the target DateFormatSymbols.
844
*/
845
private void copyMembers(DateFormatSymbols src, DateFormatSymbols dst)
846
{
847
dst.locale = src.locale;
848
dst.eras = Arrays.copyOf(src.eras, src.eras.length);
849
dst.months = Arrays.copyOf(src.months, src.months.length);
850
dst.shortMonths = Arrays.copyOf(src.shortMonths, src.shortMonths.length);
851
dst.weekdays = Arrays.copyOf(src.weekdays, src.weekdays.length);
852
dst.shortWeekdays = Arrays.copyOf(src.shortWeekdays, src.shortWeekdays.length);
853
dst.ampms = Arrays.copyOf(src.ampms, src.ampms.length);
854
if (src.zoneStrings != null) {
855
dst.zoneStrings = src.getZoneStringsImpl(true);
856
} else {
857
dst.zoneStrings = null;
858
}
859
dst.localPatternChars = src.localPatternChars;
860
dst.cachedHashCode = 0;
861
}
862
863
/**
864
* Write out the default serializable data, after ensuring the
865
* <code>zoneStrings</code> field is initialized in order to make
866
* sure the backward compatibility.
867
*
868
* @since 1.6
869
*/
870
private void writeObject(ObjectOutputStream stream) throws IOException {
871
if (zoneStrings == null) {
872
zoneStrings = TimeZoneNameUtility.getZoneStrings(locale);
873
}
874
stream.defaultWriteObject();
875
}
876
}
877
878