1 13 package com.tonbeller.wcf.format; 14 15 import java.text.DecimalFormat ; 16 import java.text.ParsePosition ; 17 18 19 22 public abstract class NumberHandler extends FormatHandlerSupport { 23 double minValue = Double.NaN; 24 25 public String format(Object o, String userPattern) { 26 if (o == null) { 27 return ""; 28 } 29 30 DecimalFormat df = (DecimalFormat ) DecimalFormat.getNumberInstance(getLocale()); 31 df.applyPattern(findPattern(userPattern)); 32 33 return df.format(o); 34 } 35 36 37 public Object parse(String s, String userPattern) throws FormatException { 38 if (s == null) { 39 throw new FormatException(getErrorMessage("")); 40 } 41 42 s = s.trim(); 43 44 if (s.length() == 0) { 45 throw new FormatException(getErrorMessage("")); 46 } 47 48 DecimalFormat df = (DecimalFormat ) DecimalFormat.getNumberInstance(getLocale()); 49 df.applyPattern(findPattern(userPattern)); 50 51 ParsePosition pos = new ParsePosition (0); 52 Number n = (Number ) df.parse(s, pos); 53 54 if ((n == null) || (pos.getIndex() != s.length())) 55 throw new FormatException(getErrorMessage(s)); 56 57 if (!Double.isNaN(minValue) && n.doubleValue() < minValue) 58 throw new FormatException(getErrorMessage(s)); 59 60 return n; 61 } 62 63 67 public double getMinValue() { 68 return minValue; 69 } 70 71 75 public void setMinValue(double minValue) { 76 this.minValue = minValue; 77 } 78 79 } | Popular Tags |