/* * Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved * (C) Copyright IBM Corp. 1996 - All Rights Reserved * * The original version of this source code and documentation is copyrighted * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These * materials are provided under terms of a License Agreement between Taligent * and Sun. This technology is protected by multiple US and International * patents. This notice and attribution to Taligent may not be removed. * Taligent is a registered trademark of Taligent, Inc. * */ package java.text; import java.io.IOException; import java.io.ObjectOutputStream; import java.io.Serializable; import java.lang.ref.SoftReference; import java.text.spi.DateFormatSymbolsProvider; import java.util.Arrays; import java.util.Hashtable; import java.util.List; import java.util.Locale; import java.util.ResourceBundle; import java.util.TimeZone; import java.util.spi.LocaleServiceProvider; import sun.util.LocaleServiceProviderPool; import sun.util.TimeZoneNameUtility; import sun.util.calendar.ZoneInfo; import sun.util.resources.LocaleData; /** * DateFormatSymbols is a public class for encapsulating * localizable date-time formatting data, such as the names of the * months, the names of the days of the week, and the time zone data. * DateFormat and SimpleDateFormat both use * DateFormatSymbols to encapsulate this information. * *

* Typically you shouldn't use DateFormatSymbols directly. * Rather, you are encouraged to create a date-time formatter with the * DateFormat class's factory methods: getTimeInstance, * getDateInstance, or getDateTimeInstance. * These methods automatically create a DateFormatSymbols for * the formatter so that you don't have to. After the * formatter is created, you may modify its format pattern using the * setPattern method. For more information about * creating formatters using DateFormat's factory methods, * see {@link DateFormat}. * *

* If you decide to create a date-time formatter with a specific * format pattern for a specific locale, you can do so with: *

*
 * new SimpleDateFormat(aPattern, DateFormatSymbols.getInstance(aLocale)).
 * 
*
* *

* DateFormatSymbols objects are cloneable. When you obtain * a DateFormatSymbols object, feel free to modify the * date-time formatting data. For instance, you can replace the localized * date-time format pattern characters with the ones that you feel easy * to remember. Or you can change the representative cities * to your favorite ones. * *

* New DateFormatSymbols subclasses may be added to support * SimpleDateFormat for date-time formatting for additional locales. * @see DateFormat * @see SimpleDateFormat * @see java.util.SimpleTimeZone * @author Chen-Lieh Huang */ public class DateFormatSymbols implements Serializable, Cloneable { /** * Construct a DateFormatSymbols object by loading format data from * resources for the default locale. This constructor can only * construct instances for the locales supported by the Java * runtime environment, not for those supported by installed * {@link java.text.spi.DateFormatSymbolsProvider DateFormatSymbolsProvider} * implementations. For full locale coverage, use the * {@link #getInstance(Locale) getInstance} method. * * @see #getInstance() * @exception java.util.MissingResourceException * if the resources for the default locale cannot be * found or cannot be loaded. */ public DateFormatSymbols() { initializeData(Locale.getDefault(Locale.Category.FORMAT)); } /** * Construct a DateFormatSymbols object by loading format data from * resources for the given locale. This constructor can only * construct instances for the locales supported by the Java * runtime environment, not for those supported by installed * {@link java.text.spi.DateFormatSymbolsProvider DateFormatSymbolsProvider} * implementations. For full locale coverage, use the * {@link #getInstance(Locale) getInstance} method. * * @see #getInstance(Locale) * @exception java.util.MissingResourceException * if the resources for the specified locale cannot be * found or cannot be loaded. */ public DateFormatSymbols(Locale locale) { initializeData(locale); } /** * Era strings. For example: "AD" and "BC". An array of 2 strings, * indexed by Calendar.BC and Calendar.AD. * @serial */ String eras[] = null; /** * Month strings. For example: "January", "February", etc. An array * of 13 strings (some calendars have 13 months), indexed by * Calendar.JANUARY, Calendar.FEBRUARY, etc. * @serial */ String months[] = null; /** * Short month strings. For example: "Jan", "Feb", etc. An array of * 13 strings (some calendars have 13 months), indexed by * Calendar.JANUARY, Calendar.FEBRUARY, etc. * @serial */ String shortMonths[] = null; /** * Weekday strings. For example: "Sunday", "Monday", etc. An array * of 8 strings, indexed by Calendar.SUNDAY, * Calendar.MONDAY, etc. * The element weekdays[0] is ignored. * @serial */ String weekdays[] = null; /** * Short weekday strings. For example: "Sun", "Mon", etc. An array * of 8 strings, indexed by Calendar.SUNDAY, * Calendar.MONDAY, etc. * The element shortWeekdays[0] is ignored. * @serial */ String shortWeekdays[] = null; /** * AM and PM strings. For example: "AM" and "PM". An array of * 2 strings, indexed by Calendar.AM and * Calendar.PM. * @serial */ String ampms[] = null; /** * Localized names of time zones in this locale. This is a * two-dimensional array of strings of size n by m, * where m is at least 5. Each of the n rows is an * entry containing the localized names for a single TimeZone. * Each such row contains (with i ranging from * 0..n-1): *

* The zone ID is not localized; it's one of the valid IDs of * the {@link java.util.TimeZone TimeZone} class that are not * custom IDs. * All other entries are localized names. * @see java.util.TimeZone * @serial */ String zoneStrings[][] = null; /** * Indicates that zoneStrings is set externally with setZoneStrings() method. */ transient boolean isZoneStringsSet = false; /** * Unlocalized date-time pattern characters. For example: 'y', 'd', etc. * All locales use the same these unlocalized pattern characters. */ static final String patternChars = "GyMdkHmsSEDFwWahKzZ"; /** * Localized date-time pattern characters. For example, a locale may * wish to use 'u' rather than 'y' to represent years in its date format * pattern strings. * This string must be exactly 18 characters long, with the index of * the characters described by DateFormat.ERA_FIELD, * DateFormat.YEAR_FIELD, etc. Thus, if the string were * "Xz...", then localized patterns would use 'X' for era and 'z' for year. * @serial */ String localPatternChars = null; /** * The locale which is used for initializing this DateFormatSymbols object. * * @since 1.6 * @serial */ Locale locale = null; /* use serialVersionUID from JDK 1.1.4 for interoperability */ static final long serialVersionUID = -5987973545549424702L; /** * Returns an array of all locales for which the * getInstance methods of this class can return * localized instances. * The returned array represents the union of locales supported by the * Java runtime and by installed * {@link java.text.spi.DateFormatSymbolsProvider DateFormatSymbolsProvider} * implementations. It must contain at least a Locale * instance equal to {@link java.util.Locale#US Locale.US}. * * @return An array of locales for which localized * DateFormatSymbols instances are available. * @since 1.6 */ public static Locale[] getAvailableLocales() { LocaleServiceProviderPool pool= LocaleServiceProviderPool.getPool(DateFormatSymbolsProvider.class); return pool.getAvailableLocales(); } /** * Gets the DateFormatSymbols instance for the default * locale. This method provides access to DateFormatSymbols * instances for locales supported by the Java runtime itself as well * as for those supported by installed * {@link java.text.spi.DateFormatSymbolsProvider DateFormatSymbolsProvider} * implementations. * @return a DateFormatSymbols instance. * @since 1.6 */ public static final DateFormatSymbols getInstance() { return getInstance(Locale.getDefault(Locale.Category.FORMAT)); } /** * Gets the DateFormatSymbols instance for the specified * locale. This method provides access to DateFormatSymbols * instances for locales supported by the Java runtime itself as well * as for those supported by installed * {@link java.text.spi.DateFormatSymbolsProvider DateFormatSymbolsProvider} * implementations. * @param locale the given locale. * @return a DateFormatSymbols instance. * @exception NullPointerException if locale is null * @since 1.6 */ public static final DateFormatSymbols getInstance(Locale locale) { // Check whether a provider can provide an implementation that's closer // to the requested locale than what the Java runtime itself can provide. LocaleServiceProviderPool pool = LocaleServiceProviderPool.getPool(DateFormatSymbolsProvider.class); if (pool.hasProviders()) { DateFormatSymbols providersInstance = pool.getLocalizedObject( DateFormatSymbolsGetter.INSTANCE, locale); if (providersInstance != null) { return providersInstance; } } return new DateFormatSymbols(locale); } /** * Gets era strings. For example: "AD" and "BC". * @return the era strings. */ public String[] getEras() { return Arrays.copyOf(eras, eras.length); } /** * Sets era strings. For example: "AD" and "BC". * @param newEras the new era strings. */ public void setEras(String[] newEras) { eras = Arrays.copyOf(newEras, newEras.length); } /** * Gets month strings. For example: "January", "February", etc. * @return the month strings. */ public String[] getMonths() { return Arrays.copyOf(months, months.length); } /** * Sets month strings. For example: "January", "February", etc. * @param newMonths the new month strings. */ public void setMonths(String[] newMonths) { months = Arrays.copyOf(newMonths, newMonths.length); } /** * Gets short month strings. For example: "Jan", "Feb", etc. * @return the short month strings. */ public String[] getShortMonths() { return Arrays.copyOf(shortMonths, shortMonths.length); } /** * Sets short month strings. For example: "Jan", "Feb", etc. * @param newShortMonths the new short month strings. */ public void setShortMonths(String[] newShortMonths) { shortMonths = Arrays.copyOf(newShortMonths, newShortMonths.length); } /** * Gets weekday strings. For example: "Sunday", "Monday", etc. * @return the weekday strings. Use Calendar.SUNDAY, * Calendar.MONDAY, etc. to index the result array. */ public String[] getWeekdays() { return Arrays.copyOf(weekdays, weekdays.length); } /** * Sets weekday strings. For example: "Sunday", "Monday", etc. * @param newWeekdays the new weekday strings. The array should * be indexed by Calendar.SUNDAY, * Calendar.MONDAY, etc. */ public void setWeekdays(String[] newWeekdays) { weekdays = Arrays.copyOf(newWeekdays, newWeekdays.length); } /** * Gets short weekday strings. For example: "Sun", "Mon", etc. * @return the short weekday strings. Use Calendar.SUNDAY, * Calendar.MONDAY, etc. to index the result array. */ public String[] getShortWeekdays() { return Arrays.copyOf(shortWeekdays, shortWeekdays.length); } /** * Sets short weekday strings. For example: "Sun", "Mon", etc. * @param newShortWeekdays the new short weekday strings. The array should * be indexed by Calendar.SUNDAY, * Calendar.MONDAY, etc. */ public void setShortWeekdays(String[] newShortWeekdays) { shortWeekdays = Arrays.copyOf(newShortWeekdays, newShortWeekdays.length); } /** * Gets ampm strings. For example: "AM" and "PM". * @return the ampm strings. */ public String[] getAmPmStrings() { return Arrays.copyOf(ampms, ampms.length); } /** * Sets ampm strings. For example: "AM" and "PM". * @param newAmpms the new ampm strings. */ public void setAmPmStrings(String[] newAmpms) { ampms = Arrays.copyOf(newAmpms, newAmpms.length); } /** * Gets time zone strings. Use of this method is discouraged; use * {@link java.util.TimeZone#getDisplayName() TimeZone.getDisplayName()} * instead. *

* The value returned is a * two-dimensional array of strings of size n by m, * where m is at least 5. Each of the n rows is an * entry containing the localized names for a single TimeZone. * Each such row contains (with i ranging from * 0..n-1): *

* The zone ID is not localized; it's one of the valid IDs of * the {@link java.util.TimeZone TimeZone} class that are not * custom IDs. * All other entries are localized names. If a zone does not implement * daylight saving time, the daylight saving time names should not be used. *

* If {@link #setZoneStrings(String[][]) setZoneStrings} has been called * on this DateFormatSymbols instance, then the strings * provided by that call are returned. Otherwise, the returned array * contains names provided by the Java runtime and by installed * {@link java.util.spi.TimeZoneNameProvider TimeZoneNameProvider} * implementations. * * @return the time zone strings. * @see #setZoneStrings(String[][]) */ public String[][] getZoneStrings() { return getZoneStringsImpl(true); } /** * Sets time zone strings. The argument must be a * two-dimensional array of strings of size n by m, * where m is at least 5. Each of the n rows is an * entry containing the localized names for a single TimeZone. * Each such row contains (with i ranging from * 0..n-1): *

* The zone ID is not localized; it's one of the valid IDs of * the {@link java.util.TimeZone TimeZone} class that are not * custom IDs. * All other entries are localized names. * * @param newZoneStrings the new time zone strings. * @exception IllegalArgumentException if the length of any row in * newZoneStrings is less than 5 * @exception NullPointerException if newZoneStrings is null * @see #getZoneStrings() */ public void setZoneStrings(String[][] newZoneStrings) { String[][] aCopy = new String[newZoneStrings.length][]; for (int i = 0; i < newZoneStrings.length; ++i) { int len = newZoneStrings[i].length; if (len < 5) { throw new IllegalArgumentException(); } aCopy[i] = Arrays.copyOf(newZoneStrings[i], len); } zoneStrings = aCopy; isZoneStringsSet = true; } /** * Gets localized date-time pattern characters. For example: 'u', 't', etc. * @return the localized date-time pattern characters. */ public String getLocalPatternChars() { return new String(localPatternChars); } /** * Sets localized date-time pattern characters. For example: 'u', 't', etc. * @param newLocalPatternChars the new localized date-time * pattern characters. */ public void setLocalPatternChars(String newLocalPatternChars) { localPatternChars = new String(newLocalPatternChars); } /** * Overrides Cloneable */ public Object clone() { try { DateFormatSymbols other = (DateFormatSymbols)super.clone(); copyMembers(this, other); return other; } catch (CloneNotSupportedException e) { throw new InternalError(); } } /** * Override hashCode. * Generates a hash code for the DateFormatSymbols object. */ public int hashCode() { int hashcode = 0; String[][] zoneStrings = getZoneStringsWrapper(); for (int index = 0; index < zoneStrings[0].length; ++index) hashcode ^= zoneStrings[0][index].hashCode(); return hashcode; } /** * Override equals */ public boolean equals(Object obj) { if (this == obj) return true; if (obj == null || getClass() != obj.getClass()) return false; DateFormatSymbols that = (DateFormatSymbols) obj; return (Arrays.equals(eras, that.eras) && Arrays.equals(months, that.months) && Arrays.equals(shortMonths, that.shortMonths) && Arrays.equals(weekdays, that.weekdays) && Arrays.equals(shortWeekdays, that.shortWeekdays) && Arrays.equals(ampms, that.ampms) && Arrays.deepEquals(getZoneStringsWrapper(), that.getZoneStringsWrapper()) && ((localPatternChars != null && localPatternChars.equals(that.localPatternChars)) || (localPatternChars == null && that.localPatternChars == null))); } // =======================privates=============================== /** * Useful constant for defining time zone offsets. */ static final int millisPerHour = 60*60*1000; /** * Cache to hold the FormatData and TimeZoneNames ResourceBundles * of a Locale. */ private static Hashtable cachedLocaleData = new Hashtable(3); /** * Look up resource data for the desiredLocale in the cache; update the * cache if necessary. */ private static ResourceBundle cacheLookup(Locale desiredLocale) { ResourceBundle rb; SoftReference data = (SoftReference)cachedLocaleData.get(desiredLocale); if (data == null) { rb = LocaleData.getDateFormatData(desiredLocale); data = new SoftReference(rb); cachedLocaleData.put(desiredLocale, data); } else { if ((rb = (ResourceBundle)data.get()) == null) { rb = LocaleData.getDateFormatData(desiredLocale); data = new SoftReference(rb); } } return rb; } private void initializeData(Locale desiredLocale) { int i; ResourceBundle resource = cacheLookup(desiredLocale); // FIXME: cache only ResourceBundle. Hence every time, will do // getObject(). This won't be necessary if the Resource itself // is cached. eras = (String[])resource.getObject("Eras"); months = resource.getStringArray("MonthNames"); shortMonths = resource.getStringArray("MonthAbbreviations"); String[] lWeekdays = resource.getStringArray("DayNames"); weekdays = new String[8]; weekdays[0] = ""; // 1-based for (i=0; izoneStrings field is initialized in order to make * sure the backward compatibility. * * @since 1.6 */ private void writeObject(ObjectOutputStream stream) throws IOException { if (zoneStrings == null) { zoneStrings = TimeZoneNameUtility.getZoneStrings(locale); } stream.defaultWriteObject(); } /** * Obtains a DateFormatSymbols instance from a DateFormatSymbolsProvider * implementation. */ private static class DateFormatSymbolsGetter implements LocaleServiceProviderPool.LocalizedObjectGetter { private static final DateFormatSymbolsGetter INSTANCE = new DateFormatSymbolsGetter(); public DateFormatSymbols getObject(DateFormatSymbolsProvider dateFormatSymbolsProvider, Locale locale, String key, Object... params) { assert params.length == 0; return dateFormatSymbolsProvider.getInstance(locale); } } }