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