1 19 20 package org.apache.cayenne.reflect; 21 22 import java.math.BigDecimal ; 23 import java.math.BigInteger ; 24 import java.util.HashMap ; 25 import java.util.Map ; 26 27 import org.apache.cayenne.util.Util; 28 29 35 class ConverterFactory { 36 37 private static final String FACTORY_CLASS_JDK15 = "org.apache.cayenne.reflect.ConverterFactory15"; 38 39 static final ConverterFactory factory = createFactory(); 40 static Map converters; 41 42 static final Converter noopConverter = new Converter() { 43 44 Object convert(Object object, Class type) { 45 return object; 46 } 47 }; 48 49 static { 50 51 Converter stringConverter = new Converter() { 52 53 Object convert(Object object, Class type) { 54 return object != null ? object.toString() : null; 55 } 56 }; 57 58 Converter booleanConverter = new Converter() { 59 60 Object convert(Object object, Class type) { 61 if (object == null) { 62 return type.isPrimitive() ? Boolean.FALSE : null; 63 } 64 65 if (object instanceof Boolean ) { 66 return object; 67 } 68 69 return "true".equalsIgnoreCase(object.toString()) 70 ? Boolean.TRUE 71 : Boolean.FALSE; 72 } 73 }; 74 75 Converter intConverter = new Converter() { 76 77 Object convert(Object object, Class type) { 78 if (object == null) { 79 return type.isPrimitive() ? new Integer (0) : null; 80 } 81 82 if (object instanceof Integer ) { 83 return object; 84 } 85 86 return new Integer (object.toString()); 87 } 88 }; 89 90 Converter byteConverter = new Converter() { 91 92 Object convert(Object object, Class type) { 93 if (object == null) { 94 return type.isPrimitive() ? new Byte ((byte) 0) : null; 95 } 96 97 if (object instanceof Byte ) { 98 return object; 99 } 100 101 return new Byte (object.toString()); 102 } 103 }; 104 105 Converter shortConverter = new Converter() { 106 107 Object convert(Object object, Class type) { 108 if (object == null) { 109 return type.isPrimitive() ? new Short ((short) 0) : null; 110 } 111 112 if (object instanceof Short ) { 113 return object; 114 } 115 116 return new Short (object.toString()); 117 } 118 }; 119 120 Converter charConverter = new Converter() { 121 122 Object convert(Object object, Class type) { 123 if (object == null) { 124 return type.isPrimitive() ? new Character ((char) 0) : null; 125 } 126 127 if (object instanceof Character ) { 128 return object; 129 } 130 131 String string = object.toString(); 132 return new Character (string.length() > 0 ? string.charAt(0) : 0); 133 } 134 }; 135 136 Converter doubleConverter = new Converter() { 137 138 Object convert(Object object, Class type) { 139 if (object == null) { 140 return type.isPrimitive() ? new Double (0d) : null; 141 } 142 143 if (object instanceof Double ) { 144 return object; 145 } 146 147 return new Double (object.toString()); 148 } 149 }; 150 151 Converter floatConverter = new Converter() { 152 153 Object convert(Object object, Class type) { 154 if (object == null) { 155 return type.isPrimitive() ? new Float (0f) : null; 156 } 157 158 if (object instanceof Float ) { 159 return object; 160 } 161 162 return new Float (object.toString()); 163 } 164 }; 165 166 Converter bigDecimalConverter = new Converter() { 167 168 Object convert(Object object, Class type) { 169 if (object == null) { 170 return null; 171 } 172 173 if (object instanceof BigDecimal ) { 174 return object; 175 } 176 177 return new BigDecimal (object.toString()); 178 } 179 }; 180 181 Converter bigIntegerConverter = new Converter() { 182 183 Object convert(Object object, Class type) { 184 if (object == null) { 185 return null; 186 } 187 188 if (object instanceof BigInteger ) { 189 return object; 190 } 191 192 return new BigInteger (object.toString()); 193 } 194 }; 195 196 198 converters = new HashMap (); 199 200 converters.put(Boolean .class.getName(), booleanConverter); 201 converters.put("boolean", booleanConverter); 202 203 converters.put(Short .class.getName(), shortConverter); 204 converters.put("short", shortConverter); 205 206 converters.put(Byte .class.getName(), byteConverter); 207 converters.put("byte", byteConverter); 208 209 converters.put(Integer .class.getName(), intConverter); 210 converters.put("int", intConverter); 211 212 converters.put(Double .class.getName(), doubleConverter); 213 converters.put("double", doubleConverter); 214 215 converters.put(Float .class.getName(), floatConverter); 216 converters.put("float", floatConverter); 217 218 converters.put(Character .class.getName(), charConverter); 219 converters.put("char", charConverter); 220 221 converters.put(BigDecimal .class.getName(), bigDecimalConverter); 222 converters.put(BigInteger .class.getName(), bigIntegerConverter); 223 converters.put(Number .class.getName(), bigDecimalConverter); 224 converters.put(String .class.getName(), stringConverter); 225 } 226 227 static ConverterFactory createFactory() { 228 try { 229 Class.forName("java.lang.StringBuilder"); 231 232 Class factoryClass = Util.getJavaClass(FACTORY_CLASS_JDK15); 233 return (ConverterFactory) factoryClass.newInstance(); 234 } 235 catch (Throwable th) { 236 return new ConverterFactory(); 238 } 239 } 240 241 Converter getConverter(Class type) { 242 if (type == null) { 243 throw new IllegalArgumentException ("Null type"); 244 } 245 246 Converter c = (Converter) converters.get(type.getName()); 247 return c != null ? c : noopConverter; 248 } 249 } 250 | Popular Tags |