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
/*
* Copyright 2004-2018 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.sql.PreparedStatement;
import java.sql.SQLException;
import org.h2.api.ErrorCode;
import org.h2.message.DbException;
/**
* Implementation of the BYTE data type.
*/
public class ValueByte extends Value {
/**
* The precision in digits.
*/
static final int PRECISION = 3;
/**
* The display size for a byte.
* Example: -127
*/
static final int DISPLAY_SIZE = 4;
private final byte value;
private ValueByte(byte value) {
this.value = value;
}
@Override
public Value add(Value v) {
ValueByte other = (ValueByte) v;
return checkRange(value + other.value);
}
private static ValueByte checkRange(int x) {
if ((byte) x != x) {
throw DbException.get(ErrorCode.NUMERIC_VALUE_OUT_OF_RANGE_1,
Integer.toString(x));
}
return ValueByte.get((byte) x);
}
@Override
public int getSignum() {
return Integer.signum(value);
}
@Override
public Value negate() {
return checkRange(-(int) value);
}
@Override
public Value subtract(Value v) {
ValueByte other = (ValueByte) v;
return checkRange(value - other.value);
}
@Override
public Value multiply(Value v) {
ValueByte other = (ValueByte) v;
return checkRange(value * other.value);
}
@Override
public Value divide(Value v) {
ValueByte other = (ValueByte) v;
if (other.value == 0) {
throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getSQL());
}
return checkRange(value / other.value);
}
@Override
public Value modulus(Value v) {
ValueByte other = (ValueByte) v;
if (other.value == 0) {
throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getSQL());
}
return ValueByte.get((byte) (value % other.value));
}
@Override
public StringBuilder getSQL(StringBuilder builder) {
return builder.append(value);
}
@Override
public int getType() {
return Value.BYTE;
}
@Override
public byte getByte() {
return value;
}
@Override
public int getInt() {
return value;
}
@Override
public int compareTypeSafe(Value o, CompareMode mode) {
return Integer.compare(value, ((ValueByte) o).value);
}
@Override
public String getString() {
return Integer.toString(value);
}
@Override
public long getPrecision() {
return PRECISION;
}
@Override
public int hashCode() {
return value;
}
@Override
public Object getObject() {
return value;
}
@Override
public void set(PreparedStatement prep, int parameterIndex)
throws SQLException {
prep.setByte(parameterIndex, value);
}
/**
* Get or create byte value for the given byte.
*
* @param i the byte
* @return the value
*/
public static ValueByte get(byte i) {
return (ValueByte) Value.cache(new ValueByte(i));
}
@Override
public int getDisplaySize() {
return DISPLAY_SIZE;
}
@Override
public boolean equals(Object other) {
return other instanceof ValueByte && value == ((ValueByte) other).value;
}
}