File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\value\ValueDecimal.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.math.BigDecimal;
10 import java.sql.PreparedStatement;
11 import java.sql.SQLException;
12 import org.h2.constant.ErrorCode;
13 import org.h2.message.DbException;
14 import org.h2.util.MathUtils;
15
16 /**
17 * Implementation of the DECIMAL data type.
18 */
19 public class ValueDecimal extends Value {
20
21 /**
22 * The value 'zero'.
23 */
24 public static final Object ZERO = new ValueDecimal(BigDecimal.ZERO);
25
26 /**
27 * The value 'one'.
28 */
29 public static final Object ONE = new ValueDecimal(BigDecimal.ONE);
30
31 /**
32 * The default precision for a decimal value.
33 */
34 static final int DEFAULT_PRECISION = 65535;
35
36 /**
37 * The default scale for a decimal value.
38 */
39 static final int DEFAULT_SCALE = 32767;
40
41 /**
42 * The default display size for a decimal value.
43 */
44 static final int DEFAULT_DISPLAY_SIZE = 65535;
45
46 private static final int DIVIDE_SCALE_ADD = 25;
47
48 private final BigDecimal value;
49 private String valueString;
50 private int precision;
51
52 private ValueDecimal(BigDecimal value) {
53 if (value == null) {
54 throw new IllegalArgumentException();
55 } else if (!value.getClass().equals(BigDecimal.class)) {
56 throw DbException.get(ErrorCode.INVALID_CLASS_2,
57 BigDecimal.class.getName(), value.getClass().getName());
58 }
59 this.value = value;
60 }
61
62 public Value add(Value v) {
63 ValueDecimal dec = (ValueDecimal) v;
64 return ValueDecimal.get(value.add(dec.value));
65 }
66
67 public Value subtract(Value v) {
68 ValueDecimal dec = (ValueDecimal) v;
69 return ValueDecimal.get(value.subtract(dec.value));
70 }
71
72 public Value negate() {
73 return ValueDecimal.get(value.negate());
74 }
75
76 public Value multiply(Value v) {
77 ValueDecimal dec = (ValueDecimal) v;
78 return ValueDecimal.get(value.multiply(dec.value));
79 }
80
81 public Value divide(Value v) {
82 ValueDecimal dec = (ValueDecimal) v;
83 if (dec.value.signum() == 0) {
84 throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getSQL());
85 }
86 BigDecimal bd = value.divide(dec.value, value.scale() + DIVIDE_SCALE_ADD, BigDecimal.ROUND_HALF_DOWN);
87 if (bd.signum() == 0) {
88 bd = BigDecimal.ZERO;
89 } else if (bd.scale() > 0) {
90 if (!bd.unscaledValue().testBit(0)) {
91 String s = bd.toString();
92 int i = s.length() - 1;
93 while (i >= 0 && s.charAt(i) == '0') {
94 i--;
95 }
96 if (i < s.length() - 1) {
97 s = s.substring(0, i + 1);
98 bd = new BigDecimal(s);
99 }
100 }
101 }
102 return ValueDecimal.get(bd);
103 }
104
105 public ValueDecimal modulus(Value v) {
106 ValueDecimal dec = (ValueDecimal) v;
107 if (dec.value.signum() == 0) {
108 throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getSQL());
109 }
110 BigDecimal bd = value.remainder(dec.value);
111 return ValueDecimal.get(bd);
112 }
113
114 public String getSQL() {
115 return getString();
116 }
117
118 public int getType() {
119 return Value.DECIMAL;
120 }
121
122 protected int compareSecure(Value o, CompareMode mode) {
123 ValueDecimal v = (ValueDecimal) o;
124 return value.compareTo(v.value);
125 }
126
127 public int getSignum() {
128 return value.signum();
129 }
130
131 public BigDecimal getBigDecimal() {
132 return value;
133 }
134
135 public String getString() {
136 if (valueString == null) {
137 String p = value.toPlainString();
138 if (p.length() < 40) {
139 valueString = p;
140 } else {
141 valueString = value.toString();
142 }
143 }
144 return valueString;
145 }
146
147 public long getPrecision() {
148 if (precision == 0) {
149 precision = value.precision();
150 }
151 return precision;
152 }
153
154 public boolean checkPrecision(long prec) {
155 if (prec == DEFAULT_PRECISION) {
156 return true;
157 }
158 return getPrecision() <= prec;
159 }
160
161 public int getScale() {
162 return value.scale();
163 }
164
165 public int hashCode() {
166 return value.hashCode();
167 }
168
169 public Object getObject() {
170 return value;
171 }
172
173 public void set(PreparedStatement prep, int parameterIndex) throws SQLException {
174 prep.setBigDecimal(parameterIndex, value);
175 }
176
177 public Value convertScale(boolean onlyToSmallerScale, int targetScale) {
178 if (value.scale() == targetScale) {
179 return this;
180 }
181 if (onlyToSmallerScale || targetScale >= DEFAULT_SCALE) {
182 if (value.scale() < targetScale) {
183 return this;
184 }
185 }
186 BigDecimal bd = MathUtils.setScale(value, targetScale);
187 return ValueDecimal.get(bd);
188 }
189
190 public Value convertPrecision(long precision, boolean force) {
191 if (getPrecision() <= precision) {
192 return this;
193 }
194 if (force) {
195 return get(BigDecimal.valueOf(value.doubleValue()));
196 }
197 throw DbException.get(ErrorCode.NUMERIC_VALUE_OUT_OF_RANGE_1, Long.toString(precision));
198 }
199
200 /**
201 * Get or create big decimal value for the given big decimal.
202 *
203 * @param dec the bit decimal
204 * @return the value
205 */
206 public static ValueDecimal get(BigDecimal dec) {
207 if (BigDecimal.ZERO.equals(dec)) {
208 return (ValueDecimal) ZERO;
209 } else if (BigDecimal.ONE.equals(dec)) {
210 return (ValueDecimal) ONE;
211 }
212 return (ValueDecimal) Value.cache(new ValueDecimal(dec));
213 }
214
215 public int getDisplaySize() {
216 // add 2 characters for '-' and '.'
217 return MathUtils.convertLongToInt(getPrecision() + 2);
218 }
219
220 public boolean equals(Object other) {
221 // Two BigDecimal objects are considered equal only if they are equal in
222 // value and scale (thus 2.0 is not equal to 2.00 when using equals;
223 // however -0.0 and 0.0 are). Can not use compareTo because 2.0 and 2.00
224 // have different hash codes
225 return other instanceof ValueDecimal && value.equals(((ValueDecimal) other).value);
226 }
227
228 public int getMemory() {
229 return value.precision() + 120;
230 }
231
232 }
233