Merge from the pain train
[official-gcc.git] / libjava / java / text / DateFormatSymbols.java
blob9555cfb67bcb166d62977e6e4d296ab946fbd353
1 /* DateFormatSymbols.java -- Format over a range of numbers
2 Copyright (C) 1998, 1999, 2000, 2001, 2003, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.text;
41 import java.util.Locale;
42 import java.util.MissingResourceException;
43 import java.util.ResourceBundle;
45 /**
46 * This class acts as container for locale specific date/time formatting
47 * information such as the days of the week and the months of the year.
48 * @author Per Bothner (bothner@cygnus.com)
49 * @date October 24, 1998.
51 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3.
52 * Status: Believed complete and correct.
54 public class DateFormatSymbols implements java.io.Serializable, Cloneable
56 String[] ampms;
57 String[] eras;
58 private String localPatternChars;
59 String[] months;
60 String[] shortMonths;
61 String[] shortWeekdays;
62 String[] weekdays;
63 private String[][] zoneStrings;
65 private static final long serialVersionUID = -5987973545549424702L;
67 // The order of these prefixes must be the same as in DateFormat
68 private static final String[] formatPrefixes =
70 "full", "long", "medium", "short"
73 // These are each arrays with a value for SHORT, MEDIUM, LONG, FULL,
74 // and DEFAULT (constants defined in java.text.DateFormat). While
75 // not part of the official spec, we need a way to get at locale-specific
76 // default formatting patterns. They are declared package scope so
77 // as to be easily accessible where needed (DateFormat, SimpleDateFormat).
78 transient String[] dateFormats;
79 transient String[] timeFormats;
81 private String[] formatsForKey(ResourceBundle res, String key)
83 String[] values = new String [formatPrefixes.length];
84 for (int i = 0; i < formatPrefixes.length; i++)
86 values[i] = res.getString(formatPrefixes[i]+key);
88 return values;
91 /**
92 * This method initializes a new instance of <code>DateFormatSymbols</code>
93 * by loading the date format information for the specified locale.
95 * @param locale The locale for which date formatting symbols should
96 * be loaded.
98 public DateFormatSymbols (Locale locale) throws MissingResourceException
100 ResourceBundle res
101 = ResourceBundle.getBundle("gnu.java.locale.LocaleInformation", locale,
102 ClassLoader.getSystemClassLoader());
104 ampms = res.getStringArray ("ampms");
105 eras = res.getStringArray ("eras");
106 localPatternChars = res.getString ("localPatternChars");
107 months = res.getStringArray ("months");
108 shortMonths = res.getStringArray ("shortMonths");
109 shortWeekdays = res.getStringArray ("shortWeekdays");
110 weekdays = res.getStringArray ("weekdays");
111 zoneStrings = (String[][]) res.getObject ("zoneStrings");
113 dateFormats = formatsForKey(res, "DateFormat");
114 timeFormats = formatsForKey(res, "TimeFormat");
118 * This method loads the format symbol information for the default
119 * locale.
121 public DateFormatSymbols () throws MissingResourceException
123 this (Locale.getDefault());
127 * This method returns the list of strings used for displaying AM or PM.
128 * This is a two element <code>String</code> array indexed by
129 * <code>Calendar.AM</code> and <code>Calendar.PM</code>
131 * @return The list of AM/PM display strings.
133 public String[] getAmPmStrings()
135 return ampms;
139 * This method returns the list of strings used for displaying eras
140 * (e.g., "BC" and "AD"). This is a two element <code>String</code>
141 * array indexed by <code>Calendar.BC</code> and <code>Calendar.AD</code>.
143 * @return The list of era disply strings.
145 public String[] getEras()
147 return eras;
151 * This method returns the pattern character information for this
152 * object. This is an 18 character string that contains the characters
153 * that are used in creating the date formatting strings in
154 * <code>SimpleDateFormat</code>. The following are the character
155 * positions in the string and which format character they correspond
156 * to (the character in parentheses is the default value in the US English
157 * locale):
158 * <p>
159 * <ul>
160 * <li>0 - era (G)</li>
161 * <li>1 - year (y)</li>
162 * <li>2 - month (M)</li>
163 * <li>3 - day of month (d)</li>
164 * <li>4 - hour out of 12, from 1-12 (h)</li>
165 * <li>5 - hour out of 24, from 0-23 (H)</li>
166 * <li>6 - minute (m)</li>
167 * <li>7 - second (s)</li>
168 * <li>8 - millisecond (S)</li>
169 * <li>9 - date of week (E)</li>
170 * <li>10 - date of year (D)</li>
171 * <li>11 - day of week in month, eg. "4th Thur in Nov" (F)</li>
172 * <li>12 - week in year (w)</li>
173 * <li>13 - week in month (W)</li>
174 * <li>14 - am/pm (a)</li>
175 * <li>15 - hour out of 24, from 1-24 (k)</li>
176 * <li>16 - hour out of 12, from 0-11 (K)</li>
177 * <li>17 - time zone (z)</li>
178 * </ul>
180 * @return The format patter characters
182 public String getLocalPatternChars()
184 return localPatternChars;
188 * This method returns the list of strings used for displaying month
189 * names (e.g., "January" and "February"). This is a thirteen element
190 * string array indexed by <code>Calendar.JANUARY</code> through
191 * <code>Calendar.UNDECEMBER</code>. Note that there are thirteen
192 * elements because some calendars have thriteen months.
194 * @return The list of month display strings.
196 public String[] getMonths ()
198 return months;
202 * This method returns the list of strings used for displaying abbreviated
203 * month names (e.g., "Jan" and "Feb"). This is a thirteen element
204 * <code>String</code> array indexed by <code>Calendar.JANUARY</code>
205 * through <code>Calendar.UNDECEMBER</code>. Note that there are thirteen
206 * elements because some calendars have thirteen months.
208 * @return The list of abbreviated month display strings.
210 public String[] getShortMonths ()
212 return shortMonths;
216 * This method returns the list of strings used for displaying abbreviated
217 * weekday names (e.g., "Sun" and "Mon"). This is an eight element
218 * <code>String</code> array indexed by <code>Calendar.SUNDAY</code>
219 * through <code>Calendar.SATURDAY</code>. Note that the first element
220 * of this array is ignored.
222 * @return This list of abbreviated weekday display strings.
224 public String[] getShortWeekdays ()
226 return shortWeekdays;
230 * This method returns the list of strings used for displaying weekday
231 * names (e.g., "Sunday" and "Monday"). This is an eight element
232 * <code>String</code> array indexed by <code>Calendar.SUNDAY</code>
233 * through <code>Calendar.SATURDAY</code>. Note that the first element
234 * of this array is ignored.
236 * @return This list of weekday display strings.
238 public String[] getWeekdays ()
240 return weekdays;
244 * This method returns this list of localized timezone display strings.
245 * This is a two dimensional <code>String</code> array where each row in
246 * the array contains five values:
247 * <P>
248 * <ul>
249 * <li>0 - The non-localized time zone id string.</li>
250 * <li>1 - The long name of the time zone (standard time).</li>
251 * <li>2 - The short name of the time zone (standard time).</li>
252 * <li>3 - The long name of the time zone (daylight savings time).</li>
253 * <li>4 - the short name of the time zone (daylight savings time).</li>
254 * </ul>
256 * @return The list of time zone display strings.
258 public String[] [] getZoneStrings ()
260 return zoneStrings;
264 * This method sets the list of strings used to display AM/PM values to
265 * the specified list.
266 * This is a two element <code>String</code> array indexed by
267 * <code>Calendar.AM</code> and <code>Calendar.PM</code>
269 * @param ampms The new list of AM/PM display strings.
271 public void setAmPmStrings (String[] value)
273 ampms = value;
277 * This method sets the list of strings used to display time eras to
278 * to the specified list.
279 * This is a two element <code>String</code>
280 * array indexed by <code>Calendar.BC</code> and <code>Calendar.AD</code>.
282 * @param eras The new list of era disply strings.
284 public void setEras (String[] value)
286 eras = value;
290 * This method sets the list of characters used to specific date/time
291 * formatting strings.
292 * This is an 18 character string that contains the characters
293 * that are used in creating the date formatting strings in
294 * <code>SimpleDateFormat</code>. The following are the character
295 * positions in the string and which format character they correspond
296 * to (the character in parentheses is the default value in the US English
297 * locale):
298 * <p>
299 * <ul>
300 * <li>0 - era (G)</li>
301 * <li>1 - year (y)</li>
302 * <li>2 - month (M)</li>
303 * <li>3 - day of month (d)</li>
304 * <li>4 - hour out of 12, from 1-12 (h)</li>
305 * <li>5 - hour out of 24, from 0-23 (H)</li>
306 * <li>6 - minute (m)</li>
307 * <li>7 - second (s)</li>
308 * <li>8 - millisecond (S)</li>
309 * <li>9 - date of week (E)</li>
310 * <li>10 - date of year (D)</li>
311 * <li>11 - day of week in month, eg. "4th Thur in Nov" (F)</li>
312 * <li>12 - week in year (w)</li>
313 * <li>13 - week in month (W)</li>
314 * <li>14 - am/pm (a)</li>
315 * <li>15 - hour out of 24, from 1-24 (k)</li>
316 * <li>16 - hour out of 12, from 0-11 (K)</li>
317 * <li>17 - time zone (z)</li>
318 * </ul>
320 * @param localPatternChars The new format patter characters
322 public void setLocalPatternChars (String value)
324 localPatternChars = value;
328 * This method sets the list of strings used to display month names.
329 * This is a thirteen element
330 * string array indexed by <code>Calendar.JANUARY</code> through
331 * <code>Calendar.UNDECEMBER</code>. Note that there are thirteen
332 * elements because some calendars have thriteen months.
334 * @param months The list of month display strings.
336 public void setMonths (String[] value)
338 months = value;
342 * This method sets the list of strings used to display abbreviated month
343 * names.
344 * This is a thirteen element
345 * <code>String</code> array indexed by <code>Calendar.JANUARY</code>
346 * through <code>Calendar.UNDECEMBER</code>. Note that there are thirteen
347 * elements because some calendars have thirteen months.
349 * @param shortMonths The new list of abbreviated month display strings.
351 public void setShortMonths (String[] value)
353 shortMonths = value;
357 * This method sets the list of strings used to display abbreviated
358 * weekday names.
359 * This is an eight element
360 * <code>String</code> array indexed by <code>Calendar.SUNDAY</code>
361 * through <code>Calendar.SATURDAY</code>. Note that the first element
362 * of this array is ignored.
364 * @param shortWeekdays This list of abbreviated weekday display strings.
366 public void setShortWeekdays (String[] value)
368 shortWeekdays = value;
372 * This method sets the list of strings used to display weekday names.
373 * This is an eight element
374 * <code>String</code> array indexed by <code>Calendar.SUNDAY</code>
375 * through <code>Calendar.SATURDAY</code>. Note that the first element
376 * of this array is ignored.
378 * @param weekdays This list of weekday display strings.
380 public void setWeekdays (String[] value)
382 weekdays = value;
386 * This method sets the list of display strings for time zones.
387 * This is a two dimensional <code>String</code> array where each row in
388 * the array contains five values:
389 * <P>
390 * <ul>
391 * <li>0 - The non-localized time zone id string.</li>
392 * <li>1 - The long name of the time zone (standard time).</li>
393 * <li>2 - The short name of the time zone (standard time).</li>
394 * <li>3 - The long name of the time zone (daylight savings time).</li>
395 * <li>4 - the short name of the time zone (daylight savings time).</li>
396 * </ul>
398 * @return The list of time zone display strings.
400 public void setZoneStrings (String[][] value)
402 zoneStrings = value;
405 /* Does a "deep" equality test - recurses into arrays. */
406 private static boolean equals (Object x, Object y)
408 if (x == y)
409 return true;
410 if (x == null || y == null)
411 return false;
412 if (! (x instanceof Object[]) || ! (y instanceof Object[]))
413 return x.equals(y);
414 Object[] xa = (Object[]) x;
415 Object[] ya = (Object[]) y;
416 if (xa.length != ya.length)
417 return false;
418 for (int i = xa.length; --i >= 0; )
420 if (! equals(xa[i], ya[i]))
421 return false;
423 return true;
426 private static int hashCode (Object x)
428 if (x == null)
429 return 0;
430 if (! (x instanceof Object[]))
431 return x.hashCode();
432 Object[] xa = (Object[]) x;
433 int hash = 0;
434 for (int i = 0; i < xa.length; i++)
435 hash = 37 * hashCode(xa[i]);
436 return hash;
440 * This method tests a specified object for equality against this object.
441 * This will be true if and only if the specified object:
442 * <p>
443 * <ul>
444 * <li> Is not <code>null</code>.</li>
445 * <li> Is an instance of <code>DateFormatSymbols</code>.</li>
446 * <li> Contains identical formatting symbols to this object.</li>
447 * </ul>
449 * @param obj The <code>Object</code> to test for equality against.
451 * @return <code>true</code> if the specified object is equal to this one,
452 * <code>false</code> otherwise.
454 public boolean equals (Object obj)
456 if (! (obj instanceof DateFormatSymbols))
457 return false;
458 DateFormatSymbols other = (DateFormatSymbols) obj;
459 return (equals(ampms, other.ampms)
460 && equals(eras, other.eras)
461 && equals(localPatternChars, other.localPatternChars)
462 && equals(months, other.months)
463 && equals(shortMonths, other.shortMonths)
464 && equals(shortWeekdays, other.shortWeekdays)
465 && equals(weekdays, other.weekdays)
466 && equals(zoneStrings, other.zoneStrings));
470 * Returns a new copy of this object.
472 * @param A copy of this object
474 public Object clone ()
478 return super.clone ();
480 catch (CloneNotSupportedException e)
482 return null;
487 * This method returns a hash value for this object.
489 * @return A hash value for this object.
491 public int hashCode ()
493 return (hashCode(ampms)
494 ^ hashCode(eras)
495 ^ hashCode(localPatternChars)
496 ^ hashCode(months)
497 ^ hashCode(shortMonths)
498 ^ hashCode(shortWeekdays)
499 ^ hashCode(weekdays)
500 ^ hashCode(zoneStrings));