/*
* @(#)DateFormatSymbols.java 1.41 03/12/19
*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
/*
* (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.util.Locale;
import java.util.ResourceBundle;
import java.io.Serializable;
import java.lang.ref.SoftReference;
import java.util.Vector;
import java.util.Enumeration;
import sun.text.Utility;
import sun.text.resources.LocaleData;
import java.util.Hashtable;
/**
* 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, new DateFormatSymbols(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
* @version 1.41 12/19/03
* @author Chen-Lieh Huang
*/
public class DateFormatSymbols implements Serializable, Cloneable {
/**
* Construct a DateFormatSymbols object by loading format data from
* resources for the default locale.
*
* @exception java.util.MissingResourceException
* if the resources for the default locale cannot be
* found or cannot be loaded.
*/
public DateFormatSymbols()
{
initializeData(Locale.getDefault());
}
/**
* Construct a DateFormatSymbols object by loading format data from
* resources for the given 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):
*
zoneStrings[i][0]
- time zone IDzoneStrings[i][1]
- long name of zone in standard
* timezoneStrings[i][2]
- short name of zone in
* standard timezoneStrings[i][3]
- long name of zone in daylight
* savings timezoneStrings[i][4]
- short name of zone in daylight
* savings timeDateFormat.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;
/* use serialVersionUID from JDK 1.1.4 for interoperability */
static final long serialVersionUID = -5987973545549424702L;
/**
* Gets era strings. For example: "AD" and "BC".
* @return the era strings.
*/
public String[] getEras() {
return duplicate(eras);
}
/**
* Sets era strings. For example: "AD" and "BC".
* @param newEras the new era strings.
*/
public void setEras(String[] newEras) {
eras = duplicate(newEras);
}
/**
* Gets month strings. For example: "January", "February", etc.
* @return the month strings.
*/
public String[] getMonths() {
return duplicate(months);
}
/**
* Sets month strings. For example: "January", "February", etc.
* @param newMonths the new month strings.
*/
public void setMonths(String[] newMonths) {
months = duplicate(newMonths);
}
/**
* Gets short month strings. For example: "Jan", "Feb", etc.
* @return the short month strings.
*/
public String[] getShortMonths() {
return duplicate(shortMonths);
}
/**
* Sets short month strings. For example: "Jan", "Feb", etc.
* @param newShortMonths the new short month strings.
*/
public void setShortMonths(String[] newShortMonths) {
shortMonths = duplicate(newShortMonths);
}
/**
* 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 duplicate(weekdays);
}
/**
* 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 = duplicate(newWeekdays);
}
/**
* 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 duplicate(shortWeekdays);
}
/**
* 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 = duplicate(newShortWeekdays);
}
/**
* Gets ampm strings. For example: "AM" and "PM".
* @return the ampm strings.
*/
public String[] getAmPmStrings() {
return duplicate(ampms);
}
/**
* Sets ampm strings. For example: "AM" and "PM".
* @param newAmpms the new ampm strings.
*/
public void setAmPmStrings(String[] newAmpms) {
ampms = duplicate(newAmpms);
}
/**
* Gets timezone strings.
* @return the timezone strings.
*/
public String[][] getZoneStrings() {
String[][] aCopy = new String[zoneStrings.length][];
for (int i = 0; i < zoneStrings.length; ++i)
aCopy[i] = duplicate(zoneStrings[i]);
return aCopy;
}
/**
* Sets timezone strings.
* @param newZoneStrings the new timezone strings.
*/
public void setZoneStrings(String[][] newZoneStrings) {
String[][] aCopy = new String[newZoneStrings.length][];
for (int i = 0; i < newZoneStrings.length; ++i)
aCopy[i] = duplicate(newZoneStrings[i]);
zoneStrings = aCopy;
}
/**
* 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;
for (int index = 0; index < this.zoneStrings[0].length; ++index)
hashcode ^= this.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 (Utility.arrayEquals(eras, that.eras)
&& Utility.arrayEquals(months, that.months)
&& Utility.arrayEquals(shortMonths, that.shortMonths)
&& Utility.arrayEquals(weekdays, that.weekdays)
&& Utility.arrayEquals(shortWeekdays, that.shortWeekdays)
&& Utility.arrayEquals(ampms, that.ampms)
&& Utility.arrayEquals(zoneStrings, that.zoneStrings)
&& Utility.arrayEquals(localPatternChars,
that.localPatternChars));
}
// =======================privates===============================
/**
* Useful constant for defining timezone offsets.
*/
static final int millisPerHour = 60*60*1000;
/**
* Cache to hold the LocaleElements and DateFormatZoneData ResourceBundles
* of a Locale.
*/
private static Hashtable cachedLocaleData = new Hashtable(3);
/**
* cache to hold time zone localized strings. Keyed by Locale
*/
private static Hashtable cachedZoneData = new Hashtable();
/**
* Look up resource data for the desiredLocale in the cache; update the
* cache if necessary.
*/
private ResourceBundle[] cacheLookup(Locale desiredLocale) {
ResourceBundle[] rbs = new ResourceBundle[2];
SoftReference[] data
= (SoftReference[])cachedLocaleData.get(desiredLocale);
if (data == null) {
rbs[0] = LocaleData.getLocaleElements(desiredLocale);
rbs[1] = LocaleData.getDateFormatZoneData(desiredLocale);
data = new SoftReference[] { new SoftReference(rbs[0]),
new SoftReference(rbs[1]) };
cachedLocaleData.put(desiredLocale, data);
} else {
ResourceBundle r;
if ((r = (ResourceBundle)data[0].get()) == null) {
r = LocaleData.getLocaleElements(desiredLocale);
data[0] = new SoftReference(r);
}
rbs[0] = r;
if ((r = (ResourceBundle)data[1].get()) == null) {
r = LocaleData.getDateFormatZoneData(desiredLocale);
data[1] = new SoftReference(r);
}
rbs[1] = r;
}
return rbs;
}
/**
* Load time zone localized strings. Enumerate all keys (except
* "localPatternChars" and "zoneStrings").
*/
private String[][] loadZoneStrings(Locale desiredLocale,
ResourceBundle rsrc)
{
String[][] zones;
SoftReference data = (SoftReference)cachedZoneData.get(desiredLocale);
if (data == null || ((zones = (String[][])data.get()) == null)) {
Vector vec = new Vector();
Enumeration keys = rsrc.getKeys();
while(keys.hasMoreElements()) {
String key = (String)keys.nextElement();
if (!key.equals("localPatternChars") &&
!key.equals("zoneStrings")) {
vec.add(rsrc.getObject(key));
}
}
zones = new String[vec.size()][];
vec.toArray(zones);
data = new SoftReference(zones);
cachedZoneData.put(desiredLocale, data);
}
return zones;
}
private void initializeData(Locale desiredLocale)
{
int i;
ResourceBundle[] rbs = cacheLookup(desiredLocale);
ResourceBundle resource = rbs[0];
ResourceBundle zoneResource = rbs[1];
// 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; i