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