1 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 ; 28 import java.io.ObjectInput ; 29 import java.io.ObjectOutput ; 30 31 41 public final class JSQLType implements Formatable 42 { 43 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 static private final String [] wrapperClassNames = 65 { 66 "java.lang.Boolean", 67 "java.lang.Integer", "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 [] primitiveNames = 77 { 78 "boolean", 79 "char", 80 "byte", 81 "short", 82 "int", 83 "long", 84 "float", 85 "double" 86 }; 87 88 89 91 private byte category = JAVA_PRIMITIVE; 92 private DataTypeDescriptor sqlType; 93 private String javaClassName; 94 private byte primitiveKind; 95 96 97 103 106 public JSQLType() { initialize( INT ); } 107 108 109 114 public JSQLType 115 ( 116 DataTypeDescriptor sqlType 117 ) 118 { initialize( sqlType ); } 119 120 125 public JSQLType 126 ( 127 String javaName 128 ) 129 { 130 byte primitiveID = getPrimitiveID( javaName ); 131 132 if ( primitiveID != NOT_PRIMITIVE ) { initialize( primitiveID ); } 133 else { initialize( javaName ); } 134 } 135 136 141 public JSQLType 142 ( 143 byte primitiveKind 144 ) 145 { initialize( primitiveKind ); } 146 147 152 public byte getCategory() { return category; } 153 154 160 public byte getPrimitiveKind() { return primitiveKind; } 161 162 168 public String getJavaClassName() { return javaClassName; } 169 170 176 public DataTypeDescriptor getSQLType 177 ( 178 ) 179 { 180 if ( sqlType == null ) 182 { 183 String 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 206 211 public int getTypeFormatId() { return StoredFormatIds.JSQLTYPEIMPL_ID; } 212 213 218 public void readExternal( ObjectInput in ) 219 throws IOException , ClassNotFoundException 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 ) in.readObject() ); 233 break; 234 235 case JAVA_PRIMITIVE: 236 237 initialize( in.readByte() ); 238 break; 239 } 240 } 241 242 246 public void writeExternal( ObjectOutput out ) 247 throws IOException 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 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 javaClassName ) 285 { initialize( JAVA_CLASS, null, javaClassName, NOT_PRIMITIVE ); } 286 287 295 private void initialize 296 ( 297 byte category, 298 DataTypeDescriptor sqlType, 299 String 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 317 324 private static String getWrapperClassName 325 ( 326 byte primitive 327 ) 328 { 329 if ( primitive == NOT_PRIMITIVE ) { return ""; } 330 return wrapperClassNames[ primitive ]; 331 } 332 333 334 342 private static byte getPrimitiveID 343 ( 344 String 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 |