1 package prefuse.data.parser; 2 3 8 public class IntParser implements DataParser { 9 10 14 public Class getType() { 15 return int.class; 16 } 17 18 21 public String format(Object value) { 22 if ( value == null ) return null; 23 if ( !(value instanceof Number ) ) 24 throw new IllegalArgumentException ( 25 "This class can only format Objects of type Number."); 26 return String.valueOf(((Number )value).intValue()); 27 } 28 29 32 public boolean canParse(String text) { 33 try { 34 Integer.parseInt(text); 35 return true; 36 } catch ( NumberFormatException e ) { 37 return false; 38 } 39 } 40 41 44 public Object parse(String text) throws DataParseException { 45 return new Integer (parseInt(text)); 46 } 47 48 54 public static int parseInt(String text) throws DataParseException { 55 try { 56 return Integer.parseInt(text); 57 } catch ( NumberFormatException e ) { 58 throw new DataParseException(e); 59 } 60 } 61 62 } | Popular Tags |