1 21 22 package org.apache.derby.iapi.types; 23 24 import org.apache.derby.catalog.TypeDescriptor; 25 26 import org.apache.derby.iapi.reference.SQLState; 27 28 import org.apache.derby.iapi.services.io.ArrayInputStream; 29 30 import org.apache.derby.iapi.services.loader.ClassInspector; 31 import org.apache.derby.iapi.services.sanity.SanityManager; 32 import org.apache.derby.iapi.services.io.StoredFormatIds; 33 34 import org.apache.derby.iapi.error.StandardException; 35 36 import org.apache.derby.iapi.types.DataValueDescriptor; 37 import org.apache.derby.iapi.types.TypeId; 38 39 import org.apache.derby.iapi.types.BooleanDataValue; 40 import org.apache.derby.iapi.types.UserDataValue; 41 42 import org.apache.derby.iapi.services.cache.ClassSize; 43 44 import java.sql.Date ; 45 import java.sql.Time ; 46 import java.sql.Timestamp ; 47 48 import java.io.ObjectOutput ; 49 import java.io.ObjectInput ; 50 import java.io.IOException ; 51 52 import java.sql.ResultSet ; 53 import java.sql.SQLException ; 54 55 import java.util.Calendar ; 56 57 58 62 63 public class UserType extends DataType 64 implements UserDataValue 65 { 66 private Object value; 67 68 72 73 private static final int BASE_MEMORY_USAGE = ClassSize.estimateBaseFromCatalog( UserType.class); 74 75 public int estimateMemoryUsage() 76 { 77 int sz = BASE_MEMORY_USAGE; 78 if( null != value) 79 { 80 sz += ClassSize.estimateAndCatalogBase( value.getClass()); 83 } 84 85 return sz; 86 } 88 public String getString() 89 { 90 if (! isNull()) 91 { 92 return value.toString(); 93 94 } 95 else 96 { 97 return null; 98 } 99 } 100 101 104 public boolean getBoolean() throws StandardException 105 { 106 if (! isNull()) 107 if (value instanceof Boolean ) return ((Boolean )value).booleanValue(); 108 return super.getBoolean(); 109 } 110 111 114 public byte getByte() throws StandardException 115 { 116 if (! isNull()) 117 if (value instanceof Number ) return ((Number )value).byteValue(); 119 return super.getByte(); 120 } 121 122 125 public short getShort() throws StandardException 126 { 127 if (! isNull()) 128 if (value instanceof Number ) return ((Number )value).shortValue(); 130 return super.getShort(); 131 } 132 133 136 public int getInt() throws StandardException 137 { 138 if (! isNull()) 139 if (value instanceof Number ) return ((Number )value).intValue(); 141 return super.getInt(); 142 } 143 144 147 148 public long getLong() throws StandardException 149 { 150 if (! isNull()) 151 if (value instanceof Number ) return ((Number )value).longValue(); 153 return super.getLong(); 154 } 155 156 157 160 public float getFloat() throws StandardException 161 { 162 if (! isNull()) 163 if (value instanceof Number ) return ((Number )value).floatValue(); 165 return super.getFloat(); 166 } 167 168 169 172 public double getDouble() throws StandardException 173 { 174 if (! isNull()) 175 if (value instanceof Number ) return ((Number )value).doubleValue(); 177 return super.getDouble(); 178 } 179 180 183 public byte[] getBytes() throws StandardException 184 { 185 if (! isNull()) 186 if (value instanceof byte[]) return ((byte[])value); 187 return super.getBytes(); 188 } 189 190 194 public Date getDate( Calendar cal) throws StandardException 195 { 196 if (! isNull()) 197 { 198 if (value instanceof Date ) 199 return ((Date )value); 200 else if (value instanceof Timestamp ) 201 return (new SQLTimestamp((Timestamp )value).getDate(cal)); 202 } 203 return super.getDate(cal); 204 } 205 206 209 public Time getTime( Calendar cal) throws StandardException 210 { 211 if (! isNull()) 212 { 213 if (value instanceof Time ) 214 return ((Time )value); 215 else if (value instanceof Timestamp ) 216 return (new SQLTimestamp((Timestamp )value).getTime(cal)); 217 } 218 return super.getTime(cal); 219 } 220 221 224 public Timestamp getTimestamp( Calendar cal) throws StandardException 225 { 226 if (! isNull()) 227 { 228 if (value instanceof Timestamp ) 229 return ((Timestamp )value); 230 else if (value instanceof Date ) 231 return (new SQLDate((Date )value).getTimestamp(cal)); 232 else if (value instanceof Time ) 233 return (new SQLTime((Time )value).getTimestamp(cal)); 234 } 235 return super.getTimestamp(cal); 236 } 237 238 public Object getObject() 239 { 240 return value; 241 } 242 243 public int getLength() 244 { 245 return TypeDescriptor.MAXIMUM_WIDTH_UNKNOWN; 246 } 247 248 249 public String getTypeName() 250 { 251 252 return isNull() ? "JAVA_OBJECT" : ClassInspector.readableClassName(value.getClass()); 253 } 254 255 259 String getTypeName(String className) 260 { 261 return className; 262 } 263 264 267 268 273 public int getTypeFormatId() { 274 return StoredFormatIds.SQL_USERTYPE_ID_V3; 275 } 276 277 281 public void writeExternal(ObjectOutput out) throws IOException { 282 283 if (SanityManager.DEBUG) 284 SanityManager.ASSERT(!isNull(), "writeExternal() is not supposed to be called for null values."); 285 286 out.writeObject(value); 287 } 288 289 296 public void readExternal(ObjectInput in) 297 throws IOException , ClassNotFoundException 298 { 299 300 value = in.readObject(); 301 } 302 public void readExternalFromArray(ArrayInputStream in) 303 throws IOException , ClassNotFoundException 304 { 305 306 value = in.readObject(); 307 } 308 309 312 313 314 public DataValueDescriptor getClone() 315 { 316 return new UserType(value); 318 } 319 320 323 public DataValueDescriptor getNewNull() 324 { 325 return new UserType(); 326 } 327 331 332 public void restoreToNull() 333 { 334 value = null; 335 } 336 337 340 341 346 public void setValueFromResultSet(ResultSet resultSet, int colNumber, 347 boolean isNullable) 348 throws SQLException 349 { 350 value = resultSet.getObject(colNumber); 351 } 352 353 361 public int compare(DataValueDescriptor other) 362 throws StandardException 363 { 364 367 if (typePrecedence() < other.typePrecedence()) 368 { 369 return - (other.compare(this)); 370 } 371 372 boolean thisNull, otherNull; 373 374 thisNull = this.isNull(); 375 otherNull = other.isNull(); 376 377 383 if (thisNull || otherNull) 384 { 385 if (!thisNull) return -1; 387 if (!otherNull) return 1; 389 return 0; 390 } 391 392 395 396 int comparison; 397 398 try 399 { 400 comparison = ((java.lang.Comparable ) value).compareTo(other.getObject()); 401 } 402 catch (ClassCastException cce) 403 { 404 throw StandardException.newException(SQLState.LANG_INVALID_COMPARE_TO, 405 getTypeName(), 406 ClassInspector.readableClassName(other.getObject().getClass())); 407 } 408 412 if (comparison < 0) 413 comparison = -1; 414 else if (comparison > 0) 415 comparison = 1; 416 417 return comparison; 418 } 419 420 423 public boolean compare(int op, 424 DataValueDescriptor other, 425 boolean orderedNulls, 426 boolean unknownRV) 427 throws StandardException 428 { 429 if (!orderedNulls) { 431 if (this.isNull() || other.isNull()) 432 return unknownRV; 433 } 434 435 439 if ( (op == ORDER_OP_EQUALS) && 440 (! this.isNull()) && (! other.isNull()) ) 441 { 442 Object o = getObject(); 446 447 if (!(o instanceof java.lang.Comparable )) 448 { 449 return o.equals(other.getObject()); 450 } 451 } 452 453 454 455 return super.compare(op, other, orderedNulls, unknownRV); 456 } 457 458 461 462 465 466 467 public UserType() { } 468 469 public UserType(Object value) 470 { 471 this.value = value; 472 } 473 477 public void setValue(Object value) 478 { 479 this.value = value; 480 } 481 protected void setFrom(DataValueDescriptor theValue) throws StandardException { 482 483 setValue(theValue.getObject()); 484 } 485 486 490 public void setBigDecimal(Number theValue) 491 { 492 setValue((Object ) theValue); 494 } 495 496 public void setValue(String theValue) 497 { 498 if (theValue == null) 499 { 500 value = null; 501 } 502 else 503 { 504 value = theValue; 506 } 507 } 508 509 512 513 524 525 public BooleanDataValue equals(DataValueDescriptor left, 526 DataValueDescriptor right) 527 throws StandardException 528 { 529 return SQLBoolean.truthValue(left, 530 right, 531 left.compare(ORDER_OP_EQUALS, right, true, false)); 532 } 533 534 546 547 public BooleanDataValue notEquals(DataValueDescriptor left, 548 DataValueDescriptor right) 549 throws StandardException 550 { 551 return SQLBoolean.truthValue(left, 552 right, 553 !left.compare(ORDER_OP_EQUALS, right, true, false)); 554 } 555 556 557 558 561 562 public String toString() 563 { 564 if (isNull()) 565 { 566 return "NULL"; 567 } 568 else 569 { 570 return value.toString(); 571 } 572 } 573 574 577 public int hashCode() 578 { 579 if (isNull()) 580 return 0; 581 return value.hashCode(); 582 } 583 584 585 public int typePrecedence() 586 { 587 return TypeId.USER_PRECEDENCE; 588 } 589 590 595 public final boolean isNull() 596 { 597 return (value == null); 598 } 599 } 600 601 | Popular Tags |