Debian Codegraph @

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