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