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 51 public class ValueShort extends Value implements Externalizable { 52 short val; 53 54 public ValueShort() {} 55 56 public ValueShort(short val) { this.val = val; } 57 58 public ValueShort(int v) throws ValueException { 59 if (v < Short.MIN_VALUE || v > Short.MAX_VALUE) { 60 throw new ValueException("Can't convert to SMALLINT: " + v); 61 } 62 this.val = (short)v; 63 } 64 65 public final int intValue() { return val; } 66 public final short shortValue() { return val; } 67 68 public Value unop(int op) throws ValueException { 69 switch (op) { 70 case Op.NULL: 71 return ValueBoolean.falseBoolean; 72 case Op.PLUS: 73 return this; 74 case Op.MINUS: 75 return new ValueShort(0 - val); 76 default: 77 throw new ValueException("Unary op: " + Op.toString(op) + 78 " not implemented for this type"); 79 } 80 } 81 82 public Value binop(int op, Value l) throws ValueException { 83 return l.binop(op, this); 84 } 85 86 public Value binop(int op, ValueNull r) throws ValueException { 87 switch (op) { 88 case Op.EQ: 89 case Op.NE: 90 case Op.LT: 91 case Op.LE: 92 case Op.GT: 93 case Op.GE: 94 return ValueUnknown.valueUnknown; 95 case Op.PLUS: 96 case Op.MINUS: 97 case Op.TIMES: 98 case Op.DIVIDE: 99 case Op.EXP: 100 return r; 101 case Op.COMPARE: 102 return r.valCmpNull(); 103 default: 104 throw badBinop(op, r); 105 } 106 } 107 108 public Value binop(int op, ValueDouble r) throws ValueException { 109 return ValueDouble.binop(op, new ValueDouble(val), r); 110 } 111 112 public Value binop(int op, ValueFloat r) throws ValueException { 113 return ValueFloat.binop(op, new ValueFloat(val), r); 114 } 115 116 public Value binop(int op, ValueInteger r) throws ValueException { 117 return ValueInteger.binop(op, new ValueInteger(val), r); 118 } 119 120 public Value binop(int op, ValueLong r) throws ValueException { 121 return ValueLong.binop(op, new ValueLong(val), r); 122 } 123 124 public Value binop(int op, ValueByte r) throws ValueException { 125 return ValueShort.binop(op, this, new ValueShort(r.val)); 126 } 127 128 public Value binop(int op, ValueScaledInteger r) throws ValueException { 129 return ValueScaledInteger.binop(op, new ValueScaledInteger(val), r); 130 } 131 132 public Value binop(int op, ValueShort r) throws ValueException { 133 return ValueShort.binop(op, this, r); 134 } 135 136 public static final Value binop(int op, ValueShort e, ValueShort f) 137 throws ValueException 138 { 139 switch (op) { 140 case Op.DIVIDE: 141 try { 142 return new ValueShort(e.val / f.val); 143 } catch (ArithmeticException ae) { 144 throw new ValueException("divide by zero"); 145 } 146 case Op.EQ: 147 return new ValueBoolean(e.val == f.val); 148 case Op.EXP: 149 return new ValueShort((int)(Math.pow(e.val, f.val))); 150 case Op.GE: 151 return new ValueBoolean(e.val >= f.val); 152 case Op.GT: 153 return new ValueBoolean(e.val > f.val); 154 case Op.LE: 155 return new ValueBoolean(e.val <= f.val); 156 case Op.LT: 157 return new ValueBoolean(e.val < f.val); 158 case Op.MINUS: 159 return new ValueShort(e.val - f.val); 160 case Op.NE: 161 return new ValueBoolean(e.val != f.val); 162 case Op.PLUS: 163 return new ValueShort(e.val + f.val); 164 case Op.TIMES: 165 return new ValueShort(e.val * f.val); 166 case Op.COMPARE: 167 if (e.val < f.val) return ValueInteger.MINUS_ONE; 168 if (e.val > f.val) return ValueInteger.PLUS_ONE; 169 return ValueInteger.ZERO; 170 171 default: 172 throw badBinop(op, e, f); 173 } 174 } 175 176 public void readExternal(ObjectInput in) throws IOException { 177 val = in.readShort(); 178 } 179 180 public void writeExternal(ObjectOutput out) 181 throws IOException 182 { 183 out.writeShort(val); 184 } 185 186 public Object asJavaObject() { 187 return new Short (val); 188 } 189 190 public void fromJavaObject(Object obj) throws ValueException { 191 if (obj instanceof Short ) { 192 val = ((Short )obj).shortValue(); 193 } else { 194 throw new ValueException("bad type: " + obj); 195 } 196 } 197 198 public String toString() { return Short.toString(val); } 199 200 public Value convert(TypeBoolean type) throws ValueException { 201 return TypeBoolean.convertNumber(val); 202 } 203 204 public Value convert(TypeTinyInt type) throws ValueException { 205 return TypeTinyInt.convertNumber(val); 206 } 207 208 public Value convert(TypeSmallInt type) throws ValueException { 209 return this; 210 } 211 212 public Value convert(TypeInt type) throws ValueException { 213 return new ValueInteger(val); 214 } 215 216 public Value convert(TypeBigInt type) throws ValueException { 217 return new ValueLong(val); 218 } 219 220 public Value convert(TypeDecimal type) throws ValueException { 221 return new ValueScaledInteger((long)val); 222 } 223 224 public Value convert(TypeReal type) throws ValueException { 225 if (type.getMaxPrecision() >= 32) { 226 return new ValueDouble((double)val); 227 } else { 228 return new ValueFloat((float)val); 229 } 230 } 231 232 public Type getType() { 233 return TypeSmallInt.typeSmallInt; 234 } 235 236 public void serializeKey(KeyStream out) throws IOException { 237 out.writeShort(val); 238 } 239 } 240 | Popular Tags |