|                                                                                                              1
 21
 22  package org.apache.derby.iapi.types;
 23
 24  import org.apache.derby.iapi.reference.SQLState;
 25
 26  import org.apache.derby.iapi.services.io.ArrayInputStream;
 27
 28  import org.apache.derby.iapi.types.BooleanDataValue;
 29  import org.apache.derby.iapi.types.DataValueDescriptor;
 30  import org.apache.derby.iapi.types.NumberDataValue;
 31  import org.apache.derby.iapi.types.StringDataValue;
 32  import org.apache.derby.iapi.types.TypeId;
 33
 34  import org.apache.derby.iapi.services.io.Storable;
 35  import org.apache.derby.iapi.services.io.StoredFormatIds;
 36
 37  import org.apache.derby.iapi.error.StandardException;
 38
 39  import org.apache.derby.iapi.services.sanity.SanityManager;
 40  import org.apache.derby.iapi.types.NumberDataType;
 41  import org.apache.derby.iapi.types.SQLBoolean;
 42
 43  import org.apache.derby.iapi.services.cache.ClassSize;
 44
 45  import java.io.ObjectOutput
  ; 46  import java.io.ObjectInput
  ; 47  import java.io.IOException
  ; 48
 49  import java.sql.ResultSet
  ; 50  import java.sql.PreparedStatement
  ; 51  import java.sql.SQLException
  ; 52
 53
 81  public final class SQLReal
 82      extends NumberDataType
 83  {
 84
 85
 89
 90
 91
 94
 98      public int  getInt() throws StandardException
 99      {
 100         if ((value > (((double) Integer.MAX_VALUE + 1.0d))) || (value < (((double) Integer.MIN_VALUE) - 1.0d)))
 101             throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, "INTEGER");
 102         return (int) value;
 103     }
 104
 105
 109     public byte getByte() throws StandardException
 110     {
 111         if ((value > (((double) Byte.MAX_VALUE + 1.0d))) || (value < (((double) Byte.MIN_VALUE) - 1.0d)))
 112             throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, "TINYINT");
 113         return (byte) value;
 114     }
 115
 116
 120     public short    getShort() throws StandardException
 121     {
 122         if ((value > (((double) Short.MAX_VALUE + 1.0d))) || (value < (((double) Short.MIN_VALUE) - 1.0d)))
 123             throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, "SMALLINT");
 124         return (short) value;
 125     }
 126
 127
 131     public long getLong() throws StandardException
 132     {
 133         if ((value > (((double) Long.MAX_VALUE + 1.0d))) || (value < (((double) Long.MIN_VALUE) - 1.0d)))
 134             throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, "BIGINT");
 135         return (long) value;
 136     }
 137
 138
 141     public float    getFloat()
 142     {
 143         return value;
 144     }
 145
 146
 149     public double   getDouble()
 150     {
 151         return (double) value;
 152     }
 153
 154
 157     public int typeToBigDecimal()
 158     {
 159         return java.sql.Types.CHAR;
 160     }
 161
 166     public boolean  getBoolean()
 167     {
 168         return (value != 0);
 169     }
 170
 171
 174     public String
  getString() 175     {
 176         if (isNull())
 177             return null;
 178         else
 179             return Float.toString(value);
 180     }
 181
 182
 185     public int  getLength()
 186     {
 187         return REAL_LENGTH;
 188     }
 189
 190
 193     public Object
  getObject() 194     {
 195         if (isNull())
 196             return null;
 197         else
 198             return new Float
  (value); 199     }
 200
 201         public String
  getTypeName() 203     {
 204         return TypeId.REAL_NAME;
 205     }
 206
 207
 210
 211
 212
 217     public int getTypeFormatId() {
 218         return StoredFormatIds.SQL_REAL_ID;
 219     }
 220
 221
 224
 225     public boolean isNull()
 226     {
 227         return isnull;
 228     }
 229
 230     public void writeExternal(ObjectOutput
  out) throws IOException  { 231
 232                 if (SanityManager.DEBUG)
 234             SanityManager.ASSERT(! isNull());
 235
 236         out.writeFloat(value);
 237     }
 238
 239
 240     public void readExternal(ObjectInput
  in) throws IOException  { 241                         value = in.readFloat();
 244         isnull = false;
 245     }
 246     public void readExternalFromArray(ArrayInputStream in) throws IOException
  { 247                         value = in.readFloat();
 250         isnull = false;
 251     }
 252
 253
 257     public void restoreToNull()
 258     {
 259         value = 0;
 260         isnull = true;
 261     }
 262
 263
 264
 265     protected int typeCompare(DataValueDescriptor arg) throws StandardException
 266     {
 267
 268
 269                 float thisValue = this.getFloat();
 271         float otherValue = NumberDataType.normalizeREAL(arg.getFloat());
 273         if (thisValue == otherValue)
 274             return 0;
 275         else if (thisValue > otherValue)
 276             return 1;
 277         else
 278             return -1;
 279     }
 280
 281
 284
 285
 286     public DataValueDescriptor getClone()
 287     {
 288         SQLReal ret = new SQLReal();
 289         ret.value = this.value;
 290         ret.isnull = this.isnull;
 291         return ret;
 292     }
 293
 294
 297     public DataValueDescriptor getNewNull()
 298     {
 299         return new SQLReal();
 300     }
 301
 302
 308     public void setValueFromResultSet(ResultSet
  resultSet, int colNumber, 309                                       boolean isNullable)
 310         throws StandardException, SQLException
  311     {
 312             float fv = resultSet.getFloat(colNumber);
 313             if (isNullable && resultSet.wasNull())
 314                 restoreToNull();
 315             else
 316                 setValue(fv);
 317     }
 318
 323     public final void setInto(PreparedStatement
  ps, int position) throws SQLException  { 324
 325         if (isNull()) {
 326             ps.setNull(position, java.sql.Types.REAL);
 327             return;
 328         }
 329
 330         ps.setFloat(position, value);
 331     }
 332
 339     public final void setInto(ResultSet
  rs, int position) throws SQLException  , StandardException { 340         rs.updateFloat(position, value);
 341     }
 342
 343
 346
 347
 350
 351
 352             public SQLReal()
 355     {
 356         isnull = true;
 357     }
 358
 359     public SQLReal(float val)
 360         throws StandardException
 361     {
 362         value = NumberDataType.normalizeREAL(val);
 363     }
 364     public SQLReal(Float
  obj) throws StandardException { 365         if (isnull = (obj == null))
 366             ;
 367         else {
 368             value = NumberDataType.normalizeREAL(obj.floatValue());
 369         }
 370     }
 371
 372
 375     public void setValue(String
  theValue) 376         throws StandardException
 377     {
 378         if (theValue == null)
 379         {
 380             value = 0;
 381             isnull = true;
 382         }
 383         else
 384         {
 385                                     try {
 388                 setValue(Double.valueOf(theValue.trim()).doubleValue());
 389             } catch (NumberFormatException
  nfe) { 390                 throw invalidFormat();
 391             }
 392         }
 393     }
 394
 395     public  void setValue(Number
  theValue) throws StandardException 396     {
 397         if (objectNull(theValue))
 398             return;
 399
 400         if (SanityManager.ASSERT)
 401         {
 402             if (!(theValue instanceof java.lang.Float
  )) 403                 SanityManager.THROWASSERT("SQLReal.setValue(Number) passed a " + theValue.getClass());
 404         }
 405
 406         setValue(theValue.floatValue());
 407     }
 408
 411     public  void setBigDecimal(Number
  bigDecimal) throws StandardException 412     {
 413         if (objectNull(bigDecimal))
 414             return;
 415
 416                                 setValue(bigDecimal.floatValue());
 420
 421     }
 422
 423     public void setValue(float theValue)
 424         throws StandardException
 425     {
 426         value = NumberDataType.normalizeREAL(theValue);
 427         isnull = false;
 428     }
 429
 430     public void setValue(int theValue)
 431     {
 432         value = theValue;
 433         isnull = false;
 434
 435     }
 436
 437     public void setValue(long theValue)
 438     {
 439         value = theValue;
 440         isnull = false;
 441
 442     }
 443
 444
 447     public void setValue(double theValue) throws StandardException
 448     {
 449                         float fv = (float) theValue;
 452                 if (fv == 0.0f && theValue != 0.0d) {
 454             throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, TypeId.REAL_NAME);
 455         }
 456         setValue(fv);
 457     }
 458
 459
 463     public void setValue(boolean theValue)
 464     {
 465         value = theValue?1:0;
 466         isnull = false;
 467     }
 468
 469
 473     void setObject(Object
  theValue) throws StandardException 474     {
 475         setValue(((Float
  ) theValue).floatValue()); 476     }
 477
 478     protected void setFrom(DataValueDescriptor theValue) throws StandardException {
 479
 480                                                 if (theValue instanceof StringDataValue) {
 484                                 setValue(theValue.getString());
 486             } else if (theValue instanceof SQLDouble) {
 487                                 setValue(theValue.getDouble());
 489             } else {
 490                 setValue(theValue.getFloat());
 491             }
 492     }
 493
 494
 495
 498
 499
 500     public int typePrecedence()
 501     {
 502         return TypeId.REAL_PRECEDENCE;
 503     }
 504
 505
 506
 509
 510
 521
 522     public BooleanDataValue equals(DataValueDescriptor left,
 523                             DataValueDescriptor right)
 524             throws StandardException
 525     {
 526         return SQLBoolean.truthValue(left,
 527                                      right,
 528                                      left.getFloat() == right.getFloat());
 529     }
 530
 531
 543
 544     public BooleanDataValue notEquals(DataValueDescriptor left,
 545                             DataValueDescriptor right)
 546             throws StandardException
 547     {
 548         return SQLBoolean.truthValue(left,
 549                                      right,
 550                                      left.getFloat() != right.getFloat());
 551     }
 552
 553
 565
 566     public BooleanDataValue lessThan(DataValueDescriptor left,
 567                             DataValueDescriptor right)
 568             throws StandardException
 569     {
 570         return SQLBoolean.truthValue(left,
 571                                      right,
 572                                      left.getFloat() < right.getFloat());
 573     }
 574
 575
 587
 588     public BooleanDataValue greaterThan(DataValueDescriptor left,
 589                             DataValueDescriptor right)
 590             throws StandardException
 591     {
 592         return SQLBoolean.truthValue(left,
 593                                      right,
 594                                      left.getFloat() > right.getFloat());
 595     }
 596
 597
 609
 610     public BooleanDataValue lessOrEquals(DataValueDescriptor left,
 611                             DataValueDescriptor right)
 612             throws StandardException
 613     {
 614         return SQLBoolean.truthValue(left,
 615                                      right,
 616                                      left.getFloat() <= right.getFloat());
 617     }
 618
 619
 631
 632     public BooleanDataValue greaterOrEquals(DataValueDescriptor left,
 633                             DataValueDescriptor right)
 634             throws StandardException
 635     {
 636         return SQLBoolean.truthValue(left,
 637                                      right,
 638                                      left.getFloat() >= right.getFloat());
 639     }
 640
 641
 642
 655
 656     public NumberDataValue plus(NumberDataValue addend1,
 657                             NumberDataValue addend2,
 658                             NumberDataValue result)
 659                 throws StandardException
 660     {
 661         if (result == null)
 662         {
 663             result = new SQLReal();
 664         }
 665
 666         if (addend1.isNull() || addend2.isNull())
 667         {
 668             result.setToNull();
 669             return result;
 670         }
 671
 672         double dsum = addend1.getDouble() + addend2.getDouble();
 673                         result.setValue(dsum);
 676
 677         return result;
 678     }
 679
 680
 693
 694     public NumberDataValue minus(NumberDataValue left,
 695                             NumberDataValue right,
 696                             NumberDataValue result)
 697                 throws StandardException
 698     {
 699         if (result == null)
 700         {
 701             result = new SQLReal();
 702         }
 703
 704         if (left.isNull() || right.isNull())
 705         {
 706             result.setToNull();
 707             return result;
 708         }
 709
 710         double ddifference = left.getDouble() - right.getDouble();
 711                         result.setValue(ddifference);
 714         return result;
 715     }
 716
 717
 730
 731     public NumberDataValue times(NumberDataValue left,
 732                             NumberDataValue right,
 733                             NumberDataValue result)
 734                 throws StandardException
 735     {
 736         if (result == null)
 737         {
 738             result = new SQLReal();
 739         }
 740
 741         if (left.isNull() || right.isNull())
 742         {
 743             result.setToNull();
 744             return result;
 745         }
 746
 747         double leftValue = left.getDouble();
 748         double rightValue = right.getDouble();
 749         double tempResult = leftValue * rightValue;
 750                 if ( (tempResult == 0.0) && ( (leftValue != 0.0) && (rightValue != 0.0) ) ) {
 752             throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, TypeId.REAL_NAME);
 753         }
 754
 755         result.setValue(tempResult);
 756         return result;
 757     }
 758
 759
 772
 773     public NumberDataValue divide(NumberDataValue dividend,
 774                              NumberDataValue divisor,
 775                              NumberDataValue result)
 776                 throws StandardException
 777     {
 778         if (result == null)
 779         {
 780             result = new SQLReal();
 781         }
 782
 783         if (dividend.isNull() || divisor.isNull())
 784         {
 785             result.setToNull();
 786             return result;
 787         }
 788
 789         double divisorValue = divisor.getDouble();
 790         if (divisorValue == 0.0e0f)
 791         {
 792             throw StandardException.newException(SQLState.LANG_DIVIDE_BY_ZERO);
 793         }
 794
 795         double dividendValue = dividend.getDouble();
 796         double resultValue = dividendValue / divisorValue;
 797         if (Double.isNaN(resultValue))
 798         {
 799             throw StandardException.newException(SQLState.LANG_DIVIDE_BY_ZERO);
 800         }
 801
 802                 if ((resultValue == 0.0e0d) && (dividendValue != 0.0e0d)) {
 804             throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, TypeId.REAL_NAME);
 805         }
 806
 807         result.setValue(resultValue);
 808         return result;
 809     }
 810
 811
 821
 822     public NumberDataValue minus(NumberDataValue result)
 823                                     throws StandardException
 824     {
 825         float       minusResult;
 826
 827         if (result == null)
 828         {
 829             result = new SQLReal();
 830         }
 831
 832         if (this.isNull())
 833         {
 834             result.setToNull();
 835             return result;
 836         }
 837
 838         minusResult = -(this.getFloat());
 839         result.setValue(minusResult);
 840         return result;
 841     }
 842
 843
 850
 851     protected boolean isNegative()
 852     {
 853         return !isNull() && (value < 0.0f);
 854     }
 855
 856
 859
 860     public String
  toString() 861     {
 862         if (isNull())
 863             return "NULL";
 864         else
 865             return Float.toString(value);
 866     }
 867
 868
 871     public int hashCode()
 872     {
 873         long longVal = (long) value;
 874
 875         if (longVal != value)
 876         {
 877             longVal = Double.doubleToLongBits(value);
 878         }
 879
 880         return (int) (longVal ^ (longVal >> 32));
 881     }
 882
 883     static final int REAL_LENGTH = 16;
 884
 885     private static final int BASE_MEMORY_USAGE = ClassSize.estimateBaseFromCatalog( SQLReal.class);
 886
 887     public int estimateMemoryUsage()
 888     {
 889         return BASE_MEMORY_USAGE;
 890     }
 891
 892
 895     private float value;
 896     private boolean isnull;
 897
 898 }
 899
                                                                                                                                                                                                             |                                                                       
 
 
 
 
 
                                                                                   Popular Tags                                                                                                                                                                                              |