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