KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > type > TypeManager


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

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 JavaDoc;
37
38 /**
39  * The manages the types known to the Amber instnce.
40  */

41 public class TypeManager {
42   private static final L10N L = new L10N(TypeManager.class);
43
44   private static HashMap JavaDoc<String JavaDoc,Type> _builtinTypes;
45   private static HashMap JavaDoc<String JavaDoc,Type> _primitiveTypes;
46
47   private HashMap JavaDoc<String JavaDoc,Type> _typeMap;
48
49   public TypeManager()
50   {
51     _typeMap = new HashMap JavaDoc<String JavaDoc,Type>();
52
53     _typeMap.putAll(_builtinTypes);
54   }
55
56   /**
57    * Returns the type.
58    */

59   public Type create(String JavaDoc 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   /**
71    * Returns the type.
72    */

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   /**
87    * Returns the type.
88    */

89   public Type get(String JavaDoc name)
90   {
91     return _typeMap.get(name);
92   }
93
94   /**
95    * Returns the type.
96    */

97   public EntityType getEntityByInstanceClass(String JavaDoc 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   /**
112    * Adds a type.
113    */

114   public void put(String JavaDoc name, Type type)
115   {
116     Type oldType = _typeMap.get(name);
117
118     if (oldType != null && oldType != type)
119       throw new IllegalStateException JavaDoc(L.l("'{0}' is a duplicate type",
120                                           name));
121
122     _typeMap.put(name, type);
123   }
124
125   static {
126     _builtinTypes = new HashMap JavaDoc<String JavaDoc,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     //XXX Need test case for timestamp
170
_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 JavaDoc<String JavaDoc,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