1 7 package com.inversoft.util.typeconverter.converters; 8 9 10 import com.inversoft.util.StringTools; 11 import com.inversoft.util.typeconverter.BaseTypeConverter; 12 import com.inversoft.util.typeconverter.TypeConversionException; 13 14 15 27 public class BooleanTypeConverter extends BaseTypeConverter { 28 29 50 public Object convertString(String value, Class convertTo) throws TypeConversionException { 51 52 if (StringTools.isTrimmedEmpty(value)) { 54 if (convertTo == Boolean.TYPE) { 55 return Boolean.FALSE; 56 } 57 58 return null; 59 } else { 60 value = value.trim(); 61 } 62 63 if (convertTo.isArray()) { 66 return convertToArray(value, convertTo.getComponentType()); 67 } 68 69 if (StringTools.isValidBoolean(value)) { 71 return Boolean.valueOf(value); 72 } 73 74 throw new TypeConversionException(value + " is not a valid boolean type"); 75 } 76 77 90 public Object convertArray(Object [] values, Class convertTo) 91 throws TypeConversionException { 92 93 if ((values == null || values.length == 0) && convertTo == Boolean.TYPE) { 95 return Boolean.FALSE; 96 } 97 98 return super.convertArray(values, convertTo); 99 } 100 } | Popular Tags |