1 7 package com.inversoft.util.typeconverter; 8 9 10 import java.util.Hashtable ; 11 import java.util.Map ; 12 13 import com.inversoft.util.ReflectionException; 14 import com.inversoft.util.ReflectionTools; 15 16 17 32 public class TypeConverterRegistry { 33 34 private static Map converters = new Hashtable (); 35 36 static { 38 Object obj; 39 40 try { 41 obj = ReflectionTools.instantiate("com.inversoft.util.typeconverter.converters.BooleanTypeConverter"); 42 converters.put(Boolean .class, obj); 43 converters.put(Boolean.TYPE, obj); 44 } catch (ReflectionException re) { 45 System.err.println("WARNING - boolean type converter not found"); 46 } 47 48 try { 49 obj = ReflectionTools.instantiate("com.inversoft.util.typeconverter.converters.NumberTypeConverter"); 50 converters.put(Number .class, obj); 51 converters.put(Byte.TYPE, obj); 52 converters.put(Double.TYPE, obj); 53 converters.put(Float.TYPE, obj); 54 converters.put(Integer.TYPE, obj); 55 converters.put(Long.TYPE, obj); 56 converters.put(Short.TYPE, obj); 57 } catch (ReflectionException re) { 58 System.err.println("WARNING - Number type converter not found"); 59 } 60 61 try { 62 obj = ReflectionTools.instantiate("com.inversoft.util.typeconverter.converters.CharacterTypeConverter"); 63 converters.put(Character .class, obj); 64 converters.put(Character.TYPE, obj); 65 } catch (ReflectionException re) { 66 System.err.println("WARNING - character type converter not found"); 67 } 68 69 try { 70 obj = ReflectionTools.instantiate("com.inversoft.util.typeconverter.BaseTypeConverter"); 71 converters.put(String .class, obj); 72 } catch (ReflectionException re) { 73 System.err.println("WARNING - String type converter not found"); 74 } 75 } 76 77 78 private TypeConverterRegistry() { 79 } 80 81 109 public static TypeConverter lookup(Class type) { 110 Class localType = type; 111 TypeConverter converter = null; 112 113 if (localType.isArray()) { 116 localType = localType.getComponentType(); 117 } 118 119 while (localType != null && localType != Object .class) { 122 converter = (TypeConverter) converters.get(localType); 123 if (converter == null) { 124 localType = localType.getSuperclass(); 125 } else { 126 break; 127 } 128 } 129 130 144 145 return converter; 146 } 147 148 157 public static TypeConverter register(Class type, TypeConverter converter) { 158 return (TypeConverter) converters.put(type, converter); 159 } 160 161 168 public static TypeConverter unregister(Class type) { 169 return (TypeConverter) converters.remove(type); 170 } 171 } | Popular Tags |