1 /* DecimalFormatSymbols.java -- Format symbols used by DecimalFormat
2 Copyright (C) 1999, 2000, 2001, 2004, 2007 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 gnu.java.locale.LocaleHelper;
43 import java.io.IOException;
44 import java.io.ObjectInputStream;
45 import java.io.Serializable;
47 import java.text.spi.DecimalFormatSymbolsProvider;
49 import java.util.Currency;
50 import java.util.Locale;
51 import java.util.MissingResourceException;
52 import java.util.ResourceBundle;
53 import java.util.ServiceLoader;
56 * This class is a container for the symbols used by
57 * <code>DecimalFormat</code> to format numbers and currency
58 * for a particular locale. These are
59 * normally handled automatically, but an application can override
60 * values as desired using this class.
62 * @author Tom Tromey (tromey@cygnus.com)
63 * @author Aaron M. Renn (arenn@urbanophile.com)
64 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
65 * @date February 24, 1999
66 * @see java.text.DecimalFormat
68 /* Written using "Java Class Libraries", 2nd edition, plus online
69 * API docs for JDK 1.2 from http://www.javasoft.com.
70 * Status: Believed complete and correct to 1.2.
72 public class DecimalFormatSymbols implements Cloneable, Serializable
74 public Object clone ()
80 catch(CloneNotSupportedException e)
87 * This method initializes a new instance of
88 * <code>DecimalFormatSymbols</code> for the default locale.
89 * This constructor only obtains instances using the runtime's resources;
90 * to also include {@link java.text.spi.DateFormatSymbolsProvider} instances,
91 * call {@link #getInstance()} instead.
95 public DecimalFormatSymbols ()
97 this (Locale.getDefault());
101 * Retrieves a valid string, either using the supplied resource
102 * bundle or the default value.
104 * @param bundle the resource bundle to use to find the string.
105 * @param name key for the string in the resource bundle.
106 * @param def default value for the string.
108 private String safeGetString(ResourceBundle bundle,
109 String name, String def)
115 return bundle.getString(name);
117 catch (MissingResourceException x)
124 private char safeGetChar(ResourceBundle bundle,
125 String name, char def)
132 r = bundle.getString(name);
134 catch (MissingResourceException x)
138 if (r == null || r.length() < 1)
144 * This method initializes a new instance of
145 * <code>DecimalFormatSymbols</code> for the specified locale.
146 * <strong>Note</strong>: if the locale does not have an associated
147 * <code>Currency</code> instance, the currency symbol and
148 * international currency symbol will be set to the strings "?"
149 * and "XXX" respectively. This generally happens with language
150 * locales (those with no specified country), such as
151 * <code>Locale.ENGLISH</code>. This constructor only obtains
152 * instances using the runtime's resources; to also include
153 * {@link java.text.spi.DecimalFormatSymbolsProvider} instances,
154 * call {@link #getInstance(java.util.Locale)} instead.
156 * @param loc The local to load symbols for.
157 * @throws NullPointerException if the locale is null.
158 * @see #getInstance(java.util.Locale)
160 public DecimalFormatSymbols (Locale loc)
166 res = ResourceBundle.getBundle("gnu.java.locale.LocaleInformation",
167 loc, ClassLoader.getSystemClassLoader());
169 catch (MissingResourceException x)
174 currency = Currency.getInstance("XXX");
175 currencySymbol = "?";
176 intlCurrencySymbol = "XXX";
179 Currency localeCurrency = Currency.getInstance(loc);
180 if (localeCurrency != null)
182 setCurrency(localeCurrency);
185 catch(IllegalArgumentException exception)
187 /* Locale has an invalid currency */
189 decimalSeparator = safeGetChar (res, "decimalSeparator", '.');
190 digit = safeGetChar (res, "digit", '#');
191 exponential = safeGetChar (res, "exponential", 'E');
192 groupingSeparator = safeGetChar (res, "groupingSeparator", ',');
193 infinity = safeGetString (res, "infinity", "\u221e");
196 monetarySeparator = safeGetChar (res, "monetarySeparator", '.');
198 catch (MissingResourceException x)
200 monetarySeparator = decimalSeparator;
202 minusSign = safeGetChar (res, "minusSign", '-');
203 NaN = safeGetString (res, "NaN", "\ufffd");
204 patternSeparator = safeGetChar (res, "patternSeparator", ';');
205 percent = safeGetChar (res, "percent", '%');
206 perMill = safeGetChar (res, "perMill", '\u2030');
207 zeroDigit = safeGetChar (res, "zeroDigit", '0');
211 * This method this this object for equality against the specified object.
212 * This will be true if and only if the following criteria are met with
213 * regard to the specified object:
216 * <li>It is not <code>null</code>.</li>
217 * <li>It is an instance of <code>DecimalFormatSymbols</code>.</li>
218 * <li>All of its symbols are identical to the symbols in this object.</li>
221 * @return <code>true</code> if the specified object is equal to this
222 * object, <code>false</code> otherwise.
224 public boolean equals (Object obj)
226 if (! (obj instanceof DecimalFormatSymbols))
228 DecimalFormatSymbols dfs = (DecimalFormatSymbols) obj;
229 return (currencySymbol.equals(dfs.currencySymbol)
230 && decimalSeparator == dfs.decimalSeparator
231 && digit == dfs.digit
232 && exponential == dfs.exponential
233 && groupingSeparator == dfs.groupingSeparator
234 && infinity.equals(dfs.infinity)
235 && intlCurrencySymbol.equals(dfs.intlCurrencySymbol)
236 && minusSign == dfs.minusSign
237 && monetarySeparator == dfs.monetarySeparator
238 && NaN.equals(dfs.NaN)
239 && patternSeparator == dfs.patternSeparator
240 && percent == dfs.percent
241 && perMill == dfs.perMill
242 && zeroDigit == dfs.zeroDigit);
246 * Returns the currency corresponding to the currency symbol stored
247 * in this instance of <code>DecimalFormatSymbols</code>.
249 * @return An instance of <code>Currency</code> which matches
250 * the currency used, or null if there is no corresponding
253 public Currency getCurrency ()
259 * This method returns the currency symbol in local format. For example,
260 * "$" for Canadian dollars.
262 * @return The currency symbol in local format.
264 public String getCurrencySymbol ()
266 return currencySymbol;
270 * This method returns the character used as the decimal point.
272 * @return The character used as the decimal point.
274 public char getDecimalSeparator ()
276 return decimalSeparator;
280 * This method returns the character used to represent a digit in a
281 * format pattern string.
283 * @return The character used to represent a digit in a format
286 public char getDigit ()
292 * This method returns the character used to represent the exponential
293 * format. This is a GNU Classpath extension.
295 * @return the character used to represent an exponential in a format
298 char getExponential ()
304 * This method sets the character used to separate groups of digits. For
305 * example, the United States uses a comma (,) to separate thousands in
308 * @return The character used to separate groups of digits.
310 public char getGroupingSeparator ()
312 return groupingSeparator;
316 * This method returns the character used to represent infinity.
318 * @return The character used to represent infinity.
320 public String getInfinity ()
326 * This method returns the ISO 4217 currency code for
329 * @return the ISO 4217 currency code.
331 public String getInternationalCurrencySymbol ()
333 return intlCurrencySymbol;
337 * This method returns the character used to represent the minus sign.
339 * @return The character used to represent the minus sign.
341 public char getMinusSign ()
347 * This method returns the character used to represent the decimal
348 * point for currency values.
350 * @return The decimal point character used in currency values.
352 public char getMonetaryDecimalSeparator ()
354 return monetarySeparator;
358 * This method returns the string used to represent the NaN (not a number)
361 * @return The string used to represent NaN
363 public String getNaN ()
369 * This method returns the character used to separate positive and negative
370 * subpatterns in a format pattern.
372 * @return The character used to separate positive and negative subpatterns
373 * in a format pattern.
375 public char getPatternSeparator ()
377 return patternSeparator;
381 * This method returns the character used as the percent sign.
383 * @return The character used as the percent sign.
385 public char getPercent ()
391 * This method returns the character used as the per mille character.
393 * @return The per mille character.
395 public char getPerMill ()
401 * This method returns the character used to represent the digit zero.
403 * @return The character used to represent the digit zero.
405 public char getZeroDigit ()
411 * This method returns a hash value for this object.
413 * @return A hash value for this object.
415 public int hashCode ()
417 // Compute based on zero digit, grouping separator, and decimal
418 // separator -- JCL book. This probably isn't a very good hash
420 return zeroDigit << 16 + groupingSeparator << 8 + decimalSeparator;
424 * This method sets the currency symbol and ISO 4217 currency
425 * code to the values obtained from the supplied currency.
427 * @param currency the currency from which to obtain the values.
428 * @throws NullPointerException if the currency is null.
430 public void setCurrency (Currency currency)
432 intlCurrencySymbol = currency.getCurrencyCode();
433 currencySymbol = currency.getSymbol(locale);
434 this.currency = currency;
438 * This method sets the currency symbol to the specified value.
440 * @param currency The new currency symbol
442 public void setCurrencySymbol (String currency)
444 currencySymbol = currency;
448 * This method sets the decimal point character to the specified value.
450 * @param decimalSep The new decimal point character
452 public void setDecimalSeparator (char decimalSep)
454 decimalSeparator = decimalSep;
458 * This method sets the character used to represents a digit in a format
459 * string to the specified value.
461 * @param digit The character used to represent a digit in a format pattern.
463 public void setDigit (char digit)
469 * This method sets the exponential character used in the format string to
470 * the specified value. This is a GNU Classpath extension.
472 * @param exp the character used for the exponential in a format pattern.
474 void setExponential (char exp)
480 * This method sets the character used to separate groups of digits.
482 * @param groupSep The character used to separate groups of digits.
484 public void setGroupingSeparator (char groupSep)
486 groupingSeparator = groupSep;
490 * This method sets the string used to represents infinity.
492 * @param infinity The string used to represent infinity.
494 public void setInfinity (String infinity)
496 this.infinity = infinity;
500 * This method sets the international currency symbol to the
501 * specified value. If a valid <code>Currency</code> instance
502 * exists for the international currency code, then this is
503 * used for the currency attribute, and the currency symbol
504 * is set to the corresponding value from this instance.
505 * Otherwise, the currency attribute is set to null and the
506 * symbol is left unmodified.
508 * @param currencyCode The new international currency symbol.
510 public void setInternationalCurrencySymbol (String currencyCode)
512 intlCurrencySymbol = currencyCode;
515 currency = Currency.getInstance(currencyCode);
517 catch (IllegalArgumentException exception)
521 if (currency != null)
523 setCurrencySymbol(currency.getSymbol(locale));
528 * This method sets the character used to represent the minus sign.
530 * @param minusSign The character used to represent the minus sign.
532 public void setMinusSign (char minusSign)
534 this.minusSign = minusSign;
538 * This method sets the character used for the decimal point in currency
541 * @param decimalSep The decimal point character used in currency values.
543 public void setMonetaryDecimalSeparator (char decimalSep)
545 monetarySeparator = decimalSep;
549 * This method sets the string used to represent the NaN (not a
552 * @param nan The string used to represent NaN
554 public void setNaN (String nan)
560 * This method sets the character used to separate positive and negative
561 * subpatterns in a format pattern.
563 * @param patternSep The character used to separate positive and
564 * negative subpatterns in a format pattern.
566 public void setPatternSeparator (char patternSep)
568 patternSeparator = patternSep;
572 * This method sets the character used as the percent sign.
574 * @param percent The character used as the percent sign.
576 public void setPercent (char percent)
578 this.percent = percent;
582 * This method sets the character used as the per mille character.
584 * @param perMill The per mille character.
586 public void setPerMill (char perMill)
588 this.perMill = perMill;
592 * This method sets the character used to represent the digit zero.
594 * @param zeroDigit The character used to represent the digit zero.
596 public void setZeroDigit (char zeroDigit)
598 this.zeroDigit = zeroDigit;
602 * @serial A string used for the local currency
604 private String currencySymbol;
606 * @serial The <code>char</code> used to separate decimals in a number.
608 private char decimalSeparator;
610 * @serial This is the <code>char</code> used to represent a digit in
611 * a format specification.
615 * @serial This is the <code>char</code> used to represent the exponent
616 * separator in exponential notation.
618 private char exponential;
620 * @serial This separates groups of thousands in numbers.
622 private char groupingSeparator;
624 * @serial This string represents infinity.
626 private String infinity;
628 * @serial This string represents the local currency in an international
629 * context, eg, "C$" for Canadian dollars.
631 private String intlCurrencySymbol;
633 * @serial This is the character used to represent the minus sign.
635 private char minusSign;
637 * @serial This character is used to separate decimals when formatting
640 private char monetarySeparator;
642 * @serial This string is used the represent the Java NaN value for
647 * @serial This is the character used to separate positive and negative
648 * subpatterns in a format pattern.
650 private char patternSeparator;
652 * @serial This is the percent symbols
654 private char percent;
656 * @serial This character is used for the mille percent sign.
658 private char perMill;
660 * @serial This value represents the type of object being de-serialized.
661 * 0 indicates a pre-Java 1.1.6 version, 1 indicates 1.1.6 or later.
662 * 0 indicates a pre-Java 1.1.6 version, 1 indicates 1.1.6 or later,
663 * 2 indicates 1.4 or later
665 private int serialVersionOnStream = 2;
667 * @serial This is the character used to represent 0.
669 private char zeroDigit;
672 * @serial The locale of these currency symbols.
674 private Locale locale;
677 * The currency used for the symbols in this instance.
678 * This is stored temporarily for efficiency reasons,
679 * as well as to ensure that the correct instance
680 * is restored from the currency code.
684 private transient Currency currency;
686 private static final long serialVersionUID = 5772796243397350300L;
688 private void readObject(ObjectInputStream stream)
689 throws IOException, ClassNotFoundException
691 stream.defaultReadObject();
692 if (serialVersionOnStream < 1)
694 monetarySeparator = decimalSeparator;
697 if (serialVersionOnStream < 2)
698 locale = Locale.getDefault();
700 serialVersionOnStream = 2;
704 * Returns a {@link DecimalFormatSymbols} instance for the
705 * default locale obtained from either the runtime itself
706 * or one of the installed
707 * {@link java.text.spi.DecimalFormatSymbolsProvider} instances.
708 * This is equivalent to calling
709 * <code>getInstance(Locale.getDefault())</code>.
711 * @return a {@link DecimalFormatSymbols} instance for the default
715 public static final DecimalFormatSymbols getInstance()
717 return getInstance(Locale.getDefault());
721 * Returns a {@link DecimalFormatSymbols} instance for the
722 * specified locale obtained from either the runtime itself
723 * or one of the installed
724 * {@link java.text.spi.DecimalFormatSymbolsProvider} instances.
726 * @param locale the locale for which an instance should be
728 * @return a {@link DecimalFormatSymbols} instance for the specified
730 * @throws NullPointerException if <code>locale</code> is
734 public static final DecimalFormatSymbols getInstance(Locale locale)
738 if (!locale.equals(Locale.ROOT))
739 ResourceBundle.getBundle("gnu.java.locale.LocaleInformation",
741 ClassLoader.getSystemClassLoader());
742 return new DecimalFormatSymbols(locale);
744 catch (MissingResourceException x)
746 /* This means runtime support for the locale
747 * is not available, so we check providers. */
749 for (DecimalFormatSymbolsProvider p :
750 ServiceLoader.load(DecimalFormatSymbolsProvider.class))
752 for (Locale loc : p.getAvailableLocales())
754 if (loc.equals(locale))
756 DecimalFormatSymbols syms = p.getInstance(locale);
763 return getInstance(LocaleHelper.getFallbackLocale(locale));