OSDN Git Service

Imported GNU Classpath 0.90
[pf3gnuchains/gcc-fork.git] / libjava / classpath / java / text / DateFormatSymbols.java
1 /* DateFormatSymbols.java -- Format over a range of numbers
2    Copyright (C) 1998, 1999, 2000, 2001, 2003, 2005  Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
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.
10  
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.
15
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.
20
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.
25
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. */
37
38
39 package java.text;
40
41 import java.util.Locale;
42 import java.util.MissingResourceException;
43 import java.util.ResourceBundle;
44
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.
50  */
51 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3.
52  * Status:  Believed complete and correct.
53  */
54 public class DateFormatSymbols implements java.io.Serializable, Cloneable
55 {
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;
64
65   private static final long serialVersionUID = -5987973545549424702L;
66
67   // The order of these prefixes must be the same as in DateFormat
68   private static final String[] formatPrefixes =
69   {
70     "full", "long", "medium", "short"
71   };
72
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;
80
81   private static String[] getStringArray(ResourceBundle res, String name)
82   { 
83     return res.getString(name).split("\u00ae");
84   }
85
86   private String[][] getZoneStrings(ResourceBundle res)
87   {
88     try
89       {
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;
97       }
98     catch (MissingResourceException e)
99       {
100         return new String[0][];
101       }
102   }
103   
104   private String[] formatsForKey(ResourceBundle res, String key) 
105   {
106     String[] values = new String[formatPrefixes.length];
107     
108     for (int i = 0; i < formatPrefixes.length; i++)
109       values[i] = res.getString(formatPrefixes[i] + key);
110   
111     return values;
112   }
113
114   /**
115    * This method initializes a new instance of <code>DateFormatSymbols</code>
116    * by loading the date format information for the specified locale.
117    *
118    * @param locale The locale for which date formatting symbols should
119    *               be loaded. 
120    */
121   public DateFormatSymbols (Locale locale) throws MissingResourceException
122   {
123     ResourceBundle res
124       = ResourceBundle.getBundle("gnu.java.locale.LocaleInformation", locale,
125                                  ClassLoader.getSystemClassLoader());
126
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");
137   }
138
139   /**
140    * This method loads the format symbol information for the default
141    * locale.
142    */
143   public DateFormatSymbols () throws MissingResourceException
144   {
145     this (Locale.getDefault());
146   }
147
148   /**
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>
152    *
153    * @return The list of AM/PM display strings.
154    */
155   public String[] getAmPmStrings()
156   {
157     return ampms;
158   }
159
160   /**
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>.
164     *
165     * @return The list of era disply strings.
166     */
167   public String[] getEras()
168   {
169     return eras;
170   }
171
172   /**
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>
201     *
202     * @return The format patter characters
203     */
204   public String getLocalPatternChars()
205   {
206     return localPatternChars;
207   }
208
209   /**
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.
215    *
216    * @return The list of month display strings.
217    */
218   public String[] getMonths ()
219   {
220     return months;
221   }
222
223   /**
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.
229    *
230    * @return The list of abbreviated month display strings.
231    */
232   public String[] getShortMonths ()
233   {
234     return shortMonths;
235   }
236
237   /**
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.
243    *
244    * @return This list of abbreviated weekday display strings.
245    */
246   public String[] getShortWeekdays ()
247   {
248     return shortWeekdays;
249   }
250
251   /**
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.
257    *
258    * @return This list of weekday display strings.
259    */
260   public String[] getWeekdays ()
261   {
262     return weekdays;
263   }
264
265   /**
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>
277    *
278    * @return The list of time zone display strings.
279    */
280   public String[] [] getZoneStrings ()
281   {
282     return zoneStrings;
283   }
284
285   /**
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>
290    *
291    * @param value The new list of AM/PM display strings.
292    */
293   public void setAmPmStrings (String[] value)
294   {
295     if(value==null)
296       throw new NullPointerException();
297     ampms = value;
298   }
299
300   /**
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>.
305    *
306    * @param labels The new list of era display strings.
307    */
308   public void setEras (String[] labels)
309   {
310     if(labels==null)
311       throw new NullPointerException();
312     eras = labels;
313   }
314
315   /**
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>
345     *
346     * @param chars The new format pattern characters
347     */
348   public void setLocalPatternChars (String chars)
349   {
350     if(chars==null)
351       throw new NullPointerException();
352     localPatternChars = chars;
353   }
354
355   /**
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.
361     *
362     * @param labels The list of month display strings.
363     */
364   public void setMonths (String[] labels)
365   {
366     if(labels==null)
367       throw new NullPointerException();
368     months = labels;
369   }
370
371   /**
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.
378    *
379    * @param labels The new list of abbreviated month display strings.
380    */
381   public void setShortMonths (String[] labels)
382   {
383     if(labels==null)
384       throw new NullPointerException();
385     shortMonths = labels;
386   }
387
388   /**
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.
395    *
396    * @param labels This list of abbreviated weekday display strings.
397    */
398   public void setShortWeekdays (String[] labels)
399   {
400     if(labels==null)
401       throw new NullPointerException();
402     shortWeekdays = labels;
403   }
404
405   /**
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.
411    *
412    * @param labels This list of weekday display strings.
413    */
414   public void setWeekdays (String[] labels)
415   {
416     if(labels==null)
417       throw new NullPointerException();
418     weekdays = labels;
419   }
420
421   /**
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>
433    *
434    * @params zones The list of time zone display strings.
435    */
436   public void setZoneStrings (String[][] zones)
437   {
438     if(zones==null)
439       throw new NullPointerException();
440     zoneStrings = zones;
441   }
442
443   /* Does a "deep" equality test - recurses into arrays. */
444   private static boolean equals (Object x, Object y)
445   {
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; )
457       {
458         if (! equals(xa[i], ya[i]))
459           return false;
460       }
461     return true;
462   }
463
464   private static int hashCode (Object x)
465   {
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;
475   }
476
477   /**
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>
486    * 
487    * @param obj The <code>Object</code> to test for equality against.
488    *
489    * @return <code>true</code> if the specified object is equal to this one,
490    * <code>false</code> otherwise.
491    */
492   public boolean equals (Object obj)
493   {
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));
505   }
506
507   /**
508    * Returns a new copy of this object.
509    *
510    * @return A copy of this object
511    */
512   public Object clone ()
513   {
514     try
515       {
516         return super.clone ();
517       } 
518     catch (CloneNotSupportedException e) 
519       {
520         return null;
521       }
522   }
523
524   /**
525    * This method returns a hash value for this object.
526    *
527    * @return A hash value for this object.
528    */
529   public int hashCode ()
530   {
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));
539   }
540 }