1 29 30 package com.caucho.amber.type; 31 32 import com.caucho.bytecode.JClass; 33 import com.caucho.config.ConfigException; 34 import com.caucho.util.L10N; 35 36 import java.util.HashMap ; 37 38 41 public class TypeManager { 42 private static final L10N L = new L10N(TypeManager.class); 43 44 private static HashMap <String ,Type> _builtinTypes; 45 private static HashMap <String ,Type> _primitiveTypes; 46 47 private HashMap <String ,Type> _typeMap; 48 49 public TypeManager() 50 { 51 _typeMap = new HashMap <String ,Type>(); 52 53 _typeMap.putAll(_builtinTypes); 54 } 55 56 59 public Type create(String name) 60 throws ConfigException 61 { 62 Type type = _typeMap.get(name); 63 64 if (type != null) 65 return type; 66 67 throw new ConfigException(L.l("'{0}' is an unknown type", name)); 68 } 69 70 73 public Type create(JClass cl) 74 throws ConfigException 75 { 76 Type type = _primitiveTypes.get(cl.getName()); 77 78 if (type != null) 79 return type; 80 81 type = _typeMap.get(cl.getName()); 82 83 return type; 84 } 85 86 89 public Type get(String name) 90 { 91 return _typeMap.get(name); 92 } 93 94 97 public EntityType getEntityByInstanceClass(String name) 98 { 99 for (Type type : _typeMap.values()) { 100 if (type instanceof EntityType) { 101 EntityType entityType = (EntityType) type; 102 103 if (name.equals(entityType.getInstanceClassName())) 104 return entityType; 105 } 106 } 107 108 return null; 109 } 110 111 114 public void put(String name, Type type) 115 { 116 Type oldType = _typeMap.get(name); 117 118 if (oldType != null && oldType != type) 119 throw new IllegalStateException (L.l("'{0}' is a duplicate type", 120 name)); 121 122 _typeMap.put(name, type); 123 } 124 125 static { 126 _builtinTypes = new HashMap <String ,Type>(); 127 128 _builtinTypes.put("boolean", BooleanType.create()); 129 _builtinTypes.put("java.lang.Boolean", BooleanType.create()); 130 _builtinTypes.put("yes_no", YesNoType.create()); 131 _builtinTypes.put("true_false", TrueFalseType.create()); 132 133 _builtinTypes.put("byte", ByteType.create()); 134 _builtinTypes.put("java.lang.Byte", ByteType.create()); 135 136 _builtinTypes.put("character", CharacterType.create()); 137 _builtinTypes.put("java.lang.Character", CharacterType.create()); 138 139 _builtinTypes.put("short", ShortType.create()); 140 _builtinTypes.put("java.lang.Short", ShortType.create()); 141 142 _builtinTypes.put("integer", IntegerType.create()); 143 _builtinTypes.put("java.lang.Integer", IntegerType.create()); 144 145 _builtinTypes.put("long", LongType.create()); 146 _builtinTypes.put("java.lang.Long", LongType.create()); 147 148 _builtinTypes.put("float", FloatType.create()); 149 _builtinTypes.put("java.lang.Float", FloatType.create()); 150 151 _builtinTypes.put("double", DoubleType.create()); 152 _builtinTypes.put("java.lang.Double", DoubleType.create()); 153 154 _builtinTypes.put("string", StringType.create()); 155 _builtinTypes.put("java.lang.String", StringType.create()); 156 157 _builtinTypes.put("date", SqlDateType.create()); 158 _builtinTypes.put("java.sql.Date", SqlDateType.create()); 159 160 _builtinTypes.put("time", SqlTimeType.create()); 161 _builtinTypes.put("java.sql.Time", SqlTimeType.create()); 162 163 _builtinTypes.put("timestamp", SqlTimestampType.create()); 164 _builtinTypes.put("java.sql.Timestamp", SqlTimestampType.create()); 165 166 _builtinTypes.put("java.util.Date", UtilDateType.create()); 167 _builtinTypes.put("java.util.Calendar", CalendarType.create()); 168 169 _builtinTypes.put("timestamp", BigDecimalType.create()); 171 _builtinTypes.put("java.math.BigDecimal", BigDecimalType.create()); 172 _builtinTypes.put("java.math.BigInteger", BigIntegerType.create()); 173 174 _builtinTypes.put("blob", BlobType.create()); 175 _builtinTypes.put("java.sql.Blob", BlobType.create()); 176 _builtinTypes.put("clob", BlobType.create()); 177 _builtinTypes.put("java.sql.Clob", ClobType.create()); 178 179 _builtinTypes.put("[B", ByteArrayType.create()); 180 _builtinTypes.put("[byte", PrimitiveByteArrayType.create()); 181 _builtinTypes.put("[java.lang.Byte", ByteArrayType.create()); 182 183 _builtinTypes.put("[char", PrimitiveCharArrayType.create()); 184 _builtinTypes.put("[java.lang.Character", CharacterArrayType.create()); 185 186 _builtinTypes.put("class", ClassType.create()); 187 _builtinTypes.put("java.lang.Class", ClassType.create()); 188 189 _primitiveTypes = new HashMap <String ,Type>(); 190 191 _primitiveTypes.put("boolean", PrimitiveBooleanType.create()); 192 _primitiveTypes.put("char", PrimitiveCharType.create()); 193 _primitiveTypes.put("byte", PrimitiveByteType.create()); 194 _primitiveTypes.put("short", PrimitiveShortType.create()); 195 _primitiveTypes.put("int", PrimitiveIntType.create()); 196 _primitiveTypes.put("long", PrimitiveLongType.create()); 197 _primitiveTypes.put("float", PrimitiveFloatType.create()); 198 _primitiveTypes.put("double", PrimitiveDoubleType.create()); 199 } 200 } 201 | Popular Tags |