1 5 package org.h2.value; 6 7 import java.sql.PreparedStatement ; 8 import java.sql.SQLException ; 9 10 import org.h2.engine.Constants; 11 import org.h2.message.Message; 12 13 16 public class ValueInt extends Value { 17 public static final int PRECISION = 10; 18 19 private int value; 20 private static final int STATIC_SIZE = 100; 21 private static final int DYNAMIC_SIZE = 256; private static ValueInt[] staticCache; 24 private static ValueInt[] dynamicCache; 25 26 static { 27 staticCache = new ValueInt[STATIC_SIZE]; 28 dynamicCache = new ValueInt[DYNAMIC_SIZE]; 29 for (int i = 0; i < STATIC_SIZE; i++) { 30 staticCache[i] = new ValueInt(i); 31 } 32 } 33 34 public static ValueInt get(int i) { 35 if (i >= 0 && i < STATIC_SIZE) { 36 return staticCache[i]; 37 } 38 ValueInt v = dynamicCache[i & DYNAMIC_SIZE - 1]; 39 if (v == null || v.value != i) { 40 v = new ValueInt(i); 41 dynamicCache[i & DYNAMIC_SIZE - 1] = v; 42 } 43 return v; 44 } 45 46 private ValueInt(int value) { 47 this.value = value; 48 } 49 50 public Value add(Value v) throws SQLException { 51 ValueInt other = (ValueInt) v; 52 if(Constants.OVERFLOW_EXCEPTIONS) { 53 return checkRange((long)value + (long)other.value); 54 } 55 return ValueInt.get(value + other.value); 56 } 57 58 private ValueInt checkRange(long value) throws SQLException { 59 if(value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) { 60 throw Message.getSQLException(Message.OVERFLOW_FOR_TYPE_1, DataType.getDataType(Value.INT).name); 61 } else { 62 return ValueInt.get((int)value); 63 } 64 } 65 66 public int getSignum() { 67 return value == 0 ? 0 : (value < 0 ? -1 : 1); 68 } 69 70 public Value negate() throws SQLException { 71 if(Constants.OVERFLOW_EXCEPTIONS) { 72 return checkRange(-(long)value); 73 } 74 return ValueInt.get(-value); 75 } 76 77 public Value subtract(Value v) throws SQLException { 78 ValueInt other = (ValueInt) v; 79 if(Constants.OVERFLOW_EXCEPTIONS) { 80 return checkRange((long)value - (long)other.value); 81 } 82 return ValueInt.get(value - other.value); 83 } 84 85 public Value multiply(Value v) throws SQLException { 86 ValueInt other = (ValueInt) v; 87 if(Constants.OVERFLOW_EXCEPTIONS) { 88 return checkRange((long)value * (long)other.value); 89 } 90 return ValueInt.get(value * other.value); 91 } 92 93 public Value divide(Value v) throws SQLException { 94 ValueInt other = (ValueInt) v; 95 if (other.value == 0) { 96 throw Message.getSQLException(Message.DIVISION_BY_ZERO_1, getSQL()); 97 } 98 return ValueInt.get(value / other.value); 99 } 100 101 public String getSQL() { 102 return getString(); 103 } 104 105 public int getType() { 106 return Value.INT; 107 } 108 109 public int getInt() { 110 return value; 111 } 112 113 protected int compareSecure(Value o, CompareMode mode) { 114 ValueInt v = (ValueInt) o; 115 if (value == v.value) { 116 return 0; 117 } 118 return value > v.value ? 1 : -1; 119 } 120 121 public String getString() { 122 return String.valueOf(value); 123 } 124 125 public long getPrecision() { 126 return PRECISION; 127 } 128 129 public int hashCode() { 130 return value; 131 } 132 133 public Object getObject() { 134 return new Integer (value); 135 } 136 137 public void set(PreparedStatement prep, int parameterIndex) throws SQLException { 138 prep.setInt(parameterIndex, value); 139 } 140 141 145 public int getDisplaySize() { 146 return PRECISION; 147 } 148 149 protected boolean isEqual(Value v) { 150 return v instanceof ValueInt && value == ((ValueInt)v).value; 151 } 152 153 } 154 | Popular Tags |