| 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 |
|
| 10 |
import org.h2.api.ErrorCode; |
| 11 |
import org.h2.engine.CastDataProvider; |
| 12 |
import org.h2.message.DbException; |
| 13 |
import org.h2.util.Bits; |
| 14 |
|
| 15 |
/** |
| 16 |
* Implementation of the INTEGER data type. |
| 17 |
*/ |
| 18 |
public final class ValueInteger extends Value { |
| 19 |
|
| 20 |
/** |
| 21 |
* The precision in bits. |
| 22 |
*/ |
| 23 |
public static final int PRECISION = 32; |
| 24 |
|
| 25 |
/** |
| 26 |
* The approximate precision in decimal digits. |
| 27 |
*/ |
| 28 |
public static final int DECIMAL_PRECISION = 10; |
| 29 |
|
| 30 |
/** |
| 31 |
* The maximum display size of an INT. |
| 32 |
* Example: -2147483648 |
| 33 |
*/ |
| 34 |
public static final int DISPLAY_SIZE = 11; |
| 35 |
|
| 36 |
private static final int STATIC_SIZE = 128; |
| 37 |
// must be a power of 2 |
| 38 |
private static final int DYNAMIC_SIZE = 256; |
| 39 |
private static final ValueInteger[] STATIC_CACHE = new ValueInteger[STATIC_SIZE]; |
| 40 |
private static final ValueInteger[] DYNAMIC_CACHE = new ValueInteger[DYNAMIC_SIZE]; |
| 41 |
|
| 42 |
private final int value; |
| 43 |
|
| 44 |
static { |
| 45 |
for (int i = 0; i < STATIC_SIZE; i++) { |
| 46 |
STATIC_CACHE[i] = new ValueInteger(i); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
private ValueInteger(int value) { |
| 51 |
this.value = value; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Get or create an INTEGER value for the given int. |
| 56 |
* |
| 57 |
* @param i the int |
| 58 |
* @return the value |
| 59 |
*/ |
| 60 |
public static ValueInteger get(int i) { |
| 61 |
if (i >= 0 && i < STATIC_SIZE) { |
| 62 |
return STATIC_CACHE[i]; |
| 63 |
} |
| 64 |
ValueInteger v = DYNAMIC_CACHE[i & (DYNAMIC_SIZE - 1)]; |
| 65 |
if (v == null || v.value != i) { |
| 66 |
v = new ValueInteger(i); |
| 67 |
DYNAMIC_CACHE[i & (DYNAMIC_SIZE - 1)] = v; |
| 68 |
} |
| 69 |
return v; |
| 70 |
} |
| 71 |
|
| 72 |
@Override |
| 73 |
public Value add(Value v) { |
| 74 |
ValueInteger other = (ValueInteger) v; |
| 75 |
return checkRange((long) value + (long) other.value); |
| 76 |
} |
| 77 |
|
| 78 |
private static ValueInteger checkRange(long x) { |
| 79 |
if ((int) x != x) { |
| 80 |
throw DbException.get(ErrorCode.NUMERIC_VALUE_OUT_OF_RANGE_1, Long.toString(x)); |
| 81 |
} |
| 82 |
return ValueInteger.get((int) x); |
| 83 |
} |
| 84 |
|
| 85 |
@Override |
| 86 |
public int getSignum() { |
| 87 |
return Integer.signum(value); |
| 88 |
} |
| 89 |
|
| 90 |
@Override |
| 91 |
public Value negate() { |
| 92 |
return checkRange(-(long) value); |
| 93 |
} |
| 94 |
|
| 95 |
@Override |
| 96 |
public Value subtract(Value v) { |
| 97 |
ValueInteger other = (ValueInteger) v; |
| 98 |
return checkRange((long) value - (long) other.value); |
| 99 |
} |
| 100 |
|
| 101 |
@Override |
| 102 |
public Value multiply(Value v) { |
| 103 |
ValueInteger other = (ValueInteger) v; |
| 104 |
return checkRange((long) value * (long) other.value); |
| 105 |
} |
| 106 |
|
| 107 |
@Override |
| 108 |
public Value divide(Value v, TypeInfo quotientType) { |
| 109 |
int y = ((ValueInteger) v).value; |
| 110 |
if (y == 0) { |
| 111 |
throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getTraceSQL()); |
| 112 |
} |
| 113 |
int x = value; |
| 114 |
if (x == Integer.MIN_VALUE && y == -1) { |
| 115 |
throw DbException.get(ErrorCode.NUMERIC_VALUE_OUT_OF_RANGE_1, "2147483648"); |
| 116 |
} |
| 117 |
return ValueInteger.get(x / y); |
| 118 |
} |
| 119 |
|
| 120 |
@Override |
| 121 |
public Value modulus(Value v) { |
| 122 |
ValueInteger other = (ValueInteger) v; |
| 123 |
if (other.value == 0) { |
| 124 |
throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getTraceSQL()); |
| 125 |
} |
| 126 |
return ValueInteger.get(value % other.value); |
| 127 |
} |
| 128 |
|
| 129 |
@Override |
| 130 |
public StringBuilder getSQL(StringBuilder builder, int sqlFlags) { |
| 131 |
return builder.append(value); |
| 132 |
} |
| 133 |
|
| 134 |
@Override |
| 135 |
public TypeInfo getType() { |
| 136 |
return TypeInfo.TYPE_INTEGER; |
| 137 |
} |
| 138 |
|
| 139 |
@Override |
| 140 |
public int getValueType() { |
| 141 |
return INTEGER; |
| 142 |
} |
| 143 |
|
| 144 |
@Override |
| 145 |
public byte[] getBytes() { |
| 146 |
byte[] b = new byte[4]; |
| 147 |
Bits.writeInt(b, 0, getInt()); |
| 148 |
return b; |
| 149 |
} |
| 150 |
|
| 151 |
@Override |
| 152 |
public int getInt() { |
| 153 |
return value; |
| 154 |
} |
| 155 |
|
| 156 |
@Override |
| 157 |
public long getLong() { |
| 158 |
return value; |
| 159 |
} |
| 160 |
|
| 161 |
@Override |
| 162 |
public BigDecimal getBigDecimal() { |
| 163 |
return BigDecimal.valueOf(value); |
| 164 |
} |
| 165 |
|
| 166 |
@Override |
| 167 |
public float getFloat() { |
| 168 |
return value; |
| 169 |
} |
| 170 |
|
| 171 |
@Override |
| 172 |
public double getDouble() { |
| 173 |
return value; |
| 174 |
} |
| 175 |
|
| 176 |
@Override |
| 177 |
public int compareTypeSafe(Value o, CompareMode mode, CastDataProvider provider) { |
| 178 |
return Integer.compare(value, ((ValueInteger) o).value); |
| 179 |
} |
| 180 |
|
| 181 |
@Override |
| 182 |
public String getString() { |
| 183 |
return Integer.toString(value); |
| 184 |
} |
| 185 |
|
| 186 |
@Override |
| 187 |
public int hashCode() { |
| 188 |
return value; |
| 189 |
} |
| 190 |
|
| 191 |
@Override |
| 192 |
public boolean equals(Object other) { |
| 193 |
return other instanceof ValueInteger && value == ((ValueInteger) other).value; |
| 194 |
} |
| 195 |
|
| 196 |
} |