KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > types > JSQLType


1 /*
2
3    Derby - Class org.apache.derby.iapi.types.JSQLType
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.iapi.types;
23
24 import org.apache.derby.iapi.services.io.StoredFormatIds;
25 import org.apache.derby.iapi.services.io.Formatable;
26
27 import java.io.IOException JavaDoc;
28 import java.io.ObjectInput JavaDoc;
29 import java.io.ObjectOutput JavaDoc;
30
31 /**
32  * Type descriptor which wraps all 3 kinds of types supported in Cloudscape's
33  * JSQL language: SQL types, Java primitives, Java classes.
34  *
35  * This interface was originally added to support the serializing of WorkUnit
36  * signatures.
37  *
38  *
39  * @author Rick
40  */

41 public final class JSQLType implements Formatable
42 {
43     ///////////////////////////////////////////////////////////////////////
44
//
45
// CONSTANTS
46
//
47
///////////////////////////////////////////////////////////////////////
48

49     public static final byte SQLTYPE = 0;
50     public static final byte JAVA_CLASS = 1;
51     public static final byte JAVA_PRIMITIVE = 2;
52
53     public static final byte NOT_PRIMITIVE = -1;
54     public static final byte BOOLEAN = 0;
55     public static final byte CHAR = 1;
56     public static final byte BYTE = 2;
57     public static final byte SHORT = 3;
58     public static final byte INT = 4;
59     public static final byte LONG = 5;
60     public static final byte FLOAT = 6;
61     public static final byte DOUBLE = 7;
62
63     // these two arrays are in the order of the primitive constants
64
static private final String JavaDoc[] wrapperClassNames =
65     {
66         "java.lang.Boolean",
67         "java.lang.Integer", // we can't serialize char, so we convert it to int
68
"java.lang.Integer",
69         "java.lang.Integer",
70         "java.lang.Integer",
71         "java.lang.Long",
72         "java.lang.Float",
73         "java.lang.Double"
74     };
75
76     static public final String JavaDoc[] primitiveNames =
77     {
78         "boolean",
79         "char",
80         "byte",
81         "short",
82         "int",
83         "long",
84         "float",
85         "double"
86     };
87
88
89     // here are the fields we serialize
90

91     private byte category = JAVA_PRIMITIVE;
92     private DataTypeDescriptor sqlType;
93     private String JavaDoc javaClassName;
94     private byte primitiveKind;
95
96
97     ///////////////////////////////////////////////////////////////////////
98
//
99
// CONSTRUCTORS
100
//
101
///////////////////////////////////////////////////////////////////////
102

103     /**
104       * Public 0-arg constructor for Formatable machinery.
105       */

106     public JSQLType() { initialize( INT ); }
107
108
109     /**
110       * Create a JSQLType from a SQL type.
111       *
112       * @param sqlType the SQL type to wrap
113       */

114     public JSQLType
115     (
116         DataTypeDescriptor sqlType
117     )
118     { initialize( sqlType ); }
119
120     /**
121       * Create a JSQLType given the name of a Java primitive or java class.
122       *
123       * @param javaName name of java primitive or class to wrap
124       */

125     public JSQLType
126     (
127         String JavaDoc javaName
128     )
129     {
130         byte primitiveID = getPrimitiveID( javaName );
131
132         if ( primitiveID != NOT_PRIMITIVE ) { initialize( primitiveID ); }
133         else { initialize( javaName ); }
134     }
135
136     /**
137       * Create a JSQLType for a Java primitive.
138       *
139       * @param primitiveKind primitive to wrap
140       */

141     public JSQLType
142     (
143         byte primitiveKind
144     )
145     { initialize( primitiveKind ); }
146
147     /**
148       * What kind of type is this:
149       *
150       * @return one of the following: SQLTYPE, JAVA_PRIMITIVE, JAVA_CLASS
151       */

152     public byte getCategory() { return category; }
153
154     /**
155       * If this is a JAVA_PRIMITIVE, what is its name?
156       *
157       * @return BOOLEAN, INT, ... if this is a JAVA_PRIMITIVE.
158       * NOT_PRIMITIVE if this is SQLTYPE or JAVA_CLASS.
159       */

160     public byte getPrimitiveKind() { return primitiveKind; }
161
162     /**
163       * If this is a JAVA_CLASS, what is it's name?
164       *
165       * @return java class name if this is a JAVA_CLASS
166       * null if this is SQLTYPE or JAVA_PRIMITIVE
167       */

168     public String JavaDoc getJavaClassName() { return javaClassName; }
169
170     /**
171       * What's our SQLTYPE?
172       *
173       * @return the DataTypeDescriptor corresponding to this type
174       *
175       */

176     public DataTypeDescriptor getSQLType
177     (
178     )
179     {
180         // might not be filled in if this is a JAVA_CLASS or JAVA_PRIMITIVE
181
if ( sqlType == null )
182         {
183             String JavaDoc className;
184
185             if ( category == JAVA_CLASS )
186             {
187                 className = javaClassName;
188             }
189             else
190             {
191                 className = getWrapperClassName( primitiveKind );
192             }
193
194             sqlType = DataTypeDescriptor.getSQLDataTypeDescriptor( className );
195         }
196
197         return sqlType;
198     }
199
200     ///////////////////////////////////////////////////////////////////////
201
//
202
// Formatable BEHAVIOR
203
//
204
///////////////////////////////////////////////////////////////////////
205

206     /**
207      * Get the formatID which corresponds to this class.
208      *
209      * @return the formatID of this class
210      */

211     public int getTypeFormatId() { return StoredFormatIds.JSQLTYPEIMPL_ID; }
212
213     /**
214       @see java.io.Externalizable#readExternal
215       @exception IOException thrown on error
216       @exception ClassNotFoundException thrown on error
217       */

218     public void readExternal( ObjectInput JavaDoc in )
219          throws IOException JavaDoc, ClassNotFoundException JavaDoc
220     {
221         byte frozenCategory = in.readByte();
222
223         switch ( frozenCategory )
224         {
225             case SQLTYPE:
226
227                 initialize( (DataTypeDescriptor) in.readObject() );
228                 break;
229
230             case JAVA_CLASS:
231
232                 initialize( (String JavaDoc) in.readObject() );
233                 break;
234
235             case JAVA_PRIMITIVE:
236
237                 initialize( in.readByte() );
238                 break;
239         }
240     }
241
242     /**
243
244       @exception IOException thrown on error
245       */

246     public void writeExternal( ObjectOutput JavaDoc out )
247          throws IOException JavaDoc
248     {
249         out.writeByte( category );
250
251         switch ( category )
252         {
253             case SQLTYPE:
254
255                 out.writeObject( sqlType );
256                 break;
257
258             case JAVA_CLASS:
259
260                 out.writeObject( javaClassName );
261                 break;
262
263             case JAVA_PRIMITIVE:
264
265                 out.writeByte( primitiveKind );
266                 break;
267
268         }
269     }
270
271
272     ///////////////////////////////////////////////////////////////////////
273
//
274
// INITIALIZATION MINIONS
275
//
276
///////////////////////////////////////////////////////////////////////
277

278     private void initialize( byte primitiveKind )
279     { initialize( JAVA_PRIMITIVE, null, null, primitiveKind ); }
280
281     private void initialize( DataTypeDescriptor sqlType )
282     { initialize( SQLTYPE, sqlType, null, NOT_PRIMITIVE ); }
283
284     private void initialize( String JavaDoc javaClassName )
285     { initialize( JAVA_CLASS, null, javaClassName, NOT_PRIMITIVE ); }
286
287     /**
288       * Initialize this JSQL type. Minion of all constructors.
289       *
290       * @param category SQLTYPE, JAVA_CLASS, JAVA_PRIMITIVE
291       * @param sqlType corresponding SQL type if category=SQLTYPE
292       * @param javaClassName corresponding java class if category=JAVA_CLASS
293       * @param primitiveKind kind of primitive if category=JAVA_PRIMITIVE
294       */

295     private void initialize
296     (
297         byte category,
298         DataTypeDescriptor sqlType,
299         String JavaDoc javaClassName,
300         byte primitiveKind
301     )
302     {
303         this.category = category;
304         this.sqlType = sqlType;
305         this.javaClassName = javaClassName;
306         this.primitiveKind = primitiveKind;
307
308     }
309      
310
311     ///////////////////////////////////////////////////////////////////////
312
//
313
// GENERAL MINIONS
314
//
315
///////////////////////////////////////////////////////////////////////
316

317     /**
318       * Gets the name of the java wrapper class corresponding to a primitive.
319       *
320       * @param primitive BOOLEAN, INT, ... etc.
321       *
322       * @return name of the java wrapper class corresponding to the primitive
323       */

324     private static String JavaDoc getWrapperClassName
325     (
326         byte primitive
327     )
328     {
329         if ( primitive == NOT_PRIMITIVE ) { return ""; }
330         return wrapperClassNames[ primitive ];
331     }
332
333
334     /**
335       * Translate the name of a java primitive to an id
336       *
337       * @param name name of primitive
338       *
339       * @return BOOLEAN, INT, ... etc if the name is that of a primitive.
340       * NOT_PRIMITIVE otherwise
341       */

342     private static byte getPrimitiveID
343     (
344         String JavaDoc name
345     )
346     {
347         for ( byte ictr = BOOLEAN; ictr <= DOUBLE; ictr++ )
348         {
349             if ( primitiveNames[ ictr ].equals( name ) ) { return ictr; }
350         }
351
352         return NOT_PRIMITIVE;
353     }
354
355
356 }
357
Popular Tags