00001
package com.quadcap.sql.types;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
import java.io.Externalizable;
00042
import java.io.IOException;
00043
import java.io.ObjectInput;
00044
import java.io.ObjectOutput;
00045
00046
import com.quadcap.util.Util;
00047
00048
import com.quadcap.sql.io.Extern;
00049
import com.quadcap.sql.io.Externable;
00050
import com.quadcap.sql.io.ExternalizeProxy;
00051
00052
00053
00054
00055
00056
00057 public class ValueInteger extends Value
00058 implements
Externalizable, ExternalizeProxy, Externable {
00059
public static ValueInteger MINUS_ONE
00060 =
new ValueInteger(-1);
00061
public static ValueInteger
ZERO
00062 =
new ValueInteger(0);
00063
public static ValueInteger
PLUS_ONE
00064 =
new ValueInteger(1);
00065
public static ValueInteger
MIN_VALUE
00066 =
new ValueInteger(Integer.MIN_VALUE);
00067
00068 int val;
00069
00070 public ValueInteger() {}
00071
00072 public ValueInteger(
int val) {
this.val = val; }
00073
00074 public int intValue() {
return val; }
00075
00076 public Value unop(
int op)
throws ValueException {
00077
switch (op) {
00078
case Op.NULL:
00079
return ValueBoolean.falseBoolean;
00080
case Op.PLUS:
00081
return this;
00082
case Op.MINUS:
00083
return new ValueInteger(0 -
val);
00084
default:
00085
throw new ValueException(
"Unary op: " +
Op.toString(op) +
00086
" not implemented for this type");
00087 }
00088 }
00089
00090 public Value binop(
int op,
Value l)
throws ValueException {
00091
return l.binop(op,
this);
00092 }
00093
00094 public Value binop(
int op,
ValueNull r)
throws ValueException {
00095
switch (op) {
00096
case Op.EQ:
00097
case Op.NE:
00098
case Op.LT:
00099
case Op.LE:
00100
case Op.GT:
00101
case Op.GE:
00102
return ValueUnknown.valueUnknown;
00103
case Op.PLUS:
00104
case Op.MINUS:
00105
case Op.TIMES:
00106
case Op.DIVIDE:
00107
case Op.EXP:
00108
return r;
00109
case Op.COMPARE:
00110
return r.valCmpNull();
00111
default:
00112
throw badBinop(op, r);
00113 }
00114 }
00115
00116 public Value binop(
int op, ValueInteger r)
throws ValueException {
00117
return ValueInteger.
binop(op,
this, r);
00118 }
00119
00120 public Value binop(
int op,
ValueScaledInteger r)
throws ValueException {
00121
return ValueScaledInteger.
binop(op,
new ValueScaledInteger(
val), r);
00122 }
00123
00124 public Value binop(
int op,
ValueShort r)
throws ValueException {
00125
return ValueInteger.
binop(op,
this,
new ValueInteger(r.val));
00126 }
00127
00128 public Value binop(
int op,
ValueByte r)
throws ValueException {
00129
return ValueInteger.
binop(op,
this,
new ValueInteger(r.val));
00130 }
00131
00132 public Value binop(
int op,
ValueDouble r)
throws ValueException {
00133
return ValueDouble.
binop(op,
new ValueDouble(
val), r);
00134 }
00135
00136 public Value binop(
int op,
ValueLong r)
throws ValueException {
00137
return ValueLong.
binop(op,
new ValueLong(
val), r);
00138 }
00139
00140 public Value binop(
int op,
ValueFloat r)
throws ValueException {
00141
return ValueFloat.
binop(op,
new ValueFloat(
val), r);
00142 }
00143
00144 public static final Value binop(
int op, ValueInteger e, ValueInteger f)
00145
throws ValueException
00146 {
00147
switch (op) {
00148
case Op.DIVIDE:
00149
try {
00150
return new ValueInteger(e.val / f.val);
00151 }
catch (ArithmeticException ex) {
00152
throw new ValueException(
"divide by zero");
00153 }
00154
case Op.EQ:
00155
return new ValueBoolean(e.val == f.val);
00156
case Op.EXP:
00157
return new ValueInteger((
int)(Math.pow(e.val, f.val)));
00158
case Op.GE:
00159
return new ValueBoolean(e.val >= f.val);
00160
case Op.GT:
00161
return new ValueBoolean(e.val > f.val);
00162
case Op.LE:
00163
return new ValueBoolean(e.val <= f.val);
00164
case Op.LT:
00165
return new ValueBoolean(e.val < f.val);
00166
case Op.MINUS:
00167
return new ValueInteger(e.val - f.val);
00168
case Op.NE:
00169
return new ValueBoolean(e.val != f.val);
00170
case Op.PLUS:
00171
return new ValueInteger(e.val + f.val);
00172
case Op.TIMES:
00173
return new ValueInteger(e.val * f.val);
00174
case Op.COMPARE:
00175
if (e.val < f.val)
return MINUS_ONE;
00176
if (e.val > f.val)
return PLUS_ONE;
00177
return ZERO;
00178
00179
default:
00180
throw badBinop(op, e, f);
00181 }
00182 }
00183
00184 public void readExternal(ObjectInput in)
throws IOException {
00185
val = in.readInt();
00186 }
00187
00188 public void writeExternal(ObjectOutput out)
00189
throws IOException
00190 {
00191 out.writeInt(
val);
00192 }
00193
00194 public Object
readObject(ObjectInput in)
00195
throws IOException, ClassNotFoundException
00196 {
00197 ValueInteger v =
new ValueInteger();
00198 v.
readExternal(in);
00199
return v;
00200 }
00201
00202 public void writeObject(ObjectOutput out, Object object)
00203
throws IOException
00204 {
00205 ((ValueInteger)object).
writeExternal(out);
00206 }
00207
00208 public Object
asJavaObject() {
00209
return new Integer(
val);
00210 }
00211
00212 public void fromJavaObject(Object obj)
throws ValueException {
00213
if (obj instanceof Integer) {
00214
val = ((Integer)obj).intValue();
00215 }
else {
00216
throw new ValueException(
"bad type: " + obj);
00217 }
00218 }
00219
00220 public String
toString() {
return Integer.toString(
val); }
00221
00222 public Value convert(
TypeBoolean type)
throws ValueException {
00223
return TypeBoolean.convertNumber(
val);
00224 }
00225
00226 public Value convert(
TypeTinyInt type)
throws ValueException {
00227
return TypeTinyInt.convertNumber(
val);
00228 }
00229
00230 public Value convert(
TypeSmallInt type)
throws ValueException {
00231
return TypeSmallInt.convertNumber(
val);
00232 }
00233
00234 public Value convert(
TypeInt type)
throws ValueException {
00235
return this;
00236 }
00237
00238 public Value convert(
TypeBigInt type)
throws ValueException {
00239
return new ValueLong(
val);
00240 }
00241
00242 public Value convert(
TypeDecimal type)
throws ValueException {
00243
return type.convertNumber((
long)
val);
00244 }
00245
00246 public Value convert(
TypeReal type)
throws ValueException {
00247
if (type.getMaxPrecision() >= 32) {
00248
return new ValueDouble((
double)
val);
00249 }
else {
00250
return new ValueFloat((
float)
val);
00251 }
00252 }
00253
00254 public Type getType() {
00255
return TypeInt.typeInt;
00256 }
00257
00258 public void serializeKey(
KeyStream out)
throws IOException {
00259 out.writeInt(
val);
00260 }
00261
00262 static Extern extern = null;
00263 public Extern getExtern() {
return extern; }
00264 public void setExtern(
Extern extern) { ValueInteger.extern =
extern; }
00265
00266 }