DecimalFormatSymbols.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. /* DecimalFormatSymbols.java -- Format symbols used by DecimalFormat
  2. Copyright (C) 1999, 2000, 2001, 2004, 2007 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.text;
  32. import gnu.java.locale.LocaleHelper;
  33. import java.io.IOException;
  34. import java.io.ObjectInputStream;
  35. import java.io.Serializable;
  36. import java.text.spi.DecimalFormatSymbolsProvider;
  37. import java.util.Currency;
  38. import java.util.Locale;
  39. import java.util.MissingResourceException;
  40. import java.util.ResourceBundle;
  41. import java.util.ServiceLoader;
  42. /**
  43. * This class is a container for the symbols used by
  44. * <code>DecimalFormat</code> to format numbers and currency
  45. * for a particular locale. These are
  46. * normally handled automatically, but an application can override
  47. * values as desired using this class.
  48. *
  49. * @author Tom Tromey (tromey@cygnus.com)
  50. * @author Aaron M. Renn (arenn@urbanophile.com)
  51. * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  52. * @date February 24, 1999
  53. * @see java.text.DecimalFormat
  54. */
  55. /* Written using "Java Class Libraries", 2nd edition, plus online
  56. * API docs for JDK 1.2 from http://www.javasoft.com.
  57. * Status: Believed complete and correct to 1.2.
  58. */
  59. public class DecimalFormatSymbols implements Cloneable, Serializable
  60. {
  61. public Object clone ()
  62. {
  63. try
  64. {
  65. return super.clone();
  66. }
  67. catch(CloneNotSupportedException e)
  68. {
  69. return null;
  70. }
  71. }
  72. /**
  73. * This method initializes a new instance of
  74. * <code>DecimalFormatSymbols</code> for the default locale.
  75. * This constructor only obtains instances using the runtime's resources;
  76. * to also include {@link java.text.spi.DateFormatSymbolsProvider} instances,
  77. * call {@link #getInstance()} instead.
  78. *
  79. * @see #getInstance()
  80. */
  81. public DecimalFormatSymbols ()
  82. {
  83. this (Locale.getDefault());
  84. }
  85. /**
  86. * Retrieves a valid string, either using the supplied resource
  87. * bundle or the default value.
  88. *
  89. * @param bundle the resource bundle to use to find the string.
  90. * @param name key for the string in the resource bundle.
  91. * @param def default value for the string.
  92. */
  93. private String safeGetString(ResourceBundle bundle,
  94. String name, String def)
  95. {
  96. if (bundle != null)
  97. {
  98. try
  99. {
  100. return bundle.getString(name);
  101. }
  102. catch (MissingResourceException x)
  103. {
  104. }
  105. }
  106. return def;
  107. }
  108. private char safeGetChar(ResourceBundle bundle,
  109. String name, char def)
  110. {
  111. String r = null;
  112. if (bundle != null)
  113. {
  114. try
  115. {
  116. r = bundle.getString(name);
  117. }
  118. catch (MissingResourceException x)
  119. {
  120. }
  121. }
  122. if (r == null || r.length() < 1)
  123. return def;
  124. return r.charAt(0);
  125. }
  126. /**
  127. * This method initializes a new instance of
  128. * <code>DecimalFormatSymbols</code> for the specified locale.
  129. * <strong>Note</strong>: if the locale does not have an associated
  130. * <code>Currency</code> instance, the currency symbol and
  131. * international currency symbol will be set to the strings "?"
  132. * and "XXX" respectively. This generally happens with language
  133. * locales (those with no specified country), such as
  134. * <code>Locale.ENGLISH</code>. This constructor only obtains
  135. * instances using the runtime's resources; to also include
  136. * {@link java.text.spi.DecimalFormatSymbolsProvider} instances,
  137. * call {@link #getInstance(java.util.Locale)} instead.
  138. *
  139. * @param loc The local to load symbols for.
  140. * @throws NullPointerException if the locale is null.
  141. * @see #getInstance(java.util.Locale)
  142. */
  143. public DecimalFormatSymbols (Locale loc)
  144. {
  145. ResourceBundle res;
  146. try
  147. {
  148. res = ResourceBundle.getBundle("gnu.java.locale.LocaleInformation",
  149. loc, ClassLoader.getSystemClassLoader());
  150. }
  151. catch (MissingResourceException x)
  152. {
  153. res = null;
  154. }
  155. locale = loc;
  156. currency = Currency.getInstance("XXX");
  157. currencySymbol = "?";
  158. intlCurrencySymbol = "XXX";
  159. try
  160. {
  161. Currency localeCurrency = Currency.getInstance(loc);
  162. if (localeCurrency != null)
  163. {
  164. setCurrency(localeCurrency);
  165. }
  166. }
  167. catch(IllegalArgumentException exception)
  168. {
  169. /* Locale has an invalid currency */
  170. }
  171. decimalSeparator = safeGetChar (res, "decimalSeparator", '.');
  172. digit = safeGetChar (res, "digit", '#');
  173. exponential = safeGetChar (res, "exponential", 'E');
  174. groupingSeparator = safeGetChar (res, "groupingSeparator", ',');
  175. infinity = safeGetString (res, "infinity", "\u221e");
  176. try
  177. {
  178. monetarySeparator = safeGetChar (res, "monetarySeparator", '.');
  179. }
  180. catch (MissingResourceException x)
  181. {
  182. monetarySeparator = decimalSeparator;
  183. }
  184. minusSign = safeGetChar (res, "minusSign", '-');
  185. NaN = safeGetString (res, "NaN", "\ufffd");
  186. patternSeparator = safeGetChar (res, "patternSeparator", ';');
  187. percent = safeGetChar (res, "percent", '%');
  188. perMill = safeGetChar (res, "perMill", '\u2030');
  189. zeroDigit = safeGetChar (res, "zeroDigit", '0');
  190. }
  191. /**
  192. * This method this this object for equality against the specified object.
  193. * This will be true if and only if the following criteria are met with
  194. * regard to the specified object:
  195. * <p>
  196. * <ul>
  197. * <li>It is not <code>null</code>.</li>
  198. * <li>It is an instance of <code>DecimalFormatSymbols</code>.</li>
  199. * <li>All of its symbols are identical to the symbols in this object.</li>
  200. * </ul>
  201. *
  202. * @return <code>true</code> if the specified object is equal to this
  203. * object, <code>false</code> otherwise.
  204. */
  205. public boolean equals (Object obj)
  206. {
  207. if (! (obj instanceof DecimalFormatSymbols))
  208. return false;
  209. DecimalFormatSymbols dfs = (DecimalFormatSymbols) obj;
  210. return (currencySymbol.equals(dfs.currencySymbol)
  211. && decimalSeparator == dfs.decimalSeparator
  212. && digit == dfs.digit
  213. && exponential == dfs.exponential
  214. && groupingSeparator == dfs.groupingSeparator
  215. && infinity.equals(dfs.infinity)
  216. && intlCurrencySymbol.equals(dfs.intlCurrencySymbol)
  217. && minusSign == dfs.minusSign
  218. && monetarySeparator == dfs.monetarySeparator
  219. && NaN.equals(dfs.NaN)
  220. && patternSeparator == dfs.patternSeparator
  221. && percent == dfs.percent
  222. && perMill == dfs.perMill
  223. && zeroDigit == dfs.zeroDigit);
  224. }
  225. /**
  226. * Returns the currency corresponding to the currency symbol stored
  227. * in this instance of <code>DecimalFormatSymbols</code>.
  228. *
  229. * @return An instance of <code>Currency</code> which matches
  230. * the currency used, or null if there is no corresponding
  231. * instance.
  232. */
  233. public Currency getCurrency ()
  234. {
  235. return currency;
  236. }
  237. /**
  238. * This method returns the currency symbol in local format. For example,
  239. * "$" for Canadian dollars.
  240. *
  241. * @return The currency symbol in local format.
  242. */
  243. public String getCurrencySymbol ()
  244. {
  245. return currencySymbol;
  246. }
  247. /**
  248. * This method returns the character used as the decimal point.
  249. *
  250. * @return The character used as the decimal point.
  251. */
  252. public char getDecimalSeparator ()
  253. {
  254. return decimalSeparator;
  255. }
  256. /**
  257. * This method returns the character used to represent a digit in a
  258. * format pattern string.
  259. *
  260. * @return The character used to represent a digit in a format
  261. * pattern string.
  262. */
  263. public char getDigit ()
  264. {
  265. return digit;
  266. }
  267. /**
  268. * This method returns the character used to represent the exponential
  269. * format. This is a GNU Classpath extension.
  270. *
  271. * @return the character used to represent an exponential in a format
  272. * pattern string.
  273. */
  274. char getExponential ()
  275. {
  276. return exponential;
  277. }
  278. /**
  279. * This method sets the character used to separate groups of digits. For
  280. * example, the United States uses a comma (,) to separate thousands in
  281. * a number.
  282. *
  283. * @return The character used to separate groups of digits.
  284. */
  285. public char getGroupingSeparator ()
  286. {
  287. return groupingSeparator;
  288. }
  289. /**
  290. * This method returns the character used to represent infinity.
  291. *
  292. * @return The character used to represent infinity.
  293. */
  294. public String getInfinity ()
  295. {
  296. return infinity;
  297. }
  298. /**
  299. * This method returns the ISO 4217 currency code for
  300. * the currency used.
  301. *
  302. * @return the ISO 4217 currency code.
  303. */
  304. public String getInternationalCurrencySymbol ()
  305. {
  306. return intlCurrencySymbol;
  307. }
  308. /**
  309. * This method returns the character used to represent the minus sign.
  310. *
  311. * @return The character used to represent the minus sign.
  312. */
  313. public char getMinusSign ()
  314. {
  315. return minusSign;
  316. }
  317. /**
  318. * This method returns the character used to represent the decimal
  319. * point for currency values.
  320. *
  321. * @return The decimal point character used in currency values.
  322. */
  323. public char getMonetaryDecimalSeparator ()
  324. {
  325. return monetarySeparator;
  326. }
  327. /**
  328. * This method returns the string used to represent the NaN (not a number)
  329. * value.
  330. *
  331. * @return The string used to represent NaN
  332. */
  333. public String getNaN ()
  334. {
  335. return NaN;
  336. }
  337. /**
  338. * This method returns the character used to separate positive and negative
  339. * subpatterns in a format pattern.
  340. *
  341. * @return The character used to separate positive and negative subpatterns
  342. * in a format pattern.
  343. */
  344. public char getPatternSeparator ()
  345. {
  346. return patternSeparator;
  347. }
  348. /**
  349. * This method returns the character used as the percent sign.
  350. *
  351. * @return The character used as the percent sign.
  352. */
  353. public char getPercent ()
  354. {
  355. return percent;
  356. }
  357. /**
  358. * This method returns the character used as the per mille character.
  359. *
  360. * @return The per mille character.
  361. */
  362. public char getPerMill ()
  363. {
  364. return perMill;
  365. }
  366. /**
  367. * This method returns the character used to represent the digit zero.
  368. *
  369. * @return The character used to represent the digit zero.
  370. */
  371. public char getZeroDigit ()
  372. {
  373. return zeroDigit;
  374. }
  375. /**
  376. * This method returns a hash value for this object.
  377. *
  378. * @return A hash value for this object.
  379. */
  380. public int hashCode ()
  381. {
  382. // Compute based on zero digit, grouping separator, and decimal
  383. // separator -- JCL book. This probably isn't a very good hash
  384. // code.
  385. return zeroDigit << 16 + groupingSeparator << 8 + decimalSeparator;
  386. }
  387. /**
  388. * This method sets the currency symbol and ISO 4217 currency
  389. * code to the values obtained from the supplied currency.
  390. *
  391. * @param currency the currency from which to obtain the values.
  392. * @throws NullPointerException if the currency is null.
  393. */
  394. public void setCurrency (Currency currency)
  395. {
  396. intlCurrencySymbol = currency.getCurrencyCode();
  397. currencySymbol = currency.getSymbol(locale);
  398. this.currency = currency;
  399. }
  400. /**
  401. * This method sets the currency symbol to the specified value.
  402. *
  403. * @param currency The new currency symbol
  404. */
  405. public void setCurrencySymbol (String currency)
  406. {
  407. currencySymbol = currency;
  408. }
  409. /**
  410. * This method sets the decimal point character to the specified value.
  411. *
  412. * @param decimalSep The new decimal point character
  413. */
  414. public void setDecimalSeparator (char decimalSep)
  415. {
  416. decimalSeparator = decimalSep;
  417. }
  418. /**
  419. * This method sets the character used to represents a digit in a format
  420. * string to the specified value.
  421. *
  422. * @param digit The character used to represent a digit in a format pattern.
  423. */
  424. public void setDigit (char digit)
  425. {
  426. this.digit = digit;
  427. }
  428. /**
  429. * This method sets the exponential character used in the format string to
  430. * the specified value. This is a GNU Classpath extension.
  431. *
  432. * @param exp the character used for the exponential in a format pattern.
  433. */
  434. void setExponential (char exp)
  435. {
  436. exponential = exp;
  437. }
  438. /**
  439. * This method sets the character used to separate groups of digits.
  440. *
  441. * @param groupSep The character used to separate groups of digits.
  442. */
  443. public void setGroupingSeparator (char groupSep)
  444. {
  445. groupingSeparator = groupSep;
  446. }
  447. /**
  448. * This method sets the string used to represents infinity.
  449. *
  450. * @param infinity The string used to represent infinity.
  451. */
  452. public void setInfinity (String infinity)
  453. {
  454. this.infinity = infinity;
  455. }
  456. /**
  457. * This method sets the international currency symbol to the
  458. * specified value. If a valid <code>Currency</code> instance
  459. * exists for the international currency code, then this is
  460. * used for the currency attribute, and the currency symbol
  461. * is set to the corresponding value from this instance.
  462. * Otherwise, the currency attribute is set to null and the
  463. * symbol is left unmodified.
  464. *
  465. * @param currencyCode The new international currency symbol.
  466. */
  467. public void setInternationalCurrencySymbol (String currencyCode)
  468. {
  469. intlCurrencySymbol = currencyCode;
  470. try
  471. {
  472. currency = Currency.getInstance(currencyCode);
  473. }
  474. catch (IllegalArgumentException exception)
  475. {
  476. currency = null;
  477. }
  478. if (currency != null)
  479. {
  480. setCurrencySymbol(currency.getSymbol(locale));
  481. }
  482. }
  483. /**
  484. * This method sets the character used to represent the minus sign.
  485. *
  486. * @param minusSign The character used to represent the minus sign.
  487. */
  488. public void setMinusSign (char minusSign)
  489. {
  490. this.minusSign = minusSign;
  491. }
  492. /**
  493. * This method sets the character used for the decimal point in currency
  494. * values.
  495. *
  496. * @param decimalSep The decimal point character used in currency values.
  497. */
  498. public void setMonetaryDecimalSeparator (char decimalSep)
  499. {
  500. monetarySeparator = decimalSep;
  501. }
  502. /**
  503. * This method sets the string used to represent the NaN (not a
  504. * number) value.
  505. *
  506. * @param nan The string used to represent NaN
  507. */
  508. public void setNaN (String nan)
  509. {
  510. NaN = nan;
  511. }
  512. /**
  513. * This method sets the character used to separate positive and negative
  514. * subpatterns in a format pattern.
  515. *
  516. * @param patternSep The character used to separate positive and
  517. * negative subpatterns in a format pattern.
  518. */
  519. public void setPatternSeparator (char patternSep)
  520. {
  521. patternSeparator = patternSep;
  522. }
  523. /**
  524. * This method sets the character used as the percent sign.
  525. *
  526. * @param percent The character used as the percent sign.
  527. */
  528. public void setPercent (char percent)
  529. {
  530. this.percent = percent;
  531. }
  532. /**
  533. * This method sets the character used as the per mille character.
  534. *
  535. * @param perMill The per mille character.
  536. */
  537. public void setPerMill (char perMill)
  538. {
  539. this.perMill = perMill;
  540. }
  541. /**
  542. * This method sets the character used to represent the digit zero.
  543. *
  544. * @param zeroDigit The character used to represent the digit zero.
  545. */
  546. public void setZeroDigit (char zeroDigit)
  547. {
  548. this.zeroDigit = zeroDigit;
  549. }
  550. /**
  551. * @serial A string used for the local currency
  552. */
  553. private String currencySymbol;
  554. /**
  555. * @serial The <code>char</code> used to separate decimals in a number.
  556. */
  557. private char decimalSeparator;
  558. /**
  559. * @serial This is the <code>char</code> used to represent a digit in
  560. * a format specification.
  561. */
  562. private char digit;
  563. /**
  564. * @serial This is the <code>char</code> used to represent the exponent
  565. * separator in exponential notation.
  566. */
  567. private char exponential;
  568. /**
  569. * @serial This separates groups of thousands in numbers.
  570. */
  571. private char groupingSeparator;
  572. /**
  573. * @serial This string represents infinity.
  574. */
  575. private String infinity;
  576. /**
  577. * @serial This string represents the local currency in an international
  578. * context, eg, "C$" for Canadian dollars.
  579. */
  580. private String intlCurrencySymbol;
  581. /**
  582. * @serial This is the character used to represent the minus sign.
  583. */
  584. private char minusSign;
  585. /**
  586. * @serial This character is used to separate decimals when formatting
  587. * currency values.
  588. */
  589. private char monetarySeparator;
  590. /**
  591. * @serial This string is used the represent the Java NaN value for
  592. * "not a number".
  593. */
  594. private String NaN;
  595. /**
  596. * @serial This is the character used to separate positive and negative
  597. * subpatterns in a format pattern.
  598. */
  599. private char patternSeparator;
  600. /**
  601. * @serial This is the percent symbols
  602. */
  603. private char percent;
  604. /**
  605. * @serial This character is used for the mille percent sign.
  606. */
  607. private char perMill;
  608. /**
  609. * @serial This value represents the type of object being de-serialized.
  610. * 0 indicates a pre-Java 1.1.6 version, 1 indicates 1.1.6 or later.
  611. * 0 indicates a pre-Java 1.1.6 version, 1 indicates 1.1.6 or later,
  612. * 2 indicates 1.4 or later
  613. */
  614. private int serialVersionOnStream = 2;
  615. /**
  616. * @serial This is the character used to represent 0.
  617. */
  618. private char zeroDigit;
  619. /**
  620. * @serial The locale of these currency symbols.
  621. */
  622. private Locale locale;
  623. /**
  624. * The currency used for the symbols in this instance.
  625. * This is stored temporarily for efficiency reasons,
  626. * as well as to ensure that the correct instance
  627. * is restored from the currency code.
  628. *
  629. * @serial Ignored.
  630. */
  631. private transient Currency currency;
  632. private static final long serialVersionUID = 5772796243397350300L;
  633. private void readObject(ObjectInputStream stream)
  634. throws IOException, ClassNotFoundException
  635. {
  636. stream.defaultReadObject();
  637. if (serialVersionOnStream < 1)
  638. {
  639. monetarySeparator = decimalSeparator;
  640. exponential = 'E';
  641. }
  642. if (serialVersionOnStream < 2)
  643. locale = Locale.getDefault();
  644. serialVersionOnStream = 2;
  645. }
  646. /**
  647. * Returns a {@link DecimalFormatSymbols} instance for the
  648. * default locale obtained from either the runtime itself
  649. * or one of the installed
  650. * {@link java.text.spi.DecimalFormatSymbolsProvider} instances.
  651. * This is equivalent to calling
  652. * <code>getInstance(Locale.getDefault())</code>.
  653. *
  654. * @return a {@link DecimalFormatSymbols} instance for the default
  655. * locale.
  656. * @since 1.6
  657. */
  658. public static final DecimalFormatSymbols getInstance()
  659. {
  660. return getInstance(Locale.getDefault());
  661. }
  662. /**
  663. * Returns a {@link DecimalFormatSymbols} instance for the
  664. * specified locale obtained from either the runtime itself
  665. * or one of the installed
  666. * {@link java.text.spi.DecimalFormatSymbolsProvider} instances.
  667. *
  668. * @param locale the locale for which an instance should be
  669. * returned.
  670. * @return a {@link DecimalFormatSymbols} instance for the specified
  671. * locale.
  672. * @throws NullPointerException if <code>locale</code> is
  673. * <code>null</code>.
  674. * @since 1.6
  675. */
  676. public static final DecimalFormatSymbols getInstance(Locale locale)
  677. {
  678. try
  679. {
  680. if (!locale.equals(Locale.ROOT))
  681. ResourceBundle.getBundle("gnu.java.locale.LocaleInformation",
  682. locale,
  683. ClassLoader.getSystemClassLoader());
  684. return new DecimalFormatSymbols(locale);
  685. }
  686. catch (MissingResourceException x)
  687. {
  688. /* This means runtime support for the locale
  689. * is not available, so we check providers. */
  690. }
  691. for (DecimalFormatSymbolsProvider p :
  692. ServiceLoader.load(DecimalFormatSymbolsProvider.class))
  693. {
  694. for (Locale loc : p.getAvailableLocales())
  695. {
  696. if (loc.equals(locale))
  697. {
  698. DecimalFormatSymbols syms = p.getInstance(locale);
  699. if (syms != null)
  700. return syms;
  701. break;
  702. }
  703. }
  704. }
  705. return getInstance(LocaleHelper.getFallbackLocale(locale));
  706. }
  707. }