1 5 package org.h2.value; 6 7 import java.sql.PreparedStatement ; 8 import java.sql.SQLException ; 9 10 import org.h2.message.Message; 11 12 15 16 public class ValueDouble extends Value { 17 public static final int PRECISION = 17; 18 19 private double value; 20 private static final double DOUBLE_ZERO = 0.0; 21 private static final double DOUBLE_ONE = 1.0; 22 private static final ValueDouble ZERO = new ValueDouble(DOUBLE_ZERO); 23 private static final ValueDouble ONE = new ValueDouble(DOUBLE_ONE); 24 25 private ValueDouble(double value) { 26 this.value = value; 27 } 28 29 public Value add(Value v) { 30 ValueDouble v2 = (ValueDouble) v; 31 return ValueDouble.get(value + v2.value); 32 } 33 34 public Value subtract(Value v) { 35 ValueDouble v2 = (ValueDouble) v; 36 return ValueDouble.get(value - v2.value); 37 } 38 39 public Value negate() { 40 return ValueDouble.get(-value); 41 } 42 43 public Value multiply(Value v) { 44 ValueDouble v2 = (ValueDouble) v; 45 return ValueDouble.get(value * v2.value); 46 } 47 48 public Value divide(Value v) throws SQLException { 49 ValueDouble v2 = (ValueDouble) v; 50 if (v2.value == 0.0) { 51 throw Message.getSQLException(Message.DIVISION_BY_ZERO_1, getSQL()); 52 } 53 return ValueDouble.get(value / v2.value); 54 } 55 56 public String getSQL() { 57 return getString(); 58 } 59 60 public int getType() { 61 return Value.DOUBLE; 62 } 63 64 protected int compareSecure(Value o, CompareMode mode) { 65 ValueDouble v = (ValueDouble) o; 66 if (value == v.value) { 67 return 0; 68 } 69 return value > v.value ? 1 : -1; 70 } 71 72 public int getSignum() { 73 return value == 0 ? 0 : (value < 0 ? -1 : 1); 74 } 75 76 public double getDouble() { 77 return value; 78 } 79 80 public String getString() { 81 return String.valueOf(value); 82 } 83 84 public long getPrecision() { 85 return PRECISION; 86 } 87 88 public int getScale() { 89 return 0; 91 } 92 93 public int hashCode() { 94 long hash = Double.doubleToLongBits(value); 95 return (int) (hash ^ (hash >> 32)); 96 } 97 98 public Object getObject() { 99 return new Double (value); 100 } 101 102 public void set(PreparedStatement prep, int parameterIndex) throws SQLException { 103 prep.setDouble(parameterIndex, value); 104 } 105 106 public static ValueDouble get(double d) { 107 if (DOUBLE_ZERO == d) { 108 return ZERO; 109 } else if (DOUBLE_ONE == d) { 110 return ONE; 111 } 112 return (ValueDouble) Value.cache(new ValueDouble(d)); 113 } 114 115 119 public int getDisplaySize() { 120 return PRECISION + 2; 121 } 122 123 protected boolean isEqual(Value v) { 124 return v instanceof ValueDouble && value == ((ValueDouble)v).value; 125 } 126 127 } 128 | Popular Tags |