1 /*
2 * Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
296 * indicate the starting point for the next call to this method.
297 * If an error occurs, then the index of {@code pos} is not
298 * changed, the error index of {@code pos} is set to the index of
299 * the character where the error occurred, and null is returned.
300 * <p>
301 * See the {@link #parse(String, ParsePosition)} method for more information
302 * on number parsing.
303 *
304 * @param source A {@code String}, part of which should be parsed.
305 * @param pos A {@code ParsePosition} object with index and error
306 * index information as described above.
307 * @return A {@code Number} 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>
|
1 /*
2 * Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
296 * indicate the starting point for the next call to this method.
297 * If an error occurs, then the index of {@code pos} is not
298 * changed, the error index of {@code pos} is set to the index of
299 * the character where the error occurred, and null is returned.
300 * <p>
301 * See the {@link #parse(String, ParsePosition)} method for more information
302 * on number parsing.
303 *
304 * @param source A {@code String}, part of which should be parsed.
305 * @param pos A {@code ParsePosition} object with index and error
306 * index information as described above.
307 * @return A {@code Number} 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>
|