This adds the retrieval of display names to Calendar, bringing it in line with 1.6. It also adds the new getInstance methods to DateFormatSymbols, so that they can be called from Calendar, but these aren't yet fully implemented. They need to use the new SPI we don't have yet, and that's too much for this one patch. I'll fix this soon.
Changelog:
2006-12-29 Andrew John Hughes <[EMAIL PROTECTED]>
* java/text/DateFormatSymbols:
(DateFormatSymbols()): Update documentation.
(DateFormatSymbols(Locale)): Likewise.
(getInstance()): Implemented.
(getInstance(Locale)): Partially implemented.
* java/util/Calendar.java:
(SHORT, LONG, ALL_STYLES): New constants.
(getDisplayName(int,int,Locale)); Implemented.
(getDisplayNames(int,int,Locale)): Likewise.
--
Andrew :-)
Escape the Java Trap with GNU Classpath!
http://www.gnu.org/philosophy/java-trap.html
public class gcj extends Freedom implements Java { ... }
Index: java/text/DateFormatSymbols.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/text/DateFormatSymbols.java,v
retrieving revision 1.20
diff -u -3 -p -u -r1.20 DateFormatSymbols.java
--- java/text/DateFormatSymbols.java 16 Mar 2006 14:13:54 -0000 1.20
+++ java/text/DateFormatSymbols.java 29 Dec 2006 02:13:23 -0000
@@ -1,5 +1,5 @@
/* DateFormatSymbols.java -- Format over a range of numbers
- Copyright (C) 1998, 1999, 2000, 2001, 2003, 2005 Free Software Foundation,
Inc.
+ Copyright (C) 1998, 1999, 2000, 2001, 2003, 2005, 2006 Free Software
Foundation, Inc.
This file is part of GNU Classpath.
@@ -45,7 +45,9 @@ import java.util.ResourceBundle;
/**
* This class acts as container for locale specific date/time formatting
* information such as the days of the week and the months of the year.
+ *
* @author Per Bothner ([EMAIL PROTECTED])
+ * @author Andrew John Hughes ([EMAIL PROTECTED])
* @date October 24, 1998.
*/
/* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3.
@@ -114,11 +116,17 @@ public class DateFormatSymbols implement
/**
* This method initializes a new instance of <code>DateFormatSymbols</code>
* by loading the date format information for the specified locale.
+ * This constructor only obtains instances using the runtime's resources;
+ * to also include [EMAIL PROTECTED]
java.text.spi.DateFormatSymbolsProvider} instances,
+ * call [EMAIL PROTECTED] #getInstance(Locale)} instead.
*
* @param locale The locale for which date formatting symbols should
* be loaded.
+ * @throws MissingResourceException if the resources for the specified
+ * locale could not be found or loaded.
*/
- public DateFormatSymbols (Locale locale) throws MissingResourceException
+ public DateFormatSymbols (Locale locale)
+ throws MissingResourceException
{
ResourceBundle res
= ResourceBundle.getBundle("gnu.java.locale.LocaleInformation", locale,
@@ -139,8 +147,12 @@ public class DateFormatSymbols implement
/**
* This method loads the format symbol information for the default
* locale.
+ *
+ * @throws MissingResourceException if the resources for the default
+ * locale could not be found or loaded.
*/
- public DateFormatSymbols () throws MissingResourceException
+ public DateFormatSymbols()
+ throws MissingResourceException
{
this (Locale.getDefault());
}
@@ -537,4 +549,50 @@ public class DateFormatSymbols implement
^ hashCode(weekdays)
^ hashCode(zoneStrings));
}
+
+ /**
+ * Returns a [EMAIL PROTECTED] DateFormatSymbols} instance for the
+ * default locale obtained from either the runtime itself
+ * or one of the installed
+ * [EMAIL PROTECTED] java.text.spi.DateFormatSymbolsProvider} instances.
+ * This is equivalent to calling
+ * <code>getInstance(Locale.getDefault())</code>.
+ *
+ * @return a [EMAIL PROTECTED] DateFormatSymbols} instance for the default
+ * locale.
+ * @since 1.6
+ */
+ public static final DateFormatSymbols getInstance()
+ {
+ return getInstance(Locale.getDefault());
+ }
+
+ /**
+ * Returns a [EMAIL PROTECTED] DateFormatSymbols} instance for the
+ * specified locale obtained from either the runtime itself
+ * or one of the installed
+ * [EMAIL PROTECTED] java.text.spi.DateFormatSymbolsProvider} instances.
+ *
+ * @param locale the locale for which an instance should be
+ * returned.
+ * @return a [EMAIL PROTECTED] DateFormatSymbols} instance for the specified
+ * locale.
+ * @throws NullPointerException if <code>locale</code> is
+ * <code>null</code>.
+ * @since 1.6
+ */
+ public static final DateFormatSymbols getInstance(Locale locale)
+ {
+ try
+ {
+ DateFormatSymbols syms = new DateFormatSymbols(locale);
+ return syms;
+ }
+ catch (MissingResourceException e)
+ {
+ /* FIXME: Check the service provider */
+ return null;
+ }
+ }
+
}
Index: java/util/Calendar.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/util/Calendar.java,v
retrieving revision 1.51
diff -u -3 -p -u -r1.51 Calendar.java
--- java/util/Calendar.java 10 Dec 2006 20:25:46 -0000 1.51
+++ java/util/Calendar.java 29 Dec 2006 02:13:25 -0000
@@ -43,9 +43,12 @@ import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
+
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
+import java.text.DateFormatSymbols;
+
/**
* This class is an abstract base class for Calendars, which can be
* used to convert between <code>Date</code> objects and a set of
@@ -99,6 +102,20 @@ day_of_week + week_of_year</pre>
* specific field by one, propagating overflows), or
* <code>add</code>ing/substracting a fixed amount to a field.
*
+ * @author Aaron M. Renn ([EMAIL PROTECTED])
+ * @author Jochen Hoenicke ([EMAIL PROTECTED])
+ * @author Warren Levy ([EMAIL PROTECTED])
+ * @author Jeff Sturm ([EMAIL PROTECTED])
+ * @author Tom Tromey ([EMAIL PROTECTED])
+ * @author Bryce McKinlay ([EMAIL PROTECTED])
+ * @author Ingo Proetel ([EMAIL PROTECTED])
+ * @author Jerry Quinn ([EMAIL PROTECTED])
+ * @author Jeroen Frijters ([EMAIL PROTECTED])
+ * @author Noa Resare ([EMAIL PROTECTED])
+ * @author Sven de Marothy ([EMAIL PROTECTED])
+ * @author David Gilbert ([EMAIL PROTECTED])
+ * @author Olivier Jolly ([EMAIL PROTECTED])
+ * @author Andrew John Hughes ([EMAIL PROTECTED])
* @see Date
* @see GregorianCalendar
* @see TimeZone
@@ -326,6 +343,34 @@ public abstract class Calendar
public static final int PM = 1;
/**
+ * A style specifier for [EMAIL PROTECTED] #getDisplayNames(int,int,Locale)}
+ * stating that names should be returned in both long and short variants.
+ *
+ * @since 1.6
+ * @see #SHORT
+ * @see #LONG
+ */
+ public static final int ALL_STYLES = 0;
+
+ /**
+ * A style specifier for [EMAIL PROTECTED] #getDisplayName(int,int,Locale)}
+ * and [EMAIL PROTECTED] #getDisplayNames(int,int,Locale)} stating that names
+ * should be returned in their short variant if applicable.
+ *
+ * @since 1.6
+ */
+ public static final int SHORT = 1;
+
+ /**
+ * A style specifier for [EMAIL PROTECTED] #getDisplayName(int,int,Locale)}
+ * and [EMAIL PROTECTED] #getDisplayNames(int,int,Locale)} stating that names
+ * should be returned in their long variant if applicable.
+ *
+ * @since 1.6
+ */
+ public static final int LONG = 2;
+
+ /**
* The time fields. The array is indexed by the constants YEAR to
* DST_OFFSET.
* @serial
@@ -527,7 +572,7 @@ public abstract class Calendar
* Cache of locale->calendar-class mappings. This avoids having to do a
ResourceBundle
* lookup for every getInstance call.
*/
- private static HashMap cache = new HashMap();
+ private static HashMap<Locale,Class> cache = new HashMap<Locale,Class>();
/** Preset argument types for calendar-class constructor lookup. */
private static Class[] ctorArgTypes = new Class[]
@@ -549,7 +594,7 @@ public abstract class Calendar
*/
public static synchronized Calendar getInstance(TimeZone zone, Locale locale)
{
- Class calendarClass = (Class) cache.get(locale);
+ Class calendarClass = cache.get(locale);
Throwable exception = null;
try
@@ -1343,4 +1388,205 @@ public abstract class Calendar
areFieldsSet = false;
}
}
+
+ /**
+ * Returns a localised textual representation of the current value
+ * of the given field using the specified style. If there is no
+ * applicable textual representation (e.g. the field has a numeric
+ * value), then <code>null</code> is returned. If one does exist,
+ * then the value is obtained from [EMAIL PROTECTED] #get(int)} and converted
+ * appropriately. For example, if the <code>MONTH</code> field is
+ * requested, then <code>get(MONTH)</code> is called. This is then
+ * converted to a textual representation based on its value and
+ * the style requested; if the <code>LONG</code> style is requested
+ * and the returned value is <code>11</code> from a
+ * [EMAIL PROTECTED] GregorianCalendar} implementation, then
<code>"December"</code>
+ * is returned. By default, a textual representation is available
+ * for all fields which have an applicable value obtainable from
+ * [EMAIL PROTECTED] java.text.DateFormatSymbols}.
+ *
+ * @param field the calendar field whose textual representation should
+ * be obtained.
+ * @param style the style to use; either [EMAIL PROTECTED] #LONG} or [EMAIL
PROTECTED] #SHORT}.
+ * @param locale the locale to use for translation.
+ * @return the textual representation of the given field in the specified
+ * style, or <code>null</code> if none is applicable.
+ * @throws IllegalArgumentException if <code>field</code> or
<code>style</code>
+ * or invalid, or the calendar is
non-lenient
+ * and has invalid values.
+ * @throws NullPointerException if <code>locale</code> is <code>null</code>.
+ * @since 1.6
+ */
+ public String getDisplayName(int field, int style, Locale locale)
+ {
+ if (field < 0 || field >= FIELD_COUNT)
+ throw new IllegalArgumentException("The field value, " + field +
+ ", is invalid.");
+ if (style != SHORT && style != LONG)
+ throw new IllegalArgumentException("The style must be either " +
+ "short or long.");
+ if (field == YEAR || field == WEEK_OF_YEAR ||
+ field == WEEK_OF_MONTH || field == DAY_OF_MONTH ||
+ field == DAY_OF_YEAR || field == DAY_OF_WEEK_IN_MONTH ||
+ field == HOUR || field == HOUR_OF_DAY || field == MINUTE ||
+ field == SECOND || field == MILLISECOND)
+ return null;
+
+ int value = get(field);
+ DateFormatSymbols syms = DateFormatSymbols.getInstance(locale);
+ if (field == ERA)
+ return syms.getEras()[value];
+ if (field == MONTH)
+ if (style == LONG)
+ return syms.getMonths()[value];
+ else
+ return syms.getShortMonths()[value];
+ if (field == DAY_OF_WEEK)
+ if (style == LONG)
+ return syms.getWeekdays()[value];
+ else
+ return syms.getShortWeekdays()[value];
+ if (field == AM_PM)
+ return syms.getAmPmStrings()[value];
+ if (field == ZONE_OFFSET)
+ if (style == LONG)
+ return syms.getZoneStrings()[value][1];
+ else
+ return syms.getZoneStrings()[value][2];
+ if (field == DST_OFFSET)
+ if (style == LONG)
+ return syms.getZoneStrings()[value][3];
+ else
+ return syms.getZoneStrings()[value][4];
+
+ throw new InternalError("Failed to resolve field " + field +
+ " with style " + style + " for locale " +
+ locale);
+ }
+
+ /**
+ * Returns a map linking all specified textual representations
+ * of the given field to their numerical values. The textual
+ * representations included are determined by the specified
+ * style and locale. For example, if the style <code>LONG</code>
+ * is specified and the German locale, then the map will
+ * contain "Montag" to [EMAIL PROTECTED] #MONDAY}, "Dienstag" to
+ * [EMAIL PROTECTED] #TUESDAY}, "Mittwoch" to [EMAIL PROTECTED] #WEDNESDAY}
and
+ * so on. The default implementation uses the values returned
+ * by [EMAIL PROTECTED] DateFormatSymbols} so, for example, the style
+ * [EMAIL PROTECTED] #ALL_STYLES} and the field [EMAIL PROTECTED] #MONTH}
will return
+ * a map filled with the values returned from
+ * [EMAIL PROTECTED] DateFormatSymbols#getMonths()} and
+ * [EMAIL PROTECTED] DateFormatSymbols#getShortMonths()}. If there are
+ * no textual representations for a given field (usually because
+ * it is purely numeric, such as the year in the
+ * [EMAIL PROTECTED] GregorianCalendar}), <code>null</code> is returned.
+ *
+ * @param field the calendar field whose textual representation should
+ * be obtained.
+ * @param style the style to use; either [EMAIL PROTECTED] #LONG}, [EMAIL
PROTECTED] #SHORT}
+ * or [EMAIL PROTECTED] ALL_STYLES}.
+ * @param locale the locale to use for translation.
+ * @return a map of the textual representations of the given field in the
+ * specified style to their numeric values, or <code>null</code>
+ * if none is applicable.
+ * @throws IllegalArgumentException if <code>field</code> or
<code>style</code>
+ * or invalid, or the calendar is
non-lenient
+ * and has invalid values.
+ * @throws NullPointerException if <code>locale</code> is <code>null</code>.
+ * @since 1.6
+ */
+ public Map<String,Integer> getDisplayNames(int field, int style, Locale
locale)
+ {
+ if (field < 0 || field >= FIELD_COUNT)
+ throw new IllegalArgumentException("The field value, " + field +
+ ", is invalid.");
+ if (style != SHORT && style != LONG && style != ALL_STYLES)
+ throw new IllegalArgumentException("The style must be either " +
+ "short, long or all styles.");
+ if (field == YEAR || field == WEEK_OF_YEAR ||
+ field == WEEK_OF_MONTH || field == DAY_OF_MONTH ||
+ field == DAY_OF_YEAR || field == DAY_OF_WEEK_IN_MONTH ||
+ field == HOUR || field == HOUR_OF_DAY || field == MINUTE ||
+ field == SECOND || field == MILLISECOND)
+ return null;
+
+ DateFormatSymbols syms = DateFormatSymbols.getInstance(locale);
+ Map<String,Integer> map = new HashMap<String,Integer>();
+ if (field == ERA)
+ {
+ String[] eras = syms.getEras();
+ for (int a = 0; a < eras.length; ++a)
+ map.put(eras[a], a);
+ return map;
+ }
+ if (field == MONTH)
+ {
+ if (style == LONG || style == ALL_STYLES)
+ {
+ String[] months = syms.getMonths();
+ for (int a = 0; a < months.length; ++a)
+ map.put(months[a], a);
+ }
+ if (style == SHORT || style == ALL_STYLES)
+ {
+ String[] months = syms.getShortMonths();
+ for (int a = 0; a < months.length; ++a)
+ map.put(months[a], a);
+ }
+ return map;
+ }
+ if (field == DAY_OF_WEEK)
+ {
+ if (style == LONG || style == ALL_STYLES)
+ {
+ String[] weekdays = syms.getWeekdays();
+ for (int a = SUNDAY; a < weekdays.length; ++a)
+ map.put(weekdays[a], a);
+ }
+ if (style == SHORT || style == ALL_STYLES)
+ {
+ String[] weekdays = syms.getShortWeekdays();
+ for (int a = SUNDAY; a < weekdays.length; ++a)
+ map.put(weekdays[a], a);
+ }
+ return map;
+ }
+ if (field == AM_PM)
+ {
+ String[] ampms = syms.getAmPmStrings();
+ for (int a = 0; a < ampms.length; ++a)
+ map.put(ampms[a], a);
+ return map;
+ }
+ if (field == ZONE_OFFSET)
+ {
+ String[][] zones = syms.getZoneStrings();
+ for (int a = 0; a < zones.length; ++a)
+ {
+ if (style == LONG || style == ALL_STYLES)
+ map.put(zones[a][1], a);
+ if (style == SHORT || style == ALL_STYLES)
+ map.put(zones[a][2], a);
+ }
+ return map;
+ }
+ if (field == DST_OFFSET)
+ {
+ String[][] zones = syms.getZoneStrings();
+ for (int a = 0; a < zones.length; ++a)
+ {
+ if (style == LONG || style == ALL_STYLES)
+ map.put(zones[a][3], a);
+ if (style == SHORT || style == ALL_STYLES)
+ map.put(zones[a][4], a);
+ }
+ return map;
+ }
+
+ throw new InternalError("Failed to resolve field " + field +
+ " with style " + style + " for locale " +
+ locale);
+ }
+
}
signature.asc
Description: Digital signature
