001: /* $Id: NumberFormat.java 732 2006-10-30 17:16:52Z hengels $ */
002: package org.conform.format;
003:
004: import java.text.*;
005: import java.math.BigDecimal;
006: import java.math.BigInteger;
007:
008: /**
009: * java.sql.Date
010: * @version $Revision: 732 $
011: */
012: public class NumberFormat extends AbstractFormat {
013: int scale = -1;
014: int precision = -1;
015: boolean percentage;
016: DecimalFormatSymbols decimalFormatSymbols;
017: Class type;
018:
019: public NumberFormat(Class type) {
020: message = "validation.unparsableNumber";
021: this .type = type;
022: }
023:
024: public NumberFormat(Class type, String pattern) {
025: message = "validation.unparsableNumber";
026: this .type = type;
027: this .pattern = pattern;
028: }
029:
030: public java.text.NumberFormat getFormat() {
031: java.text.DecimalFormat numberFormat;
032:
033: if (pattern != null) {
034: numberFormat = new DecimalFormat(pattern);
035: } else if (percentage)
036: numberFormat = (DecimalFormat) java.text.NumberFormat
037: .getPercentInstance(FormatFactory.getInstance()
038: .getLocale());
039: else if (Float.class.equals(type) || Double.class.equals(type)
040: || float.class.equals(type)
041: || double.class.equals(type)
042: || BigDecimal.class.equals(type)) {
043: numberFormat = (DecimalFormat) java.text.NumberFormat
044: .getNumberInstance(FormatFactory.getInstance()
045: .getLocale());
046: } else
047: numberFormat = (DecimalFormat) java.text.NumberFormat
048: .getIntegerInstance(FormatFactory.getInstance()
049: .getLocale());
050:
051: if (scale != -1) {
052: numberFormat.setMinimumFractionDigits(scale);
053: numberFormat.setMaximumFractionDigits(scale);
054: }
055:
056: if (decimalFormatSymbols != null)
057: numberFormat.setDecimalFormatSymbols(decimalFormatSymbols);
058:
059: if (BigDecimal.class.equals(type))
060: numberFormat.setParseBigDecimal(true);
061:
062: return numberFormat;
063: }
064:
065: public int getScale() {
066: return scale;
067: }
068:
069: public void setScale(int scale) {
070: this .scale = scale;
071: }
072:
073: public int getPrecision() {
074: return precision;
075: }
076:
077: public void setPrecision(int precision) {
078: this .precision = precision;
079: }
080:
081: public boolean isPercentage() {
082: return percentage;
083: }
084:
085: public void setPercentage(boolean percentage) {
086: this .percentage = percentage;
087: }
088:
089: public DecimalFormatSymbols getDecimalFormatSymbols() {
090: return decimalFormatSymbols;
091: }
092:
093: public void setDecimalFormatSymbols(
094: DecimalFormatSymbols decimalFormatSymbols) {
095: this .decimalFormatSymbols = decimalFormatSymbols;
096: }
097:
098: public String format(Object value) {
099: return getFormat().format(value);
100: }
101:
102: public Object parse(String string) throws ParseException {
103: Number number = (Number) getFormat().parseObject(string);
104: if (float.class.equals(type))
105: return number.floatValue();
106: if (double.class.equals(type))
107: return number.doubleValue();
108: if (Float.class.equals(type))
109: return number.floatValue();
110: if (Double.class.equals(type))
111: return number.doubleValue();
112: if (Long.class.equals(type))
113: return number.longValue();
114: if (Integer.class.equals(type))
115: return number.intValue();
116: if (Short.class.equals(type))
117: return number.shortValue();
118: if (Byte.class.equals(type))
119: return number.byteValue();
120: if (BigDecimal.class.equals(type))
121: return number;
122: if (BigInteger.class.equals(type))
123: return new BigInteger(number.toString());
124: else
125: throw new IllegalArgumentException("unknown type: " + type);
126: }
127: }
|