1 /* DecimalFormatSymbols.java -- Format symbols used by DecimalFormat
2 Copyright (C) 1999, 2000, 2001, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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
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
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. */
41 import java
.io
.IOException
;
42 import java
.io
.ObjectInputStream
;
43 import java
.io
.Serializable
;
44 import java
.util
.Currency
;
45 import java
.util
.Locale
;
46 import java
.util
.MissingResourceException
;
47 import java
.util
.ResourceBundle
;
50 * This class is a container for the symbols used by
51 * <code>DecimalFormat</code> to format numbers and currency
52 * for a particular locale. These are
53 * normally handled automatically, but an application can override
54 * values as desired using this class.
56 * @author Tom Tromey (tromey@cygnus.com)
57 * @author Aaron M. Renn (arenn@urbanophile.com)
58 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
59 * @date February 24, 1999
60 * @see java.text.DecimalFormat
62 /* Written using "Java Class Libraries", 2nd edition, plus online
63 * API docs for JDK 1.2 from http://www.javasoft.com.
64 * Status: Believed complete and correct to 1.2.
66 public final class DecimalFormatSymbols
implements Cloneable
, Serializable
68 public Object
clone ()
72 return super.clone ();
74 catch(CloneNotSupportedException e
)
81 * This method initializes a new instance of
82 * <code>DecimalFormatSymbols</code> for the default locale.
84 public DecimalFormatSymbols ()
86 this (Locale
.getDefault());
90 * Retrieves a valid string, either using the supplied resource
91 * bundle or the default value.
93 * @param bundle the resource bundle to use to find the string.
94 * @param name key for the string in the resource bundle.
95 * @param def default value for the string.
97 private String
safeGetString(ResourceBundle bundle
,
98 String name
, String def
)
104 return bundle
.getString(name
);
106 catch (MissingResourceException x
)
113 private char safeGetChar(ResourceBundle bundle
,
114 String name
, char def
)
121 r
= bundle
.getString(name
);
123 catch (MissingResourceException x
)
127 if (r
== null || r
.length() < 1)
133 * This method initializes a new instance of
134 * <code>DecimalFormatSymbols</code> for the specified locale.
135 * <strong>Note</strong>: if the locale does not have an associated
136 * <code>Currency</code> instance, the currency symbol and
137 * international currency symbol will be set to the strings "?"
138 * and "XXX" respectively. This generally happens with language
139 * locales (those with no specified country), such as
140 * <code>Locale.ENGLISH</code>.
142 * @param loc The local to load symbols for.
143 * @throws NullPointerException if the locale is null.
145 public DecimalFormatSymbols (Locale loc
)
149 currency
= Currency
.getInstance("XXX");
150 currencySymbol
= "?";
151 intlCurrencySymbol
= "XXX";
154 res
= ResourceBundle
.getBundle("gnu.java.locale.LocaleInformation",
155 loc
, ClassLoader
.getSystemClassLoader());
157 catch (MissingResourceException x
)
163 Currency localeCurrency
= Currency
.getInstance(loc
);
164 if (localeCurrency
!= null)
166 setCurrency(localeCurrency
);
169 catch(IllegalArgumentException exception
)
171 /* Locale has an invalid currency */
173 decimalSeparator
= safeGetChar (res
, "decimalSeparator", '.');
174 digit
= safeGetChar (res
, "digit", '#');
175 exponential
= safeGetChar (res
, "exponential", 'E');
176 groupingSeparator
= safeGetChar (res
, "groupingSeparator", ',');
177 infinity
= safeGetString (res
, "infinity", "\u221e");
180 monetarySeparator
= safeGetChar (res
, "monetarySeparator", '.');
182 catch (MissingResourceException x
)
184 monetarySeparator
= decimalSeparator
;
186 minusSign
= safeGetChar (res
, "minusSign", '-');
187 NaN
= safeGetString (res
, "NaN", "\ufffd");
188 patternSeparator
= safeGetChar (res
, "patternSeparator", ';');
189 percent
= safeGetChar (res
, "percent", '%');
190 perMill
= safeGetChar (res
, "perMill", '\u2030');
191 zeroDigit
= safeGetChar (res
, "zeroDigit", '0');
196 * This method this this object for equality against the specified object.
197 * This will be true if and only if the following criteria are met with
198 * regard to the specified object:
201 * <li>It is not <code>null</code>.</li>
202 * <li>It is an instance of <code>DecimalFormatSymbols</code>.</li>
203 * <li>All of its symbols are identical to the symbols in this object.</li>
206 * @return <code>true</code> if the specified object is equal to this
207 * object, <code>false</code> otherwise.
209 public boolean equals (Object obj
)
211 if (! (obj
instanceof DecimalFormatSymbols
))
213 DecimalFormatSymbols dfs
= (DecimalFormatSymbols
) obj
;
214 return (currencySymbol
.equals(dfs
.currencySymbol
)
215 && decimalSeparator
== dfs
.decimalSeparator
216 && digit
== dfs
.digit
217 && exponential
== dfs
.exponential
218 && groupingSeparator
== dfs
.groupingSeparator
219 && infinity
.equals(dfs
.infinity
)
220 && intlCurrencySymbol
.equals(dfs
.intlCurrencySymbol
)
221 && minusSign
== dfs
.minusSign
222 && monetarySeparator
== dfs
.monetarySeparator
223 && NaN
.equals(dfs
.NaN
)
224 && patternSeparator
== dfs
.patternSeparator
225 && percent
== dfs
.percent
226 && perMill
== dfs
.perMill
227 && zeroDigit
== dfs
.zeroDigit
);
231 * Returns the currency corresponding to the currency symbol stored
232 * in this instance of <code>DecimalFormatSymbols</code>.
234 * @return An instance of <code>Currency</code> which matches
235 * the currency used, or null if there is no corresponding
238 public Currency
getCurrency ()
244 * This method returns the currency symbol in local format. For example,
245 * "$" for Canadian dollars.
247 * @return The currency symbol in local format.
249 public String
getCurrencySymbol ()
251 return currencySymbol
;
255 * This method returns the character used as the decimal point.
257 * @return The character used as the decimal point.
259 public char getDecimalSeparator ()
261 return decimalSeparator
;
265 * This method returns the character used to represent a digit in a
266 * format pattern string.
268 * @return The character used to represent a digit in a format
271 public char getDigit ()
277 * This method returns the character used to represent the exponential
278 * format. This is a GNU Classpath extension.
280 * @return the character used to represent an exponential in a format
283 char getExponential ()
289 * This method sets the character used to separate groups of digits. For
290 * example, the United States uses a comma (,) to separate thousands in
293 * @return The character used to separate groups of digits.
295 public char getGroupingSeparator ()
297 return groupingSeparator
;
301 * This method returns the character used to represent infinity.
303 * @return The character used to represent infinity.
305 public String
getInfinity ()
311 * This method returns the ISO 4217 currency code for
314 * @return the ISO 4217 currency code.
316 public String
getInternationalCurrencySymbol ()
318 return intlCurrencySymbol
;
322 * This method returns the character used to represent the minus sign.
324 * @return The character used to represent the minus sign.
326 public char getMinusSign ()
332 * This method returns the character used to represent the decimal
333 * point for currency values.
335 * @return The decimal point character used in currency values.
337 public char getMonetaryDecimalSeparator ()
339 return monetarySeparator
;
343 * This method returns the string used to represent the NaN (not a number)
346 * @return The string used to represent NaN
348 public String
getNaN ()
354 * This method returns the character used to separate positive and negative
355 * subpatterns in a format pattern.
357 * @return The character used to separate positive and negative subpatterns
358 * in a format pattern.
360 public char getPatternSeparator ()
362 return patternSeparator
;
366 * This method returns the character used as the percent sign.
368 * @return The character used as the percent sign.
370 public char getPercent ()
376 * This method returns the character used as the per mille character.
378 * @return The per mille character.
380 public char getPerMill ()
386 * This method returns the character used to represent the digit zero.
388 * @return The character used to represent the digit zero.
390 public char getZeroDigit ()
396 * This method returns a hash value for this object.
398 * @return A hash value for this object.
400 public int hashCode ()
402 // Compute based on zero digit, grouping separator, and decimal
403 // separator -- JCL book. This probably isn't a very good hash
405 return zeroDigit
<< 16 + groupingSeparator
<< 8 + decimalSeparator
;
409 * This method sets the currency symbol and ISO 4217 currency
410 * code to the values obtained from the supplied currency.
412 * @param currency the currency from which to obtain the values.
413 * @throws NullPointerException if the currency is null.
415 public void setCurrency (Currency currency
)
417 intlCurrencySymbol
= currency
.getCurrencyCode();
418 currencySymbol
= currency
.getSymbol();
419 this.currency
= currency
;
423 * This method sets the currency symbol to the specified value.
425 * @param currency The new currency symbol
427 public void setCurrencySymbol (String currency
)
429 currencySymbol
= currency
;
433 * This method sets the decimal point character to the specified value.
435 * @param decimalSep The new decimal point character
437 public void setDecimalSeparator (char decimalSep
)
439 decimalSeparator
= decimalSep
;
443 * This method sets the character used to represents a digit in a format
444 * string to the specified value.
446 * @param digit The character used to represent a digit in a format pattern.
448 public void setDigit (char digit
)
454 * This method sets the exponential character used in the format string to
455 * the specified value. This is a GNU Classpath extension.
457 * @param exp the character used for the exponential in a format pattern.
459 void setExponential (char exp
)
465 * This method sets the character used to separate groups of digits.
467 * @param groupSep The character used to separate groups of digits.
469 public void setGroupingSeparator (char groupSep
)
471 groupingSeparator
= groupSep
;
475 * This method sets the string used to represents infinity.
477 * @param infinity The string used to represent infinity.
479 public void setInfinity (String infinity
)
481 this.infinity
= infinity
;
485 * This method sets the international currency symbol to the
486 * specified value. If a valid <code>Currency</code> instance
487 * exists for the international currency code, then this is
488 * used for the currency attribute, and the currency symbol
489 * is set to the corresponding value from this instance.
490 * Otherwise, the currency attribute is set to null and the
491 * symbol is left unmodified.
493 * @param currencyCode The new international currency symbol.
495 public void setInternationalCurrencySymbol (String currencyCode
)
497 intlCurrencySymbol
= currencyCode
;
500 currency
= Currency
.getInstance(currencyCode
);
502 catch (IllegalArgumentException exception
)
506 if (currency
!= null)
508 setCurrencySymbol(currency
.getSymbol(locale
));
513 * This method sets the character used to represent the minus sign.
515 * @param minusSign The character used to represent the minus sign.
517 public void setMinusSign (char minusSign
)
519 this.minusSign
= minusSign
;
523 * This method sets the character used for the decimal point in currency
526 * @param decimalSep The decimal point character used in currency values.
528 public void setMonetaryDecimalSeparator (char decimalSep
)
530 monetarySeparator
= decimalSep
;
534 * This method sets the string used to represent the NaN (not a
537 * @param nan The string used to represent NaN
539 public void setNaN (String nan
)
545 * This method sets the character used to separate positive and negative
546 * subpatterns in a format pattern.
548 * @param patternSep The character used to separate positive and
549 * negative subpatterns in a format pattern.
551 public void setPatternSeparator (char patternSep
)
553 patternSeparator
= patternSep
;
557 * This method sets the character used as the percent sign.
559 * @param percent The character used as the percent sign.
561 public void setPercent (char percent
)
563 this.percent
= percent
;
567 * This method sets the character used as the per mille character.
569 * @param perMill The per mille character.
571 public void setPerMill (char perMill
)
573 this.perMill
= perMill
;
577 * This method sets the character used to represent the digit zero.
579 * @param zeroDigit The character used to represent the digit zero.
581 public void setZeroDigit (char zeroDigit
)
583 this.zeroDigit
= zeroDigit
;
587 * @serial A string used for the local currency
589 private String currencySymbol
;
591 * @serial The <code>char</code> used to separate decimals in a number.
593 private char decimalSeparator
;
595 * @serial This is the <code>char</code> used to represent a digit in
596 * a format specification.
600 * @serial This is the <code>char</code> used to represent the exponent
601 * separator in exponential notation.
603 private char exponential
;
605 * @serial This separates groups of thousands in numbers.
607 private char groupingSeparator
;
609 * @serial This string represents infinity.
611 private String infinity
;
613 * @serial This string represents the local currency in an international
614 * context, eg, "C$" for Canadian dollars.
616 private String intlCurrencySymbol
;
618 * @serial This is the character used to represent the minus sign.
620 private char minusSign
;
622 * @serial This character is used to separate decimals when formatting
625 private char monetarySeparator
;
627 * @serial This string is used the represent the Java NaN value for
632 * @serial This is the character used to separate positive and negative
633 * subpatterns in a format pattern.
635 private char patternSeparator
;
637 * @serial This is the percent symbols
639 private char percent
;
641 * @serial This character is used for the mille percent sign.
643 private char perMill
;
645 * @serial This value represents the type of object being de-serialized.
646 * 0 indicates a pre-Java 1.1.6 version, 1 indicates 1.1.6 or later.
647 * 0 indicates a pre-Java 1.1.6 version, 1 indicates 1.1.6 or later,
648 * 2 indicates 1.4 or later
650 private int serialVersionOnStream
= 2;
652 * @serial This is the character used to represent 0.
654 private char zeroDigit
;
657 * @serial The locale of these currency symbols.
659 private Locale locale
;
662 * The currency used for the symbols in this instance.
663 * This is stored temporarily for efficiency reasons,
664 * as well as to ensure that the correct instance
665 * is restored from the currency code.
669 private transient Currency currency
;
671 private static final long serialVersionUID
= 5772796243397350300L;
673 private void readObject(ObjectInputStream stream
)
674 throws IOException
, ClassNotFoundException
676 stream
.defaultReadObject();
677 if (serialVersionOnStream
< 1)
679 monetarySeparator
= decimalSeparator
;
682 if (serialVersionOnStream
< 2)
683 locale
= Locale
.getDefault();
685 serialVersionOnStream
= 2;