File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\value\ValueInt.java.html

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