1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://h2database.com/html/license.html). |
4 | * Initial Developer: H2 Group |
5 | */ |
6 | package org.h2.value; |
7 | |
8 | import java.sql.PreparedStatement; |
9 | import java.sql.SQLException; |
10 | |
11 | import org.h2.api.ErrorCode; |
12 | import org.h2.message.DbException; |
13 | import org.h2.util.MathUtils; |
14 | |
15 | /** |
16 | * Implementation of the SMALLINT data type. |
17 | */ |
18 | public class ValueShort extends Value { |
19 | |
20 | /** |
21 | * The precision in digits. |
22 | */ |
23 | static final int PRECISION = 5; |
24 | |
25 | /** |
26 | * The maximum display size of a short. |
27 | * Example: -32768 |
28 | */ |
29 | static final int DISPLAY_SIZE = 6; |
30 | |
31 | private final short value; |
32 | |
33 | private ValueShort(short value) { |
34 | this.value = value; |
35 | } |
36 | |
37 | @Override |
38 | public Value add(Value v) { |
39 | ValueShort other = (ValueShort) v; |
40 | return checkRange(value + other.value); |
41 | } |
42 | |
43 | private static ValueShort checkRange(int x) { |
44 | if (x < Short.MIN_VALUE || x > Short.MAX_VALUE) { |
45 | throw DbException.get(ErrorCode.NUMERIC_VALUE_OUT_OF_RANGE_1, |
46 | Integer.toString(x)); |
47 | } |
48 | return ValueShort.get((short) x); |
49 | } |
50 | |
51 | @Override |
52 | public int getSignum() { |
53 | return Integer.signum(value); |
54 | } |
55 | |
56 | @Override |
57 | public Value negate() { |
58 | return checkRange(-(int) value); |
59 | } |
60 | |
61 | @Override |
62 | public Value subtract(Value v) { |
63 | ValueShort other = (ValueShort) v; |
64 | return checkRange(value - other.value); |
65 | } |
66 | |
67 | @Override |
68 | public Value multiply(Value v) { |
69 | ValueShort other = (ValueShort) v; |
70 | return checkRange(value * other.value); |
71 | } |
72 | |
73 | @Override |
74 | public Value divide(Value v) { |
75 | ValueShort other = (ValueShort) v; |
76 | if (other.value == 0) { |
77 | throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getSQL()); |
78 | } |
79 | return ValueShort.get((short) (value / other.value)); |
80 | } |
81 | |
82 | @Override |
83 | public Value modulus(Value v) { |
84 | ValueShort other = (ValueShort) v; |
85 | if (other.value == 0) { |
86 | throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getSQL()); |
87 | } |
88 | return ValueShort.get((short) (value % other.value)); |
89 | } |
90 | |
91 | @Override |
92 | public String getSQL() { |
93 | return getString(); |
94 | } |
95 | |
96 | @Override |
97 | public int getType() { |
98 | return Value.SHORT; |
99 | } |
100 | |
101 | @Override |
102 | public short getShort() { |
103 | return value; |
104 | } |
105 | |
106 | @Override |
107 | protected int compareSecure(Value o, CompareMode mode) { |
108 | ValueShort v = (ValueShort) o; |
109 | return MathUtils.compareInt(value, v.value); |
110 | } |
111 | |
112 | @Override |
113 | public String getString() { |
114 | return String.valueOf(value); |
115 | } |
116 | |
117 | @Override |
118 | public long getPrecision() { |
119 | return PRECISION; |
120 | } |
121 | |
122 | @Override |
123 | public int hashCode() { |
124 | return value; |
125 | } |
126 | |
127 | @Override |
128 | public Object getObject() { |
129 | return Short.valueOf(value); |
130 | } |
131 | |
132 | @Override |
133 | public void set(PreparedStatement prep, int parameterIndex) |
134 | throws SQLException { |
135 | prep.setShort(parameterIndex, value); |
136 | } |
137 | |
138 | /** |
139 | * Get or create a short value for the given short. |
140 | * |
141 | * @param i the short |
142 | * @return the value |
143 | */ |
144 | public static ValueShort get(short i) { |
145 | return (ValueShort) Value.cache(new ValueShort(i)); |
146 | } |
147 | |
148 | @Override |
149 | public int getDisplaySize() { |
150 | return DISPLAY_SIZE; |
151 | } |
152 | |
153 | @Override |
154 | public boolean equals(Object other) { |
155 | return other instanceof ValueShort && value == ((ValueShort) other).value; |
156 | } |
157 | |
158 | } |