1 /*
2 * Copyright (c) 1996, 2010, 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
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.List;
48 import java.util.Locale;
49 import java.util.ResourceBundle;
50 import java.util.TimeZone;
51 import java.util.concurrent.ConcurrentHashMap;
52 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;
57 import sun.util.resources.LocaleData;
58
59 /**
60 * <code>DateFormatSymbols</code> is a public class for encapsulating
61 * localizable date-time formatting data, such as the names of the
62 * months, the names of the days of the week, and the time zone data.
63 * <code>DateFormat</code> and <code>SimpleDateFormat</code> both use
64 * <code>DateFormatSymbols</code> to encapsulate this information.
65 *
66 * <p>
67 * Typically you shouldn't use <code>DateFormatSymbols</code> directly.
68 * Rather, you are encouraged to create a date-time formatter with the
69 * <code>DateFormat</code> class's factory methods: <code>getTimeInstance</code>,
70 * <code>getDateInstance</code>, or <code>getDateTimeInstance</code>.
71 * These methods automatically create a <code>DateFormatSymbols</code> for
72 * the formatter so that you don't have to. After the
73 * formatter is created, you may modify its format pattern using the
74 * <code>setPattern</code> method. For more information about
75 * creating formatters using <code>DateFormat</code>'s factory methods,
76 * see {@link DateFormat}.
77 *
210 * saving time</li>
211 * </ul>
212 * The zone ID is <em>not</em> localized; it's one of the valid IDs of
213 * the {@link java.util.TimeZone TimeZone} class that are not
214 * <a href="../java/util/TimeZone.html#CustomID">custom IDs</a>.
215 * All other entries are localized names.
216 * @see java.util.TimeZone
217 * @serial
218 */
219 String zoneStrings[][] = null;
220
221 /**
222 * Indicates that zoneStrings is set externally with setZoneStrings() method.
223 */
224 transient boolean isZoneStringsSet = false;
225
226 /**
227 * Unlocalized date-time pattern characters. For example: 'y', 'd', etc.
228 * All locales use the same these unlocalized pattern characters.
229 */
230 static final String patternChars = "GyMdkHmsSEDFwWahKzZYuX";
231
232 static final int PATTERN_ERA = 0; // G
233 static final int PATTERN_YEAR = 1; // y
234 static final int PATTERN_MONTH = 2; // M
235 static final int PATTERN_DAY_OF_MONTH = 3; // d
236 static final int PATTERN_HOUR_OF_DAY1 = 4; // k
237 static final int PATTERN_HOUR_OF_DAY0 = 5; // H
238 static final int PATTERN_MINUTE = 6; // m
239 static final int PATTERN_SECOND = 7; // s
240 static final int PATTERN_MILLISECOND = 8; // S
241 static final int PATTERN_DAY_OF_WEEK = 9; // E
242 static final int PATTERN_DAY_OF_YEAR = 10; // D
243 static final int PATTERN_DAY_OF_WEEK_IN_MONTH = 11; // F
244 static final int PATTERN_WEEK_OF_YEAR = 12; // w
245 static final int PATTERN_WEEK_OF_MONTH = 13; // W
246 static final int PATTERN_AM_PM = 14; // a
247 static final int PATTERN_HOUR1 = 15; // h
248 static final int PATTERN_HOUR0 = 16; // K
249 static final int PATTERN_ZONE_NAME = 17; // z
250 static final int PATTERN_ZONE_VALUE = 18; // Z
251 static final int PATTERN_WEEK_YEAR = 19; // Y
252 static final int PATTERN_ISO_DAY_OF_WEEK = 20; // u
253 static final int PATTERN_ISO_ZONE = 21; // X
254
255 /**
256 * Localized date-time pattern characters. For example, a locale may
257 * wish to use 'u' rather than 'y' to represent years in its date format
258 * pattern strings.
259 * This string must be exactly 18 characters long, with the index of
260 * the characters described by <code>DateFormat.ERA_FIELD</code>,
261 * <code>DateFormat.YEAR_FIELD</code>, etc. Thus, if the string were
262 * "Xz...", then localized patterns would use 'X' for era and 'z' for year.
263 * @serial
264 */
265 String localPatternChars = null;
266
267 /**
268 * The locale which is used for initializing this DateFormatSymbols object.
269 *
270 * @since 1.6
271 * @serial
272 */
273 Locale locale = null;
309 return getInstance(Locale.getDefault(Locale.Category.FORMAT));
310 }
311
312 /**
313 * Gets the <code>DateFormatSymbols</code> instance for the specified
314 * locale. This method provides access to <code>DateFormatSymbols</code>
315 * instances for locales supported by the Java runtime itself as well
316 * as for those supported by installed
317 * {@link java.text.spi.DateFormatSymbolsProvider DateFormatSymbolsProvider}
318 * implementations.
319 * @param locale the given locale.
320 * @return a <code>DateFormatSymbols</code> instance.
321 * @exception NullPointerException if <code>locale</code> is null
322 * @since 1.6
323 */
324 public static final DateFormatSymbols getInstance(Locale locale) {
325 DateFormatSymbols dfs = getProviderInstance(locale);
326 if (dfs != null) {
327 return dfs;
328 }
329 return (DateFormatSymbols) getCachedInstance(locale).clone();
330 }
331
332 /**
333 * Returns a DateFormatSymbols provided by a provider or found in
334 * the cache. Note that this method returns a cached instance,
335 * not its clone. Therefore, the instance should never be given to
336 * an application.
337 */
338 static final DateFormatSymbols getInstanceRef(Locale locale) {
339 DateFormatSymbols dfs = getProviderInstance(locale);
340 if (dfs != null) {
341 return dfs;
342 }
343 return getCachedInstance(locale);
344 }
345
346 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;
358 }
359
360 /**
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 * Gets era strings. For example: "AD" and "BC".
387 * @return the era strings.
388 */
389 public String[] getEras() {
390 return Arrays.copyOf(eras, eras.length);
391 }
392
393 /**
394 * Sets era strings. For example: "AD" and "BC".
395 * @param newEras the new era strings.
396 */
397 public void setEras(String[] newEras) {
398 eras = Arrays.copyOf(newEras, newEras.length);
399 }
400
401 /**
402 * Gets month strings. For example: "January", "February", etc.
403 * @return the month strings.
404 */
405 public String[] getMonths() {
406 return Arrays.copyOf(months, months.length);
407 }
408
409 /**
410 * Sets month strings. For example: "January", "February", etc.
411 * @param newMonths the new month strings.
412 */
413 public void setMonths(String[] newMonths) {
414 months = Arrays.copyOf(newMonths, newMonths.length);
415 }
416
417 /**
418 * Gets short month strings. For example: "Jan", "Feb", etc.
419 * @return the short month strings.
420 */
421 public String[] getShortMonths() {
422 return Arrays.copyOf(shortMonths, shortMonths.length);
423 }
424
425 /**
426 * Sets short month strings. For example: "Jan", "Feb", etc.
427 * @param newShortMonths the new short month strings.
428 */
429 public void setShortMonths(String[] newShortMonths) {
430 shortMonths = Arrays.copyOf(newShortMonths, newShortMonths.length);
431 }
432
433 /**
434 * Gets weekday strings. For example: "Sunday", "Monday", etc.
435 * @return the weekday strings. Use <code>Calendar.SUNDAY</code>,
436 * <code>Calendar.MONDAY</code>, etc. to index the result array.
437 */
438 public String[] getWeekdays() {
628 && Arrays.equals(shortWeekdays, that.shortWeekdays)
629 && Arrays.equals(ampms, that.ampms)
630 && Arrays.deepEquals(getZoneStringsWrapper(), that.getZoneStringsWrapper())
631 && ((localPatternChars != null
632 && localPatternChars.equals(that.localPatternChars))
633 || (localPatternChars == null
634 && that.localPatternChars == null)));
635 }
636
637 // =======================privates===============================
638
639 /**
640 * Useful constant for defining time zone offsets.
641 */
642 static final int millisPerHour = 60*60*1000;
643
644 /**
645 * Cache to hold DateFormatSymbols instances per Locale.
646 */
647 private static final ConcurrentMap<Locale, SoftReference<DateFormatSymbols>> cachedInstances
648 = new ConcurrentHashMap<Locale, SoftReference<DateFormatSymbols>>(3);
649
650 private transient int lastZoneIndex = 0;
651
652 private void initializeData(Locale desiredLocale) {
653 locale = desiredLocale;
654
655 // Copy values of a cached instance if any.
656 SoftReference<DateFormatSymbols> ref = cachedInstances.get(locale);
657 DateFormatSymbols dfs;
658 if (ref != null && (dfs = ref.get()) != null) {
659 copyMembers(dfs, this);
660 return;
661 }
662
663 // Initialize the fields from the ResourceBundle for locale.
664 ResourceBundle resource = LocaleData.getDateFormatData(locale);
665
666 eras = resource.getStringArray("Eras");
667 months = resource.getStringArray("MonthNames");
668 shortMonths = resource.getStringArray("MonthAbbreviations");
669 ampms = resource.getStringArray("AmPmMarkers");
670 localPatternChars = resource.getString("DateTimePatternChars");
671
672 // Day of week names are stored in a 1-based array.
673 weekdays = toOneBasedArray(resource.getStringArray("DayNames"));
674 shortWeekdays = toOneBasedArray(resource.getStringArray("DayAbbreviations"));
675 }
676
677 private static String[] toOneBasedArray(String[] src) {
678 int len = src.length;
679 String[] dst = new String[len + 1];
680 dst[0] = "";
681 for (int i = 0; i < len; i++) {
682 dst[i + 1] = src[i];
683 }
684 return dst;
685 }
686
687 /**
688 * Package private: used by SimpleDateFormat
689 * Gets the index for the given time zone ID to obtain the time zone
690 * strings for formatting. The time zone ID is just for programmatic
691 * lookup. NOT LOCALIZED!!!
692 * @param ID the given time zone ID.
693 * @return the index of the given time zone ID. Returns -1 if
694 * the given time zone ID can't be located in the DateFormatSymbols object.
695 * @see java.util.SimpleTimeZone
713 return index;
714 }
715 }
716
717 return -1;
718 }
719
720 /**
721 * Wrapper method to the getZoneStrings(), which is called from inside
722 * the java.text package and not to mutate the returned arrays, so that
723 * it does not need to create a defensive copy.
724 */
725 final String[][] getZoneStringsWrapper() {
726 if (isSubclassObject()) {
727 return getZoneStrings();
728 } else {
729 return getZoneStringsImpl(false);
730 }
731 }
732
733 private final String[][] getZoneStringsImpl(boolean needsCopy) {
734 if (zoneStrings == null) {
735 zoneStrings = TimeZoneNameUtility.getZoneStrings(locale);
736 }
737
738 if (!needsCopy) {
739 return zoneStrings;
740 }
741
742 int len = zoneStrings.length;
743 String[][] aCopy = new String[len][];
744 for (int i = 0; i < len; i++) {
745 aCopy[i] = Arrays.copyOf(zoneStrings[i], zoneStrings[i].length);
746 }
747 return aCopy;
748 }
749
750 private final boolean isSubclassObject() {
751 return !getClass().getName().equals("java.text.DateFormatSymbols");
752 }
753
754 /**
755 * Clones all the data members from the source DateFormatSymbols to
756 * the target DateFormatSymbols. This is only for subclasses.
757 * @param src the source DateFormatSymbols.
758 * @param dst the target DateFormatSymbols.
759 */
760 private final void copyMembers(DateFormatSymbols src, DateFormatSymbols dst)
761 {
762 dst.eras = Arrays.copyOf(src.eras, src.eras.length);
763 dst.months = Arrays.copyOf(src.months, src.months.length);
764 dst.shortMonths = Arrays.copyOf(src.shortMonths, src.shortMonths.length);
765 dst.weekdays = Arrays.copyOf(src.weekdays, src.weekdays.length);
766 dst.shortWeekdays = Arrays.copyOf(src.shortWeekdays, src.shortWeekdays.length);
767 dst.ampms = Arrays.copyOf(src.ampms, src.ampms.length);
768 if (src.zoneStrings != null) {
769 dst.zoneStrings = src.getZoneStringsImpl(true);
770 } else {
771 dst.zoneStrings = null;
772 }
773 dst.localPatternChars = src.localPatternChars;
774 }
775
776 /**
777 * Write out the default serializable data, after ensuring the
778 * <code>zoneStrings</code> field is initialized in order to make
779 * sure the backward compatibility.
780 *
781 * @since 1.6
782 */
783 private void writeObject(ObjectOutputStream stream) throws IOException {
784 if (zoneStrings == null) {
785 zoneStrings = TimeZoneNameUtility.getZoneStrings(locale);
786 }
787 stream.defaultWriteObject();
788 }
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 }
|
1 /*
2 * Copyright (c) 1996, 2012, 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
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.ResourceBundle;
49 import java.util.TimeZone;
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.TimeZoneNameUtility;
55
56 /**
57 * <code>DateFormatSymbols</code> is a public class for encapsulating
58 * localizable date-time formatting data, such as the names of the
59 * months, the names of the days of the week, and the time zone data.
60 * <code>DateFormat</code> and <code>SimpleDateFormat</code> both use
61 * <code>DateFormatSymbols</code> to encapsulate this information.
62 *
63 * <p>
64 * Typically you shouldn't use <code>DateFormatSymbols</code> directly.
65 * Rather, you are encouraged to create a date-time formatter with the
66 * <code>DateFormat</code> class's factory methods: <code>getTimeInstance</code>,
67 * <code>getDateInstance</code>, or <code>getDateTimeInstance</code>.
68 * These methods automatically create a <code>DateFormatSymbols</code> for
69 * the formatter so that you don't have to. After the
70 * formatter is created, you may modify its format pattern using the
71 * <code>setPattern</code> method. For more information about
72 * creating formatters using <code>DateFormat</code>'s factory methods,
73 * see {@link DateFormat}.
74 *
207 * saving time</li>
208 * </ul>
209 * The zone ID is <em>not</em> localized; it's one of the valid IDs of
210 * the {@link java.util.TimeZone TimeZone} class that are not
211 * <a href="../java/util/TimeZone.html#CustomID">custom IDs</a>.
212 * All other entries are localized names.
213 * @see java.util.TimeZone
214 * @serial
215 */
216 String zoneStrings[][] = null;
217
218 /**
219 * Indicates that zoneStrings is set externally with setZoneStrings() method.
220 */
221 transient boolean isZoneStringsSet = false;
222
223 /**
224 * Unlocalized date-time pattern characters. For example: 'y', 'd', etc.
225 * All locales use the same these unlocalized pattern characters.
226 */
227 static final String patternChars = "GyMdkHmsSEDFwWahKzZYuXL";
228
229 static final int PATTERN_ERA = 0; // G
230 static final int PATTERN_YEAR = 1; // y
231 static final int PATTERN_MONTH = 2; // M
232 static final int PATTERN_DAY_OF_MONTH = 3; // d
233 static final int PATTERN_HOUR_OF_DAY1 = 4; // k
234 static final int PATTERN_HOUR_OF_DAY0 = 5; // H
235 static final int PATTERN_MINUTE = 6; // m
236 static final int PATTERN_SECOND = 7; // s
237 static final int PATTERN_MILLISECOND = 8; // S
238 static final int PATTERN_DAY_OF_WEEK = 9; // E
239 static final int PATTERN_DAY_OF_YEAR = 10; // D
240 static final int PATTERN_DAY_OF_WEEK_IN_MONTH = 11; // F
241 static final int PATTERN_WEEK_OF_YEAR = 12; // w
242 static final int PATTERN_WEEK_OF_MONTH = 13; // W
243 static final int PATTERN_AM_PM = 14; // a
244 static final int PATTERN_HOUR1 = 15; // h
245 static final int PATTERN_HOUR0 = 16; // K
246 static final int PATTERN_ZONE_NAME = 17; // z
247 static final int PATTERN_ZONE_VALUE = 18; // Z
248 static final int PATTERN_WEEK_YEAR = 19; // Y
249 static final int PATTERN_ISO_DAY_OF_WEEK = 20; // u
250 static final int PATTERN_ISO_ZONE = 21; // X
251 static final int PATTERN_MONTH_STANDALONE = 22; // L
252
253 /**
254 * Localized date-time pattern characters. For example, a locale may
255 * wish to use 'u' rather than 'y' to represent years in its date format
256 * pattern strings.
257 * This string must be exactly 18 characters long, with the index of
258 * the characters described by <code>DateFormat.ERA_FIELD</code>,
259 * <code>DateFormat.YEAR_FIELD</code>, etc. Thus, if the string were
260 * "Xz...", then localized patterns would use 'X' for era and 'z' for year.
261 * @serial
262 */
263 String localPatternChars = null;
264
265 /**
266 * The locale which is used for initializing this DateFormatSymbols object.
267 *
268 * @since 1.6
269 * @serial
270 */
271 Locale locale = null;
307 return getInstance(Locale.getDefault(Locale.Category.FORMAT));
308 }
309
310 /**
311 * Gets the <code>DateFormatSymbols</code> instance for the specified
312 * locale. This method provides access to <code>DateFormatSymbols</code>
313 * instances for locales supported by the Java runtime itself as well
314 * as for those supported by installed
315 * {@link java.text.spi.DateFormatSymbolsProvider DateFormatSymbolsProvider}
316 * implementations.
317 * @param locale the given locale.
318 * @return a <code>DateFormatSymbols</code> instance.
319 * @exception NullPointerException if <code>locale</code> is null
320 * @since 1.6
321 */
322 public static final DateFormatSymbols getInstance(Locale locale) {
323 DateFormatSymbols dfs = getProviderInstance(locale);
324 if (dfs != null) {
325 return dfs;
326 }
327 throw new RuntimeException("DateFormatSymbols instance creation failed.");
328 }
329
330 /**
331 * Returns a DateFormatSymbols provided by a provider or found in
332 * the cache. Note that this method returns a cached instance,
333 * not its clone. Therefore, the instance should never be given to
334 * an application.
335 */
336 static final DateFormatSymbols getInstanceRef(Locale locale) {
337 DateFormatSymbols dfs = getProviderInstance(locale);
338 if (dfs != null) {
339 return dfs;
340 }
341 throw new RuntimeException("DateFormatSymbols instance creation failed.");
342 }
343
344 private static DateFormatSymbols getProviderInstance(Locale locale) {
345 LocaleProviderAdapter adapter = LocaleProviderAdapter.getAdapter(DateFormatSymbolsProvider.class, locale);
346 DateFormatSymbolsProvider provider = adapter.getDateFormatSymbolsProvider();
347 DateFormatSymbols dfsyms = provider.getInstance(locale);
348 if (dfsyms == null) {
349 provider = LocaleProviderAdapter.forJRE().getDateFormatSymbolsProvider();
350 dfsyms = provider.getInstance(locale);
351 }
352 return dfsyms;
353 }
354
355 /**
356 * Gets era strings. For example: "AD" and "BC".
357 * @return the era strings.
358 */
359 public String[] getEras() {
360 return Arrays.copyOf(eras, eras.length);
361 }
362
363 /**
364 * Sets era strings. For example: "AD" and "BC".
365 * @param newEras the new era strings.
366 */
367 public void setEras(String[] newEras) {
368 eras = Arrays.copyOf(newEras, newEras.length);
369 }
370
371 /**
372 * Gets month strings. For example: "January", "February", etc.
373 *
374 * <p>If the language requires different forms for formatting and
375 * stand-alone usages, this method returns month names in the
376 * formatting form. For example, the preferred month name for
377 * January in the Czech language is <em>ledna</em> in the
378 * formatting form, while it is <em>leden</em> in the stand-alone
379 * form. This method returns {@code "ledna"} in this case. Refer
380 * to the <a href="http://unicode.org/reports/tr35/#Calendar_Elements">
381 * Calendar Elements in the Unicode Locale Data Markup Language
382 * (LDML) specification</a> for more details.
383 *
384 * @return the month strings.
385 */
386 public String[] getMonths() {
387 return Arrays.copyOf(months, months.length);
388 }
389
390 /**
391 * Sets month strings. For example: "January", "February", etc.
392 * @param newMonths the new month strings.
393 */
394 public void setMonths(String[] newMonths) {
395 months = Arrays.copyOf(newMonths, newMonths.length);
396 }
397
398 /**
399 * Gets short month strings. For example: "Jan", "Feb", etc.
400 *
401 * <p>If the language requires different forms for formatting and
402 * stand-alone usages, This method returns short month names in
403 * the formatting form. For example, the preferred abbreviation
404 * for January in the Catalan language is <em>de gen.</em> in the
405 * formatting form, while it is <em>gen.</em> in the stand-alone
406 * form. This method returns {@code "de gen."} in this case. Refer
407 * to the <a href="http://unicode.org/reports/tr35/#Calendar_Elements">
408 * Calendar Elements in the Unicode Locale Data Markup Language
409 * (LDML) specification</a> for more details.
410 *
411 * @return the short month strings.
412 */
413 public String[] getShortMonths() {
414 return Arrays.copyOf(shortMonths, shortMonths.length);
415 }
416
417 /**
418 * Sets short month strings. For example: "Jan", "Feb", etc.
419 * @param newShortMonths the new short month strings.
420 */
421 public void setShortMonths(String[] newShortMonths) {
422 shortMonths = Arrays.copyOf(newShortMonths, newShortMonths.length);
423 }
424
425 /**
426 * Gets weekday strings. For example: "Sunday", "Monday", etc.
427 * @return the weekday strings. Use <code>Calendar.SUNDAY</code>,
428 * <code>Calendar.MONDAY</code>, etc. to index the result array.
429 */
430 public String[] getWeekdays() {
620 && Arrays.equals(shortWeekdays, that.shortWeekdays)
621 && Arrays.equals(ampms, that.ampms)
622 && Arrays.deepEquals(getZoneStringsWrapper(), that.getZoneStringsWrapper())
623 && ((localPatternChars != null
624 && localPatternChars.equals(that.localPatternChars))
625 || (localPatternChars == null
626 && that.localPatternChars == null)));
627 }
628
629 // =======================privates===============================
630
631 /**
632 * Useful constant for defining time zone offsets.
633 */
634 static final int millisPerHour = 60*60*1000;
635
636 /**
637 * Cache to hold DateFormatSymbols instances per Locale.
638 */
639 private static final ConcurrentMap<Locale, SoftReference<DateFormatSymbols>> cachedInstances
640 = new ConcurrentHashMap<>(3);
641
642 private transient int lastZoneIndex = 0;
643
644 private void initializeData(Locale desiredLocale) {
645 locale = desiredLocale;
646
647 // Copy values of a cached instance if any.
648 SoftReference<DateFormatSymbols> ref = cachedInstances.get(locale);
649 DateFormatSymbols dfs;
650 if (ref != null && (dfs = ref.get()) != null) {
651 copyMembers(dfs, this);
652 return;
653 }
654
655 // Initialize the fields from the ResourceBundle for locale.
656 LocaleProviderAdapter adapter = LocaleProviderAdapter.getAdapter(DateFormatSymbolsProvider.class, locale);
657 // Avoid any potential recursions
658 switch (adapter.getAdapterType()) {
659 case HOST:
660 case SPI:
661 adapter = LocaleProviderAdapter.getResourceBundleBased();
662 break;
663 }
664 ResourceBundle resource = adapter.getLocaleData().getDateFormatData(locale);
665
666 eras = resource.getStringArray("Eras");
667 months = resource.getStringArray("MonthNames");
668 shortMonths = resource.getStringArray("MonthAbbreviations");
669 ampms = resource.getStringArray("AmPmMarkers");
670 localPatternChars = resource.getString("DateTimePatternChars");
671
672 // Day of week names are stored in a 1-based array.
673 weekdays = toOneBasedArray(resource.getStringArray("DayNames"));
674 shortWeekdays = toOneBasedArray(resource.getStringArray("DayAbbreviations"));
675
676 // Put a clone in the cache
677 ref = new SoftReference<>((DateFormatSymbols)this.clone());
678 SoftReference<DateFormatSymbols> x = cachedInstances.putIfAbsent(locale, ref);
679 if (x != null) {
680 DateFormatSymbols y = x.get();
681 if (y == null) {
682 // Replace the empty SoftReference with ref.
683 cachedInstances.put(locale, ref);
684 }
685 }
686 }
687
688 private static String[] toOneBasedArray(String[] src) {
689 int len = src.length;
690 String[] dst = new String[len + 1];
691 dst[0] = "";
692 for (int i = 0; i < len; i++) {
693 dst[i + 1] = src[i];
694 }
695 return dst;
696 }
697
698 /**
699 * Package private: used by SimpleDateFormat
700 * Gets the index for the given time zone ID to obtain the time zone
701 * strings for formatting. The time zone ID is just for programmatic
702 * lookup. NOT LOCALIZED!!!
703 * @param ID the given time zone ID.
704 * @return the index of the given time zone ID. Returns -1 if
705 * the given time zone ID can't be located in the DateFormatSymbols object.
706 * @see java.util.SimpleTimeZone
724 return index;
725 }
726 }
727
728 return -1;
729 }
730
731 /**
732 * Wrapper method to the getZoneStrings(), which is called from inside
733 * the java.text package and not to mutate the returned arrays, so that
734 * it does not need to create a defensive copy.
735 */
736 final String[][] getZoneStringsWrapper() {
737 if (isSubclassObject()) {
738 return getZoneStrings();
739 } else {
740 return getZoneStringsImpl(false);
741 }
742 }
743
744 private String[][] getZoneStringsImpl(boolean needsCopy) {
745 if (zoneStrings == null) {
746 zoneStrings = TimeZoneNameUtility.getZoneStrings(locale);
747 }
748
749 if (!needsCopy) {
750 return zoneStrings;
751 }
752
753 int len = zoneStrings.length;
754 String[][] aCopy = new String[len][];
755 for (int i = 0; i < len; i++) {
756 aCopy[i] = Arrays.copyOf(zoneStrings[i], zoneStrings[i].length);
757 }
758 return aCopy;
759 }
760
761 private boolean isSubclassObject() {
762 return !getClass().getName().equals("java.text.DateFormatSymbols");
763 }
764
765 /**
766 * Clones all the data members from the source DateFormatSymbols to
767 * the target DateFormatSymbols. This is only for subclasses.
768 * @param src the source DateFormatSymbols.
769 * @param dst the target DateFormatSymbols.
770 */
771 private void copyMembers(DateFormatSymbols src, DateFormatSymbols dst)
772 {
773 dst.eras = Arrays.copyOf(src.eras, src.eras.length);
774 dst.months = Arrays.copyOf(src.months, src.months.length);
775 dst.shortMonths = Arrays.copyOf(src.shortMonths, src.shortMonths.length);
776 dst.weekdays = Arrays.copyOf(src.weekdays, src.weekdays.length);
777 dst.shortWeekdays = Arrays.copyOf(src.shortWeekdays, src.shortWeekdays.length);
778 dst.ampms = Arrays.copyOf(src.ampms, src.ampms.length);
779 if (src.zoneStrings != null) {
780 dst.zoneStrings = src.getZoneStringsImpl(true);
781 } else {
782 dst.zoneStrings = null;
783 }
784 dst.localPatternChars = src.localPatternChars;
785 }
786
787 /**
788 * Write out the default serializable data, after ensuring the
789 * <code>zoneStrings</code> field is initialized in order to make
790 * sure the backward compatibility.
791 *
792 * @since 1.6
793 */
794 private void writeObject(ObjectOutputStream stream) throws IOException {
795 if (zoneStrings == null) {
796 zoneStrings = TimeZoneNameUtility.getZoneStrings(locale);
797 }
798 stream.defaultWriteObject();
799 }
800 }
|