Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / java / text / DateFormatSymbols.java
blobbffd31fb6a7de518d8dd1e5abc2a407920b6357d
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., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 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 static String[] getStringArray(ResourceBundle res, String name)
83 return res.getString(name).split("\u00ae");
86 private String[][] getZoneStrings(ResourceBundle res)
88 try
90 int index = 0;
91 String data = res.getString("zoneStrings");
92 String[] zones = data.split("\u00a9");
93 String[][] array = new String[zones.length][];
94 for (int a = 0; a < zones.length; ++a)
95 array[a] = zones[a].split("\u00ae");
96 return array;
98 catch (MissingResourceException e)
100 return new String[0][];
104 private String[] formatsForKey(ResourceBundle res, String key)
106 String[] values = new String[formatPrefixes.length];
108 for (int i = 0; i < formatPrefixes.length; i++)
109 values[i] = res.getString(formatPrefixes[i] + key);
111 return values;
115 * This method initializes a new instance of <code>DateFormatSymbols</code>
116 * by loading the date format information for the specified locale.
118 * @param locale The locale for which date formatting symbols should
119 * be loaded.
121 public DateFormatSymbols (Locale locale) throws MissingResourceException
123 ResourceBundle res
124 = ResourceBundle.getBundle("gnu.java.locale.LocaleInformation", locale,
125 ClassLoader.getSystemClassLoader());
127 ampms = getStringArray(res, "ampms");
128 eras = getStringArray(res, "eras");
129 localPatternChars = res.getString("localPatternChars");
130 months = getStringArray(res, "months");
131 shortMonths = getStringArray(res, "shortMonths");
132 shortWeekdays = getStringArray(res, "shortWeekdays");
133 weekdays = getStringArray(res, "weekdays");
134 zoneStrings = getZoneStrings(res);
135 dateFormats = formatsForKey(res, "DateFormat");
136 timeFormats = formatsForKey(res, "TimeFormat");
140 * This method loads the format symbol information for the default
141 * locale.
143 public DateFormatSymbols () throws MissingResourceException
145 this (Locale.getDefault());
149 * This method returns the list of strings used for displaying AM or PM.
150 * This is a two element <code>String</code> array indexed by
151 * <code>Calendar.AM</code> and <code>Calendar.PM</code>
153 * @return The list of AM/PM display strings.
155 public String[] getAmPmStrings()
157 return ampms;
161 * This method returns the list of strings used for displaying eras
162 * (e.g., "BC" and "AD"). This is a two element <code>String</code>
163 * array indexed by <code>Calendar.BC</code> and <code>Calendar.AD</code>.
165 * @return The list of era disply strings.
167 public String[] getEras()
169 return eras;
173 * This method returns the pattern character information for this
174 * object. This is an 18 character string that contains the characters
175 * that are used in creating the date formatting strings in
176 * <code>SimpleDateFormat</code>. The following are the character
177 * positions in the string and which format character they correspond
178 * to (the character in parentheses is the default value in the US English
179 * locale):
180 * <p>
181 * <ul>
182 * <li>0 - era (G)</li>
183 * <li>1 - year (y)</li>
184 * <li>2 - month (M)</li>
185 * <li>3 - day of month (d)</li>
186 * <li>4 - hour out of 12, from 1-12 (h)</li>
187 * <li>5 - hour out of 24, from 0-23 (H)</li>
188 * <li>6 - minute (m)</li>
189 * <li>7 - second (s)</li>
190 * <li>8 - millisecond (S)</li>
191 * <li>9 - date of week (E)</li>
192 * <li>10 - date of year (D)</li>
193 * <li>11 - day of week in month, eg. "4th Thur in Nov" (F)</li>
194 * <li>12 - week in year (w)</li>
195 * <li>13 - week in month (W)</li>
196 * <li>14 - am/pm (a)</li>
197 * <li>15 - hour out of 24, from 1-24 (k)</li>
198 * <li>16 - hour out of 12, from 0-11 (K)</li>
199 * <li>17 - time zone (z)</li>
200 * </ul>
202 * @return The format patter characters
204 public String getLocalPatternChars()
206 return localPatternChars;
210 * This method returns the list of strings used for displaying month
211 * names (e.g., "January" and "February"). This is a thirteen element
212 * string array indexed by <code>Calendar.JANUARY</code> through
213 * <code>Calendar.UNDECEMBER</code>. Note that there are thirteen
214 * elements because some calendars have thriteen months.
216 * @return The list of month display strings.
218 public String[] getMonths ()
220 return months;
224 * This method returns the list of strings used for displaying abbreviated
225 * month names (e.g., "Jan" and "Feb"). This is a thirteen element
226 * <code>String</code> array indexed by <code>Calendar.JANUARY</code>
227 * through <code>Calendar.UNDECEMBER</code>. Note that there are thirteen
228 * elements because some calendars have thirteen months.
230 * @return The list of abbreviated month display strings.
232 public String[] getShortMonths ()
234 return shortMonths;
238 * This method returns the list of strings used for displaying abbreviated
239 * weekday names (e.g., "Sun" and "Mon"). This is an eight element
240 * <code>String</code> array indexed by <code>Calendar.SUNDAY</code>
241 * through <code>Calendar.SATURDAY</code>. Note that the first element
242 * of this array is ignored.
244 * @return This list of abbreviated weekday display strings.
246 public String[] getShortWeekdays ()
248 return shortWeekdays;
252 * This method returns the list of strings used for displaying weekday
253 * names (e.g., "Sunday" and "Monday"). This is an eight element
254 * <code>String</code> array indexed by <code>Calendar.SUNDAY</code>
255 * through <code>Calendar.SATURDAY</code>. Note that the first element
256 * of this array is ignored.
258 * @return This list of weekday display strings.
260 public String[] getWeekdays ()
262 return weekdays;
266 * This method returns this list of localized timezone display strings.
267 * This is a two dimensional <code>String</code> array where each row in
268 * the array contains five values:
269 * <P>
270 * <ul>
271 * <li>0 - The non-localized time zone id string.</li>
272 * <li>1 - The long name of the time zone (standard time).</li>
273 * <li>2 - The short name of the time zone (standard time).</li>
274 * <li>3 - The long name of the time zone (daylight savings time).</li>
275 * <li>4 - the short name of the time zone (daylight savings time).</li>
276 * </ul>
278 * @return The list of time zone display strings.
280 public String[] [] getZoneStrings ()
282 return zoneStrings;
286 * This method sets the list of strings used to display AM/PM values to
287 * the specified list.
288 * This is a two element <code>String</code> array indexed by
289 * <code>Calendar.AM</code> and <code>Calendar.PM</code>
291 * @param value The new list of AM/PM display strings.
293 public void setAmPmStrings (String[] value)
295 if(value==null)
296 throw new NullPointerException();
297 ampms = value;
301 * This method sets the list of strings used to display time eras to
302 * to the specified list.
303 * This is a two element <code>String</code>
304 * array indexed by <code>Calendar.BC</code> and <code>Calendar.AD</code>.
306 * @param labels The new list of era display strings.
308 public void setEras (String[] labels)
310 if(labels==null)
311 throw new NullPointerException();
312 eras = labels;
316 * This method sets the list of characters used to specific date/time
317 * formatting strings.
318 * This is an 18 character string that contains the characters
319 * that are used in creating the date formatting strings in
320 * <code>SimpleDateFormat</code>. The following are the character
321 * positions in the string and which format character they correspond
322 * to (the character in parentheses is the default value in the US English
323 * locale):
324 * <p>
325 * <ul>
326 * <li>0 - era (G)</li>
327 * <li>1 - year (y)</li>
328 * <li>2 - month (M)</li>
329 * <li>3 - day of month (d)</li>
330 * <li>4 - hour out of 12, from 1-12 (h)</li>
331 * <li>5 - hour out of 24, from 0-23 (H)</li>
332 * <li>6 - minute (m)</li>
333 * <li>7 - second (s)</li>
334 * <li>8 - millisecond (S)</li>
335 * <li>9 - date of week (E)</li>
336 * <li>10 - date of year (D)</li>
337 * <li>11 - day of week in month, eg. "4th Thur in Nov" (F)</li>
338 * <li>12 - week in year (w)</li>
339 * <li>13 - week in month (W)</li>
340 * <li>14 - am/pm (a)</li>
341 * <li>15 - hour out of 24, from 1-24 (k)</li>
342 * <li>16 - hour out of 12, from 0-11 (K)</li>
343 * <li>17 - time zone (z)</li>
344 * </ul>
346 * @param chars The new format pattern characters
348 public void setLocalPatternChars (String chars)
350 if(chars==null)
351 throw new NullPointerException();
352 localPatternChars = chars;
356 * This method sets the list of strings used to display month names.
357 * This is a thirteen element
358 * string array indexed by <code>Calendar.JANUARY</code> through
359 * <code>Calendar.UNDECEMBER</code>. Note that there are thirteen
360 * elements because some calendars have thriteen months.
362 * @param labels The list of month display strings.
364 public void setMonths (String[] labels)
366 if(labels==null)
367 throw new NullPointerException();
368 months = labels;
372 * This method sets the list of strings used to display abbreviated month
373 * names.
374 * This is a thirteen element
375 * <code>String</code> array indexed by <code>Calendar.JANUARY</code>
376 * through <code>Calendar.UNDECEMBER</code>. Note that there are thirteen
377 * elements because some calendars have thirteen months.
379 * @param labels The new list of abbreviated month display strings.
381 public void setShortMonths (String[] labels)
383 if(labels==null)
384 throw new NullPointerException();
385 shortMonths = labels;
389 * This method sets the list of strings used to display abbreviated
390 * weekday names.
391 * This is an eight element
392 * <code>String</code> array indexed by <code>Calendar.SUNDAY</code>
393 * through <code>Calendar.SATURDAY</code>. Note that the first element
394 * of this array is ignored.
396 * @param labels This list of abbreviated weekday display strings.
398 public void setShortWeekdays (String[] labels)
400 if(labels==null)
401 throw new NullPointerException();
402 shortWeekdays = labels;
406 * This method sets the list of strings used to display weekday names.
407 * This is an eight element
408 * <code>String</code> array indexed by <code>Calendar.SUNDAY</code>
409 * through <code>Calendar.SATURDAY</code>. Note that the first element
410 * of this array is ignored.
412 * @param labels This list of weekday display strings.
414 public void setWeekdays (String[] labels)
416 if(labels==null)
417 throw new NullPointerException();
418 weekdays = labels;
422 * This method sets the list of display strings for time zones.
423 * This is a two dimensional <code>String</code> array where each row in
424 * the array contains five values:
425 * <P>
426 * <ul>
427 * <li>0 - The non-localized time zone id string.</li>
428 * <li>1 - The long name of the time zone (standard time).</li>
429 * <li>2 - The short name of the time zone (standard time).</li>
430 * <li>3 - The long name of the time zone (daylight savings time).</li>
431 * <li>4 - the short name of the time zone (daylight savings time).</li>
432 * </ul>
434 * @params zones The list of time zone display strings.
436 public void setZoneStrings (String[][] zones)
438 if(zones==null)
439 throw new NullPointerException();
440 zoneStrings = zones;
443 /* Does a "deep" equality test - recurses into arrays. */
444 private static boolean equals (Object x, Object y)
446 if (x == y)
447 return true;
448 if (x == null || y == null)
449 return false;
450 if (! (x instanceof Object[]) || ! (y instanceof Object[]))
451 return x.equals(y);
452 Object[] xa = (Object[]) x;
453 Object[] ya = (Object[]) y;
454 if (xa.length != ya.length)
455 return false;
456 for (int i = xa.length; --i >= 0; )
458 if (! equals(xa[i], ya[i]))
459 return false;
461 return true;
464 private static int hashCode (Object x)
466 if (x == null)
467 return 0;
468 if (! (x instanceof Object[]))
469 return x.hashCode();
470 Object[] xa = (Object[]) x;
471 int hash = 0;
472 for (int i = 0; i < xa.length; i++)
473 hash = 37 * hashCode(xa[i]);
474 return hash;
478 * This method tests a specified object for equality against this object.
479 * This will be true if and only if the specified object:
480 * <p>
481 * <ul>
482 * <li> Is not <code>null</code>.</li>
483 * <li> Is an instance of <code>DateFormatSymbols</code>.</li>
484 * <li> Contains identical formatting symbols to this object.</li>
485 * </ul>
487 * @param obj The <code>Object</code> to test for equality against.
489 * @return <code>true</code> if the specified object is equal to this one,
490 * <code>false</code> otherwise.
492 public boolean equals (Object obj)
494 if (! (obj instanceof DateFormatSymbols))
495 return false;
496 DateFormatSymbols other = (DateFormatSymbols) obj;
497 return (equals(ampms, other.ampms)
498 && equals(eras, other.eras)
499 && equals(localPatternChars, other.localPatternChars)
500 && equals(months, other.months)
501 && equals(shortMonths, other.shortMonths)
502 && equals(shortWeekdays, other.shortWeekdays)
503 && equals(weekdays, other.weekdays)
504 && equals(zoneStrings, other.zoneStrings));
508 * Returns a new copy of this object.
510 * @return A copy of this object
512 public Object clone ()
516 return super.clone ();
518 catch (CloneNotSupportedException e)
520 return null;
525 * This method returns a hash value for this object.
527 * @return A hash value for this object.
529 public int hashCode ()
531 return (hashCode(ampms)
532 ^ hashCode(eras)
533 ^ hashCode(localPatternChars)
534 ^ hashCode(months)
535 ^ hashCode(shortMonths)
536 ^ hashCode(shortWeekdays)
537 ^ hashCode(weekdays)
538 ^ hashCode(zoneStrings));