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 26 public class CharacterTypeConverter extends BaseTypeConverter { 27 28 43 public Object convertString(String value, Class convertTo) throws TypeConversionException { 44 45 boolean empty = StringTools.isTrimmedEmpty(value); 46 if (empty && convertTo == Character.TYPE) { 47 return new Character ('\u0000'); 48 } else if (empty) { 49 return null; 50 } 51 52 if (convertTo.isArray()) { 53 return convertToArray(value, convertTo); 54 } 55 56 if (value.length() > 1) { 58 throw new TypeConversionException("Setter expected a single" + 59 " character but receieved string with value: " + value); 60 } 61 62 return new Character (value.charAt(0)); 63 } 64 65 78 public Object convertArray(Object [] values, Class convertTo) 79 throws TypeConversionException { 80 81 if ((values == null || values.length == 0) && convertTo == Character.TYPE) { 83 return new Character ('\u0000'); 84 } 85 86 return super.convertArray(values, convertTo); 87 } 88 } 89 | Popular Tags |