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: import java.util.StringTokenizer;
  45: 
  46: /**
  47:  * This class acts as container for locale specific date/time formatting
  48:  * information such as the days of the week and the months of the year.
  49:  * @author Per Bothner (bothner@cygnus.com)
  50:  * @date October 24, 1998.
  51:  */
  52: /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3.
  53:  * Status:  Believed complete and correct.
  54:  */
  55: public class DateFormatSymbols implements java.io.Serializable, Cloneable
  56: {
  57:   String[] ampms;
  58:   String[] eras;
  59:   private String localPatternChars;
  60:   String[] months;
  61:   String[] shortMonths;
  62:   String[] shortWeekdays;
  63:   String[] weekdays;
  64:   private String[][] zoneStrings;
  65: 
  66:   private static final long serialVersionUID = -5987973545549424702L;
  67: 
  68:   // The order of these prefixes must be the same as in DateFormat
  69:   private static final String[] formatPrefixes =
  70:   {
  71:     "full", "long", "medium", "short"
  72:   };
  73: 
  74:   // These are each arrays with a value for SHORT, MEDIUM, LONG, FULL,
  75:   // and DEFAULT (constants defined in java.text.DateFormat).  While
  76:   // not part of the official spec, we need a way to get at locale-specific
  77:   // default formatting patterns.  They are declared package scope so
  78:   // as to be easily accessible where needed (DateFormat, SimpleDateFormat).
  79:   transient String[] dateFormats;
  80:   transient String[] timeFormats;
  81: 
  82:   private static String[] getStringArray(ResourceBundle res, String name)
  83:   { 
  84:     return res.getString(name).split("\u00ae");
  85:   }
  86: 
  87:   private String[][] getZoneStrings(ResourceBundle res)
  88:   {
  89:     try
  90:       {
  91:         int index = 0;
  92:         String data = res.getString("zoneStrings");
  93:     String[] zones = data.split("\u00a9");
  94:     String[][] array = new String[zones.length][];
  95:     for (int a = 0; a < zones.length; ++a)
  96:       array[a] = zones[a].split("\u00ae");
  97:     return array;
  98:       }
  99:     catch (MissingResourceException e)
 100:       {
 101:     return new String[0][];
 102:       }
 103:   }
 104:   
 105:   private String[] formatsForKey(ResourceBundle res, String key) 
 106:   {
 107:     String[] values = new String[formatPrefixes.length];
 108:     
 109:     for (int i = 0; i < formatPrefixes.length; i++)
 110:       values[i] = res.getString(formatPrefixes[i] + key);
 111:   
 112:     return values;
 113:   }
 114: 
 115:   /**
 116:    * This method initializes a new instance of <code>DateFormatSymbols</code>
 117:    * by loading the date format information for the specified locale.
 118:    *
 119:    * @param locale The locale for which date formatting symbols should
 120:    *               be loaded. 
 121:    */
 122:   public DateFormatSymbols (Locale locale) throws MissingResourceException
 123:   {
 124:     ResourceBundle res
 125:       = ResourceBundle.getBundle("gnu.java.locale.LocaleInformation", locale,
 126:                        ClassLoader.getSystemClassLoader());
 127: 
 128:     ampms = getStringArray(res, "ampms");
 129:     eras = getStringArray(res, "eras");
 130:     localPatternChars = res.getString("localPatternChars");
 131:     months = getStringArray(res, "months");
 132:     shortMonths = getStringArray(res, "shortMonths");
 133:     shortWeekdays = getStringArray(res, "shortWeekdays");
 134:     weekdays = getStringArray(res, "weekdays");
 135:     zoneStrings = getZoneStrings(res);
 136:     dateFormats = formatsForKey(res, "DateFormat");
 137:     timeFormats = formatsForKey(res, "TimeFormat");
 138:   }
 139: 
 140:   /**
 141:    * This method loads the format symbol information for the default
 142:    * locale.
 143:    */
 144:   public DateFormatSymbols () throws MissingResourceException
 145:   {
 146:     this (Locale.getDefault());
 147:   }
 148: 
 149:   /**
 150:    * This method returns the list of strings used for displaying AM or PM.
 151:    * This is a two element <code>String</code> array indexed by
 152:    * <code>Calendar.AM</code> and <code>Calendar.PM</code>
 153:    *
 154:    * @return The list of AM/PM display strings.
 155:    */
 156:   public String[] getAmPmStrings()
 157:   {
 158:     return ampms;
 159:   }
 160: 
 161:   /**
 162:     * This method returns the list of strings used for displaying eras
 163:     * (e.g., "BC" and "AD").  This is a two element <code>String</code>
 164:     * array indexed by <code>Calendar.BC</code> and <code>Calendar.AD</code>.
 165:     *
 166:     * @return The list of era disply strings.
 167:     */
 168:   public String[] getEras()
 169:   {
 170:     return eras;
 171:   }
 172: 
 173:   /**
 174:     * This method returns the pattern character information for this
 175:     * object.  This is an 18 character string that contains the characters
 176:     * that are used in creating the date formatting strings in 
 177:     * <code>SimpleDateFormat</code>.   The following are the character
 178:     * positions in the string and which format character they correspond
 179:     * to (the character in parentheses is the default value in the US English
 180:     * locale):
 181:     * <p>
 182:     * <ul>
 183:     * <li>0 - era (G)</li>
 184:     * <li>1 - year (y)</li>
 185:     * <li>2 - month (M)</li>
 186:     * <li>3 - day of month (d)</li>
 187:     * <li>4 - hour out of 12, from 1-12 (h)</li>
 188:     * <li>5 - hour out of 24, from 0-23 (H)</li>
 189:     * <li>6 - minute (m)</li>
 190:     * <li>7 - second (s)</li>
 191:     * <li>8 - millisecond (S)</li>
 192:     * <li>9 - date of week (E)</li>
 193:     * <li>10 - date of year (D)</li>
 194:     * <li>11 - day of week in month, eg. "4th Thur in Nov" (F)</li>
 195:     * <li>12 - week in year (w)</li>
 196:     * <li>13 - week in month (W)</li>
 197:     * <li>14 - am/pm (a)</li>
 198:     * <li>15 - hour out of 24, from 1-24 (k)</li>
 199:     * <li>16 - hour out of 12, from 0-11 (K)</li>
 200:     * <li>17 - time zone (z)</li>
 201:     * </ul>
 202:     *
 203:     * @return The format patter characters
 204:     */
 205:   public String getLocalPatternChars()
 206:   {
 207:     return localPatternChars;
 208:   }
 209: 
 210:   /**
 211:    * This method returns the list of strings used for displaying month
 212:    * names (e.g., "January" and "February").  This is a thirteen element
 213:    * string array indexed by <code>Calendar.JANUARY</code> through
 214:    * <code>Calendar.UNDECEMBER</code>.  Note that there are thirteen
 215:    * elements because some calendars have thriteen months.
 216:    *
 217:    * @return The list of month display strings.
 218:    */
 219:   public String[] getMonths ()
 220:   {
 221:     return months;
 222:   }
 223: 
 224:   /**
 225:    * This method returns the list of strings used for displaying abbreviated
 226:    * month names (e.g., "Jan" and "Feb").  This is a thirteen element
 227:    * <code>String</code> array indexed by <code>Calendar.JANUARY</code>
 228:    * through <code>Calendar.UNDECEMBER</code>.  Note that there are thirteen
 229:    * elements because some calendars have thirteen months.
 230:    *
 231:    * @return The list of abbreviated month display strings.
 232:    */
 233:   public String[] getShortMonths ()
 234:   {
 235:     return shortMonths;
 236:   }
 237: 
 238:   /**
 239:    * This method returns the list of strings used for displaying abbreviated 
 240:    * weekday names (e.g., "Sun" and "Mon").  This is an eight element
 241:    * <code>String</code> array indexed by <code>Calendar.SUNDAY</code>
 242:    * through <code>Calendar.SATURDAY</code>.  Note that the first element
 243:    * of this array is ignored.
 244:    *
 245:    * @return This list of abbreviated weekday display strings.
 246:    */
 247:   public String[] getShortWeekdays ()
 248:   {
 249:     return shortWeekdays;
 250:   }
 251: 
 252:   /**
 253:    * This method returns the list of strings used for displaying weekday
 254:    * names (e.g., "Sunday" and "Monday").  This is an eight element
 255:    * <code>String</code> array indexed by <code>Calendar.SUNDAY</code>
 256:    * through <code>Calendar.SATURDAY</code>.  Note that the first element
 257:    * of this array is ignored.
 258:    *
 259:    * @return This list of weekday display strings.
 260:    */
 261:   public String[] getWeekdays ()
 262:   {
 263:     return weekdays;
 264:   }
 265: 
 266:   /**
 267:    * This method returns this list of localized timezone display strings.
 268:    * This is a two dimensional <code>String</code> array where each row in
 269:    * the array contains five values:
 270:    * <P>
 271:    * <ul>
 272:    * <li>0 - The non-localized time zone id string.</li>
 273:    * <li>1 - The long name of the time zone (standard time).</li>
 274:    * <li>2 - The short name of the time zone (standard time).</li>
 275:    * <li>3 - The long name of the time zone (daylight savings time).</li>
 276:    * <li>4 - the short name of the time zone (daylight savings time).</li>
 277:    * </ul>
 278:    *
 279:    * @return The list of time zone display strings.
 280:    */
 281:   public String[] [] getZoneStrings ()
 282:   {
 283:     return zoneStrings;
 284:   }
 285: 
 286:   /**
 287:    * This method sets the list of strings used to display AM/PM values to
 288:    * the specified list.
 289:    * This is a two element <code>String</code> array indexed by
 290:    * <code>Calendar.AM</code> and <code>Calendar.PM</code>
 291:    *
 292:    * @param ampms The new list of AM/PM display strings.
 293:    */
 294:   public void setAmPmStrings (String[] value)
 295:   {
 296:     ampms = value;
 297:   }
 298: 
 299:   /**
 300:    * This method sets the list of strings used to display time eras to
 301:    * to the specified list.
 302:    * This is a two element <code>String</code>
 303:    * array indexed by <code>Calendar.BC</code> and <code>Calendar.AD</code>.
 304:    *
 305:    * @param eras The new list of era disply strings.
 306:    */
 307:   public void setEras (String[] value)
 308:   {
 309:     eras = value;
 310:   }
 311: 
 312:   /**
 313:     * This method sets the list of characters used to specific date/time
 314:     * formatting strings.
 315:     * This is an 18 character string that contains the characters
 316:     * that are used in creating the date formatting strings in 
 317:     * <code>SimpleDateFormat</code>.   The following are the character
 318:     * positions in the string and which format character they correspond
 319:     * to (the character in parentheses is the default value in the US English
 320:     * locale):
 321:     * <p>
 322:     * <ul>
 323:     * <li>0 - era (G)</li>
 324:     * <li>1 - year (y)</li>
 325:     * <li>2 - month (M)</li>
 326:     * <li>3 - day of month (d)</li>
 327:     * <li>4 - hour out of 12, from 1-12 (h)</li>
 328:     * <li>5 - hour out of 24, from 0-23 (H)</li>
 329:     * <li>6 - minute (m)</li>
 330:     * <li>7 - second (s)</li>
 331:     * <li>8 - millisecond (S)</li>
 332:     * <li>9 - date of week (E)</li>
 333:     * <li>10 - date of year (D)</li>
 334:     * <li>11 - day of week in month, eg. "4th Thur in Nov" (F)</li>
 335:     * <li>12 - week in year (w)</li>
 336:     * <li>13 - week in month (W)</li>
 337:     * <li>14 - am/pm (a)</li>
 338:     * <li>15 - hour out of 24, from 1-24 (k)</li>
 339:     * <li>16 - hour out of 12, from 0-11 (K)</li>
 340:     * <li>17 - time zone (z)</li>
 341:     * </ul>
 342:     *
 343:     * @param localPatternChars The new format patter characters
 344:     */
 345:   public void setLocalPatternChars (String value)
 346:   {
 347:     localPatternChars = value;
 348:   }
 349: 
 350:   /**
 351:     * This method sets the list of strings used to display month names.
 352:     * This is a thirteen element
 353:     * string array indexed by <code>Calendar.JANUARY</code> through
 354:     * <code>Calendar.UNDECEMBER</code>.  Note that there are thirteen
 355:     * elements because some calendars have thriteen months.
 356:     *
 357:     * @param months The list of month display strings.
 358:     */
 359:   public void setMonths (String[] value)
 360:   {
 361:     months = value;
 362:   }
 363: 
 364:   /**
 365:    * This method sets the list of strings used to display abbreviated month
 366:    * names.
 367:    * This is a thirteen element
 368:    * <code>String</code> array indexed by <code>Calendar.JANUARY</code>
 369:    * through <code>Calendar.UNDECEMBER</code>.  Note that there are thirteen
 370:    * elements because some calendars have thirteen months.
 371:    *
 372:    * @param shortMonths The new list of abbreviated month display strings.
 373:    */
 374:   public void setShortMonths (String[] value)
 375:   {
 376:     shortMonths = value;
 377:   }
 378: 
 379:   /**
 380:    * This method sets the list of strings used to display abbreviated
 381:    * weekday names.
 382:    * This is an eight element
 383:    * <code>String</code> array indexed by <code>Calendar.SUNDAY</code>
 384:    * through <code>Calendar.SATURDAY</code>.  Note that the first element
 385:    * of this array is ignored.
 386:    *
 387:    * @param shortWeekdays This list of abbreviated weekday display strings.
 388:    */
 389:   public void setShortWeekdays (String[] value)
 390:   {
 391:     shortWeekdays = value;
 392:   }
 393: 
 394:   /**
 395:    * This method sets the list of strings used to display weekday names.
 396:    * This is an eight element
 397:    * <code>String</code> array indexed by <code>Calendar.SUNDAY</code>
 398:    * through <code>Calendar.SATURDAY</code>.  Note that the first element
 399:    * of this array is ignored.
 400:    *
 401:    * @param weekdays This list of weekday display strings.
 402:    */
 403:   public void setWeekdays (String[] value)
 404:   {
 405:     weekdays = value;
 406:   }
 407: 
 408:   /**
 409:    * This method sets the list of display strings for time zones.
 410:    * This is a two dimensional <code>String</code> array where each row in
 411:    * the array contains five values:
 412:    * <P>
 413:    * <ul>
 414:    * <li>0 - The non-localized time zone id string.</li>
 415:    * <li>1 - The long name of the time zone (standard time).</li>
 416:    * <li>2 - The short name of the time zone (standard time).</li>
 417:    * <li>3 - The long name of the time zone (daylight savings time).</li>
 418:    * <li>4 - the short name of the time zone (daylight savings time).</li>
 419:    * </ul>
 420:    *
 421:    * @return The list of time zone display strings.
 422:    */
 423:   public void setZoneStrings (String[][] value)
 424:   {
 425:     zoneStrings = value;
 426:   }
 427: 
 428:   /* Does a "deep" equality test - recurses into arrays. */
 429:   private static boolean equals (Object x, Object y)
 430:   {
 431:     if (x == y)
 432:       return true;
 433:     if (x == null || y == null)
 434:       return false;
 435:     if (! (x instanceof Object[]) || ! (y instanceof Object[]))
 436:       return x.equals(y);
 437:     Object[] xa = (Object[]) x;
 438:     Object[] ya = (Object[]) y;
 439:     if (xa.length != ya.length)
 440:       return false;
 441:     for (int i = xa.length;  --i >= 0; )
 442:       {
 443:     if (! equals(xa[i], ya[i]))
 444:       return false;
 445:       }
 446:     return true;
 447:   }
 448: 
 449:   private static int hashCode (Object x)
 450:   {
 451:     if (x == null)
 452:       return 0;
 453:     if (! (x instanceof Object[]))
 454:       return x.hashCode();
 455:     Object[] xa = (Object[]) x;
 456:     int hash = 0;
 457:     for (int i = 0;  i < xa.length;  i++)
 458:       hash = 37 * hashCode(xa[i]);
 459:     return hash;
 460:   }
 461: 
 462:   /**
 463:    * This method tests a specified object for equality against this object.
 464:    * This will be true if and only if the specified object:
 465:    * <p>
 466:    * <ul>
 467:    * <li> Is not <code>null</code>.</li>
 468:    * <li> Is an instance of <code>DateFormatSymbols</code>.</li>
 469:    * <li> Contains identical formatting symbols to this object.</li>
 470:    * </ul>
 471:    * 
 472:    * @param obj The <code>Object</code> to test for equality against.
 473:    *
 474:    * @return <code>true</code> if the specified object is equal to this one,
 475:    * <code>false</code> otherwise.
 476:    */
 477:   public boolean equals (Object obj)
 478:   {
 479:     if (! (obj instanceof DateFormatSymbols))
 480:       return false;
 481:     DateFormatSymbols other = (DateFormatSymbols) obj;
 482:     return (equals(ampms, other.ampms)
 483:         && equals(eras, other.eras)
 484:         && equals(localPatternChars, other.localPatternChars)
 485:         && equals(months, other.months)
 486:         && equals(shortMonths, other.shortMonths)
 487:         && equals(shortWeekdays, other.shortWeekdays)
 488:         && equals(weekdays, other.weekdays)
 489:         && equals(zoneStrings, other.zoneStrings));
 490:   }
 491: 
 492:   /**
 493:    * Returns a new copy of this object.
 494:    *
 495:    * @param A copy of this object
 496:    */
 497:   public Object clone ()
 498:   {
 499:     try
 500:       {
 501:         return super.clone ();
 502:       } 
 503:     catch (CloneNotSupportedException e) 
 504:       {
 505:         return null;
 506:       }
 507:   }
 508: 
 509:   /**
 510:    * This method returns a hash value for this object.
 511:    *
 512:    * @return A hash value for this object.
 513:    */
 514:   public int hashCode ()
 515:   {
 516:     return (hashCode(ampms)
 517:         ^ hashCode(eras)
 518:         ^ hashCode(localPatternChars)
 519:         ^ hashCode(months)
 520:         ^ hashCode(shortMonths)
 521:         ^ hashCode(shortWeekdays)
 522:         ^ hashCode(weekdays)
 523:         ^ hashCode(zoneStrings));
 524:   }
 525: }