1 16 package org.springframework.binding.format.support; 17 18 import java.math.BigInteger ; 19 import java.text.NumberFormat ; 20 21 import org.springframework.binding.format.InvalidFormatException; 22 import org.springframework.util.NumberUtils; 23 24 29 public class NumberFormatter extends AbstractFormatter { 30 31 private NumberFormat numberFormat; 32 33 public NumberFormatter(NumberFormat numberFormat) { 34 this.numberFormat = numberFormat; 35 } 36 37 public NumberFormatter(NumberFormat numberFormat, boolean allowEmpty) { 38 super(allowEmpty); 39 this.numberFormat = numberFormat; 40 } 41 42 protected String doFormatValue(Object number) { 43 if (this.numberFormat != null) { 44 return this.numberFormat.format(number); 46 } 47 else { 48 return number.toString(); 50 } 51 } 52 53 protected Object doParseValue(String text, Class targetClass) throws IllegalArgumentException { 54 if (this.numberFormat != null) { 56 return NumberUtils.parseNumber(text, targetClass, this.numberFormat); 57 } 58 else { 60 return NumberUtils.parseNumber(text, targetClass); 61 } 62 } 63 64 public Short parseShort(String formattedString) throws InvalidFormatException { 65 return (Short )parseValue(formattedString, Short .class); 66 } 67 68 public Integer parseInteger(String formattedString) throws InvalidFormatException { 69 return (Integer )parseValue(formattedString, Integer .class); 70 } 71 72 public Long parseLong(String formattedString) throws InvalidFormatException { 73 return (Long )parseValue(formattedString, Long .class); 74 } 75 76 public Double parseDouble(String formattedString) throws InvalidFormatException { 77 return (Double )parseValue(formattedString, Double .class); 78 } 79 80 public Float parseFloat(String formattedString) throws InvalidFormatException { 81 return (Float )parseValue(formattedString, Float .class); 82 } 83 84 public BigInteger parseBigInteger(String formattedString) throws InvalidFormatException { 85 return (BigInteger )parseValue(formattedString, BigInteger .class); 86 } 87 88 public Byte parseByte(String formattedString) throws InvalidFormatException { 89 return (Byte )parseValue(formattedString, Byte .class); 90 } 91 } | Popular Tags |