File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\value\ValueByte.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 BYTE data type.
17 */
18 public class ValueByte extends Value {
19
20 /**
21 * The precision in digits.
22 */
23 static final int PRECISION = 3;
24
25 /**
26 * The display size for a byte.
27 * Example: -127
28 */
29 static final int DISPLAY_SIZE = 4;
30
31 private final byte value;
32
33 private ValueByte(byte value) {
34 this.value = value;
35 }
36
37 public Value add(Value v) {
38 ValueByte other = (ValueByte) v;
39 return checkRange(value + other.value);
40 }
41
42 private static ValueByte checkRange(int x) {
43 if (x < Byte.MIN_VALUE || x > Byte.MAX_VALUE) {
44 throw DbException.get(ErrorCode.NUMERIC_VALUE_OUT_OF_RANGE_1, Integer.toString(x));
45 }
46 return ValueByte.get((byte) x);
47 }
48
49 public int getSignum() {
50 return Integer.signum(value);
51 }
52
53 public Value negate() {
54 return checkRange(-(int) value);
55 }
56
57 public Value subtract(Value v) {
58 ValueByte other = (ValueByte) v;
59 return checkRange(value - other.value);
60 }
61
62 public Value multiply(Value v) {
63 ValueByte other = (ValueByte) v;
64 return checkRange(value * other.value);
65 }
66
67 public Value divide(Value v) {
68 ValueByte other = (ValueByte) v;
69 if (other.value == 0) {
70 throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getSQL());
71 }
72 return ValueByte.get((byte) (value / other.value));
73 }
74
75 public Value modulus(Value v) {
76 ValueByte other = (ValueByte) v;
77 if (other.value == 0) {
78 throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getSQL());
79 }
80 return ValueByte.get((byte) (value % other.value));
81 }
82
83 public String getSQL() {
84 return getString();
85 }
86
87 public int getType() {
88 return Value.BYTE;
89 }
90
91 public byte getByte() {
92 return value;
93 }
94
95 protected int compareSecure(Value o, CompareMode mode) {
96 ValueByte v = (ValueByte) o;
97 return MathUtils.compareInt(value, v.value);
98 }
99
100 public String getString() {
101 return String.valueOf(value);
102 }
103
104 public long getPrecision() {
105 return PRECISION;
106 }
107
108 public int hashCode() {
109 return value;
110 }
111
112 public Object getObject() {
113 return Byte.valueOf(value);
114 }
115
116 public void set(PreparedStatement prep, int parameterIndex) throws SQLException {
117 prep.setByte(parameterIndex, value);
118 }
119
120 /**
121 * Get or create byte value for the given byte.
122 *
123 * @param i the byte
124 * @return the value
125 */
126 public static ValueByte get(byte i) {
127 return (ValueByte) Value.cache(new ValueByte(i));
128 }
129
130 public int getDisplaySize() {
131 return DISPLAY_SIZE;
132 }
133
134 public boolean equals(Object other) {
135 return other instanceof ValueByte && value == ((ValueByte) other).value;
136 }
137
138 }
139