1 package prefuse.data.parser; 2 3 8 public class DoubleParser implements DataParser { 9 10 private boolean m_blockExplicitFloats = true; 11 12 16 public Class getType() { 17 return double.class; 18 } 19 20 23 public String format(Object value) { 24 if ( value == null ) return null; 25 if ( !(value instanceof Number ) ) 26 throw new IllegalArgumentException ( 27 "This class can only format Objects of type Number."); 28 return String.valueOf(((Number )value).doubleValue()); 29 } 30 31 34 public boolean canParse(String text) { 35 try { 36 if ( m_blockExplicitFloats && text.endsWith("f") ) { 37 return false; 39 } 40 Double.parseDouble(text); 41 return true; 42 } catch ( NumberFormatException e ) { 43 return false; 44 } 45 } 46 47 50 public Object parse(String text) throws DataParseException { 51 return new Double (parseDouble(text)); 52 } 53 54 60 public static double parseDouble(String text) throws DataParseException { 61 try { 62 return Double.parseDouble(text); 63 } catch ( NumberFormatException e ) { 64 throw new DataParseException(e); 65 } 66 } 67 68 } | Popular Tags |