1979 * </ul>
1980 * <p>
1981 * <code>DecimalFormat</code> parses all Unicode characters that represent
1982 * decimal digits, as defined by <code>Character.digit()</code>. In
1983 * addition, <code>DecimalFormat</code> also recognizes as digits the ten
1984 * consecutive characters starting with the localized zero digit defined in
1985 * the <code>DecimalFormatSymbols</code> object.
1986 *
1987 * @param text the string to be parsed
1988 * @param pos A <code>ParsePosition</code> object with index and error
1989 * index information as described above.
1990 * @return the parsed value, or <code>null</code> if the parse fails
1991 * @exception NullPointerException if <code>text</code> or
1992 * <code>pos</code> is null.
1993 */
1994 @Override
1995 public Number parse(String text, ParsePosition pos) {
1996 // special case NaN
1997 if (text.regionMatches(pos.index, symbols.getNaN(), 0, symbols.getNaN().length())) {
1998 pos.index = pos.index + symbols.getNaN().length();
1999 return new Double(Double.NaN);
2000 }
2001
2002 boolean[] status = new boolean[STATUS_LENGTH];
2003 if (!subparse(text, pos, positivePrefix, negativePrefix, digitList, false, status)) {
2004 return null;
2005 }
2006
2007 // special case INFINITY
2008 if (status[STATUS_INFINITE]) {
2009 if (status[STATUS_POSITIVE] == (multiplier >= 0)) {
2010 return new Double(Double.POSITIVE_INFINITY);
2011 } else {
2012 return new Double(Double.NEGATIVE_INFINITY);
2013 }
2014 }
2015
2016 if (multiplier == 0) {
2017 if (digitList.isZero()) {
2018 return new Double(Double.NaN);
2019 } else if (status[STATUS_POSITIVE]) {
2020 return new Double(Double.POSITIVE_INFINITY);
2021 } else {
2022 return new Double(Double.NEGATIVE_INFINITY);
2023 }
2024 }
2025
2026 if (isParseBigDecimal()) {
2027 BigDecimal bigDecimalResult = digitList.getBigDecimal();
2028
2029 if (multiplier != 1) {
2030 try {
2031 bigDecimalResult = bigDecimalResult.divide(getBigDecimalMultiplier());
2032 }
2033 catch (ArithmeticException e) { // non-terminating decimal expansion
2034 bigDecimalResult = bigDecimalResult.divide(getBigDecimalMultiplier(), roundingMode);
2035 }
2036 }
2037
2038 if (!status[STATUS_POSITIVE]) {
2039 bigDecimalResult = bigDecimalResult.negate();
2040 }
2041 return bigDecimalResult;
2042 } else {
2076 doubleResult = -doubleResult;
2077 longResult = -longResult;
2078 }
2079
2080 // At this point, if we divided the result by the multiplier, the
2081 // result may fit into a long. We check for this case and return
2082 // a long if possible.
2083 // We must do this AFTER applying the negative (if appropriate)
2084 // in order to handle the case of LONG_MIN; otherwise, if we do
2085 // this with a positive value -LONG_MIN, the double is > 0, but
2086 // the long is < 0. We also must retain a double in the case of
2087 // -0.0, which will compare as == to a long 0 cast to a double
2088 // (bug 4162852).
2089 if (multiplier != 1 && gotDouble) {
2090 longResult = (long)doubleResult;
2091 gotDouble = ((doubleResult != (double)longResult) ||
2092 (doubleResult == 0.0 && 1/doubleResult < 0.0)) &&
2093 !isParseIntegerOnly();
2094 }
2095
2096 return gotDouble ?
2097 (Number)new Double(doubleResult) : (Number)Long.valueOf(longResult);
2098 }
2099 }
2100
2101 /**
2102 * Return a BigInteger multiplier.
2103 */
2104 private BigInteger getBigIntegerMultiplier() {
2105 if (bigIntegerMultiplier == null) {
2106 bigIntegerMultiplier = BigInteger.valueOf(multiplier);
2107 }
2108 return bigIntegerMultiplier;
2109 }
2110 private transient BigInteger bigIntegerMultiplier;
2111
2112 /**
2113 * Return a BigDecimal multiplier.
2114 */
2115 private BigDecimal getBigDecimalMultiplier() {
2116 if (bigDecimalMultiplier == null) {
2117 bigDecimalMultiplier = new BigDecimal(multiplier);
|
1979 * </ul>
1980 * <p>
1981 * <code>DecimalFormat</code> parses all Unicode characters that represent
1982 * decimal digits, as defined by <code>Character.digit()</code>. In
1983 * addition, <code>DecimalFormat</code> also recognizes as digits the ten
1984 * consecutive characters starting with the localized zero digit defined in
1985 * the <code>DecimalFormatSymbols</code> object.
1986 *
1987 * @param text the string to be parsed
1988 * @param pos A <code>ParsePosition</code> object with index and error
1989 * index information as described above.
1990 * @return the parsed value, or <code>null</code> if the parse fails
1991 * @exception NullPointerException if <code>text</code> or
1992 * <code>pos</code> is null.
1993 */
1994 @Override
1995 public Number parse(String text, ParsePosition pos) {
1996 // special case NaN
1997 if (text.regionMatches(pos.index, symbols.getNaN(), 0, symbols.getNaN().length())) {
1998 pos.index = pos.index + symbols.getNaN().length();
1999 return Double.valueOf(Double.NaN);
2000 }
2001
2002 boolean[] status = new boolean[STATUS_LENGTH];
2003 if (!subparse(text, pos, positivePrefix, negativePrefix, digitList, false, status)) {
2004 return null;
2005 }
2006
2007 // special case INFINITY
2008 if (status[STATUS_INFINITE]) {
2009 if (status[STATUS_POSITIVE] == (multiplier >= 0)) {
2010 return Double.valueOf(Double.POSITIVE_INFINITY);
2011 } else {
2012 return Double.valueOf(Double.NEGATIVE_INFINITY);
2013 }
2014 }
2015
2016 if (multiplier == 0) {
2017 if (digitList.isZero()) {
2018 return Double.valueOf(Double.NaN);
2019 } else if (status[STATUS_POSITIVE]) {
2020 return Double.valueOf(Double.POSITIVE_INFINITY);
2021 } else {
2022 return Double.valueOf(Double.NEGATIVE_INFINITY);
2023 }
2024 }
2025
2026 if (isParseBigDecimal()) {
2027 BigDecimal bigDecimalResult = digitList.getBigDecimal();
2028
2029 if (multiplier != 1) {
2030 try {
2031 bigDecimalResult = bigDecimalResult.divide(getBigDecimalMultiplier());
2032 }
2033 catch (ArithmeticException e) { // non-terminating decimal expansion
2034 bigDecimalResult = bigDecimalResult.divide(getBigDecimalMultiplier(), roundingMode);
2035 }
2036 }
2037
2038 if (!status[STATUS_POSITIVE]) {
2039 bigDecimalResult = bigDecimalResult.negate();
2040 }
2041 return bigDecimalResult;
2042 } else {
2076 doubleResult = -doubleResult;
2077 longResult = -longResult;
2078 }
2079
2080 // At this point, if we divided the result by the multiplier, the
2081 // result may fit into a long. We check for this case and return
2082 // a long if possible.
2083 // We must do this AFTER applying the negative (if appropriate)
2084 // in order to handle the case of LONG_MIN; otherwise, if we do
2085 // this with a positive value -LONG_MIN, the double is > 0, but
2086 // the long is < 0. We also must retain a double in the case of
2087 // -0.0, which will compare as == to a long 0 cast to a double
2088 // (bug 4162852).
2089 if (multiplier != 1 && gotDouble) {
2090 longResult = (long)doubleResult;
2091 gotDouble = ((doubleResult != (double)longResult) ||
2092 (doubleResult == 0.0 && 1/doubleResult < 0.0)) &&
2093 !isParseIntegerOnly();
2094 }
2095
2096 // cast inside of ?: because of binary numeric promotion, JLS 15.25
2097 return gotDouble ? (Number)doubleResult : (Number)longResult;
2098 }
2099 }
2100
2101 /**
2102 * Return a BigInteger multiplier.
2103 */
2104 private BigInteger getBigIntegerMultiplier() {
2105 if (bigIntegerMultiplier == null) {
2106 bigIntegerMultiplier = BigInteger.valueOf(multiplier);
2107 }
2108 return bigIntegerMultiplier;
2109 }
2110 private transient BigInteger bigIntegerMultiplier;
2111
2112 /**
2113 * Return a BigDecimal multiplier.
2114 */
2115 private BigDecimal getBigDecimalMultiplier() {
2116 if (bigDecimalMultiplier == null) {
2117 bigDecimalMultiplier = new BigDecimal(multiplier);
|