< prev index next >

src/java.base/share/classes/java/text/NumberFormat.java

Print this page
rev 56290 : 8230648: Replace @exception tag with @throws in java.base
Summary: Minor coding style update of javadoc tag in any file in java.base
Reviewed-by: prappo, lancea


 242      * including <code>BigInteger</code> values with a
 243      * {@link java.math.BigInteger#bitLength() bit length} of less than 64,
 244      * and {@link java.lang.Number#doubleValue()} for all other types. It
 245      * then calls
 246      * {@link #format(long,java.lang.StringBuffer,java.text.FieldPosition)}
 247      * or {@link #format(double,java.lang.StringBuffer,java.text.FieldPosition)}.
 248      * This may result in loss of magnitude information and precision for
 249      * <code>BigInteger</code> and <code>BigDecimal</code> values.
 250      * @param number     the number to format
 251      * @param toAppendTo the <code>StringBuffer</code> to which the formatted
 252      *                   text is to be appended
 253      * @param pos        keeps track on the position of the field within the
 254      *                   returned string. For example, for formatting a number
 255      *                   {@code 1234567.89} in {@code Locale.US} locale,
 256      *                   if the given {@code fieldPosition} is
 257      *                   {@link NumberFormat#INTEGER_FIELD}, the begin index
 258      *                   and end index of {@code fieldPosition} will be set
 259      *                   to 0 and 9, respectively for the output string
 260      *                   {@code 1,234,567.89}.
 261      * @return           the value passed in as <code>toAppendTo</code>
 262      * @exception        IllegalArgumentException if <code>number</code> is
 263      *                   null or not an instance of <code>Number</code>.
 264      * @exception        NullPointerException if <code>toAppendTo</code> or
 265      *                   <code>pos</code> is null
 266      * @exception        ArithmeticException if rounding is needed with rounding
 267      *                   mode being set to RoundingMode.UNNECESSARY
 268      * @see              java.text.FieldPosition
 269      */
 270     @Override
 271     public StringBuffer format(Object number,
 272                                StringBuffer toAppendTo,
 273                                FieldPosition pos) {
 274         if (number instanceof Long || number instanceof Integer ||
 275             number instanceof Short || number instanceof Byte ||
 276             number instanceof AtomicInteger || number instanceof AtomicLong ||
 277             (number instanceof BigInteger &&
 278              ((BigInteger)number).bitLength() < 64)) {
 279             return format(((Number)number).longValue(), toAppendTo, pos);
 280         } else if (number instanceof Number) {
 281             return format(((Number)number).doubleValue(), toAppendTo, pos);
 282         } else {
 283             throw new IllegalArgumentException("Cannot format given Object as a Number");
 284         }
 285     }
 286 


 301      * See the {@link #parse(String, ParsePosition)} method for more information
 302      * on number parsing.
 303      *
 304      * @param source A <code>String</code>, part of which should be parsed.
 305      * @param pos A <code>ParsePosition</code> object with index and error
 306      *            index information as described above.
 307      * @return A <code>Number</code> parsed from the string. In case of
 308      *         error, returns null.
 309      * @throws NullPointerException if {@code source} or {@code pos} is null.
 310      */
 311     @Override
 312     public final Object parseObject(String source, ParsePosition pos) {
 313         return parse(source, pos);
 314     }
 315 
 316    /**
 317      * Specialization of format.
 318      *
 319      * @param number the double number to format
 320      * @return the formatted String
 321      * @exception        ArithmeticException if rounding is needed with rounding
 322      *                   mode being set to RoundingMode.UNNECESSARY
 323      * @see java.text.Format#format
 324      */
 325     public final String format(double number) {
 326         // Use fast-path for double result if that works
 327         String result = fastFormat(number);
 328         if (result != null)
 329             return result;
 330 
 331         return format(number, new StringBuffer(),
 332                       DontCareFieldPosition.INSTANCE).toString();
 333     }
 334 
 335     /*
 336      * fastFormat() is supposed to be implemented in concrete subclasses only.
 337      * Default implem always returns null.
 338      */
 339     String fastFormat(double number) { return null; }
 340 
 341    /**
 342      * Specialization of format.
 343      *
 344      * @param number the long number to format
 345      * @return the formatted String
 346      * @exception        ArithmeticException if rounding is needed with rounding
 347      *                   mode being set to RoundingMode.UNNECESSARY
 348      * @see java.text.Format#format
 349      */
 350     public final String format(long number) {
 351         return format(number, new StringBuffer(),
 352                       DontCareFieldPosition.INSTANCE).toString();
 353     }
 354 
 355    /**
 356      * Specialization of format.
 357      *
 358      * @param number     the double number to format
 359      * @param toAppendTo the StringBuffer to which the formatted text is to be
 360      *                   appended
 361      * @param pos        keeps track on the position of the field within the
 362      *                   returned string. For example, for formatting a number
 363      *                   {@code 1234567.89} in {@code Locale.US} locale,
 364      *                   if the given {@code fieldPosition} is
 365      *                   {@link NumberFormat#INTEGER_FIELD}, the begin index
 366      *                   and end index of {@code fieldPosition} will be set
 367      *                   to 0 and 9, respectively for the output string
 368      *                   {@code 1,234,567.89}.
 369      * @return the formatted StringBuffer
 370      * @exception        ArithmeticException if rounding is needed with rounding
 371      *                   mode being set to RoundingMode.UNNECESSARY
 372      * @see java.text.Format#format
 373      */
 374     public abstract StringBuffer format(double number,
 375                                         StringBuffer toAppendTo,
 376                                         FieldPosition pos);
 377 
 378    /**
 379      * Specialization of format.
 380      *
 381      * @param number     the long number to format
 382      * @param toAppendTo the StringBuffer to which the formatted text is to be
 383      *                   appended
 384      * @param pos        keeps track on the position of the field within the
 385      *                   returned string. For example, for formatting a number
 386      *                   {@code 123456789} in {@code Locale.US} locale,
 387      *                   if the given {@code fieldPosition} is
 388      *                   {@link NumberFormat#INTEGER_FIELD}, the begin index
 389      *                   and end index of {@code fieldPosition} will be set
 390      *                   to 0 and 11, respectively for the output string
 391      *                   {@code 123,456,789}.
 392      * @return the formatted StringBuffer
 393      * @exception        ArithmeticException if rounding is needed with rounding
 394      *                   mode being set to RoundingMode.UNNECESSARY
 395      * @see java.text.Format#format
 396      */
 397     public abstract StringBuffer format(long number,
 398                                         StringBuffer toAppendTo,
 399                                         FieldPosition pos);
 400 
 401    /**
 402      * Returns a Long if possible (e.g., within the range [Long.MIN_VALUE,
 403      * Long.MAX_VALUE] and with no decimals), otherwise a Double.
 404      * If IntegerOnly is set, will stop at a decimal
 405      * point (or equivalent; e.g., for rational numbers "1 2/3", will stop
 406      * after the 1).
 407      * Does not throw an exception; if no object can be parsed, index is
 408      * unchanged!
 409      *
 410      * @param source the String to parse
 411      * @param parsePosition the parse position
 412      * @return the parsed value
 413      * @see java.text.NumberFormat#isParseIntegerOnly
 414      * @see java.text.Format#parseObject
 415      */
 416     public abstract Number parse(String source, ParsePosition parsePosition);
 417 
 418     /**
 419      * Parses text from the beginning of the given string to produce a number.
 420      * The method may not use the entire text of the given string.
 421      * <p>
 422      * See the {@link #parse(String, ParsePosition)} method for more information
 423      * on number parsing.
 424      *
 425      * @param source A <code>String</code> whose beginning should be parsed.
 426      * @return A <code>Number</code> parsed from the string.
 427      * @exception ParseException if the beginning of the specified string
 428      *            cannot be parsed.
 429      */
 430     public Number parse(String source) throws ParseException {
 431         ParsePosition parsePosition = new ParsePosition(0);
 432         Number result = parse(source, parsePosition);
 433         if (parsePosition.index == 0) {
 434             throw new ParseException("Unparseable number: \"" + source + "\"",
 435                                      parsePosition.errorIndex);
 436         }
 437         return result;
 438     }
 439 
 440     /**
 441      * Returns true if this format will parse numbers as integers only.
 442      * For example in the English locale, with ParseIntegerOnly true, the
 443      * string "1234." would be parsed as the integer value 1234 and parsing
 444      * would stop at the "." character.  Of course, the exact format accepted
 445      * by the parse operation is locale dependent and determined by sub-classes
 446      * of NumberFormat.
 447      *


 874      * @see #getMinimumFractionDigits
 875      */
 876     public void setMinimumFractionDigits(int newValue) {
 877         minimumFractionDigits = Math.max(0,newValue);
 878         if (maximumFractionDigits < minimumFractionDigits) {
 879             maximumFractionDigits = minimumFractionDigits;
 880         }
 881     }
 882 
 883     /**
 884      * Gets the currency used by this number format when formatting
 885      * currency values. The initial value is derived in a locale dependent
 886      * way. The returned value may be null if no valid
 887      * currency could be determined and no currency has been set using
 888      * {@link #setCurrency(java.util.Currency) setCurrency}.
 889      * <p>
 890      * The default implementation throws
 891      * <code>UnsupportedOperationException</code>.
 892      *
 893      * @return the currency used by this number format, or <code>null</code>
 894      * @exception UnsupportedOperationException if the number format class
 895      * doesn't implement currency formatting
 896      * @since 1.4
 897      */
 898     public Currency getCurrency() {
 899         throw new UnsupportedOperationException();
 900     }
 901 
 902     /**
 903      * Sets the currency used by this number format when formatting
 904      * currency values. This does not update the minimum or maximum
 905      * number of fraction digits used by the number format.
 906      * <p>
 907      * The default implementation throws
 908      * <code>UnsupportedOperationException</code>.
 909      *
 910      * @param currency the new currency to be used by this number format
 911      * @exception UnsupportedOperationException if the number format class
 912      * doesn't implement currency formatting
 913      * @exception NullPointerException if <code>currency</code> is null
 914      * @since 1.4
 915      */
 916     public void setCurrency(Currency currency) {
 917         throw new UnsupportedOperationException();
 918     }
 919 
 920     /**
 921      * Gets the {@link java.math.RoundingMode} used in this NumberFormat.
 922      * The default implementation of this method in NumberFormat
 923      * always throws {@link java.lang.UnsupportedOperationException}.
 924      * Subclasses which handle different rounding modes should override
 925      * this method.
 926      *
 927      * @exception UnsupportedOperationException The default implementation
 928      *     always throws this exception
 929      * @return The <code>RoundingMode</code> used for this NumberFormat.
 930      * @see #setRoundingMode(RoundingMode)
 931      * @since 1.6
 932      */
 933     public RoundingMode getRoundingMode() {
 934         throw new UnsupportedOperationException();
 935     }
 936 
 937     /**
 938      * Sets the {@link java.math.RoundingMode} used in this NumberFormat.
 939      * The default implementation of this method in NumberFormat always
 940      * throws {@link java.lang.UnsupportedOperationException}.
 941      * Subclasses which handle different rounding modes should override
 942      * this method.
 943      *
 944      * @exception UnsupportedOperationException The default implementation
 945      *     always throws this exception
 946      * @exception NullPointerException if <code>roundingMode</code> is null
 947      * @param roundingMode The <code>RoundingMode</code> to be used
 948      * @see #getRoundingMode()
 949      * @since 1.6
 950      */
 951     public void setRoundingMode(RoundingMode roundingMode) {
 952         throw new UnsupportedOperationException();
 953     }
 954 
 955     // =======================privates===============================
 956 
 957     private static NumberFormat getInstance(Locale desiredLocale,
 958                                             Style formatStyle, int choice) {
 959         LocaleProviderAdapter adapter;
 960         adapter = LocaleProviderAdapter.getAdapter(NumberFormatProvider.class,
 961                 desiredLocale);
 962         NumberFormat numberFormat = getInstance(adapter, desiredLocale,
 963                 formatStyle, choice);
 964         if (numberFormat == null) {
 965             numberFormat = getInstance(LocaleProviderAdapter.forJRE(),
 966                     desiredLocale, formatStyle, choice);




 242      * including <code>BigInteger</code> values with a
 243      * {@link java.math.BigInteger#bitLength() bit length} of less than 64,
 244      * and {@link java.lang.Number#doubleValue()} for all other types. It
 245      * then calls
 246      * {@link #format(long,java.lang.StringBuffer,java.text.FieldPosition)}
 247      * or {@link #format(double,java.lang.StringBuffer,java.text.FieldPosition)}.
 248      * This may result in loss of magnitude information and precision for
 249      * <code>BigInteger</code> and <code>BigDecimal</code> values.
 250      * @param number     the number to format
 251      * @param toAppendTo the <code>StringBuffer</code> to which the formatted
 252      *                   text is to be appended
 253      * @param pos        keeps track on the position of the field within the
 254      *                   returned string. For example, for formatting a number
 255      *                   {@code 1234567.89} in {@code Locale.US} locale,
 256      *                   if the given {@code fieldPosition} is
 257      *                   {@link NumberFormat#INTEGER_FIELD}, the begin index
 258      *                   and end index of {@code fieldPosition} will be set
 259      *                   to 0 and 9, respectively for the output string
 260      *                   {@code 1,234,567.89}.
 261      * @return           the value passed in as <code>toAppendTo</code>
 262      * @throws           IllegalArgumentException if <code>number</code> is
 263      *                   null or not an instance of <code>Number</code>.
 264      * @throws           NullPointerException if <code>toAppendTo</code> or
 265      *                   <code>pos</code> is null
 266      * @throws           ArithmeticException if rounding is needed with rounding
 267      *                   mode being set to RoundingMode.UNNECESSARY
 268      * @see              java.text.FieldPosition
 269      */
 270     @Override
 271     public StringBuffer format(Object number,
 272                                StringBuffer toAppendTo,
 273                                FieldPosition pos) {
 274         if (number instanceof Long || number instanceof Integer ||
 275             number instanceof Short || number instanceof Byte ||
 276             number instanceof AtomicInteger || number instanceof AtomicLong ||
 277             (number instanceof BigInteger &&
 278              ((BigInteger)number).bitLength() < 64)) {
 279             return format(((Number)number).longValue(), toAppendTo, pos);
 280         } else if (number instanceof Number) {
 281             return format(((Number)number).doubleValue(), toAppendTo, pos);
 282         } else {
 283             throw new IllegalArgumentException("Cannot format given Object as a Number");
 284         }
 285     }
 286 


 301      * See the {@link #parse(String, ParsePosition)} method for more information
 302      * on number parsing.
 303      *
 304      * @param source A <code>String</code>, part of which should be parsed.
 305      * @param pos A <code>ParsePosition</code> object with index and error
 306      *            index information as described above.
 307      * @return A <code>Number</code> parsed from the string. In case of
 308      *         error, returns null.
 309      * @throws NullPointerException if {@code source} or {@code pos} is null.
 310      */
 311     @Override
 312     public final Object parseObject(String source, ParsePosition pos) {
 313         return parse(source, pos);
 314     }
 315 
 316    /**
 317      * Specialization of format.
 318      *
 319      * @param number the double number to format
 320      * @return the formatted String
 321      * @throws           ArithmeticException if rounding is needed with rounding
 322      *                   mode being set to RoundingMode.UNNECESSARY
 323      * @see java.text.Format#format
 324      */
 325     public final String format(double number) {
 326         // Use fast-path for double result if that works
 327         String result = fastFormat(number);
 328         if (result != null)
 329             return result;
 330 
 331         return format(number, new StringBuffer(),
 332                       DontCareFieldPosition.INSTANCE).toString();
 333     }
 334 
 335     /*
 336      * fastFormat() is supposed to be implemented in concrete subclasses only.
 337      * Default implem always returns null.
 338      */
 339     String fastFormat(double number) { return null; }
 340 
 341    /**
 342      * Specialization of format.
 343      *
 344      * @param number the long number to format
 345      * @return the formatted String
 346      * @throws           ArithmeticException if rounding is needed with rounding
 347      *                   mode being set to RoundingMode.UNNECESSARY
 348      * @see java.text.Format#format
 349      */
 350     public final String format(long number) {
 351         return format(number, new StringBuffer(),
 352                       DontCareFieldPosition.INSTANCE).toString();
 353     }
 354 
 355    /**
 356      * Specialization of format.
 357      *
 358      * @param number     the double number to format
 359      * @param toAppendTo the StringBuffer to which the formatted text is to be
 360      *                   appended
 361      * @param pos        keeps track on the position of the field within the
 362      *                   returned string. For example, for formatting a number
 363      *                   {@code 1234567.89} in {@code Locale.US} locale,
 364      *                   if the given {@code fieldPosition} is
 365      *                   {@link NumberFormat#INTEGER_FIELD}, the begin index
 366      *                   and end index of {@code fieldPosition} will be set
 367      *                   to 0 and 9, respectively for the output string
 368      *                   {@code 1,234,567.89}.
 369      * @return the formatted StringBuffer
 370      * @throws           ArithmeticException if rounding is needed with rounding
 371      *                   mode being set to RoundingMode.UNNECESSARY
 372      * @see java.text.Format#format
 373      */
 374     public abstract StringBuffer format(double number,
 375                                         StringBuffer toAppendTo,
 376                                         FieldPosition pos);
 377 
 378    /**
 379      * Specialization of format.
 380      *
 381      * @param number     the long number to format
 382      * @param toAppendTo the StringBuffer to which the formatted text is to be
 383      *                   appended
 384      * @param pos        keeps track on the position of the field within the
 385      *                   returned string. For example, for formatting a number
 386      *                   {@code 123456789} in {@code Locale.US} locale,
 387      *                   if the given {@code fieldPosition} is
 388      *                   {@link NumberFormat#INTEGER_FIELD}, the begin index
 389      *                   and end index of {@code fieldPosition} will be set
 390      *                   to 0 and 11, respectively for the output string
 391      *                   {@code 123,456,789}.
 392      * @return the formatted StringBuffer
 393      * @throws           ArithmeticException if rounding is needed with rounding
 394      *                   mode being set to RoundingMode.UNNECESSARY
 395      * @see java.text.Format#format
 396      */
 397     public abstract StringBuffer format(long number,
 398                                         StringBuffer toAppendTo,
 399                                         FieldPosition pos);
 400 
 401    /**
 402      * Returns a Long if possible (e.g., within the range [Long.MIN_VALUE,
 403      * Long.MAX_VALUE] and with no decimals), otherwise a Double.
 404      * If IntegerOnly is set, will stop at a decimal
 405      * point (or equivalent; e.g., for rational numbers "1 2/3", will stop
 406      * after the 1).
 407      * Does not throw an exception; if no object can be parsed, index is
 408      * unchanged!
 409      *
 410      * @param source the String to parse
 411      * @param parsePosition the parse position
 412      * @return the parsed value
 413      * @see java.text.NumberFormat#isParseIntegerOnly
 414      * @see java.text.Format#parseObject
 415      */
 416     public abstract Number parse(String source, ParsePosition parsePosition);
 417 
 418     /**
 419      * Parses text from the beginning of the given string to produce a number.
 420      * The method may not use the entire text of the given string.
 421      * <p>
 422      * See the {@link #parse(String, ParsePosition)} method for more information
 423      * on number parsing.
 424      *
 425      * @param source A <code>String</code> whose beginning should be parsed.
 426      * @return A <code>Number</code> parsed from the string.
 427      * @throws    ParseException if the beginning of the specified string
 428      *            cannot be parsed.
 429      */
 430     public Number parse(String source) throws ParseException {
 431         ParsePosition parsePosition = new ParsePosition(0);
 432         Number result = parse(source, parsePosition);
 433         if (parsePosition.index == 0) {
 434             throw new ParseException("Unparseable number: \"" + source + "\"",
 435                                      parsePosition.errorIndex);
 436         }
 437         return result;
 438     }
 439 
 440     /**
 441      * Returns true if this format will parse numbers as integers only.
 442      * For example in the English locale, with ParseIntegerOnly true, the
 443      * string "1234." would be parsed as the integer value 1234 and parsing
 444      * would stop at the "." character.  Of course, the exact format accepted
 445      * by the parse operation is locale dependent and determined by sub-classes
 446      * of NumberFormat.
 447      *


 874      * @see #getMinimumFractionDigits
 875      */
 876     public void setMinimumFractionDigits(int newValue) {
 877         minimumFractionDigits = Math.max(0,newValue);
 878         if (maximumFractionDigits < minimumFractionDigits) {
 879             maximumFractionDigits = minimumFractionDigits;
 880         }
 881     }
 882 
 883     /**
 884      * Gets the currency used by this number format when formatting
 885      * currency values. The initial value is derived in a locale dependent
 886      * way. The returned value may be null if no valid
 887      * currency could be determined and no currency has been set using
 888      * {@link #setCurrency(java.util.Currency) setCurrency}.
 889      * <p>
 890      * The default implementation throws
 891      * <code>UnsupportedOperationException</code>.
 892      *
 893      * @return the currency used by this number format, or <code>null</code>
 894      * @throws    UnsupportedOperationException if the number format class
 895      * doesn't implement currency formatting
 896      * @since 1.4
 897      */
 898     public Currency getCurrency() {
 899         throw new UnsupportedOperationException();
 900     }
 901 
 902     /**
 903      * Sets the currency used by this number format when formatting
 904      * currency values. This does not update the minimum or maximum
 905      * number of fraction digits used by the number format.
 906      * <p>
 907      * The default implementation throws
 908      * <code>UnsupportedOperationException</code>.
 909      *
 910      * @param currency the new currency to be used by this number format
 911      * @throws    UnsupportedOperationException if the number format class
 912      * doesn't implement currency formatting
 913      * @throws    NullPointerException if <code>currency</code> is null
 914      * @since 1.4
 915      */
 916     public void setCurrency(Currency currency) {
 917         throw new UnsupportedOperationException();
 918     }
 919 
 920     /**
 921      * Gets the {@link java.math.RoundingMode} used in this NumberFormat.
 922      * The default implementation of this method in NumberFormat
 923      * always throws {@link java.lang.UnsupportedOperationException}.
 924      * Subclasses which handle different rounding modes should override
 925      * this method.
 926      *
 927      * @throws    UnsupportedOperationException The default implementation
 928      *     always throws this exception
 929      * @return The <code>RoundingMode</code> used for this NumberFormat.
 930      * @see #setRoundingMode(RoundingMode)
 931      * @since 1.6
 932      */
 933     public RoundingMode getRoundingMode() {
 934         throw new UnsupportedOperationException();
 935     }
 936 
 937     /**
 938      * Sets the {@link java.math.RoundingMode} used in this NumberFormat.
 939      * The default implementation of this method in NumberFormat always
 940      * throws {@link java.lang.UnsupportedOperationException}.
 941      * Subclasses which handle different rounding modes should override
 942      * this method.
 943      *
 944      * @throws    UnsupportedOperationException The default implementation
 945      *     always throws this exception
 946      * @throws    NullPointerException if <code>roundingMode</code> is null
 947      * @param roundingMode The <code>RoundingMode</code> to be used
 948      * @see #getRoundingMode()
 949      * @since 1.6
 950      */
 951     public void setRoundingMode(RoundingMode roundingMode) {
 952         throw new UnsupportedOperationException();
 953     }
 954 
 955     // =======================privates===============================
 956 
 957     private static NumberFormat getInstance(Locale desiredLocale,
 958                                             Style formatStyle, int choice) {
 959         LocaleProviderAdapter adapter;
 960         adapter = LocaleProviderAdapter.getAdapter(NumberFormatProvider.class,
 961                 desiredLocale);
 962         NumberFormat numberFormat = getInstance(adapter, desiredLocale,
 963                 formatStyle, choice);
 964         if (numberFormat == null) {
 965             numberFormat = getInstance(LocaleProviderAdapter.forJRE(),
 966                     desiredLocale, formatStyle, choice);


< prev index next >