001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (http://h2database.com/html/license.html).
004: * Initial Developer: H2 Group
005: */
006: package org.h2.value;
007:
008: import java.sql.PreparedStatement;
009: import java.sql.SQLException;
010:
011: import org.h2.constant.ErrorCode;
012: import org.h2.constant.SysProperties;
013: import org.h2.message.Message;
014: import org.h2.util.ObjectUtils;
015:
016: /**
017: * Implementation of the INT data type.
018: */
019: public class ValueInt extends Value {
020: public static final int PRECISION = 10;
021: public static final int DISPLAY_SIZE = 11; // -2147483648
022:
023: private final int value;
024: private static final int STATIC_SIZE = 100;
025: private static final int DYNAMIC_SIZE = 256; // must be a power of 2
026: // TODO check performance of final static!
027: private static ValueInt[] staticCache;
028: private static ValueInt[] dynamicCache;
029:
030: static {
031: staticCache = new ValueInt[STATIC_SIZE];
032: dynamicCache = new ValueInt[DYNAMIC_SIZE];
033: for (int i = 0; i < STATIC_SIZE; i++) {
034: staticCache[i] = new ValueInt(i);
035: }
036: }
037:
038: public static ValueInt get(int i) {
039: if (i >= 0 && i < STATIC_SIZE) {
040: return staticCache[i];
041: }
042: ValueInt v = dynamicCache[i & DYNAMIC_SIZE - 1];
043: if (v == null || v.value != i) {
044: v = new ValueInt(i);
045: dynamicCache[i & DYNAMIC_SIZE - 1] = v;
046: }
047: return v;
048: }
049:
050: private ValueInt(int value) {
051: this .value = value;
052: }
053:
054: public Value add(Value v) throws SQLException {
055: ValueInt other = (ValueInt) v;
056: if (SysProperties.OVERFLOW_EXCEPTIONS) {
057: return checkRange((long) value + (long) other.value);
058: }
059: return ValueInt.get(value + other.value);
060: }
061:
062: private ValueInt checkRange(long value) throws SQLException {
063: if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
064: throw Message.getSQLException(
065: ErrorCode.OVERFLOW_FOR_TYPE_1, DataType
066: .getDataType(Value.INT).name);
067: } else {
068: return ValueInt.get((int) value);
069: }
070: }
071:
072: public int getSignum() {
073: return value == 0 ? 0 : (value < 0 ? -1 : 1);
074: }
075:
076: public Value negate() throws SQLException {
077: if (SysProperties.OVERFLOW_EXCEPTIONS) {
078: return checkRange(-(long) value);
079: }
080: return ValueInt.get(-value);
081: }
082:
083: public Value subtract(Value v) throws SQLException {
084: ValueInt other = (ValueInt) v;
085: if (SysProperties.OVERFLOW_EXCEPTIONS) {
086: return checkRange((long) value - (long) other.value);
087: }
088: return ValueInt.get(value - other.value);
089: }
090:
091: public Value multiply(Value v) throws SQLException {
092: ValueInt other = (ValueInt) v;
093: if (SysProperties.OVERFLOW_EXCEPTIONS) {
094: return checkRange((long) value * (long) other.value);
095: }
096: return ValueInt.get(value * other.value);
097: }
098:
099: public Value divide(Value v) throws SQLException {
100: ValueInt other = (ValueInt) v;
101: if (other.value == 0) {
102: throw Message.getSQLException(ErrorCode.DIVISION_BY_ZERO_1,
103: getSQL());
104: }
105: return ValueInt.get(value / other.value);
106: }
107:
108: public String getSQL() {
109: return getString();
110: }
111:
112: public int getType() {
113: return Value.INT;
114: }
115:
116: public int getInt() {
117: return value;
118: }
119:
120: protected int compareSecure(Value o, CompareMode mode) {
121: ValueInt v = (ValueInt) o;
122: if (value == v.value) {
123: return 0;
124: }
125: return value > v.value ? 1 : -1;
126: }
127:
128: public String getString() {
129: return String.valueOf(value);
130: }
131:
132: public long getPrecision() {
133: return PRECISION;
134: }
135:
136: public int hashCode() {
137: return value;
138: }
139:
140: public Object getObject() {
141: return ObjectUtils.getInteger(value);
142: }
143:
144: public void set(PreparedStatement prep, int parameterIndex)
145: throws SQLException {
146: prep.setInt(parameterIndex, value);
147: }
148:
149: public int getDisplaySize() {
150: return DISPLAY_SIZE;
151: }
152:
153: public boolean equals(Object other) {
154: return other instanceof ValueInt
155: && value == ((ValueInt) other).value;
156: }
157:
158: }
|