NumberFormat.java 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. /* NumberFormat.java -- Formats and parses numbers
  2. Copyright (C) 1998, 1999, 2000, 2001, 2003, 2004, 2007, 2012
  3. Free Software Foundation, Inc.
  4. This file is part of GNU Classpath.
  5. GNU Classpath is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU Classpath is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Classpath; see the file COPYING. If not, write to the
  15. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA.
  17. Linking this library statically or dynamically with other modules is
  18. making a combined work based on this library. Thus, the terms and
  19. conditions of the GNU General Public License cover the whole
  20. combination.
  21. As a special exception, the copyright holders of this library give you
  22. permission to link this library with independent modules to produce an
  23. executable, regardless of the license terms of these independent
  24. modules, and to copy and distribute the resulting executable under
  25. terms of your choice, provided that you also meet, for each linked
  26. independent module, the terms and conditions of the license of that
  27. module. An independent module is a module which is not derived from
  28. or based on this library. If you modify this library, you may extend
  29. this exception to your version of the library, but you are not
  30. obligated to do so. If you do not wish to do so, delete this
  31. exception statement from your version. */
  32. package java.text;
  33. import gnu.java.locale.LocaleHelper;
  34. import java.io.IOException;
  35. import java.io.InvalidObjectException;
  36. import java.io.ObjectInputStream;
  37. import java.io.ObjectOutputStream;
  38. import java.text.spi.NumberFormatProvider;
  39. import java.util.Currency;
  40. import java.util.Locale;
  41. import java.util.MissingResourceException;
  42. import java.util.ResourceBundle;
  43. import java.util.ServiceLoader;
  44. /**
  45. * This is the abstract superclass of all classes which format and
  46. * parse numeric values such as decimal numbers, integers, currency values,
  47. * and percentages. These classes perform their parsing and formatting
  48. * in a locale specific manner, accounting for such items as differing
  49. * currency symbols and thousands separators.
  50. * <p>
  51. * To create an instance of a concrete subclass of <code>NumberFormat</code>,
  52. * do not call a class constructor directly. Instead, use one of the
  53. * static factory methods in this class such as
  54. * <code>getCurrencyInstance</code>.
  55. *
  56. * @author Tom Tromey (tromey@cygnus.com)
  57. * @author Aaron M. Renn (arenn@urbanophile.com)
  58. * @date March 4, 1999
  59. */
  60. /* Written using "Java Class Libraries", 2nd edition, plus online
  61. * API docs for JDK 1.2 from http://www.javasoft.com.
  62. * Status: Believed complete and correct to 1.2, except getAvailableLocales.
  63. */
  64. public abstract class NumberFormat extends Format implements Cloneable
  65. {
  66. /**
  67. * This is a constant used to create a <code>FieldPosition</code> object
  68. * that will return the integer portion of a formatted number.
  69. */
  70. public static final int INTEGER_FIELD = 0;
  71. /**
  72. * This is a constant used to create a <code>FieldPosition</code> object
  73. * that will return the fractional portion of a formatted number.
  74. */
  75. public static final int FRACTION_FIELD = 1;
  76. public static class Field extends Format.Field
  77. {
  78. static final long serialVersionUID = 7494728892700160890L;
  79. /**
  80. * Attribute set to all characters containing digits of the integer
  81. * part.
  82. */
  83. public static final NumberFormat.Field INTEGER
  84. = new Field("integer");
  85. /**
  86. * Attribute set to all characters containing digits of the fractional
  87. * part.
  88. */
  89. public static final NumberFormat.Field FRACTION
  90. = new Field("fraction");
  91. /**
  92. * Attribute set to all characters containing digits of the exponential
  93. * part.
  94. */
  95. public static final NumberFormat.Field EXPONENT
  96. = new Field("exponent");
  97. /**
  98. * Attribute set to all characters containing a decimal separator.
  99. */
  100. public static final NumberFormat.Field DECIMAL_SEPARATOR
  101. = new Field("decimal separator");
  102. /**
  103. * Attribute set to all characters containing a sign (plus or minus).
  104. */
  105. public static final NumberFormat.Field SIGN
  106. = new Field("sign");
  107. /**
  108. * Attribute set to all characters containing a grouping separator (e.g.
  109. * a comma, a white space,...).
  110. */
  111. public static final NumberFormat.Field GROUPING_SEPARATOR
  112. = new Field("grouping separator");
  113. /**
  114. * Attribute set to all characters containing an exponential symbol (e.g.
  115. * 'E')
  116. */
  117. public static final NumberFormat.Field EXPONENT_SYMBOL
  118. = new Field("exponent symbol");
  119. /**
  120. * Attribute set to all characters containing a percent symbol (e.g. '%')
  121. */
  122. public static final NumberFormat.Field PERCENT
  123. = new Field("percent");
  124. /**
  125. * Attribute set to all characters containing a permille symbol.
  126. */
  127. public static final NumberFormat.Field PERMILLE
  128. = new Field("permille");
  129. /**
  130. * Attribute set to all characters containing the currency unit.
  131. */
  132. public static final NumberFormat.Field CURRENCY
  133. = new Field("currency");
  134. /**
  135. * Attribute set to all characters containing the exponent sign.
  136. */
  137. public static final NumberFormat.Field EXPONENT_SIGN
  138. = new Field("exponent sign");
  139. /**
  140. * Private fields to register all fields contained in this descriptor.
  141. */
  142. private static final NumberFormat.Field[] allFields =
  143. {
  144. INTEGER, FRACTION, EXPONENT, DECIMAL_SEPARATOR, SIGN,
  145. GROUPING_SEPARATOR, EXPONENT_SYMBOL, PERCENT,
  146. PERMILLE, CURRENCY, EXPONENT_SIGN
  147. };
  148. /**
  149. * This constructor is only used by the deserializer. Without it,
  150. * it would fail to construct a valid object.
  151. */
  152. private Field()
  153. {
  154. super("");
  155. }
  156. /**
  157. * Create a Field instance with the specified field name.
  158. *
  159. * @param field_name Field name for the new Field instance.
  160. */
  161. protected Field(String field_name)
  162. {
  163. super (field_name);
  164. }
  165. /**
  166. * This function is used by the deserializer to know which object
  167. * to use when it encounters an encoded NumberFormat.Field in a
  168. * serialization stream. If the stream is valid it should return
  169. * one of the above field. In the other case we throw an exception.
  170. *
  171. * @return a valid official NumberFormat.Field instance.
  172. *
  173. * @throws InvalidObjectException if the field name is invalid.
  174. */
  175. protected Object readResolve() throws InvalidObjectException
  176. {
  177. String s = getName();
  178. for (int i = 0; i < allFields.length; i++)
  179. if (s.equals(allFields[i].getName()))
  180. return allFields[i];
  181. throw new InvalidObjectException("no such NumberFormat field called "
  182. + s);
  183. }
  184. }
  185. /**
  186. * This method is a specialization of the format method that performs
  187. * a simple formatting of the specified <code>long</code> number.
  188. *
  189. * @param number The <code>long</code> to format.
  190. *
  191. * @return The formatted number
  192. */
  193. public final String format (long number)
  194. {
  195. StringBuffer sbuf = new StringBuffer(50);
  196. format (number, sbuf, new FieldPosition(0));
  197. return sbuf.toString();
  198. }
  199. /**
  200. * @specnote this method was final in releases before 1.5
  201. */
  202. public StringBuffer format (Object obj, StringBuffer sbuf,
  203. FieldPosition pos)
  204. {
  205. if (obj instanceof Number)
  206. return format(((Number) obj).doubleValue(), sbuf, pos);
  207. throw new
  208. IllegalArgumentException("Cannot format given Object as a Number");
  209. }
  210. /**
  211. * This method formats the specified <code>double</code> and appends it to
  212. * a <code>StringBuffer</code>.
  213. *
  214. * @param number The <code>double</code> to format.
  215. * @param sbuf The <code>StringBuffer</code> to append the formatted number
  216. * to.
  217. * @param pos The desired <code>FieldPosition</code>.
  218. *
  219. * @return The <code>StringBuffer</code> with the appended number.
  220. */
  221. public abstract StringBuffer format (double number,
  222. StringBuffer sbuf, FieldPosition pos);
  223. /**
  224. * This method formats the specified <code>long</code> and appends it to
  225. * a <code>StringBuffer</code>.
  226. *
  227. * @param number The <code>long</code> to format.
  228. * @param sbuf The <code>StringBuffer</code> to append the formatted number
  229. * to.
  230. * @param pos The desired <code>FieldPosition</code>.
  231. *
  232. * @return The <code>StringBuffer</code> with the appended number.
  233. */
  234. public abstract StringBuffer format (long number,
  235. StringBuffer sbuf, FieldPosition pos);
  236. /**
  237. * This method tests the specified object for equality against this object.
  238. * This will be <code>true</code> if the following conditions are met:
  239. * <p>
  240. * <ul>
  241. * <li>The specified object is not <code>null</code>.
  242. * <li>The specified object is an instance of <code>NumberFormat</code>.
  243. * </ul>
  244. * <p>
  245. * Since this method does not test much, it is highly advised that
  246. * concrete subclasses override this method.
  247. *
  248. * @param obj The <code>Object</code> to test against equality with
  249. * this object.
  250. *
  251. * @return <code>true</code> if the specified object is equal to
  252. * this object, <code>false</code> otherwise.
  253. */
  254. public boolean equals (Object obj)
  255. {
  256. if (! (obj instanceof NumberFormat))
  257. return false;
  258. NumberFormat nf = (NumberFormat) obj;
  259. return (groupingUsed == nf.groupingUsed
  260. && maximumFractionDigits == nf.maximumFractionDigits
  261. && maximumIntegerDigits == nf.maximumIntegerDigits
  262. && minimumFractionDigits == nf.minimumFractionDigits
  263. && minimumIntegerDigits == nf.minimumIntegerDigits
  264. && parseIntegerOnly == nf.parseIntegerOnly);
  265. }
  266. /**
  267. * This method returns a list of locales for which concrete instances
  268. * of <code>NumberFormat</code> subclasses may be created.
  269. *
  270. * @return The list of available locales.
  271. */
  272. public static Locale[] getAvailableLocales ()
  273. {
  274. Locale[] list = new Locale[1];
  275. list[0] = Locale.US;
  276. return list;
  277. }
  278. private static NumberFormat computeInstance(Locale loc, String resource,
  279. String def)
  280. throws MissingResourceException
  281. {
  282. if (loc.equals(Locale.ROOT))
  283. return new DecimalFormat(def, DecimalFormatSymbols.getInstance(loc));
  284. ResourceBundle res =
  285. ResourceBundle.getBundle("gnu.java.locale.LocaleInformation",
  286. loc, ClassLoader.getSystemClassLoader());
  287. String fmt;
  288. try
  289. {
  290. fmt = res == null ? def : res.getString(resource);
  291. }
  292. catch (MissingResourceException x)
  293. {
  294. fmt = def;
  295. }
  296. DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(loc);
  297. return new DecimalFormat (fmt, dfs);
  298. }
  299. /**
  300. * This method returns an instance of <code>NumberFormat</code> suitable
  301. * for formatting and parsing currency values in the default locale.
  302. *
  303. * @return An instance of <code>NumberFormat</code> for handling currencies.
  304. */
  305. public static final NumberFormat getCurrencyInstance ()
  306. {
  307. return getCurrencyInstance (Locale.getDefault());
  308. }
  309. /**
  310. * This method returns an instance of <code>NumberFormat</code> suitable
  311. * for formatting and parsing currency values in the specified locale.
  312. *
  313. * @return An instance of <code>NumberFormat</code> for handling currencies.
  314. */
  315. public static NumberFormat getCurrencyInstance (Locale loc)
  316. {
  317. try
  318. {
  319. NumberFormat format;
  320. format = computeInstance (loc, "currencyFormat",
  321. "\u00A4#,##0.00;(\u00A4#,##0.00)");
  322. format.setMaximumFractionDigits(format.getCurrency().getDefaultFractionDigits());
  323. return format;
  324. }
  325. catch (MissingResourceException e)
  326. {
  327. for (NumberFormatProvider p :
  328. ServiceLoader.load(NumberFormatProvider.class))
  329. {
  330. for (Locale l : p.getAvailableLocales())
  331. {
  332. if (l.equals(loc))
  333. {
  334. NumberFormat nf = p.getCurrencyInstance(loc);
  335. if (nf != null)
  336. return nf;
  337. break;
  338. }
  339. }
  340. }
  341. return getCurrencyInstance(LocaleHelper.getFallbackLocale(loc));
  342. }
  343. }
  344. /**
  345. * This method returns a default instance for the default locale. This
  346. * will be a concrete subclass of <code>NumberFormat</code>, but the
  347. * actual class returned is dependent on the locale.
  348. *
  349. * @return An instance of the default <code>NumberFormat</code> class.
  350. */
  351. public static final NumberFormat getInstance ()
  352. {
  353. return getInstance (Locale.getDefault());
  354. }
  355. /**
  356. * This method returns a default instance for the specified locale. This
  357. * will be a concrete subclass of <code>NumberFormat</code>, but the
  358. * actual class returned is dependent on the locale.
  359. *
  360. * @param loc The desired locale.
  361. *
  362. * @return An instance of the default <code>NumberFormat</code> class.
  363. */
  364. public static NumberFormat getInstance (Locale loc)
  365. {
  366. // For now always return a number instance.
  367. return getNumberInstance (loc);
  368. }
  369. /**
  370. * This method returns the maximum number of digits allowed in the fraction
  371. * portion of a number.
  372. *
  373. * @return The maximum number of digits allowed in the fraction
  374. * portion of a number.
  375. */
  376. public int getMaximumFractionDigits ()
  377. {
  378. return maximumFractionDigits;
  379. }
  380. /**
  381. * This method returns the maximum number of digits allowed in the integer
  382. * portion of a number.
  383. *
  384. * @return The maximum number of digits allowed in the integer
  385. * portion of a number.
  386. */
  387. public int getMaximumIntegerDigits ()
  388. {
  389. return maximumIntegerDigits;
  390. }
  391. /**
  392. * This method returns the minimum number of digits allowed in the fraction
  393. * portion of a number.
  394. *
  395. * @return The minimum number of digits allowed in the fraction
  396. * portion of a number.
  397. */
  398. public int getMinimumFractionDigits ()
  399. {
  400. return minimumFractionDigits;
  401. }
  402. /**
  403. * This method returns the minimum number of digits allowed in the integer
  404. * portion of a number.
  405. *
  406. * @return The minimum number of digits allowed in the integer
  407. * portion of a number.
  408. */
  409. public int getMinimumIntegerDigits ()
  410. {
  411. return minimumIntegerDigits;
  412. }
  413. /**
  414. * This method returns a default instance for the specified locale. This
  415. * will be a concrete subclass of <code>NumberFormat</code>, but the
  416. * actual class returned is dependent on the locale.
  417. *
  418. * @return An instance of the default <code>NumberFormat</code> class.
  419. */
  420. public static final NumberFormat getNumberInstance ()
  421. {
  422. return getNumberInstance (Locale.getDefault());
  423. }
  424. /**
  425. * This method returns a general purpose number formatting and parsing
  426. * class for the default locale. This will be a concrete subclass of
  427. * <code>NumberFormat</code>, but the actual class returned is dependent
  428. * on the locale.
  429. *
  430. * @return An instance of a generic number formatter for the default locale.
  431. */
  432. public static NumberFormat getNumberInstance (Locale loc)
  433. {
  434. try
  435. {
  436. return computeInstance (loc, "numberFormat", "#,##0.###");
  437. }
  438. catch (MissingResourceException e)
  439. {
  440. for (NumberFormatProvider p :
  441. ServiceLoader.load(NumberFormatProvider.class))
  442. {
  443. for (Locale l : p.getAvailableLocales())
  444. {
  445. if (l.equals(loc))
  446. {
  447. NumberFormat nf = p.getNumberInstance(loc);
  448. if (nf != null)
  449. return nf;
  450. break;
  451. }
  452. }
  453. }
  454. return getNumberInstance(LocaleHelper.getFallbackLocale(loc));
  455. }
  456. }
  457. /**
  458. * This method returns an integer formatting and parsing class for the
  459. * default locale. This will be a concrete subclass of <code>NumberFormat</code>,
  460. * but the actual class returned is dependent on the locale.
  461. *
  462. * @return An instance of an integer number formatter for the default locale.
  463. * @since 1.4
  464. */
  465. public static final NumberFormat getIntegerInstance()
  466. {
  467. return getIntegerInstance (Locale.getDefault());
  468. }
  469. /**
  470. * This method returns an integer formatting and parsing class for the
  471. * default locale. This will be a concrete subclass of <code>NumberFormat</code>,
  472. * but the actual class returned is dependent on the locale.
  473. *
  474. * @param locale the desired locale.
  475. *
  476. * @return An instance of an integer number formatter for the desired locale.
  477. * @since 1.4
  478. */
  479. public static NumberFormat getIntegerInstance(Locale locale)
  480. {
  481. try
  482. {
  483. NumberFormat format = computeInstance (locale,
  484. "integerFormat", "#,##0");
  485. format.setMaximumFractionDigits(0);
  486. format.setParseIntegerOnly (true);
  487. return format;
  488. }
  489. catch (MissingResourceException e)
  490. {
  491. for (NumberFormatProvider p :
  492. ServiceLoader.load(NumberFormatProvider.class))
  493. {
  494. for (Locale l : p.getAvailableLocales())
  495. {
  496. if (l.equals(locale))
  497. {
  498. NumberFormat nf = p.getIntegerInstance(locale);
  499. if (nf != null)
  500. return nf;
  501. break;
  502. }
  503. }
  504. }
  505. return getIntegerInstance(LocaleHelper.getFallbackLocale(locale));
  506. }
  507. }
  508. /**
  509. * This method returns an instance of <code>NumberFormat</code> suitable
  510. * for formatting and parsing percentage values in the default locale.
  511. *
  512. * @return An instance of <code>NumberFormat</code> for handling percentages.
  513. */
  514. public static final NumberFormat getPercentInstance ()
  515. {
  516. return getPercentInstance (Locale.getDefault());
  517. }
  518. /**
  519. * This method returns an instance of <code>NumberFormat</code> suitable
  520. * for formatting and parsing percentage values in the specified locale.
  521. *
  522. * @param loc The desired locale.
  523. *
  524. * @return An instance of <code>NumberFormat</code> for handling percentages.
  525. */
  526. public static NumberFormat getPercentInstance (Locale loc)
  527. {
  528. try
  529. {
  530. return computeInstance (loc, "percentFormat", "#,##0%");
  531. }
  532. catch (MissingResourceException e)
  533. {
  534. for (NumberFormatProvider p :
  535. ServiceLoader.load(NumberFormatProvider.class))
  536. {
  537. for (Locale l : p.getAvailableLocales())
  538. {
  539. if (l.equals(loc))
  540. {
  541. NumberFormat nf = p.getPercentInstance(loc);
  542. if (nf != null)
  543. return nf;
  544. break;
  545. }
  546. }
  547. }
  548. return getPercentInstance(LocaleHelper.getFallbackLocale(loc));
  549. }
  550. }
  551. /**
  552. * This method returns a hash value for this object.
  553. *
  554. * @return The hash code.
  555. */
  556. public int hashCode ()
  557. {
  558. int hash = super.hashCode();
  559. hash ^= (maximumFractionDigits + maximumIntegerDigits
  560. + minimumFractionDigits + minimumIntegerDigits);
  561. if (groupingUsed)
  562. hash ^= 0xf0f0;
  563. if (parseIntegerOnly)
  564. hash ^= 0x0f0f;
  565. return hash;
  566. }
  567. /**
  568. * This method tests whether or not grouping is in use. Grouping is
  569. * a method of marking separations in numbers, such as thousand separators
  570. * in the US English locale. The grouping positions and symbols are all
  571. * locale specific. As an example, with grouping disabled, the number one
  572. * million would appear as "1000000". With grouping enabled, this number
  573. * might appear as "1,000,000". (Both of these assume the US English
  574. * locale).
  575. *
  576. * @return <code>true</code> if grouping is enabled,
  577. * <code>false</code> otherwise.
  578. */
  579. public boolean isGroupingUsed ()
  580. {
  581. return groupingUsed;
  582. }
  583. /**
  584. * This method tests whether or not only integer values should be parsed.
  585. * If this class is parsing only integers, parsing stops at the decimal
  586. * point.
  587. *
  588. * @return <code>true</code> if only integers are parsed,
  589. * <code>false</code> otherwise.
  590. */
  591. public boolean isParseIntegerOnly ()
  592. {
  593. return parseIntegerOnly;
  594. }
  595. /**
  596. * This is a default constructor for use by subclasses.
  597. */
  598. protected NumberFormat ()
  599. {
  600. }
  601. /**
  602. * This method parses the specified string into a <code>Number</code>. This
  603. * will be a <code>Long</code> if possible, otherwise it will be a
  604. * <code>Double</code>. If no number can be parsed, no exception is
  605. * thrown. Instead, the parse position remains at its initial index.
  606. *
  607. * @param sourceStr The string to parse.
  608. * @param pos The desired <code>ParsePosition</code>.
  609. *
  610. * @return The parsed <code>Number</code>
  611. */
  612. public abstract Number parse (String sourceStr, ParsePosition pos);
  613. /**
  614. * This method parses the specified string into a <code>Number</code>. This
  615. * will be a <code>Long</code> if possible, otherwise it will be a
  616. * <code>Double</code>. If no number can be parsed, an exception will be
  617. * thrown.
  618. *
  619. * @param sourceStr The string to parse.
  620. *
  621. * @return The parsed <code>Number</code>
  622. *
  623. * @exception ParseException If no number can be parsed.
  624. */
  625. public Number parse (String sourceStr) throws ParseException
  626. {
  627. ParsePosition pp = new ParsePosition (0);
  628. Number r = parse (sourceStr, pp);
  629. if (r == null)
  630. {
  631. int index = pp.getErrorIndex();
  632. if (index < 0)
  633. index = pp.getIndex();
  634. throw new ParseException ("couldn't parse number", index);
  635. }
  636. return r;
  637. }
  638. /**
  639. * This method parses the specified string into an <code>Object</code>. This
  640. * will be a <code>Long</code> if possible, otherwise it will be a
  641. * <code>Double</code>. If no number can be parsed, no exception is
  642. * thrown. Instead, the parse position remains at its initial index.
  643. *
  644. * @param sourceStr The string to parse.
  645. * @param pos The desired <code>ParsePosition</code>.
  646. *
  647. * @return The parsed <code>Object</code>
  648. */
  649. public final Object parseObject (String sourceStr, ParsePosition pos)
  650. {
  651. return parse (sourceStr, pos);
  652. }
  653. /**
  654. * This method sets the grouping behavior of this formatter. Grouping is
  655. * a method of marking separations in numbers, such as thousand separators
  656. * in the US English locale. The grouping positions and symbols are all
  657. * locale specific. As an example, with grouping disabled, the number one
  658. * million would appear as "1000000". With grouping enabled, this number
  659. * might appear as "1,000,000". (Both of these assume the US English
  660. * locale).
  661. *
  662. * @param newValue <code>true</code> to enable grouping,
  663. * <code>false</code> to disable it.
  664. */
  665. public void setGroupingUsed (boolean newValue)
  666. {
  667. groupingUsed = newValue;
  668. }
  669. /**
  670. * This method sets the maximum number of digits allowed in the fraction
  671. * portion of a number to the specified value. If this is less than the
  672. * current minimum allowed digits, the minimum allowed digits value will
  673. * be lowered to be equal to the new maximum allowed digits value.
  674. *
  675. * @param digits The new maximum fraction digits value.
  676. */
  677. public void setMaximumFractionDigits (int digits)
  678. {
  679. maximumFractionDigits = digits;
  680. if (getMinimumFractionDigits () > maximumFractionDigits)
  681. setMinimumFractionDigits (maximumFractionDigits);
  682. }
  683. /**
  684. * This method sets the maximum number of digits allowed in the integer
  685. * portion of a number to the specified value. If this is less than the
  686. * current minimum allowed digits, the minimum allowed digits value will
  687. * be lowered to be equal to the new maximum allowed digits value.
  688. *
  689. * @param digits The new maximum integer digits value.
  690. */
  691. public void setMaximumIntegerDigits (int digits)
  692. {
  693. maximumIntegerDigits = digits;
  694. if (getMinimumIntegerDigits () > maximumIntegerDigits)
  695. setMinimumIntegerDigits (maximumIntegerDigits);
  696. }
  697. /**
  698. * This method sets the minimum number of digits allowed in the fraction
  699. * portion of a number to the specified value. If this is greater than the
  700. * current maximum allowed digits, the maximum allowed digits value will
  701. * be raised to be equal to the new minimum allowed digits value.
  702. *
  703. * @param digits The new minimum fraction digits value.
  704. */
  705. public void setMinimumFractionDigits (int digits)
  706. {
  707. minimumFractionDigits = digits;
  708. if (getMaximumFractionDigits () < minimumFractionDigits)
  709. setMaximumFractionDigits (minimumFractionDigits);
  710. }
  711. /**
  712. * This method sets the minimum number of digits allowed in the integer
  713. * portion of a number to the specified value. If this is greater than the
  714. * current maximum allowed digits, the maximum allowed digits value will
  715. * be raised to be equal to the new minimum allowed digits value.
  716. *
  717. * @param digits The new minimum integer digits value.
  718. */
  719. public void setMinimumIntegerDigits (int digits)
  720. {
  721. minimumIntegerDigits = digits;
  722. if (getMaximumIntegerDigits () < minimumIntegerDigits)
  723. setMaximumIntegerDigits (minimumIntegerDigits);
  724. }
  725. /**
  726. * This method sets the parsing behavior of this object to parse only
  727. * integers or not.
  728. *
  729. * @param value <code>true</code> to parse only integers,
  730. * <code>false</code> otherwise.
  731. */
  732. public void setParseIntegerOnly (boolean value)
  733. {
  734. parseIntegerOnly = value;
  735. }
  736. /**
  737. * This method is a specialization of the format method that performs
  738. * a simple formatting of the specified <code>double</code> number.
  739. *
  740. * @param number The <code>double</code> to format.
  741. *
  742. * @return The formatted number
  743. */
  744. public final String format (double number)
  745. {
  746. StringBuffer sbuf = new StringBuffer(50);
  747. FieldPosition position = new FieldPosition(0);
  748. format (number, sbuf, position);
  749. return sbuf.toString();
  750. }
  751. // These field names are fixed by the serialization spec.
  752. boolean groupingUsed;
  753. int maximumFractionDigits;
  754. private byte maxFractionDigits;
  755. int maximumIntegerDigits;
  756. private byte maxIntegerDigits;
  757. int minimumFractionDigits;
  758. private byte minFractionDigits;
  759. int minimumIntegerDigits;
  760. private byte minIntegerDigits;
  761. boolean parseIntegerOnly;
  762. private int serialVersionOnStream;
  763. private static final long serialVersionUID = -2308460125733713944L;
  764. private void readObject(ObjectInputStream stream)
  765. throws IOException, ClassNotFoundException
  766. {
  767. stream.defaultReadObject();
  768. if (serialVersionOnStream < 1)
  769. {
  770. maximumFractionDigits = maxFractionDigits;
  771. maximumIntegerDigits = maxIntegerDigits;
  772. minimumFractionDigits = minFractionDigits;
  773. minimumIntegerDigits = minIntegerDigits;
  774. serialVersionOnStream = 1;
  775. }
  776. }
  777. private void writeObject(ObjectOutputStream stream) throws IOException
  778. {
  779. maxFractionDigits = maximumFractionDigits < Byte.MAX_VALUE ?
  780. (byte) maximumFractionDigits : Byte.MAX_VALUE;
  781. maxIntegerDigits = maximumIntegerDigits < Byte.MAX_VALUE ?
  782. (byte) maximumIntegerDigits : Byte.MAX_VALUE;
  783. minFractionDigits = minimumFractionDigits < Byte.MAX_VALUE ?
  784. (byte) minimumFractionDigits : Byte.MAX_VALUE;
  785. minIntegerDigits = minimumIntegerDigits < Byte.MAX_VALUE ?
  786. (byte) minimumIntegerDigits : Byte.MAX_VALUE;
  787. serialVersionOnStream = 1;
  788. stream.defaultWriteObject();
  789. }
  790. /**
  791. * Returns the currency used by this number format when formatting currency
  792. * values.
  793. *
  794. * The default implementation throws UnsupportedOperationException.
  795. *
  796. * @return The used currency object, or null.
  797. *
  798. * @throws UnsupportedOperationException If the number format class doesn't
  799. * implement currency formatting.
  800. *
  801. * @since 1.4
  802. */
  803. public Currency getCurrency()
  804. {
  805. throw new UnsupportedOperationException();
  806. }
  807. /**
  808. * Sets the currency used by this number format when formatting currency
  809. * values.
  810. *
  811. * The default implementation throws UnsupportedOperationException.
  812. *
  813. * @param currency The new currency to be used by this number format.
  814. *
  815. * @throws NullPointerException If currenc is null.
  816. * @throws UnsupportedOperationException If the number format class doesn't
  817. * implement currency formatting.
  818. *
  819. * @since 1.4
  820. */
  821. public void setCurrency(Currency currency)
  822. {
  823. if (currency == null)
  824. throw new NullPointerException("currency may not be null");
  825. throw new UnsupportedOperationException();
  826. }
  827. }