Source for java.text.DateFormatSymbols

   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 String[] formatsForKey(ResourceBundle res, String key) 
  82:   {
  83:     String[] values = new String [formatPrefixes.length];
  84:     for (int i = 0; i < formatPrefixes.length; i++)
  85:       {
  86:         values[i] = res.getString(formatPrefixes[i]+key);
  87:       }
  88:     return values;
  89:   }
  90: 
  91:   /**
  92:    * This method initializes a new instance of <code>DateFormatSymbols</code>
  93:    * by loading the date format information for the specified locale.
  94:    *
  95:    * @param locale The locale for which date formatting symbols should
  96:    *               be loaded. 
  97:    */
  98:   public DateFormatSymbols (Locale locale) throws MissingResourceException
  99:   {
 100:     ResourceBundle res
 101:       = ResourceBundle.getBundle("gnu.java.locale.LocaleInformation", locale,
 102:                        ClassLoader.getSystemClassLoader());
 103: 
 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");
 112: 
 113:     dateFormats = formatsForKey(res, "DateFormat");
 114:     timeFormats = formatsForKey(res, "TimeFormat");
 115:   }
 116: 
 117:   /**
 118:    * This method loads the format symbol information for the default
 119:    * locale.
 120:    */
 121:   public DateFormatSymbols () throws MissingResourceException
 122:   {
 123:     this (Locale.getDefault());
 124:   }
 125: 
 126:   /**
 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>
 130:    *
 131:    * @return The list of AM/PM display strings.
 132:    */
 133:   public String[] getAmPmStrings()
 134:   {
 135:     return ampms;
 136:   }
 137: 
 138:   /**
 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>.
 142:     *
 143:     * @return The list of era disply strings.
 144:     */
 145:   public String[] getEras()
 146:   {
 147:     return eras;
 148:   }
 149: 
 150:   /**
 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>
 179:     *
 180:     * @return The format patter characters
 181:     */
 182:   public String getLocalPatternChars()
 183:   {
 184:     return localPatternChars;
 185:   }
 186: 
 187:   /**
 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.
 193:    *
 194:    * @return The list of month display strings.
 195:    */
 196:   public String[] getMonths ()
 197:   {
 198:     return months;
 199:   }
 200: 
 201:   /**
 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.
 207:    *
 208:    * @return The list of abbreviated month display strings.
 209:    */
 210:   public String[] getShortMonths ()
 211:   {
 212:     return shortMonths;
 213:   }
 214: 
 215:   /**
 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.
 221:    *
 222:    * @return This list of abbreviated weekday display strings.
 223:    */
 224:   public String[] getShortWeekdays ()
 225:   {
 226:     return shortWeekdays;
 227:   }
 228: 
 229:   /**
 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.
 235:    *
 236:    * @return This list of weekday display strings.
 237:    */
 238:   public String[] getWeekdays ()
 239:   {
 240:     return weekdays;
 241:   }
 242: 
 243:   /**
 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>
 255:    *
 256:    * @return The list of time zone display strings.
 257:    */
 258:   public String[] [] getZoneStrings ()
 259:   {
 260:     return zoneStrings;
 261:   }
 262: 
 263:   /**
 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>
 268:    *
 269:    * @param value The new list of AM/PM display strings.
 270:    */
 271:   public void setAmPmStrings (String[] value)
 272:   {
 273:     ampms = value;
 274:   }
 275: 
 276:   /**
 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>.
 281:    *
 282:    * @param labels The new list of era display strings.
 283:    */
 284:   public void setEras (String[] labels)
 285:   {
 286:     eras = labels;
 287:   }
 288: 
 289:   /**
 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>
 319:     *
 320:     * @param chars The new format pattern characters
 321:     */
 322:   public void setLocalPatternChars (String chars)
 323:   {
 324:     localPatternChars = chars;
 325:   }
 326: 
 327:   /**
 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.
 333:     *
 334:     * @param labels The list of month display strings.
 335:     */
 336:   public void setMonths (String[] labels)
 337:   {
 338:     months = labels;
 339:   }
 340: 
 341:   /**
 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.
 348:    *
 349:    * @param labels The new list of abbreviated month display strings.
 350:    */
 351:   public void setShortMonths (String[] labels)
 352:   {
 353:     shortMonths = labels;
 354:   }
 355: 
 356:   /**
 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.
 363:    *
 364:    * @param labels This list of abbreviated weekday display strings.
 365:    */
 366:   public void setShortWeekdays (String[] labels)
 367:   {
 368:     shortWeekdays = labels;
 369:   }
 370: 
 371:   /**
 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.
 377:    *
 378:    * @param labels This list of weekday display strings.
 379:    */
 380:   public void setWeekdays (String[] labels)
 381:   {
 382:     weekdays = labels;
 383:   }
 384: 
 385:   /**
 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>
 397:    *
 398:    * @params zones The list of time zone display strings.
 399:    */
 400:   public void setZoneStrings (String[][] zones)
 401:   {
 402:     zoneStrings = zones;
 403:   }
 404: 
 405:   /* Does a "deep" equality test - recurses into arrays. */
 406:   private static boolean equals (Object x, Object y)
 407:   {
 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; )
 419:       {
 420:     if (! equals(xa[i], ya[i]))
 421:       return false;
 422:       }
 423:     return true;
 424:   }
 425: 
 426:   private static int hashCode (Object x)
 427:   {
 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;
 437:   }
 438: 
 439:   /**
 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>
 448:    * 
 449:    * @param obj The <code>Object</code> to test for equality against.
 450:    *
 451:    * @return <code>true</code> if the specified object is equal to this one,
 452:    * <code>false</code> otherwise.
 453:    */
 454:   public boolean equals (Object obj)
 455:   {
 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));
 467:   }
 468: 
 469:   /**
 470:    * Returns a new copy of this object.
 471:    *
 472:    * @return A copy of this object
 473:    */
 474:   public Object clone ()
 475:   {
 476:     try
 477:       {
 478:         return super.clone ();
 479:       } 
 480:     catch (CloneNotSupportedException e) 
 481:       {
 482:         return null;
 483:       }
 484:   }
 485: 
 486:   /**
 487:    * This method returns a hash value for this object.
 488:    *
 489:    * @return A hash value for this object.
 490:    */
 491:   public int hashCode ()
 492:   {
 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));
 501:   }
 502: }