gcc/
[official-gcc.git] / libjava / java / text / DecimalFormatSymbols.java
blob7a4e56e9a4210a463f019ac1dc535790ca32872b
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)
9 any later version.
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
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.
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. */
39 package java.text;
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;
49 /**
50 * This class is a container for the symbols used by
51 * <code>DecimalFormat</code> to format numbers and currency. These are
52 * normally handled automatically, but an application can override
53 * values as desired using this class.
55 * @author Tom Tromey <tromey@cygnus.com>
56 * @author Aaron M. Renn (arenn@urbanophile.com)
57 * @date February 24, 1999
59 /* Written using "Java Class Libraries", 2nd edition, plus online
60 * API docs for JDK 1.2 from http://www.javasoft.com.
61 * Status: Believed complete and correct to 1.2.
63 public final class DecimalFormatSymbols implements Cloneable, Serializable
65 public Object clone ()
67 try
69 return super.clone ();
71 catch(CloneNotSupportedException e)
73 return null;
77 /**
78 * This method initializes a new instance of
79 * <code>DecimalFormatSymbols</code> for the default locale.
81 public DecimalFormatSymbols ()
83 this (Locale.getDefault());
86 private String safeGetString(ResourceBundle bundle,
87 String name, String def)
89 if (bundle != null)
91 try
93 return bundle.getString(name);
95 catch (MissingResourceException x)
99 return def;
102 private char safeGetChar(ResourceBundle bundle,
103 String name, char def)
105 String r = null;
106 if (bundle != null)
110 r = bundle.getString(name);
112 catch (MissingResourceException x)
116 if (r == null || r.length() < 1)
117 return def;
118 return r.charAt(0);
122 * This method initializes a new instance of
123 * <code>DecimalFormatSymbols</code> for the specified locale.
125 * @param locale The local to load symbols for.
127 public DecimalFormatSymbols (Locale loc)
129 ResourceBundle res;
132 res = ResourceBundle.getBundle("gnu.java.locale.LocaleInformation",
133 loc, ClassLoader.getSystemClassLoader());
135 catch (MissingResourceException x)
137 res = null;
139 currencySymbol = safeGetString (res, "currencySymbol", "$");
140 decimalSeparator = safeGetChar (res, "decimalSeparator", '.');
141 digit = safeGetChar (res, "digit", '#');
142 exponential = safeGetChar (res, "exponential", 'E');
143 groupingSeparator = safeGetChar (res, "groupingSeparator", ',');
144 infinity = safeGetString (res, "infinity", "\u221e");
145 // FIXME: default?
146 intlCurrencySymbol = safeGetString (res, "intlCurrencySymbol", "$");
149 monetarySeparator = safeGetChar (res, "monetarySeparator", '.');
151 catch (MissingResourceException x)
153 monetarySeparator = decimalSeparator;
155 minusSign = safeGetChar (res, "minusSign", '-');
156 NaN = safeGetString (res, "NaN", "\ufffd");
157 patternSeparator = safeGetChar (res, "patternSeparator", ';');
158 percent = safeGetChar (res, "percent", '%');
159 perMill = safeGetChar (res, "perMill", '\u2030');
160 zeroDigit = safeGetChar (res, "zeroDigit", '0');
161 locale = loc;
165 * This method this this object for equality against the specified object.
166 * This will be true if and only if the following criteria are met with
167 * regard to the specified object:
168 * <p>
169 * <ul>
170 * <li>It is not <code>null</code>.</li>
171 * <li>It is an instance of <code>DecimalFormatSymbols</code>.</li>
172 * <li>All of its symbols are identical to the symbols in this object.</li>
173 * </ul>
175 * @return <code>true</code> if the specified object is equal to this
176 * object, <code>false</code> otherwise.
178 public boolean equals (Object obj)
180 if (! (obj instanceof DecimalFormatSymbols))
181 return false;
182 DecimalFormatSymbols dfs = (DecimalFormatSymbols) obj;
183 return (currencySymbol.equals(dfs.currencySymbol)
184 && decimalSeparator == dfs.decimalSeparator
185 && digit == dfs.digit
186 && exponential == dfs.exponential
187 && groupingSeparator == dfs.groupingSeparator
188 && infinity.equals(dfs.infinity)
189 && intlCurrencySymbol.equals(dfs.intlCurrencySymbol)
190 && minusSign == dfs.minusSign
191 && monetarySeparator == dfs.monetarySeparator
192 && NaN.equals(dfs.NaN)
193 && patternSeparator == dfs.patternSeparator
194 && percent == dfs.percent
195 && perMill == dfs.perMill
196 && zeroDigit == dfs.zeroDigit);
200 * Returns the currency corresponding to the currency symbol stored
201 * in the instance of <code>DecimalFormatSymbols</code>.
203 * @return A new instance of <code>Currency</code> if
204 * the currency code matches a known one.
206 public Currency getCurrency ()
208 return Currency.getInstance (currencySymbol);
212 * This method returns the currency symbol in local format. For example,
213 * "$" for Canadian dollars.
215 * @return The currency symbol in local format.
217 public String getCurrencySymbol ()
219 return currencySymbol;
223 * This method returns the character used as the decimal point.
225 * @return The character used as the decimal point.
227 public char getDecimalSeparator ()
229 return decimalSeparator;
233 * This method returns the character used to represent a digit in a
234 * format pattern string.
236 * @return The character used to represent a digit in a format
237 * pattern string.
239 public char getDigit ()
241 return digit;
244 // This is our own extension.
245 char getExponential ()
247 return exponential;
251 * This method sets the character used to separate groups of digits. For
252 * example, the United States uses a comma (,) to separate thousands in
253 * a number.
255 * @return The character used to separate groups of digits.
257 public char getGroupingSeparator ()
259 return groupingSeparator;
263 * This method returns the character used to represent infinity.
265 * @return The character used to represent infinity.
267 public String getInfinity ()
269 return infinity;
273 * This method returns the currency symbol in international format. For
274 * example, "C$" for Canadian dollars.
276 * @return The currency symbol in international format.
278 public String getInternationalCurrencySymbol ()
280 return intlCurrencySymbol;
284 * This method returns the character used to represent the minus sign.
286 * @return The character used to represent the minus sign.
288 public char getMinusSign ()
290 return minusSign;
294 * This method returns the character used to represent the decimal
295 * point for currency values.
297 * @return The decimal point character used in currency values.
299 public char getMonetaryDecimalSeparator ()
301 return monetarySeparator;
305 * This method returns the string used to represent the NaN (not a number)
306 * value.
308 * @return The string used to represent NaN
310 public String getNaN ()
312 return NaN;
316 * This method returns the character used to separate positive and negative
317 * subpatterns in a format pattern.
319 * @return The character used to separate positive and negative subpatterns
320 * in a format pattern.
322 public char getPatternSeparator ()
324 return patternSeparator;
328 * This method returns the character used as the percent sign.
330 * @return The character used as the percent sign.
332 public char getPercent ()
334 return percent;
338 * This method returns the character used as the per mille character.
340 * @return The per mille character.
342 public char getPerMill ()
344 return perMill;
348 * This method returns the character used to represent the digit zero.
350 * @return The character used to represent the digit zero.
352 public char getZeroDigit ()
354 return zeroDigit;
358 * This method returns a hash value for this object.
360 * @return A hash value for this object.
362 public int hashCode ()
364 // Compute based on zero digit, grouping separator, and decimal
365 // separator -- JCL book. This probably isn't a very good hash
366 // code.
367 return zeroDigit << 16 + groupingSeparator << 8 + decimalSeparator;
371 * This method sets the currency to the specified value.
373 * @param currency The new currency
375 public void setCurrency (Currency currency)
377 setCurrencySymbol (currency.getSymbol());
381 * This method sets the currency symbol to the specified value.
383 * @param currencySymbol The new currency symbol
385 public void setCurrencySymbol (String currency)
387 currencySymbol = currency;
391 * This method sets the decimal point character to the specified value.
393 * @param decimalSeparator The new decimal point character
395 public void setDecimalSeparator (char decimalSep)
397 decimalSeparator = decimalSep;
401 * This method sets the character used to represents a digit in a format
402 * string to the specified value.
404 * @param digit The character used to represent a digit in a format pattern.
406 public void setDigit (char digit)
408 this.digit = digit;
411 // This is our own extension.
412 void setExponential (char exp)
414 exponential = exp;
418 * This method sets the character used to separate groups of digits.
420 * @param groupingSeparator The character used to separate groups of digits.
422 public void setGroupingSeparator (char groupSep)
424 groupingSeparator = groupSep;
428 * This method sets the string used to represents infinity.
430 * @param infinity The string used to represent infinity.
432 public void setInfinity (String infinity)
434 this.infinity = infinity;
438 * This method sets the international currency symbols to the
439 * specified value.
441 * @param intlCurrencySymbol The new international currency symbol.
443 public void setInternationalCurrencySymbol (String currency)
445 intlCurrencySymbol = currency;
449 * This method sets the character used to represent the minus sign.
451 * @param minusSign The character used to represent the minus sign.
453 public void setMinusSign (char minusSign)
455 this.minusSign = minusSign;
459 * This method sets the character used for the decimal point in currency
460 * values.
462 * @param monetarySeparator The decimal point character used in
463 * currency values.
465 public void setMonetaryDecimalSeparator (char decimalSep)
467 monetarySeparator = decimalSep;
471 * This method sets the string used to represent the NaN (not a
472 * number) value.
474 * @param NaN The string used to represent NaN
476 public void setNaN (String nan)
478 NaN = nan;
482 * This method sets the character used to separate positive and negative
483 * subpatterns in a format pattern.
485 * @param patternSeparator The character used to separate positive and
486 * negative subpatterns in a format pattern.
488 public void setPatternSeparator (char patternSep)
490 patternSeparator = patternSep;
494 * This method sets the character used as the percent sign.
496 * @param percent The character used as the percent sign.
498 public void setPercent (char percent)
500 this.percent = percent;
504 * This method sets the character used as the per mille character.
506 * @param perMill The per mille character.
508 public void setPerMill (char perMill)
510 this.perMill = perMill;
514 * This method sets the character used to represent the digit zero.
516 * @param zeroDigit The character used to represent the digit zero.
518 public void setZeroDigit (char zeroDigit)
520 this.zeroDigit = zeroDigit;
524 * @serial A string used for the local currency
526 private String currencySymbol;
528 * @serial The <code>char</code> used to separate decimals in a number.
530 private char decimalSeparator;
532 * @serial This is the <code>char</code> used to represent a digit in
533 * a format specification.
535 private char digit;
537 * @serial This is the <code>char</code> used to represent the exponent
538 * separator in exponential notation.
540 private char exponential;
542 * @serial This separates groups of thousands in numbers.
544 private char groupingSeparator;
546 * @serial This string represents infinity.
548 private String infinity;
550 * @serial This string represents the local currency in an international
551 * context, eg, "C$" for Canadian dollars.
553 private String intlCurrencySymbol;
555 * @serial This is the character used to represent the minus sign.
557 private char minusSign;
559 * @serial This character is used to separate decimals when formatting
560 * currency values.
562 private char monetarySeparator;
564 * @serial This string is used the represent the Java NaN value for
565 * "not a number".
567 private String NaN;
569 * @serial This is the character used to separate positive and negative
570 * subpatterns in a format pattern.
572 private char patternSeparator;
574 * @serial This is the percent symbols
576 private char percent;
578 * @serial This character is used for the mille percent sign.
580 private char perMill;
582 * @serial This value represents the type of object being de-serialized.
583 * 0 indicates a pre-Java 1.1.6 version, 1 indicates 1.1.6 or later.
584 * 0 indicates a pre-Java 1.1.6 version, 1 indicates 1.1.6 or later,
585 * 2 indicates 1.4 or later
587 private int serialVersionOnStream = 2;
589 * @serial This is the character used to represent 0.
591 private char zeroDigit;
594 * @serial The locale of these currency symbols.
596 private Locale locale;
598 private static final long serialVersionUID = 5772796243397350300L;
600 private void readObject(ObjectInputStream stream)
601 throws IOException, ClassNotFoundException
603 stream.defaultReadObject();
604 if (serialVersionOnStream < 1)
606 monetarySeparator = decimalSeparator;
607 exponential = 'E';
609 if (serialVersionOnStream < 2)
610 locale = Locale.getDefault();
612 serialVersionOnStream = 2;