1 5 package org.h2.value; 6 7 import java.math.BigInteger ; 8 import java.sql.PreparedStatement ; 9 import java.sql.SQLException ; 10 11 import org.h2.engine.Constants; 12 import org.h2.message.Message; 13 14 public class ValueLong extends Value { 15 16 private long value; 17 18 public static final int PRECISION = 19; 19 private static final int STATIC_SIZE = 10; 20 private static ValueLong[] cache; 21 private static final BigInteger MIN = new BigInteger ("" + Long.MIN_VALUE); 22 private static final BigInteger MAX = new BigInteger ("" + Long.MAX_VALUE); 23 24 static { 25 cache = new ValueLong[STATIC_SIZE]; 26 for (int i = 0; i < STATIC_SIZE; i++) { 27 cache[i] = new ValueLong(i); 28 } 29 } 30 31 private ValueLong(long value) { 32 this.value = value; 33 } 34 35 public Value add(Value v) throws SQLException { 36 ValueLong other = (ValueLong) v; 37 if(Constants.OVERFLOW_EXCEPTIONS) { 38 long result = value + other.value; 39 int sv = value == 0 ? 0 : (value < 0 ? -1 : 1); 40 int so = other.value == 0 ? 0 : (other.value < 0 ? -1 : 1); 41 int sr = result == 0 ? 0 : (result < 0 ? -1 : 1); 42 if(sv!=so || sr==so || sv==0 || so==0) { 46 return ValueLong.get(result); 47 } 48 throw getOverflow(); 49 } 50 return ValueLong.get(value + other.value); 51 } 52 53 public int getSignum() { 54 return value == 0 ? 0 : (value < 0 ? -1 : 1); 55 } 56 57 public Value negate() throws SQLException { 58 if(Constants.OVERFLOW_EXCEPTIONS) { 59 if(value == Long.MIN_VALUE) { 60 throw getOverflow(); 61 } 62 } 63 return ValueLong.get(-value); 64 } 65 66 private SQLException getOverflow() { 67 return Message.getSQLException(Message.OVERFLOW_FOR_TYPE_1, DataType.getDataType(Value.LONG).name); 68 } 69 70 public Value subtract(Value v) throws SQLException { 71 ValueLong other = (ValueLong) v; 72 if(Constants.OVERFLOW_EXCEPTIONS) { 73 int sv = value == 0 ? 0 : (value < 0 ? -1 : 1); 74 int so = other.value == 0 ? 0 : (other.value < 0 ? -1 : 1); 75 if(sv==so || so==0) { 78 return ValueLong.get(value - other.value); 79 } 80 return add(other.negate()); 83 } 84 return ValueLong.get(value - other.value); 85 } 86 87 private boolean isInteger(long a) { 88 return a >= Integer.MIN_VALUE && a <= Integer.MAX_VALUE; 89 } 90 91 public Value multiply(Value v) throws SQLException { 92 ValueLong other = (ValueLong) v; 93 if(Constants.OVERFLOW_EXCEPTIONS) { 94 long result = value * other.value; 95 if(value == 0 || value == 1 || other.value == 0 || other.value == 1) { 96 return ValueLong.get(result); 97 } 98 if(isInteger(value) && isInteger(other.value)) { 99 return ValueLong.get(result); 100 } 101 BigInteger bv = new BigInteger ("" + value); 107 BigInteger bo = new BigInteger ("" + other.value); 108 BigInteger br = bv.multiply(bo); 109 if(br.compareTo(MIN) < 0 || br.compareTo(MAX) > 0) { 110 throw getOverflow(); 111 } 112 return ValueLong.get(br.longValue()); 113 } 114 return ValueLong.get(value * other.value); 115 } 116 117 public Value divide(Value v) throws SQLException { 118 ValueLong other = (ValueLong) v; 119 if (other.value == 0) { 120 throw Message.getSQLException(Message.DIVISION_BY_ZERO_1, getSQL()); 121 } 122 return ValueLong.get(value / other.value); 123 } 124 125 public String getSQL() { 126 return getString(); 127 } 128 129 public int getType() { 130 return Value.LONG; 131 } 132 133 public long getLong() { 134 return value; 135 } 136 137 protected int compareSecure(Value o, CompareMode mode) { 138 ValueLong v = (ValueLong) o; 139 if (value == v.value) { 140 return 0; 141 } 142 return value > v.value ? 1 : -1; 143 } 144 145 public String getString() { 146 return String.valueOf(value); 147 } 148 149 public long getPrecision() { 150 return PRECISION; 151 } 152 153 public int hashCode() { 154 return (int) (value ^ (value >> 32)); 155 } 156 157 public Object getObject() { 158 return new Long (value); 159 } 160 161 public void set(PreparedStatement prep, int parameterIndex) throws SQLException { 162 prep.setLong(parameterIndex, value); 163 } 164 165 public static ValueLong get(long i) { 166 if (i >= 0 && i < STATIC_SIZE) { 167 return cache[(int) i]; 168 } 169 return (ValueLong) Value.cache(new ValueLong(i)); 170 } 171 172 176 public int getDisplaySize() { 177 return PRECISION; 178 } 179 180 protected boolean isEqual(Value v) { 181 return v instanceof ValueLong && value == ((ValueLong)v).value; 182 } 183 184 } 185 | Popular Tags |