1 package prefuse.data.parser; 2 3 11 public class LongParser implements DataParser { 12 13 17 public Class getType() { 18 return long.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).longValue())+"L"; 30 } 31 32 35 public boolean canParse(String text) { 36 try { 37 parseLong(text); 38 return true; 39 } catch ( DataParseException e ) { 40 return false; 41 } 42 } 43 44 47 public Object parse(String text) throws DataParseException { 48 return new Long (parseLong(text)); 49 } 50 51 57 public static long parseLong(String text) throws DataParseException { 58 try { 59 if ( text.length() > 0 ) { 61 char c = text.charAt(text.length()-1); 62 if ( c == 'l' || c == 'L' ) 63 text = text.substring(0,text.length()-1); 64 } 65 return Long.parseLong(text); 67 } catch ( NumberFormatException e ) { 68 throw new DataParseException(e); 69 } 70 } 71 72 } | Popular Tags |