1 package prefuse.data.parser; 2 3 10 public class BooleanParser implements DataParser { 11 12 13 public static final String TRUE = "true"; 14 15 public static final String FALSE = "false"; 16 17 21 public Class getType() { 22 return boolean.class; 23 } 24 25 28 public String format(Object value) { 29 if ( value == null ) return null; 30 if ( !(value instanceof Boolean ) ) 31 throw new IllegalArgumentException ( 32 "This class can only format Objects of type Boolean."); 33 return ((Boolean )value).toString(); 34 } 35 36 39 public boolean canParse(String text) { 40 return TRUE.equalsIgnoreCase(text) || FALSE.equalsIgnoreCase(text); 41 } 42 43 46 public Object parse(String text) throws DataParseException { 47 return parseBoolean(text) ? Boolean.TRUE : Boolean.FALSE; 48 } 49 50 56 public boolean parseBoolean(String text) throws DataParseException { 57 if ( TRUE.equalsIgnoreCase(text) ) { 58 return true; 59 } else if ( FALSE.equalsIgnoreCase(text) ) { 60 return false; 61 } else { 62 throw new DataParseException( 63 "Input does not represent a boolean value: "+ text); 64 } 65 } 66 67 } | Popular Tags |