Debian Codegraph @

src/main/org/h2/value/ValueBigint.java

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