home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / text / DecimalFormatSymbols.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  11.4 KB  |  396 lines

  1. /*
  2.  * @(#)DecimalFormatSymbols.java    1.22 98/03/18
  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-1998 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.io.IOException;
  33. import java.io.ObjectInputStream;
  34. import java.io.Serializable;
  35. import java.util.ResourceBundle;
  36. import java.util.Locale;
  37. import java.util.Hashtable;
  38.  
  39. /**
  40.  * This class represents the set of symbols (such as the decimal separator,
  41.  * the grouping separator, and so on) needed by <code>DecimalFormat</code>
  42.  * to format numbers. <code>DecimalFormat</code> creates for itself an instance of
  43.  * <code>DecimalFormatSymbols</code> from its locale data.  If you need to change any
  44.  * of these symbols, you can get the <code>DecimalFormatSymbols</code> object from
  45.  * your <code>DecimalFormat</code> and modify it.
  46.  *
  47.  * @see          java.util.Locale
  48.  * @see          DecimalFormat
  49.  * @version      1.12 29 Jan 1997
  50.  * @author       Mark Davis
  51.  * @author       Alan Liu
  52.  */
  53.  
  54. final public class DecimalFormatSymbols implements Cloneable, Serializable {
  55.  
  56.     /**
  57.      * Create a DecimalFormatSymbols object for the default locale.
  58.      */
  59.     public DecimalFormatSymbols() {
  60.         initialize( Locale.getDefault() );
  61.     }
  62.  
  63.     /**
  64.      * Create a DecimalFormatSymbols object for the given locale.
  65.      */
  66.     public DecimalFormatSymbols( Locale locale ) {
  67.         initialize( locale );
  68.     }
  69.  
  70.     /**
  71.      * character used for zero. Different for Arabic, etc.
  72.      */
  73.     public char getZeroDigit() {
  74.         return zeroDigit;
  75.     }
  76.  
  77.     public void setZeroDigit(char zeroDigit) {
  78.         this.zeroDigit = zeroDigit;
  79.     }
  80.  
  81.     /**
  82.      * character used for thousands separator. Different for French, etc.
  83.      */
  84.     public char getGroupingSeparator() {
  85.         return groupingSeparator;
  86.     }
  87.  
  88.     public void setGroupingSeparator(char groupingSeparator) {
  89.         this.groupingSeparator = groupingSeparator;
  90.     }
  91.  
  92.     /**
  93.      * character used for decimal sign. Different for French, etc.
  94.      */
  95.     public char getDecimalSeparator() {
  96.         return decimalSeparator;
  97.     }
  98.  
  99.     public void setDecimalSeparator(char decimalSeparator) {
  100.         this.decimalSeparator = decimalSeparator;
  101.     }
  102.  
  103.     /**
  104.      * character used for mille percent sign. Different for Arabic, etc.
  105.      */
  106.     public char getPerMill() {
  107.         return perMill;
  108.     }
  109.  
  110.     public void setPerMill(char perMill) {
  111.         this.perMill = perMill;
  112.     }
  113.  
  114.     /**
  115.      * character used for percent sign. Different for Arabic, etc.
  116.      */
  117.     public char getPercent() {
  118.         return percent;
  119.     }
  120.  
  121.     public void setPercent(char percent) {
  122.         this.percent = percent;
  123.     }
  124.  
  125.     /**
  126.      * character used for a digit in a pattern.
  127.      */
  128.     public char getDigit() {
  129.         return digit;
  130.     }
  131.  
  132.     public void setDigit(char digit) {
  133.         this.digit = digit;
  134.     }
  135.  
  136.     /**
  137.      * character used to separate positive and negative subpatterns
  138.      * in a pattern.
  139.      */
  140.     public char getPatternSeparator() {
  141.         return patternSeparator;
  142.     }
  143.  
  144.     public void setPatternSeparator(char patternSeparator) {
  145.         this.patternSeparator = patternSeparator;
  146.     }
  147.  
  148.     /**
  149.      * character used to represent infinity. Almost always left
  150.      * unchanged.
  151.      */
  152.  
  153.     public String getInfinity() {
  154.         return infinity;
  155.     }
  156.  
  157.     public void setInfinity(String infinity) {
  158.         this.infinity = infinity;
  159.     }
  160.  
  161.     /**
  162.      * character used to represent NaN. Almost always left
  163.      * unchanged.
  164.      */
  165.     public String getNaN() {
  166.         return NaN;
  167.     }
  168.  
  169.     public void setNaN(String NaN) {
  170.         this.NaN = NaN;
  171.     }
  172.  
  173.     /**
  174.      * character used to represent minus sign. If no explicit
  175.      * negative format is specified, one is formed by prefixing
  176.      * minusSign to the positive format.
  177.      */
  178.     public char getMinusSign() {
  179.         return minusSign;
  180.     }
  181.  
  182.     public void setMinusSign(char minusSign) {
  183.         this.minusSign = minusSign;
  184.     }
  185.  
  186.     /**
  187.      * 1.2 Back-out comment : HShih  Made the following package private
  188.      * get/setCurrencySymbol
  189.      * get/setInternationalCurrencySymbol
  190.      * get/setMonetaryDecimalSeparator
  191.      */
  192.     /**
  193.      * Return the string denoting the local currency.
  194.      */
  195.     String getCurrencySymbol()
  196.     {
  197.         return currencySymbol;
  198.     }
  199.  
  200.     /**
  201.      * Set the string denoting the local currency.
  202.      */
  203.     void setCurrencySymbol(String currency)
  204.     {
  205.         currencySymbol = currency;
  206.     }
  207.  
  208.     /**
  209.      * Return the international string denoting the local currency.
  210.      */
  211.     String getInternationalCurrencySymbol()
  212.     {
  213.         return intlCurrencySymbol;
  214.     }
  215.  
  216.     /**
  217.      * Set the international string denoting the local currency.
  218.      */
  219.     void setInternationalCurrencySymbol(String currency)
  220.     {
  221.         intlCurrencySymbol = currency;
  222.     }
  223.  
  224.     /**
  225.      * Return the monetary decimal separator.
  226.      */
  227.     char getMonetaryDecimalSeparator()
  228.     {
  229.         return monetarySeparator;
  230.     }
  231.  
  232.     /**
  233.      * Set the monetary decimal separator.
  234.      */
  235.     void setMonetaryDecimalSeparator(char sep)
  236.     {
  237.         monetarySeparator = sep;
  238.     }
  239.  
  240.     //------------------------------------------------------------
  241.     // BEGIN   Package Private methods ... to be made public later
  242.     //------------------------------------------------------------
  243.  
  244.     /**
  245.      * Return the character used to separate the mantissa from the exponent.
  246.      */
  247.     char getExponentialSymbol()
  248.     {
  249.         return exponential;
  250.     }
  251.  
  252.     /**
  253.      * Set the character used to separate the mantissa from the exponent.
  254.      */
  255.     void setExponentialSymbol(char exp)
  256.     {
  257.         exponential = exp;
  258.     }
  259.  
  260.  
  261.     //------------------------------------------------------------
  262.     // END     Package Private methods ... to be made public later
  263.     //------------------------------------------------------------
  264.  
  265.     /**
  266.      * Standard override.
  267.      */
  268.     public Object clone() {
  269.         try {
  270.             return (DecimalFormatSymbols)super.clone();
  271.             // other fields are bit-copied
  272.         } catch (CloneNotSupportedException e) {
  273.             throw new InternalError();
  274.         }
  275.     }
  276.  
  277.     /**
  278.      * Override equals
  279.      */
  280.     public boolean equals(Object obj) {
  281.         if (obj == null) return false;
  282.         if (this == obj) return true;
  283.         if (getClass() != obj.getClass()) return false;
  284.         DecimalFormatSymbols other = (DecimalFormatSymbols) obj;
  285.         return (zeroDigit == other.zeroDigit &&
  286.         groupingSeparator == other.groupingSeparator &&
  287.         decimalSeparator == other.decimalSeparator &&
  288.         percent == other.percent &&
  289.         perMill == other.perMill &&
  290.         digit == other.digit &&
  291.         minusSign == other.minusSign &&
  292.         patternSeparator == other.patternSeparator &&
  293.         infinity.equals(other.infinity) &&
  294.         NaN.equals(other.NaN) &&
  295.         currencySymbol.equals(other.currencySymbol) &&
  296.         intlCurrencySymbol.equals(other.intlCurrencySymbol) &&
  297.         monetarySeparator == other.monetarySeparator);
  298.     }
  299.  
  300.     /**
  301.      * Override hashCode
  302.      */
  303.     public int hashCode() {
  304.             int result = zeroDigit;
  305.             result = result * 37 + groupingSeparator;
  306.             result = result * 37 + decimalSeparator;
  307.             return result;
  308.     }
  309.  
  310.     /**
  311.      * Initializes the symbols from the LocaleElements resource bundle.
  312.      * Note: The organization of LocaleElements badly needs to be
  313.      * cleaned up.
  314.      */
  315.     private void initialize( Locale locale ) {
  316.     /* try the cache first */
  317.     String[][] data = (String[][]) cachedLocaleData.get(locale);
  318.     String[] numberElements;
  319.     String[] currencyElements;
  320.     if (data == null) {  /* cache miss */
  321.         data = new String[2][];
  322.         ResourceBundle rb = ResourceBundle.getBundle
  323.         ("java.text.resources.LocaleElements", locale);
  324.         data[0] = rb.getStringArray("NumberElements");
  325.         data[1] = rb.getStringArray("CurrencyElements");
  326.         /* update cache */
  327.         cachedLocaleData.put(locale, data);
  328.     }
  329.     numberElements = data[0];
  330.     currencyElements = data[1];
  331.  
  332.         decimalSeparator = numberElements[0].charAt(0);
  333.         groupingSeparator = numberElements[1].charAt(0);
  334.         patternSeparator = numberElements[2].charAt(0);
  335.         percent = numberElements[3].charAt(0);
  336.         zeroDigit = numberElements[4].charAt(0); //different for Arabic,etc.
  337.         digit = numberElements[5].charAt(0);
  338.         minusSign = numberElements[6].charAt(0);
  339.     exponential = numberElements[7].charAt(0);
  340.         perMill = numberElements[8].charAt(0);
  341.         infinity  = numberElements[9];
  342.         NaN = numberElements[10];
  343.  
  344.     currencySymbol = currencyElements[0];
  345.         intlCurrencySymbol = currencyElements[1];
  346.     monetarySeparator = currencyElements[2].charAt(0);
  347.     }
  348.  
  349.     /**
  350.      * Override readObject.
  351.      */
  352.     private void readObject(ObjectInputStream stream)
  353.      throws IOException, ClassNotFoundException
  354.     {
  355.     stream.defaultReadObject();
  356.     if (serialVersionOnStream < 1)
  357.     {
  358.         // Didn't have monetarySeparator or exponential field;
  359.         // use defaults.
  360.         monetarySeparator = decimalSeparator;
  361.         exponential       = 'E';
  362.     }
  363.     serialVersionOnStream = currentSerialVersion;
  364.     }
  365.  
  366.     private  char    zeroDigit;
  367.     private  char    groupingSeparator;
  368.     private  char    decimalSeparator;
  369.     private  char    perMill;
  370.     private  char    percent;
  371.     private  char    digit;
  372.     private  char    patternSeparator;
  373.     private  String  infinity;
  374.     private  String  NaN;
  375.     private  char    minusSign;
  376.     private  String  currencySymbol;
  377.     private  String  intlCurrencySymbol;
  378.     private  char    monetarySeparator; // Field new in JDK 1.1.4
  379.     private  char    exponential;       // Field new in JDK 1.1.4
  380.  
  381.     // Proclaim JDK 1.1 FCS compatibility
  382.     static final long serialVersionUID = 5772796243397350300L;
  383.  
  384.     // The internal serial version which says which version was written
  385.     // - 0 (default) for version up to JDK 1.1.3
  386.     // - 1 for version from JDK 1.1.4, which includes two new fields
  387.     static final int currentSerialVersion = 1;
  388.     private int serialVersionOnStream = currentSerialVersion;
  389.  
  390.     /**
  391.      * cache to hold the NumberElements and the CurrencyElements
  392.      * of a Locale.
  393.      */
  394.     private static final Hashtable cachedLocaleData = new Hashtable(3);
  395. }
  396.