|                                                                                                              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.services.io.StoredFormatIds;
 29  import org.apache.derby.iapi.services.io.Storable;
 30
 31  import org.apache.derby.iapi.services.sanity.SanityManager;
 32
 33  import org.apache.derby.iapi.error.StandardException;
 34
 35  import org.apache.derby.iapi.types.BooleanDataValue;
 36  import org.apache.derby.iapi.types.DataValueDescriptor;
 37  import org.apache.derby.iapi.types.NumberDataValue;
 38  import org.apache.derby.iapi.types.TypeId;
 39
 40  import org.apache.derby.iapi.services.cache.ClassSize;
 41
 42  import org.apache.derby.iapi.types.NumberDataType;
 43  import org.apache.derby.iapi.types.SQLBoolean;
 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
 80  public final class SQLDouble extends NumberDataType
 81  {
 82
 83
 87
 88
 89
 95      public int  getInt() throws StandardException
 96      {
 97                  if ((value > (((double) Integer.MAX_VALUE) + 1.0d)) || (value < (((double) Integer.MIN_VALUE) - 1.0d)))
 99              throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, "INTEGER");
 100         return (int)value;
 101     }
 102
 103
 106     public byte getByte() throws StandardException
 107     {
 108         if ((value > (((double) Byte.MAX_VALUE) + 1.0d)) || (value < (((double) Byte.MIN_VALUE) - 1.0d)))
 109             throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, "TINYINT");
 110         return (byte) value;
 111     }
 112
 113
 116     public short    getShort() throws StandardException
 117     {
 118         if ((value > (((double) Short.MAX_VALUE) + 1.0d)) || (value < (((double) Short.MIN_VALUE) - 1.0d)))
 119             throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, "SMALLINT");
 120         return (short) value;
 121     }
 122
 123
 126     public long getLong() throws StandardException
 127     {
 128         if ((value > (((double) Long.MAX_VALUE) + 1.0d)) || (value < (((double) Long.MIN_VALUE) - 1.0d)))
 129             throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, "BIGINT");
 130         return (long) value;
 131     }
 132
 133
 136     public float    getFloat() throws StandardException
 137     {
 138         if (Float.isInfinite((float)value))
 139             throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, TypeId.REAL_NAME);
 140         return (float) value;
 141     }
 142
 143     public double   getDouble()
 144     {
 145
 146         return value;
 147     }
 148
 149
 152     public int typeToBigDecimal()
 153     {
 154         return java.sql.Types.CHAR;
 155     }
 156             public boolean  getBoolean()
 159     {
 160         return (value != 0);
 161     }
 162
 163     public String
  getString() 164     {
 165         if (isNull())
 166             return null;
 167         else
 168             return Double.toString(value);
 169     }
 170
 171     public Object
  getObject() 172     {
 173                 if (isNull())
 175             return null;
 176         else
 177             return new Double
  (value); 178     }
 179
 180
 181
 185     void setObject(Object
  theValue) throws StandardException 186     {
 187         setValue(((Double
  ) theValue).doubleValue()); 188     }
 189
 190     protected void setFrom(DataValueDescriptor theValue) throws StandardException {
 191         setValue(theValue.getDouble());
 192     }
 193
 194     public int  getLength()
 195     {
 196         return DOUBLE_LENGTH;
 197     }
 198
 199         public String
  getTypeName() 201     {
 202         return TypeId.DOUBLE_NAME;
 203     }
 204
 205
 208
 209
 214     public int getTypeFormatId() {
 215         return StoredFormatIds.SQL_DOUBLE_ID;
 216     }
 217
 218
 221
 222     public boolean isNull()
 223     {
 224         return isnull;
 225     }
 226
 227     public void writeExternal(ObjectOutput
  out) throws IOException  { 228
 229
 230                 if (SanityManager.DEBUG)
 232             SanityManager.ASSERT(! isNull());
 233
 234         out.writeDouble(value);
 235     }
 236
 237
 238     public void readExternal(ObjectInput
  in) throws IOException  { 239
 240         value = in.readDouble();
 241         isnull = false;
 242     }
 243
 244
 245     public void readExternalFromArray(ArrayInputStream in) throws IOException
  { 246
 247         value = in.readDouble();
 248         isnull = false;
 249     }
 250
 251
 255
 256     public void restoreToNull()
 257     {
 258         value = 0;
 259         isnull = true;
 260     }
 261
 262
 263
 264     protected int typeCompare(DataValueDescriptor arg) throws StandardException
 265     {
 266
 267
 268         double thisValue = this.getDouble();
 269
 270         double otherValue = arg.getDouble();
 271
 272         if (thisValue == otherValue)
 273             return 0;
 274         else if (thisValue > otherValue)
 275             return 1;
 276         else
 277             return -1;
 278     }
 279
 280
 283
 284
 285     public DataValueDescriptor getClone()
 286     {
 287         try
 288         {
 289             return new SQLDouble(value, isnull);
 290         } catch (StandardException se)
 291         {
 292             if (SanityManager.DEBUG)
 293                 SanityManager.THROWASSERT(
 294                     "error on clone, se = " + se +
 295                     " value = " + value +
 296                     " isnull = " + isnull);
 297             return null;
 298         }
 299     }
 300
 301
 304     public DataValueDescriptor getNewNull()
 305     {
 306         return new SQLDouble();
 307     }
 308
 309
 315     public void setValueFromResultSet(ResultSet
  resultSet, int colNumber, 316                                       boolean isNullable)
 317         throws StandardException, SQLException
  318     {
 319             double dv = resultSet.getDouble(colNumber);
 320             isnull = (isNullable && resultSet.wasNull());
 321             if (isnull)
 322                 value = 0;
 323             else
 324                 value = NumberDataType.normalizeDOUBLE(dv);
 325     }
 326
 331     public final void setInto(PreparedStatement
  ps, int position) throws SQLException  { 332
 333         if (isNull()) {
 334             ps.setNull(position, java.sql.Types.DOUBLE);
 335             return;
 336         }
 337
 338         ps.setDouble(position, value);
 339     }
 340
 347     public final void setInto(ResultSet
  rs, int position) throws SQLException  , StandardException { 348         rs.updateDouble(position, value);
 349     }
 350
 353
 354
 357
 358
 359             public SQLDouble() {
 362         isnull = true;
 363     }
 364
 365     public SQLDouble(double val) throws StandardException
 366     {
 367         value = NumberDataType.normalizeDOUBLE(val);
 368     }
 369
 370     public SQLDouble(Double
  obj) throws StandardException { 371         if (isnull = (obj == null))
 372             ;
 373         else
 374             value = NumberDataType.normalizeDOUBLE(obj.doubleValue());
 375     }
 376
 377     private SQLDouble(double val, boolean startsnull) throws StandardException
 378     {
 379         value = NumberDataType.normalizeDOUBLE(val);         isnull = startsnull;
 381     }
 382
 383
 387     public void setValue(String
  theValue) throws StandardException 388     {
 389         if (theValue == null)
 390         {
 391             value = 0;
 392             isnull = true;
 393         }
 394         else
 395         {
 396             double doubleValue = 0;
 397             try {
 398                                 doubleValue = Double.valueOf(theValue.trim()).doubleValue();
 400             } catch (NumberFormatException
  nfe) { 401                 throw invalidFormat();
 402             }
 403             value = NumberDataType.normalizeDOUBLE(doubleValue);
 404             isnull = false;
 405         }
 406     }
 407
 408
 411     public void setValue(double theValue) throws StandardException
 412     {
 413         value = NumberDataType.normalizeDOUBLE(theValue);
 414         isnull = false;
 415     }
 416
 417
 420     public void setValue(float theValue) throws StandardException
 421     {
 422         value = NumberDataType.normalizeDOUBLE(theValue);
 423         isnull = false;
 424     }
 425
 426     public void setValue(long theValue)
 427     {
 428         value = theValue;         isnull = false;
 430     }
 431
 432     public void setValue(int theValue)
 433     {
 434         value = theValue;         isnull = false;
 436     }
 437
 438     public  void setValue(Number
  theValue) throws StandardException 439     {
 440         if (objectNull(theValue))
 441             return;
 442
 443         if (SanityManager.ASSERT)
 444         {
 445             if (!(theValue instanceof java.lang.Double
  )) 446                 SanityManager.THROWASSERT("SQLDouble.setValue(Number) passed a " + theValue.getClass());
 447         }
 448
 449         setValue(theValue.doubleValue());
 450     }
 451
 452
 455     public  void setBigDecimal(Number
  bigDecimal) throws StandardException 456     {
 457         if (objectNull(bigDecimal))
 458             return;
 459
 460                                 setValue(bigDecimal.doubleValue());
 464
 465     }
 466
 467
 471     public void setValue(boolean theValue)
 472     {
 473         value = theValue?1:0;
 474         isnull = false;
 475     }
 476
 477
 478
 481
 482
 483     public int typePrecedence()
 484     {
 485         return TypeId.DOUBLE_PRECEDENCE;
 486     }
 487
 488
 489
 492
 493
 505
 506     public BooleanDataValue equals(DataValueDescriptor left,
 507                             DataValueDescriptor right)
 508             throws StandardException
 509     {
 510         return SQLBoolean.truthValue(left,
 511                                      right,
 512                                      left.getDouble() == right.getDouble());
 513     }
 514
 515
 528
 529     public BooleanDataValue notEquals(DataValueDescriptor left,
 530                             DataValueDescriptor right)
 531             throws StandardException
 532     {
 533         return SQLBoolean.truthValue(left,
 534                                      right,
 535                                      left.getDouble() != right.getDouble());
 536     }
 537
 538
 550
 551     public BooleanDataValue lessThan(DataValueDescriptor left,
 552                             DataValueDescriptor right)
 553             throws StandardException
 554     {
 555         return SQLBoolean.truthValue(left,
 556                                      right,
 557                                      left.getDouble() < right.getDouble());
 558     }
 559
 560
 572
 573     public BooleanDataValue greaterThan(DataValueDescriptor left,
 574                             DataValueDescriptor right)
 575             throws StandardException
 576     {
 577         return SQLBoolean.truthValue(left,
 578                                      right,
 579                                      left.getDouble() > right.getDouble());
 580     }
 581
 582
 594
 595     public BooleanDataValue lessOrEquals(DataValueDescriptor left,
 596                             DataValueDescriptor right)
 597             throws StandardException
 598     {
 599         return SQLBoolean.truthValue(left,
 600                                      right,
 601                                      left.getDouble() <= right.getDouble());
 602     }
 603
 604
 616
 617     public BooleanDataValue greaterOrEquals(DataValueDescriptor left,
 618                             DataValueDescriptor right)
 619             throws StandardException
 620     {
 621         return SQLBoolean.truthValue(left,
 622                                      right,
 623                                      left.getDouble() >= right.getDouble());
 624     }
 625
 626
 638
 639     public NumberDataValue plus(NumberDataValue addend1,
 640                             NumberDataValue addend2,
 641                             NumberDataValue result)
 642                 throws StandardException
 643     {
 644         if (result == null)
 645         {
 646             result = new SQLDouble();
 647         }
 648
 649         if (addend1.isNull() || addend2.isNull())
 650         {
 651             result.setToNull();
 652             return result;
 653         }
 654
 655         double tmpresult = addend1.getDouble() + addend2.getDouble();
 656                         result.setValue(tmpresult);
 659
 660         return result;
 661     }
 662
 663
 675
 676     public NumberDataValue minus(NumberDataValue left,
 677                             NumberDataValue right,
 678                             NumberDataValue result)
 679                 throws StandardException
 680     {
 681         if (result == null)
 682         {
 683             result = new SQLDouble();
 684         }
 685
 686         if (left.isNull() || right.isNull())
 687         {
 688             result.setToNull();
 689             return result;
 690         }
 691
 692         double tmpresult = left.getDouble() - right.getDouble();
 693                         result.setValue(tmpresult);
 696         return result;
 697     }
 698
 699
 711
 712     public NumberDataValue times(NumberDataValue left,
 713                             NumberDataValue right,
 714                             NumberDataValue result)
 715                 throws StandardException
 716     {
 717         if (result == null)
 718         {
 719             result = new SQLDouble();
 720         }
 721
 722         if (left.isNull() || right.isNull())
 723         {
 724             result.setToNull();
 725             return result;
 726         }
 727
 728         double leftValue = left.getDouble();
 729         double rightValue = right.getDouble();
 730         double tempResult = leftValue * rightValue;
 731                 if ( (tempResult == 0.0) && ( (leftValue != 0.0) && (rightValue != 0.0) ) ) {
 733             throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, TypeId.DOUBLE_NAME);
 734         }
 735
 736         result.setValue(tempResult);
 737         return result;
 738     }
 739
 740
 752
 753     public NumberDataValue divide(NumberDataValue dividend,
 754                              NumberDataValue divisor,
 755                              NumberDataValue result)
 756                 throws StandardException
 757     {
 758         if (result == null)
 759         {
 760             result = new SQLDouble();
 761         }
 762
 763         if (dividend.isNull() || divisor.isNull())
 764         {
 765             result.setToNull();
 766             return result;
 767         }
 768
 769
 773
 774         double divisorValue = divisor.getDouble();
 775
 776         if (divisorValue == 0.0e0D)
 777         {
 778             throw StandardException.newException(SQLState.LANG_DIVIDE_BY_ZERO);
 779         }
 780
 781         double dividendValue = dividend.getDouble();
 782         double divideResult =  dividendValue / divisorValue;
 783
 784         if (Double.isNaN(divideResult))
 785         {
 786             throw StandardException.newException(SQLState.LANG_DIVIDE_BY_ZERO);
 787         }
 788
 789                 if ((divideResult == 0.0d) && (dividendValue != 0.0d)) {
 791             throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, TypeId.DOUBLE_NAME);
 792         }
 793
 794         result.setValue(divideResult);
 795         return result;
 796     }
 797
 798
 808
 809     public NumberDataValue minus(NumberDataValue result)
 810                                     throws StandardException
 811     {
 812         double      minusResult;
 813
 814         if (result == null)
 815         {
 816             result = new SQLDouble();
 817         }
 818
 819         if (this.isNull())
 820         {
 821             result.setToNull();
 822             return result;
 823         }
 824
 825
 830         minusResult = -(this.getDouble());
 831
 832         result.setValue(minusResult);
 833         return result;
 834     }
 835
 836
 842
 843     protected boolean isNegative()
 844     {
 845         return !isNull() && (value < 0.0d);
 846     }
 847
 848
 851
 852     public String
  toString() 853     {
 854         if (isNull())
 855             return "NULL";
 856         else
 857             return Double.toString(value);
 858     }
 859
 860
 863     public int hashCode()
 864     {
 865         long longVal = (long) value;
 866         double doubleLongVal = (double) longVal;
 867
 868
 882         if (doubleLongVal != value)
 883         {
 884             longVal = Double.doubleToLongBits(value);
 885         }
 886
 887         return (int) (longVal ^ (longVal >> 32));
 888     }
 889
 890
 893     static final int DOUBLE_LENGTH      = 32;
 895     private static final int BASE_MEMORY_USAGE = ClassSize.estimateBaseFromCatalog( SQLDouble.class);
 896
 897     public int estimateMemoryUsage()
 898     {
 899         return BASE_MEMORY_USAGE;
 900     }
 901
 902
 905     private double  value;
 906     private boolean isnull;
 907 }
 908
                                                                                                                                                                                                             |                                                                       
 
 
 
 
 
                                                                                   Popular Tags                                                                                                                                                                                              |