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 SMALLINT data type.
018: */
019: public class ValueShort extends Value {
020: public static final int PRECISION = 5;
021: public static final int DISPLAY_SIZE = 6; // -32768
022:
023: private final short value;
024:
025: private ValueShort(short value) {
026: this .value = value;
027: }
028:
029: public Value add(Value v) throws SQLException {
030: ValueShort other = (ValueShort) v;
031: if (SysProperties.OVERFLOW_EXCEPTIONS) {
032: return checkRange(value + other.value);
033: }
034: return ValueShort.get((short) (value + other.value));
035: }
036:
037: private ValueShort checkRange(int value) throws SQLException {
038: if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) {
039: throw Message.getSQLException(
040: ErrorCode.OVERFLOW_FOR_TYPE_1, DataType
041: .getDataType(Value.SHORT).name);
042: } else {
043: return ValueShort.get((short) value);
044: }
045: }
046:
047: public int getSignum() {
048: return value == 0 ? 0 : (value < 0 ? -1 : 1);
049: }
050:
051: public Value negate() throws SQLException {
052: if (SysProperties.OVERFLOW_EXCEPTIONS) {
053: return checkRange(-(int) value);
054: }
055: return ValueShort.get((short) (-value));
056: }
057:
058: public Value subtract(Value v) throws SQLException {
059: ValueShort other = (ValueShort) v;
060: if (SysProperties.OVERFLOW_EXCEPTIONS) {
061: return checkRange(value - other.value);
062: }
063: return ValueShort.get((short) (value - other.value));
064: }
065:
066: public Value multiply(Value v) throws SQLException {
067: ValueShort other = (ValueShort) v;
068: if (SysProperties.OVERFLOW_EXCEPTIONS) {
069: return checkRange(value * other.value);
070: }
071: return ValueShort.get((short) (value * other.value));
072: }
073:
074: public Value divide(Value v) throws SQLException {
075: ValueShort other = (ValueShort) v;
076: if (other.value == 0) {
077: throw Message.getSQLException(ErrorCode.DIVISION_BY_ZERO_1,
078: getSQL());
079: }
080: return ValueShort.get((short) (value / other.value));
081: }
082:
083: public String getSQL() {
084: return getString();
085: }
086:
087: public int getType() {
088: return Value.SHORT;
089: }
090:
091: public short getShort() {
092: return value;
093: }
094:
095: protected int compareSecure(Value o, CompareMode mode) {
096: ValueShort v = (ValueShort) o;
097: if (value == v.value) {
098: return 0;
099: }
100: return value > v.value ? 1 : -1;
101: }
102:
103: public String getString() {
104: return String.valueOf(value);
105: }
106:
107: public long getPrecision() {
108: return PRECISION;
109: }
110:
111: public int hashCode() {
112: return value;
113: }
114:
115: public Object getObject() {
116: return ObjectUtils.getShort(value);
117: }
118:
119: public void set(PreparedStatement prep, int parameterIndex)
120: throws SQLException {
121: prep.setShort(parameterIndex, value);
122: }
123:
124: public static ValueShort get(short i) {
125: return (ValueShort) Value.cache(new ValueShort(i));
126: }
127:
128: public int getDisplaySize() {
129: return DISPLAY_SIZE;
130: }
131:
132: public boolean equals(Object other) {
133: return other instanceof ValueShort
134: && value == ((ValueShort) other).value;
135: }
136:
137: }
|