File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\value\ValueDouble.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.value;
8
9 import java.sql.PreparedStatement;
10 import java.sql.SQLException;
11 import org.h2.constant.ErrorCode;
12 import org.h2.message.DbException;
13
14 /**
15 * Implementation of the DOUBLE data type.
16 */
17 public class ValueDouble extends Value {
18
19 /**
20 * The precision in digits.
21 */
22 public static final int PRECISION = 17;
23
24 /**
25 * The maximum display size of a double.
26 * Example: -3.3333333333333334E-100
27 */
28 public static final int DISPLAY_SIZE = 24;
29
30 /**
31 * Double.doubleToLongBits(0.0)
32 */
33 public static final long ZERO_BITS = Double.doubleToLongBits(0.0);
34
35 private static final ValueDouble ZERO = new ValueDouble(0.0);
36 private static final ValueDouble ONE = new ValueDouble(1.0);
37 private static final ValueDouble NAN = new ValueDouble(Double.NaN);
38
39 private final double value;
40
41 private ValueDouble(double value) {
42 this.value = value;
43 }
44
45 public Value add(Value v) {
46 ValueDouble v2 = (ValueDouble) v;
47 return ValueDouble.get(value + v2.value);
48 }
49
50 public Value subtract(Value v) {
51 ValueDouble v2 = (ValueDouble) v;
52 return ValueDouble.get(value - v2.value);
53 }
54
55 public Value negate() {
56 return ValueDouble.get(-value);
57 }
58
59 public Value multiply(Value v) {
60 ValueDouble v2 = (ValueDouble) v;
61 return ValueDouble.get(value * v2.value);
62 }
63
64 public Value divide(Value v) {
65 ValueDouble v2 = (ValueDouble) v;
66 if (v2.value == 0.0) {
67 throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getSQL());
68 }
69 return ValueDouble.get(value / v2.value);
70 }
71
72 public ValueDouble modulus(Value v) {
73 ValueDouble other = (ValueDouble) v;
74 if (other.value == 0) {
75 throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getSQL());
76 }
77 return ValueDouble.get(value % other.value);
78 }
79
80 public String getSQL() {
81 if (value == Double.POSITIVE_INFINITY) {
82 return "POWER(0, -1)";
83 } else if (value == Double.NEGATIVE_INFINITY) {
84 return "(-POWER(0, -1))";
85 } else if (Double.isNaN(value)) {
86 return "SQRT(-1)";
87 }
88 String s = getString();
89 if (s.equals("-0.0")) {
90 return "-CAST(0 AS DOUBLE)";
91 }
92 return s;
93 }
94
95 public int getType() {
96 return Value.DOUBLE;
97 }
98
99 protected int compareSecure(Value o, CompareMode mode) {
100 ValueDouble v = (ValueDouble) o;
101 return Double.compare(value, v.value);
102 }
103
104 public int getSignum() {
105 return value == 0 ? 0 : (value < 0 ? -1 : 1);
106 }
107
108 public double getDouble() {
109 return value;
110 }
111
112 public String getString() {
113 return String.valueOf(value);
114 }
115
116 public long getPrecision() {
117 return PRECISION;
118 }
119
120 public int getScale() {
121 return 0;
122 }
123
124 public int hashCode() {
125 long hash = Double.doubleToLongBits(value);
126 return (int) (hash ^ (hash >> 32));
127 }
128
129 public Object getObject() {
130 return Double.valueOf(value);
131 }
132
133 public void set(PreparedStatement prep, int parameterIndex) throws SQLException {
134 prep.setDouble(parameterIndex, value);
135 }
136
137 /**
138 * Get or create double value for the given double.
139 *
140 * @param d the double
141 * @return the value
142 */
143 public static ValueDouble get(double d) {
144 if (d == 1.0) {
145 return ONE;
146 } else if (d == 0.0) {
147 // unfortunately, -0.0 == 0.0, but we don't want to return
148 // 0.0 in this case
149 if (Double.doubleToLongBits(d) == ZERO_BITS) {
150 return ZERO;
151 }
152 } else if (Double.isNaN(d)) {
153 return NAN;
154 }
155 return (ValueDouble) Value.cache(new ValueDouble(d));
156 }
157
158 public int getDisplaySize() {
159 return DISPLAY_SIZE;
160 }
161
162 public boolean equals(Object other) {
163 if (!(other instanceof ValueDouble)) {
164 return false;
165 }
166 return compareSecure((ValueDouble) other, null) == 0;
167 }
168
169 }
170