1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://h2database.com/html/license.html). |
4 | * Initial Developer: H2 Group |
5 | */ |
6 | package org.h2.value; |
7 | |
8 | import java.math.BigDecimal; |
9 | import java.math.BigInteger; |
10 | import java.sql.PreparedStatement; |
11 | import java.sql.SQLException; |
12 | |
13 | import org.h2.api.ErrorCode; |
14 | import org.h2.message.DbException; |
15 | import org.h2.util.MathUtils; |
16 | |
17 | /** |
18 | * Implementation of the BIGINT data type. |
19 | */ |
20 | public class ValueLong extends Value { |
21 | |
22 | /** |
23 | * The largest Long value, as a BigInteger. |
24 | */ |
25 | public static final BigInteger MAX = BigInteger.valueOf(Long.MAX_VALUE); |
26 | |
27 | /** |
28 | * The smallest Long value, as a BigDecimal. |
29 | */ |
30 | public static final BigDecimal MIN_BD = BigDecimal.valueOf(Long.MIN_VALUE); |
31 | |
32 | /** |
33 | * The precision in digits. |
34 | */ |
35 | public static final int PRECISION = 19; |
36 | |
37 | /** |
38 | * The maximum display size of a long. |
39 | * Example: 9223372036854775808 |
40 | */ |
41 | public static final int DISPLAY_SIZE = 20; |
42 | |
43 | private static final BigInteger MIN = BigInteger.valueOf(Long.MIN_VALUE); |
44 | private static final int STATIC_SIZE = 100; |
45 | private static final ValueLong[] STATIC_CACHE; |
46 | |
47 | private final long value; |
48 | |
49 | static { |
50 | STATIC_CACHE = new ValueLong[STATIC_SIZE]; |
51 | for (int i = 0; i < STATIC_SIZE; i++) { |
52 | STATIC_CACHE[i] = new ValueLong(i); |
53 | } |
54 | } |
55 | |
56 | private ValueLong(long value) { |
57 | this.value = value; |
58 | } |
59 | |
60 | @Override |
61 | public Value add(Value v) { |
62 | ValueLong other = (ValueLong) v; |
63 | long result = value + other.value; |
64 | int sv = Long.signum(value); |
65 | int so = Long.signum(other.value); |
66 | int sr = Long.signum(result); |
67 | // if the operands have different signs overflow can not occur |
68 | // if the operands have the same sign, |
69 | // and the result has a different sign, then it is an overflow |
70 | // it can not be an overflow when one of the operands is 0 |
71 | if (sv != so || sr == so || sv == 0 || so == 0) { |
72 | return ValueLong.get(result); |
73 | } |
74 | throw getOverflow(); |
75 | } |
76 | |
77 | @Override |
78 | public int getSignum() { |
79 | return Long.signum(value); |
80 | } |
81 | |
82 | @Override |
83 | public Value negate() { |
84 | if (value == Long.MIN_VALUE) { |
85 | throw getOverflow(); |
86 | } |
87 | return ValueLong.get(-value); |
88 | } |
89 | |
90 | private DbException getOverflow() { |
91 | return DbException.get(ErrorCode.NUMERIC_VALUE_OUT_OF_RANGE_1, |
92 | Long.toString(value)); |
93 | } |
94 | |
95 | @Override |
96 | public Value subtract(Value v) { |
97 | ValueLong other = (ValueLong) v; |
98 | int sv = Long.signum(value); |
99 | int so = Long.signum(other.value); |
100 | // if the operands have the same sign, then overflow can not occur |
101 | // if the second operand is 0, then overflow can not occur |
102 | if (sv == so || so == 0) { |
103 | return ValueLong.get(value - other.value); |
104 | } |
105 | // now, if the other value is Long.MIN_VALUE, it must be an overflow |
106 | // x - Long.MIN_VALUE overflows for x>=0 |
107 | return add(other.negate()); |
108 | } |
109 | |
110 | private static boolean isInteger(long a) { |
111 | return a >= Integer.MIN_VALUE && a <= Integer.MAX_VALUE; |
112 | } |
113 | |
114 | @Override |
115 | public Value multiply(Value v) { |
116 | ValueLong other = (ValueLong) v; |
117 | long result = value * other.value; |
118 | if (value == 0 || value == 1 || other.value == 0 || other.value == 1) { |
119 | return ValueLong.get(result); |
120 | } |
121 | if (isInteger(value) && isInteger(other.value)) { |
122 | return ValueLong.get(result); |
123 | } |
124 | // just checking one case is not enough: Long.MIN_VALUE * -1 |
125 | // probably this is correct but I'm not sure |
126 | // if (result / value == other.value && result / other.value == value) { |
127 | // return ValueLong.get(result); |
128 | //} |
129 | BigInteger bv = BigInteger.valueOf(value); |
130 | BigInteger bo = BigInteger.valueOf(other.value); |
131 | BigInteger br = bv.multiply(bo); |
132 | if (br.compareTo(MIN) < 0 || br.compareTo(MAX) > 0) { |
133 | throw getOverflow(); |
134 | } |
135 | return ValueLong.get(br.longValue()); |
136 | } |
137 | |
138 | @Override |
139 | public Value divide(Value v) { |
140 | ValueLong other = (ValueLong) v; |
141 | if (other.value == 0) { |
142 | throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getSQL()); |
143 | } |
144 | return ValueLong.get(value / other.value); |
145 | } |
146 | |
147 | @Override |
148 | public Value modulus(Value v) { |
149 | ValueLong other = (ValueLong) v; |
150 | if (other.value == 0) { |
151 | throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getSQL()); |
152 | } |
153 | return ValueLong.get(this.value % other.value); |
154 | } |
155 | |
156 | @Override |
157 | public String getSQL() { |
158 | return getString(); |
159 | } |
160 | |
161 | @Override |
162 | public int getType() { |
163 | return Value.LONG; |
164 | } |
165 | |
166 | @Override |
167 | public long getLong() { |
168 | return value; |
169 | } |
170 | |
171 | @Override |
172 | protected int compareSecure(Value o, CompareMode mode) { |
173 | ValueLong v = (ValueLong) o; |
174 | return MathUtils.compareLong(value, v.value); |
175 | } |
176 | |
177 | @Override |
178 | public String getString() { |
179 | return String.valueOf(value); |
180 | } |
181 | |
182 | @Override |
183 | public long getPrecision() { |
184 | return PRECISION; |
185 | } |
186 | |
187 | @Override |
188 | public int hashCode() { |
189 | return (int) (value ^ (value >> 32)); |
190 | } |
191 | |
192 | @Override |
193 | public Object getObject() { |
194 | return value; |
195 | } |
196 | |
197 | @Override |
198 | public void set(PreparedStatement prep, int parameterIndex) |
199 | throws SQLException { |
200 | prep.setLong(parameterIndex, value); |
201 | } |
202 | |
203 | /** |
204 | * Get or create a long value for the given long. |
205 | * |
206 | * @param i the long |
207 | * @return the value |
208 | */ |
209 | public static ValueLong get(long i) { |
210 | if (i >= 0 && i < STATIC_SIZE) { |
211 | return STATIC_CACHE[(int) i]; |
212 | } |
213 | return (ValueLong) Value.cache(new ValueLong(i)); |
214 | } |
215 | |
216 | @Override |
217 | public int getDisplaySize() { |
218 | return DISPLAY_SIZE; |
219 | } |
220 | |
221 | @Override |
222 | public boolean equals(Object other) { |
223 | return other instanceof ValueLong && value == ((ValueLong) other).value; |
224 | } |
225 | |
226 | } |