1 package com.quadcap.sql.types; 2 3 40 41 import java.io.Externalizable ; 42 import java.io.IOException ; 43 import java.io.ObjectInput ; 44 import java.io.ObjectOutput ; 45 46 import com.quadcap.util.Util; 47 48 import com.quadcap.sql.io.Extern; 49 import com.quadcap.sql.io.Externable; 50 import com.quadcap.sql.io.ExternalizeProxy; 51 52 57 public class ValueInteger extends Value 58 implements Externalizable , ExternalizeProxy, Externable { 59 public static ValueInteger MINUS_ONE 60 = new ValueInteger(-1); 61 public static ValueInteger ZERO 62 = new ValueInteger(0); 63 public static ValueInteger PLUS_ONE 64 = new ValueInteger(1); 65 public static ValueInteger MIN_VALUE 66 = new ValueInteger(Integer.MIN_VALUE); 67 68 int val; 69 70 public ValueInteger() {} 71 72 public ValueInteger(int val) { this.val = val; } 73 74 public int intValue() { return val; } 75 76 public Value unop(int op) throws ValueException { 77 switch (op) { 78 case Op.NULL: 79 return ValueBoolean.falseBoolean; 80 case Op.PLUS: 81 return this; 82 case Op.MINUS: 83 return new ValueInteger(0 - val); 84 default: 85 throw new ValueException("Unary op: " + Op.toString(op) + 86 " not implemented for this type"); 87 } 88 } 89 90 public Value binop(int op, Value l) throws ValueException { 91 return l.binop(op, this); 92 } 93 94 public Value binop(int op, ValueNull r) throws ValueException { 95 switch (op) { 96 case Op.EQ: 97 case Op.NE: 98 case Op.LT: 99 case Op.LE: 100 case Op.GT: 101 case Op.GE: 102 return ValueUnknown.valueUnknown; 103 case Op.PLUS: 104 case Op.MINUS: 105 case Op.TIMES: 106 case Op.DIVIDE: 107 case Op.EXP: 108 return r; 109 case Op.COMPARE: 110 return r.valCmpNull(); 111 default: 112 throw badBinop(op, r); 113 } 114 } 115 116 public Value binop(int op, ValueInteger r) throws ValueException { 117 return ValueInteger.binop(op, this, r); 118 } 119 120 public Value binop(int op, ValueScaledInteger r) throws ValueException { 121 return ValueScaledInteger.binop(op, new ValueScaledInteger(val), r); 122 } 123 124 public Value binop(int op, ValueShort r) throws ValueException { 125 return ValueInteger.binop(op, this, new ValueInteger(r.val)); 126 } 127 128 public Value binop(int op, ValueByte r) throws ValueException { 129 return ValueInteger.binop(op, this, new ValueInteger(r.val)); 130 } 131 132 public Value binop(int op, ValueDouble r) throws ValueException { 133 return ValueDouble.binop(op, new ValueDouble(val), r); 134 } 135 136 public Value binop(int op, ValueLong r) throws ValueException { 137 return ValueLong.binop(op, new ValueLong(val), r); 138 } 139 140 public Value binop(int op, ValueFloat r) throws ValueException { 141 return ValueFloat.binop(op, new ValueFloat(val), r); 142 } 143 144 public static final Value binop(int op, ValueInteger e, ValueInteger f) 145 throws ValueException 146 { 147 switch (op) { 148 case Op.DIVIDE: 149 try { 150 return new ValueInteger(e.val / f.val); 151 } catch (ArithmeticException ex) { 152 throw new ValueException("divide by zero"); 153 } 154 case Op.EQ: 155 return new ValueBoolean(e.val == f.val); 156 case Op.EXP: 157 return new ValueInteger((int)(Math.pow(e.val, f.val))); 158 case Op.GE: 159 return new ValueBoolean(e.val >= f.val); 160 case Op.GT: 161 return new ValueBoolean(e.val > f.val); 162 case Op.LE: 163 return new ValueBoolean(e.val <= f.val); 164 case Op.LT: 165 return new ValueBoolean(e.val < f.val); 166 case Op.MINUS: 167 return new ValueInteger(e.val - f.val); 168 case Op.NE: 169 return new ValueBoolean(e.val != f.val); 170 case Op.PLUS: 171 return new ValueInteger(e.val + f.val); 172 case Op.TIMES: 173 return new ValueInteger(e.val * f.val); 174 case Op.COMPARE: 175 if (e.val < f.val) return MINUS_ONE; 176 if (e.val > f.val) return PLUS_ONE; 177 return ZERO; 178 179 default: 180 throw badBinop(op, e, f); 181 } 182 } 183 184 public void readExternal(ObjectInput in) throws IOException { 185 val = in.readInt(); 186 } 187 188 public void writeExternal(ObjectOutput out) 189 throws IOException 190 { 191 out.writeInt(val); 192 } 193 194 public Object readObject(ObjectInput in) 195 throws IOException , ClassNotFoundException 196 { 197 ValueInteger v = new ValueInteger(); 198 v.readExternal(in); 199 return v; 200 } 201 202 public void writeObject(ObjectOutput out, Object object) 203 throws IOException 204 { 205 ((ValueInteger)object).writeExternal(out); 206 } 207 208 public Object asJavaObject() { 209 return new Integer (val); 210 } 211 212 public void fromJavaObject(Object obj) throws ValueException { 213 if (obj instanceof Integer ) { 214 val = ((Integer )obj).intValue(); 215 } else { 216 throw new ValueException("bad type: " + obj); 217 } 218 } 219 220 public String toString() { return Integer.toString(val); } 221 222 public Value convert(TypeBoolean type) throws ValueException { 223 return TypeBoolean.convertNumber(val); 224 } 225 226 public Value convert(TypeTinyInt type) throws ValueException { 227 return TypeTinyInt.convertNumber(val); 228 } 229 230 public Value convert(TypeSmallInt type) throws ValueException { 231 return TypeSmallInt.convertNumber(val); 232 } 233 234 public Value convert(TypeInt type) throws ValueException { 235 return this; 236 } 237 238 public Value convert(TypeBigInt type) throws ValueException { 239 return new ValueLong(val); 240 } 241 242 public Value convert(TypeDecimal type) throws ValueException { 243 return type.convertNumber((long)val); 244 } 245 246 public Value convert(TypeReal type) throws ValueException { 247 if (type.getMaxPrecision() >= 32) { 248 return new ValueDouble((double)val); 249 } else { 250 return new ValueFloat((float)val); 251 } 252 } 253 254 public Type getType() { 255 return TypeInt.typeInt; 256 } 257 258 public void serializeKey(KeyStream out) throws IOException { 259 out.writeInt(val); 260 } 261 262 static Extern extern = null; 263 public Extern getExtern() { return extern; } 264 public void setExtern(Extern extern) { ValueInteger.extern = extern; } 265 266 } 267 | Popular Tags |