1 package prefuse.data.parser; 2 3 import prefuse.util.ColorLib; 4 5 11 public class ColorIntParser implements DataParser { 12 13 17 public Class getType() { 18 return int.class; 19 } 20 21 24 public String format(Object value) { 25 if ( value == null ) return null; 26 if ( !(value instanceof Number ) ) 27 throw new IllegalArgumentException ( 28 "This class can only format Objects of type Number."); 29 return String.valueOf(((Number )value).intValue()); 30 } 31 32 35 public boolean canParse(String text) { 36 try { 37 if ( text.charAt(0) == ColorLib.HEX_PREFIX ) { 38 parseInt(text); 39 } else { 40 return false; 41 } 42 return true; 43 } catch ( Exception e ) { 44 return false; 45 } 46 } 47 48 51 public Object parse(String text) throws DataParseException { 52 return new Integer (parseInt(text)); 53 } 54 55 61 public static int parseInt(String text) throws DataParseException { 62 try { 63 return ColorLib.hex(text); 64 } catch ( Exception e ) { 65 throw new DataParseException(e); 66 } 67 } 68 69 } | Popular Tags |