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}.
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 import sun.util.resources.LocaleData;
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>DateFormat</code> and <code>SimpleDateFormat</code> both use
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}.
208 * saving time</li>
209 * </ul>
210 * The zone ID is <em>not</em> localized; it's one of the valid IDs of
211 * the {@link java.util.TimeZone TimeZone} class that are not
212 * <a href="../java/util/TimeZone.html#CustomID">custom IDs</a>.
213 * All other entries are localized names.
214 * @see java.util.TimeZone
215 * @serial
216 */
217 String zoneStrings[][] = null;
218
219 /**
220 * Indicates that zoneStrings is set externally with setZoneStrings() method.
221 */
222 transient boolean isZoneStringsSet = false;
223
224 /**
225 * Unlocalized date-time pattern characters. For example: 'y', 'd', etc.
226 * All locales use the same these unlocalized pattern characters.
227 */
228 static final String patternChars = "GyMdkHmsSEDFwWahKzZYuXL";
229
230 static final int PATTERN_ERA = 0; // G
231 static final int PATTERN_YEAR = 1; // y
232 static final int PATTERN_MONTH = 2; // M
233 static final int PATTERN_DAY_OF_MONTH = 3; // d
234 static final int PATTERN_HOUR_OF_DAY1 = 4; // k
235 static final int PATTERN_HOUR_OF_DAY0 = 5; // H
236 static final int PATTERN_MINUTE = 6; // m
237 static final int PATTERN_SECOND = 7; // s
238 static final int PATTERN_MILLISECOND = 8; // S
239 static final int PATTERN_DAY_OF_WEEK = 9; // E
240 static final int PATTERN_DAY_OF_YEAR = 10; // D
241 static final int PATTERN_DAY_OF_WEEK_IN_MONTH = 11; // F
242 static final int PATTERN_WEEK_OF_YEAR = 12; // w
243 static final int PATTERN_WEEK_OF_MONTH = 13; // W
244 static final int PATTERN_AM_PM = 14; // a
245 static final int PATTERN_HOUR1 = 15; // h
246 static final int PATTERN_HOUR0 = 16; // K
247 static final int PATTERN_ZONE_NAME = 17; // z
248 static final int PATTERN_ZONE_VALUE = 18; // Z
249 static final int PATTERN_WEEK_YEAR = 19; // Y
250 static final int PATTERN_ISO_DAY_OF_WEEK = 20; // u
251 static final int PATTERN_ISO_ZONE = 21; // X
252 static final int PATTERN_MONTH_STANDALONE = 22; // L
253
254 /**
255 * Localized date-time pattern characters. For example, a locale may
256 * wish to use 'u' rather than 'y' to represent years in its date format
257 * pattern strings.
258 * This string must be exactly 18 characters long, with the index of
259 * the characters described by <code>DateFormat.ERA_FIELD</code>,
260 * <code>DateFormat.YEAR_FIELD</code>, etc. Thus, if the string were
261 * "Xz...", then localized patterns would use 'X' for era and 'z' for year.
262 * @serial
263 */
264 String localPatternChars = null;
265
266 /**
267 * The locale which is used for initializing this DateFormatSymbols object.
268 *
269 * @since 1.6
270 * @serial
271 */
272 Locale locale = null;
308 return getInstance(Locale.getDefault(Locale.Category.FORMAT));
309 }
310
311 /**
312 * Gets the <code>DateFormatSymbols</code> instance for the specified
313 * locale. This method provides access to <code>DateFormatSymbols</code>
314 * instances for locales supported by the Java runtime itself as well
315 * as for those supported by installed
316 * {@link java.text.spi.DateFormatSymbolsProvider DateFormatSymbolsProvider}
317 * implementations.
318 * @param locale the given locale.
319 * @return a <code>DateFormatSymbols</code> instance.
320 * @exception NullPointerException if <code>locale</code> is null
321 * @since 1.6
322 */
323 public static final DateFormatSymbols getInstance(Locale locale) {
324 DateFormatSymbols dfs = getProviderInstance(locale);
325 if (dfs != null) {
326 return dfs;
327 }
328 throw new RuntimeException("DateFormatSymbols instance creation failed.");
329 }
330
331 /**
332 * Returns a DateFormatSymbols provided by a provider or found in
333 * the cache. Note that this method returns a cached instance,
334 * not its clone. Therefore, the instance should never be given to
335 * an application.
336 */
337 static final DateFormatSymbols getInstanceRef(Locale locale) {
338 DateFormatSymbols dfs = getProviderInstance(locale);
339 if (dfs != null) {
340 return dfs;
341 }
342 throw new RuntimeException("DateFormatSymbols instance creation failed.");
343 }
344
345 private static DateFormatSymbols getProviderInstance(Locale locale) {
346 LocaleProviderAdapter adapter = LocaleProviderAdapter.getAdapter(DateFormatSymbolsProvider.class, locale);
347 DateFormatSymbolsProvider provider = adapter.getDateFormatSymbolsProvider();
348 return provider.getInstance(locale);
349 }
350
351 /**
352 * Gets era strings. For example: "AD" and "BC".
353 * @return the era strings.
354 */
355 public String[] getEras() {
356 return Arrays.copyOf(eras, eras.length);
357 }
358
359 /**
360 * Sets era strings. For example: "AD" and "BC".
361 * @param newEras the new era strings.
362 */
363 public void setEras(String[] newEras) {
364 eras = Arrays.copyOf(newEras, newEras.length);
365 }
366
367 /**
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 *
380 * @return the month strings.
381 */
382 public String[] getMonths() {
383 return Arrays.copyOf(months, months.length);
384 }
385
386 /**
387 * Sets month strings. For example: "January", "February", etc.
388 * @param newMonths the new month strings.
389 */
390 public void setMonths(String[] newMonths) {
391 months = Arrays.copyOf(newMonths, newMonths.length);
392 }
393
394 /**
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 *
407 * @return the short month strings.
408 */
409 public String[] getShortMonths() {
410 return Arrays.copyOf(shortMonths, shortMonths.length);
411 }
412
413 /**
414 * Sets short month strings. For example: "Jan", "Feb", etc.
415 * @param newShortMonths the new short month strings.
416 */
417 public void setShortMonths(String[] newShortMonths) {
418 shortMonths = Arrays.copyOf(newShortMonths, newShortMonths.length);
419 }
420
421 /**
422 * Gets weekday strings. For example: "Sunday", "Monday", etc.
423 * @return the weekday strings. Use <code>Calendar.SUNDAY</code>,
424 * <code>Calendar.MONDAY</code>, etc. to index the result array.
425 */
426 public String[] getWeekdays() {
616 && Arrays.equals(shortWeekdays, that.shortWeekdays)
617 && Arrays.equals(ampms, that.ampms)
618 && Arrays.deepEquals(getZoneStringsWrapper(), that.getZoneStringsWrapper())
619 && ((localPatternChars != null
620 && localPatternChars.equals(that.localPatternChars))
621 || (localPatternChars == null
622 && that.localPatternChars == null)));
623 }
624
625 // =======================privates===============================
626
627 /**
628 * Useful constant for defining time zone offsets.
629 */
630 static final int millisPerHour = 60*60*1000;
631
632 /**
633 * Cache to hold DateFormatSymbols instances per Locale.
634 */
635 private static final ConcurrentMap<Locale, SoftReference<DateFormatSymbols>> cachedInstances
636 = new ConcurrentHashMap<>(3);
637
638 private transient int lastZoneIndex = 0;
639
640 private void initializeData(Locale desiredLocale) {
641 locale = desiredLocale;
642
643 // Copy values of a cached instance if any.
644 SoftReference<DateFormatSymbols> ref = cachedInstances.get(locale);
645 DateFormatSymbols dfs;
646 if (ref != null && (dfs = ref.get()) != null) {
647 copyMembers(dfs, this);
648 return;
649 }
650
651 // Initialize the fields from the ResourceBundle for 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);
661
662 eras = resource.getStringArray("Eras");
663 months = resource.getStringArray("MonthNames");
664 shortMonths = resource.getStringArray("MonthAbbreviations");
665 ampms = resource.getStringArray("AmPmMarkers");
666 localPatternChars = resource.getString("DateTimePatternChars");
667
668 // Day of week names are stored in a 1-based array.
669 weekdays = toOneBasedArray(resource.getStringArray("DayNames"));
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 }
682 }
683
684 private static String[] toOneBasedArray(String[] src) {
685 int len = src.length;
686 String[] dst = new String[len + 1];
687 dst[0] = "";
688 for (int i = 0; i < len; i++) {
689 dst[i + 1] = src[i];
690 }
691 return dst;
692 }
693
694 /**
695 * Package private: used by SimpleDateFormat
696 * Gets the index for the given time zone ID to obtain the time zone
697 * strings for formatting. The time zone ID is just for programmatic
698 * lookup. NOT LOCALIZED!!!
699 * @param ID the given time zone ID.
700 * @return the index of the given time zone ID. Returns -1 if
701 * the given time zone ID can't be located in the DateFormatSymbols object.
702 * @see java.util.SimpleTimeZone
720 return index;
721 }
722 }
723
724 return -1;
725 }
726
727 /**
728 * Wrapper method to the getZoneStrings(), which is called from inside
729 * the java.text package and not to mutate the returned arrays, so that
730 * it does not need to create a defensive copy.
731 */
732 final String[][] getZoneStringsWrapper() {
733 if (isSubclassObject()) {
734 return getZoneStrings();
735 } else {
736 return getZoneStringsImpl(false);
737 }
738 }
739
740 private String[][] getZoneStringsImpl(boolean needsCopy) {
741 if (zoneStrings == null) {
742 zoneStrings = TimeZoneNameUtility.getZoneStrings(locale);
743 }
744
745 if (!needsCopy) {
746 return zoneStrings;
747 }
748
749 int len = zoneStrings.length;
750 String[][] aCopy = new String[len][];
751 for (int i = 0; i < len; i++) {
752 aCopy[i] = Arrays.copyOf(zoneStrings[i], zoneStrings[i].length);
753 }
754 return aCopy;
755 }
756
757 private boolean isSubclassObject() {
758 return !getClass().getName().equals("java.text.DateFormatSymbols");
759 }
760
761 /**
762 * Clones all the data members from the source DateFormatSymbols to
763 * the target DateFormatSymbols. This is only for subclasses.
764 * @param src the source DateFormatSymbols.
765 * @param dst the target DateFormatSymbols.
766 */
767 private void copyMembers(DateFormatSymbols src, DateFormatSymbols dst)
768 {
769 dst.eras = Arrays.copyOf(src.eras, src.eras.length);
770 dst.months = Arrays.copyOf(src.months, src.months.length);
771 dst.shortMonths = Arrays.copyOf(src.shortMonths, src.shortMonths.length);
772 dst.weekdays = Arrays.copyOf(src.weekdays, src.weekdays.length);
773 dst.shortWeekdays = Arrays.copyOf(src.shortWeekdays, src.shortWeekdays.length);
774 dst.ampms = Arrays.copyOf(src.ampms, src.ampms.length);
775 if (src.zoneStrings != null) {
776 dst.zoneStrings = src.getZoneStringsImpl(true);
777 } else {
778 dst.zoneStrings = null;
779 }
780 dst.localPatternChars = src.localPatternChars;
781 }
782
783 /**
784 * Write out the default serializable data, after ensuring the
785 * <code>zoneStrings</code> field is initialized in order to make
786 * sure the backward compatibility.
787 *
788 * @since 1.6
789 */
790 private void writeObject(ObjectOutputStream stream) throws IOException {
791 if (zoneStrings == null) {
792 zoneStrings = TimeZoneNameUtility.getZoneStrings(locale);
793 }
794 stream.defaultWriteObject();
795 }
796 }
|