Revision: 7307 Author: [email protected] Date: Sun Dec 13 22:19:25 2009 Log: Add a framework for formatting/parsing BigNumber/BigDecimal values.
http://code.google.com/p/google-web-toolkit/source/detail?r=7307 Modified: /changes/jat/bigdecimal/user/src/com/google/gwt/i18n/client/NumberFormat.java /changes/jat/bigdecimal/user/src/com/google/gwt/i18n/rebind/MessagesMethodCreator.java /changes/jat/bigdecimal/user/super/com/google/gwt/emul/java/math/Logical.java ======================================= --- /changes/jat/bigdecimal/user/src/com/google/gwt/i18n/client/NumberFormat.java Thu Dec 10 15:31:32 2009 +++ /changes/jat/bigdecimal/user/src/com/google/gwt/i18n/client/NumberFormat.java Sun Dec 13 22:19:25 2009 @@ -18,6 +18,9 @@ import com.google.gwt.core.client.GWT; import com.google.gwt.i18n.client.constants.NumberConstants; +import java.math.BigDecimal; +import java.math.BigInteger; + /** * Formats and parses numbers using locale-sensitive patterns. * @@ -747,6 +750,27 @@ return result.toString(); } + + /** + * This method formats a double to produce a string. + * + * @param number The double to format + * @return the formatted number string + */ + public String format(Number number) { + int scale = 0; + BigInteger bigInt = null; + if (number instanceof BigDecimal) { + BigDecimal bigDec = (BigDecimal) number; + bigInt = bigDec.unscaledValue(); + scale = bigDec.scale(); + } else if (number instanceof BigInteger) { + bigInt = (BigInteger) number; + } else { + return format(number.doubleValue()); + } + return formatBig(bigInt, scale); + } /** * Returns the pattern used by this number format. @@ -862,6 +886,47 @@ return ret; } + + /** + * Parses text to produce a numeric value. A {...@link NumberFormatException} is + * thrown if either the text is empty or if the parse does not consume all + * characters of the text. + * + * @param text the string being parsed + * @return a parsed number value, which may be a Double, BigInteger, or + * BigDecimal + * @throws NumberFormatException if the entire text could not be converted + * into a number + */ + public Number parseBig(String text) throws NumberFormatException { + // TODO(jat): implement + return Double.valueOf(parse(text)); + } + + /** + * Parses text to produce a numeric value. + * + * <p> + * The method attempts to parse text starting at the index given by pos. If + * parsing succeeds, then the index of <code>pos</code> is updated to the + * index after the last character used (parsing does not necessarily use all + * characters up to the end of the string), and the parsed number is returned. + * The updated <code>pos</code> can be used to indicate the starting point + * for the next call to this method. If an error occurs, then the index of + * <code>pos</code> is not changed. + * </p> + * + * @param text the string to be parsed + * @param inOutPos position to pass in and get back + * @return a parsed number value, which may be a Double, BigInteger, or + * BigDecimal, or {...@code Double(0.0)} if the parse fails. + * @throws NumberFormatException if the text segment could not be converted into a number + */ + public Number parseBig(String text, int[] inOutPos) + throws NumberFormatException { + // TODO(jat): implement + return Double.valueOf(parse(text, inOutPos)); + } protected int getGroupingSize() { return groupingSize; @@ -915,6 +980,19 @@ result.append((char) (exponentDigits.charAt(i) + zeroDelta)); } } + + /** + * Format a scaled BigInteger. + * + * @param bigInt value to format + * @param scale number of places to move the decimal point to the left; + * i.e., multiply by 10^-scale for the value to format. + * @return formatted value + */ + private String formatBig(BigInteger bigInt, int scale) { + // TODO(jat): implement + return format(bigInt.doubleValue() * Math.pow(10, -scale)); + } /** * This method return the digit that represented by current character, it ======================================= --- /changes/jat/bigdecimal/user/src/com/google/gwt/i18n/rebind/MessagesMethodCreator.java Tue Nov 10 11:52:25 2009 +++ /changes/jat/bigdecimal/user/src/com/google/gwt/i18n/rebind/MessagesMethodCreator.java Sun Dec 13 22:19:25 2009 @@ -83,7 +83,23 @@ public String format(StringGenerator out, String subformat, String argName, JType argType) { JPrimitiveType argPrimType = argType.isPrimitive(); - if (argPrimType == null || argPrimType == JPrimitiveType.BOOLEAN + if (argPrimType != null) { + if (argPrimType == JPrimitiveType.BOOLEAN + || argPrimType == JPrimitiveType.VOID) { + return "Illegal argument type for number format"; + } + } else { + JClassType classType = argType.isClass(); + if (classType == null) { + return "Unexpected argument type for number format"; + } + TypeOracle oracle = classType.getOracle(); + JClassType numberType = oracle.findType("java.lang.Number"); + if (!classType.isAssignableTo(numberType)) { + return "Only Number subclasses may be formatted as a number"; + } + } + if (argPrimType == JPrimitiveType.BOOLEAN || argPrimType == JPrimitiveType.VOID) { return "Illegal argument type for number format"; } ======================================= --- /changes/jat/bigdecimal/user/super/com/google/gwt/emul/java/math/Logical.java Sun Dec 13 21:28:30 2009 +++ /changes/jat/bigdecimal/user/super/com/google/gwt/emul/java/math/Logical.java Sun Dec 13 22:19:25 2009 @@ -503,7 +503,8 @@ return new BigInteger(-val.sign, i, resDigits); } - /*...@see BigInteger#or(BigInteger) + /** + * @see BigInteger#or(BigInteger). * @param val * @param that * @return -- http://groups.google.com/group/Google-Web-Toolkit-Contributors
