home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / DateFormatSymbols.java < prev    next >
Text File  |  1997-05-20  |  15KB  |  442 lines

  1. /*
  2.  * @(#)DateFormatSymbols.java    1.16 97/02/13
  3.  *
  4.  * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
  5.  * (C) Copyright IBM Corp. 1996 - All Rights Reserved
  6.  *
  7.  * Portions copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.
  8.  *
  9.  *   The original version of this source code and documentation is copyrighted
  10.  * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
  11.  * materials are provided under terms of a License Agreement between Taligent
  12.  * and Sun. This technology is protected by multiple US and International
  13.  * patents. This notice and attribution to Taligent may not be removed.
  14.  *   Taligent is a registered trademark of Taligent, Inc.
  15.  *
  16.  * Permission to use, copy, modify, and distribute this software
  17.  * and its documentation for NON-COMMERCIAL purposes and without
  18.  * fee is hereby granted provided that this copyright notice
  19.  * appears in all copies. Please refer to the file "copyright.html"
  20.  * for further important copyright and licensing information.
  21.  *
  22.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  23.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  24.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  25.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  26.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  27.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  28.  *
  29.  */
  30.  
  31. package java.text;
  32. import java.util.Locale;
  33. import java.util.ResourceBundle;
  34. import java.io.Serializable;
  35.  
  36. /**
  37.  * <code>DateFormatSymbols</code> is a public class for encapsulating
  38.  * localizable date-time formatting data, such as the names of the
  39.  * months, the names of the days of the week, and the time zone data.
  40.  * <code>DateFormat</code> and <code>SimpleDateFormat</code> both use
  41.  * <code>DateFormatSymbols</code> to encapsulate this information.
  42.  *
  43.  * <p>
  44.  * Typically you shouldn't use <code>DateFormatSymbols</code> directly.
  45.  * Rather, you are encouraged to create a date-time formatter with the
  46.  * <code>DateFormat</code> class's factory methods: <code>getTimeInstance</code>,
  47.  * <code>getDateInstance</code>, or <code>getDateTimeInstance</code>. 
  48.  * These methods automatically create a <code>DateFormatSymbols</code> for
  49.  * the formatter so that you don't have to. After the
  50.  * formatter is created, you may modify its format pattern using the
  51.  * <code>setPattern</code> method. For more information about
  52.  * creating formatters using <code>DateFormat</code>'s factory methods,
  53.  * see <a href="java.text.DateFormat.html"><code>DateFormat</code></a>.
  54.  *
  55.  * <p>
  56.  * If you decide to create a date-time formatter with a specific
  57.  * format pattern for a specific locale, you can do so with:
  58.  * <blockquote>
  59.  * <pre>
  60.  * new SimpleDateFormat(aPattern, new DateFormatSymbols(aLocale)).
  61.  * </pre>
  62.  * </blockquote>
  63.  *
  64.  * <p>
  65.  * <code>DateFormatSymbols</code> objects are clonable. When you obtain
  66.  * a <code>DateFormatSymbols</code> object, feel free to modify the
  67.  * date-time formatting data. For instance, you can replace the localized
  68.  * date-time format pattern characters with the ones that you feel easy
  69.  * to remember. Or you can change the representative cities
  70.  * to your favorite ones.
  71.  *
  72.  * <p>
  73.  * New <code>DateFormatSymbols</code> subclasses may be added to support
  74.  * <code>SimpleDateFormat</code> for date-time formatting for additional locales.
  75.  
  76.  * @see          DateFormat
  77.  * @see          SimpleDateFormat
  78.  * @see          java.util.SimpleTimeZone
  79.  * @version      1.16 02/13/97
  80.  * @author       Chen-Lieh Huang
  81.  */
  82. public class DateFormatSymbols implements Serializable, Cloneable {
  83.  
  84.     /**
  85.      * Construct a DateFormatSymbols object by loading format data from
  86.      * resources for the default locale.
  87.      *
  88.      * @exception  java.util.MissingResourceException
  89.      *             if the resources for the default locale cannot be
  90.      *             found or cannot be loaded.
  91.      */
  92.     public DateFormatSymbols()
  93.     {
  94.         initializeData(Locale.getDefault());
  95.     }
  96.  
  97.     /**
  98.      * Construct a DateFormatSymbols object by loading format data from
  99.      * resources for the given locale.
  100.      *
  101.      * @exception  java.util.MissingResourceException
  102.      *             if the resources for the specified locale cannot be
  103.      *             found or cannot be loaded.
  104.      */
  105.     public DateFormatSymbols(Locale locale)
  106.     {
  107.         initializeData(locale);
  108.     }
  109.  
  110.     /**
  111.      * Era strings. For example: "AD" and "BC".
  112.      */
  113.     String eras[] = null;
  114.     /**
  115.      * Month strings. For example: "January", "February", etc.
  116.      */
  117.     String months[] = null;
  118.     /**
  119.      * Short month strings. For example: "Jan", "Feb", etc.
  120.      */
  121.     String shortMonths[] = null;
  122.     /**
  123.      * Weekday strings. For example: "Sunday", "Monday", etc.
  124.      */
  125.     String weekdays[] = null;
  126.     /**
  127.      * Short weekday strings. For example: "Sun", "Mon", etc.
  128.      */
  129.     String shortWeekdays[] = null;
  130.     /**
  131.      * Ampm strings. For example: "AM" and "PM".
  132.      */
  133.     String ampms[] = null;
  134.     /**
  135.      * The format data of all the timezones in this locale.
  136.      */
  137.     String zoneStrings[][] = null;
  138.     /**
  139.      * Unlocalized date-time pattern characters. For example: 'y', 'd', etc.
  140.      * All locales use the same these unlocalized pattern characters.
  141.      */
  142.     static final String  patternChars = "GyMdkHmsSEDFwWahKz";
  143.     /**
  144.      * Localized date-time pattern characters. For example: use 'u' as 'y'.
  145.      */
  146.     String  localPatternChars = null;
  147.  
  148.     /**
  149.      * Gets era strings. For example: "AD" and "BC".
  150.      * @return the era strings.
  151.      */
  152.     public String[] getEras() {
  153.         return duplicate(eras);
  154.     }
  155.  
  156.     /**
  157.      * Sets era strings. For example: "AD" and "BC".
  158.      * @param newEras the new era strings.
  159.      */
  160.     public void setEras(String[] newEras) {
  161.         eras = duplicate(newEras);
  162.     }
  163.  
  164.     /**
  165.      * Gets month strings. For example: "January", "February", etc.
  166.      * @return the month strings.
  167.      */
  168.     public String[] getMonths() {
  169.         return duplicate(months);
  170.     }
  171.  
  172.     /**
  173.      * Sets month strings. For example: "January", "February", etc.
  174.      * @param newMonths the new month strings.
  175.      */
  176.     public void setMonths(String[] newMonths) {
  177.         months = duplicate(newMonths);
  178.     }
  179.  
  180.     /**
  181.      * Gets short month strings. For example: "Jan", "Feb", etc.
  182.      * @return the short month strings.
  183.      */
  184.     public String[] getShortMonths() {
  185.         return duplicate(shortMonths);
  186.     }
  187.  
  188.     /**
  189.      * Sets short month strings. For example: "Jan", "Feb", etc.
  190.      * @param newShortMonths the new short month strings.
  191.      */
  192.     public void setShortMonths(String[] newShortMonths) {
  193.         shortMonths = duplicate(newShortMonths);
  194.     }
  195.  
  196.     /**
  197.      * Gets weekday strings. For example: "Sunday", "Monday", etc.
  198.      * @return the weekday strings.
  199.      */
  200.     public String[] getWeekdays() {
  201.         return duplicate(weekdays);
  202.     }
  203.  
  204.     /**
  205.      * Sets weekday strings. For example: "Sunday", "Monday", etc.
  206.      * @param newWeekdays the new weekday strings.
  207.      */
  208.     public void setWeekdays(String[] newWeekdays) {
  209.         weekdays = duplicate(newWeekdays);
  210.     }
  211.  
  212.     /**
  213.      * Gets short weekday strings. For example: "Sun", "Mon", etc.
  214.      * @return the short weekday strings.
  215.      */
  216.     public String[] getShortWeekdays() {
  217.         return duplicate(shortWeekdays);
  218.     }
  219.  
  220.     /**
  221.      * Sets short weekday strings. For example: "Sun", "Mon", etc.
  222.      * @param newShortWeekdays the new short weekday strings.
  223.      */
  224.     public void setShortWeekdays(String[] newShortWeekdays) {
  225.         shortWeekdays = duplicate(newShortWeekdays);
  226.     }
  227.  
  228.     /**
  229.      * Gets ampm strings. For example: "AM" and "PM".
  230.      * @return the weekday strings.
  231.      */
  232.     public String[] getAmPmStrings() {
  233.         return duplicate(ampms);
  234.     }
  235.  
  236.     /**
  237.      * Sets ampm strings. For example: "AM" and "PM".
  238.      * @param newAmpms the new ampm strings.
  239.      */
  240.     public void setAmPmStrings(String[] newAmpms) {
  241.         ampms = duplicate(newAmpms);
  242.     }
  243.  
  244.     /**
  245.      * Gets timezone strings.
  246.      * @return the timezone strings.
  247.      */
  248.     public String[][] getZoneStrings() {
  249.         String[][] aCopy = new String[zoneStrings.length][];
  250.         for (int i = 0; i < zoneStrings.length; ++i)
  251.             aCopy[i] = duplicate(zoneStrings[i]);
  252.         return aCopy;
  253.     }
  254.  
  255.     /**
  256.      * Sets timezone strings.
  257.      * @param newZoneStrings the new timezone strings.
  258.      */
  259.     public void setZoneStrings(String[][] newZoneStrings) {
  260.         String[][] aCopy = new String[newZoneStrings.length][];
  261.         for (int i = 0; i < newZoneStrings.length; ++i)
  262.             aCopy[i] = duplicate(newZoneStrings[i]);
  263.         zoneStrings = aCopy;
  264.     }
  265.  
  266.     /**
  267.      * Gets localized date-time pattern characters. For example: 'u', 't', etc.
  268.      * @return the localized date-time pattern characters.
  269.      */
  270.     public String getLocalPatternChars() {
  271.         return new String(localPatternChars);
  272.     }
  273.  
  274.     /**
  275.      * Sets localized date-time pattern characters. For example: 'u', 't', etc.
  276.      * @param newLocalPatternChars the new localized date-time
  277.      * pattern characters.
  278.      */
  279.     public void setLocalPatternChars(String newLocalPatternChars) {
  280.         localPatternChars = new String(newLocalPatternChars);
  281.     }
  282.  
  283.     /**
  284.      * Overrides Cloneable
  285.      */
  286.     public Object clone()
  287.     {
  288.         try
  289.         {
  290.             DateFormatSymbols other = (DateFormatSymbols)super.clone();
  291.             copyMembers(this, other);
  292.             return other;
  293.         } catch (CloneNotSupportedException e) {
  294.             throw new InternalError();
  295.         }
  296.     }
  297.  
  298.     /**
  299.      * Override hashCode.
  300.      * Generates a hash code for the DateFormatSymbols object.
  301.      */
  302.     public int hashCode() {
  303.         int hashcode = 0;
  304.         for (int index = 0; index < this.zoneStrings[0].length; ++index)
  305.             hashcode ^= this.zoneStrings[0][index].hashCode();
  306.         return hashcode;
  307.     }
  308.  
  309.     /**
  310.      * Override equals
  311.      */
  312.     public boolean equals(Object obj)
  313.     {
  314.         if (this == obj) return true;
  315.         if (getClass() != obj.getClass()) return false;
  316.         DateFormatSymbols that = (DateFormatSymbols) obj;
  317.         return (Utility.arrayEquals(eras, that.eras)
  318.                 && Utility.arrayEquals(months, that.months)
  319.                 && Utility.arrayEquals(shortMonths, that.shortMonths)
  320.                 && Utility.arrayEquals(weekdays, that.weekdays)
  321.                 && Utility.arrayEquals(shortWeekdays, that.shortWeekdays)
  322.                 && Utility.arrayEquals(ampms, that.ampms)
  323.                 && Utility.arrayEquals(zoneStrings, that.zoneStrings)
  324.                 && Utility.arrayEquals(localPatternChars,
  325.                                        that.localPatternChars));
  326.     }
  327.  
  328.     // =======================privates===============================
  329.  
  330.     /**
  331.      * Useful constant for defining timezone offsets.
  332.      */
  333.     static final int millisPerHour = 60*60*1000;
  334.  
  335.     private void initializeData(Locale desiredLocale)
  336.     {
  337.         int i;
  338.         ResourceBundle resource
  339.             = ResourceBundle.getBundle
  340.         ("java.text.resources.LocaleElements", desiredLocale);
  341.         ResourceBundle zoneResource
  342.             = ResourceBundle.getBundle
  343.         ("java.text.resources.DateFormatZoneData", desiredLocale);
  344.         eras = (String[])resource.getObject("Eras");
  345.         months = resource.getStringArray("MonthNames");
  346.         shortMonths = resource.getStringArray("MonthAbbreviations");
  347.         String[] lWeekdays = resource.getStringArray("DayNames");
  348.         weekdays = new String[8];
  349.         weekdays[0] = "";  // 1-based
  350.         for (i=0; i<lWeekdays.length; i++)
  351.             weekdays[i+1] = lWeekdays[i];
  352.         String[] sWeekdays = resource.getStringArray("DayAbbreviations");
  353.         shortWeekdays = new String[8];
  354.         shortWeekdays[0] = "";  // 1-based
  355.         for (i=0; i<sWeekdays.length; i++)
  356.             shortWeekdays[i+1] = sWeekdays[i];
  357.         ampms = resource.getStringArray("AmPmMarkers");
  358.         zoneStrings = (String[][])zoneResource.getObject("zoneStrings");
  359.         localPatternChars
  360.             = (String) zoneResource.getObject("localPatternChars");
  361.     }
  362.  
  363.     /**
  364.      * Package private: used by SimpleDateFormat
  365.      * Gets the index for the given time zone ID to obtain the timezone
  366.      * strings for formatting. The time zone ID is just for programmatic
  367.      * lookup. NOT LOCALIZED!!!
  368.      * @param ID the given time zone ID.
  369.      * @return the index of the given time zone ID.  Returns -1 if
  370.      * the given time zone ID can't be located in the DateFormatSymbols object.
  371.      * @see java.util.SimpleTimeZone
  372.      */
  373.     final int getZoneIndex (String ID)
  374.     {
  375.         int index;
  376.  
  377.         for (index=0; index<zoneStrings.length; index++)
  378.             if (ID.regionMatches(true, 0, zoneStrings[index][2], 0,
  379.                                  zoneStrings[index][2].length()))
  380.                 break;
  381.         if (index < zoneStrings.length)
  382.             return index;
  383.  
  384.         for (index=0; index<zoneStrings.length; index++)
  385.             if (ID.regionMatches(true, 0, zoneStrings[index][4], 0,
  386.                                  zoneStrings[index][4].length()))
  387.                 break;
  388.         if (index < zoneStrings.length)
  389.             return index;
  390.  
  391.         return -1;
  392.     }
  393.  
  394.     /**
  395.      * Clones an array of Strings.
  396.      * @param srcArray the source array to be cloned.
  397.      * @param count the number of elements in the given source array.
  398.      * @return a cloned array.
  399.      */
  400.     private final String[] duplicate(String[] srcArray)
  401.     {
  402.         String[] dstArray = new String[srcArray.length];
  403.         System.arraycopy(srcArray, 0, dstArray, 0, srcArray.length);
  404.         return dstArray;
  405.     }
  406.  
  407.     /**
  408.      * Clones all the data members from the source DateFormatSymbols to
  409.      * the target DateFormatSymbols. This is only for subclasses.
  410.      * @param src the source DateFormatSymbols.
  411.      * @param dst the target DateFormatSymbols.
  412.      */
  413.     private final void copyMembers(DateFormatSymbols src, DateFormatSymbols dst)
  414.     {
  415.         dst.eras = duplicate(src.eras);
  416.         dst.months = duplicate(src.months);
  417.         dst.shortMonths = duplicate(src.shortMonths);
  418.         dst.weekdays = duplicate(src.weekdays);
  419.         dst.shortWeekdays = duplicate(src.shortWeekdays);
  420.         dst.ampms = duplicate(src.ampms);
  421.         for (int i = 0; i < dst.zoneStrings.length; ++i)
  422.             dst.zoneStrings[i] = duplicate(src.zoneStrings[i]);
  423.         dst.localPatternChars = new String (src.localPatternChars);
  424.     }
  425.  
  426.     /**
  427.      * Compares the equality of the two arrays of String.
  428.      * @param current this String array.
  429.      * @param other that String array.
  430.      */
  431.     private final boolean equals(String[] current, String[] other)
  432.     {
  433.         int count = current.length;
  434.  
  435.         for (int i = 0; i < count; ++i)
  436.             if (!current[i].equals(other[i]))
  437.                 return false;
  438.         return true;
  439.     }
  440.  
  441. }
  442.