1 4 package com.tctest.performance.simulate.type; 5 6 import java.lang.reflect.Constructor ; 7 import java.util.Collection ; 8 import java.util.Collections ; 9 import java.util.HashMap ; 10 import java.util.Map ; 11 12 public final class SimulatedTypeFactory { 13 14 private static final Map SIM_TYPES = Collections.synchronizedMap(new HashMap ()); 15 static { 16 SIM_TYPES.put(String .class.getName(), SimulatedPrimitiveType.class); 17 SIM_TYPES.put(Integer .class.getName(), SimulatedPrimitiveType.class); 18 SIM_TYPES.put(Long .class.getName(), SimulatedPrimitiveType.class); 19 SIM_TYPES.put(Collection .class.getName(), SimulatedCollection.class); 20 SIM_TYPES.put(Map .class.getName(), SimulatedMap.class); 21 } 22 23 private SimulatedTypeFactory() { 24 } 26 27 public static SimulatedType create(Object obj) { 28 Constructor [] constructor = resolveSimType(obj).getDeclaredConstructors(); 29 try { 30 return (SimulatedType) constructor[0].newInstance(new Object [] { obj }); 31 } catch (Exception e) { 32 throw new RuntimeException (e); 33 } 34 } 35 36 private static Class resolveSimType(Object obj) { 37 Class simType = (Class ) SIM_TYPES.get(obj.getClass().getName()); 38 if (simType != null) return simType; 39 40 simType = resolveInterface(obj.getClass()); 41 if (simType != null) return simType; 42 43 throw new RuntimeException ("Type not supported: " + obj.getClass().getName()); 44 } 45 46 private static Class resolveInterface(Class clazz) { 47 Class simType = null; 48 Class [] interfaces = clazz.getInterfaces(); 49 for (int i = 0; i < interfaces.length; i++) { 50 simType = (Class ) SIM_TYPES.get(interfaces[i].getName()); 51 if (simType != null) return simType; 52 simType = resolveInterface(interfaces[i]); 53 if (simType != null) return simType; 54 } 55 return null; 56 } 57 } 58 | Popular Tags |