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 java.sql.Types;
00047
00048
00049
00050
00051
00052
00053 public class ValueFloat extends Value implements
Externalizable {
00054 float val;
00055
00056 public ValueFloat() {}
00057
00058 public ValueFloat(
float val) {
this.val = val; }
00059
00060 public ValueFloat(String s)
throws ValueException {
00061
try {
00062
00063
00064
00065
this.val = Float.parseFloat(s);
00066
00067 }
catch (NumberFormatException ne) {
00068
throw new ValueException(
"FLOAT format error");
00069 }
00070 }
00071
00072 public final float floatValue() {
return val; }
00073
00074 public Value unop(
int op)
throws ValueException {
00075
switch (op) {
00076
case Op.NULL:
00077
return ValueBoolean.falseBoolean;
00078
case Op.PLUS:
00079
return this;
00080
case Op.MINUS:
00081
return new ValueFloat(0.0f -
val);
00082
default:
00083
throw new ValueException(
"Unary op: " +
Op.toString(op) +
00084
" not implemented for this type");
00085 }
00086 }
00087
00088 public Value binop(
int op,
Value l)
throws ValueException {
00089
return l.binop(op,
this);
00090 }
00091
00092 public Value binop(
int op,
ValueFloat r)
throws ValueException {
00093
return ValueFloat.
binop(op,
this, r);
00094 }
00095
00096 public Value binop(
int op,
ValueDouble r)
throws ValueException {
00097
return ValueDouble.
binop(op,
new ValueDouble(
val), r);
00098 }
00099
00100 public Value binop(
int op,
ValueInteger r)
throws ValueException {
00101
return Value.
binop(op,
this,
new ValueFloat(r.val));
00102 }
00103
00104 public Value binop(
int op,
ValueShort r)
throws ValueException {
00105
return Value.
binop(op,
this,
new ValueFloat(r.val));
00106 }
00107
00108 public Value binop(
int op,
ValueByte r)
throws ValueException {
00109
return Value.
binop(op,
this,
new ValueFloat(r.val));
00110 }
00111
00112 public Value binop(
int op,
ValueLong r)
throws ValueException {
00113
return Value.
binop(op,
this,
new ValueFloat(r.val));
00114 }
00115
00116 public Value binop(
int op,
ValueScaledInteger r)
throws ValueException {
00117
return ValueDouble.
binop(op,
new ValueDouble(
val),
00118
new ValueDouble(r.doubleValue()));
00119 }
00120
00121 public Value binop(
int op,
ValueNull r)
throws ValueException {
00122
switch (op) {
00123
case Op.EQ:
00124
case Op.NE:
00125
case Op.LT:
00126
case Op.LE:
00127
case Op.GT:
00128
case Op.GE:
00129
return ValueUnknown.valueUnknown;
00130
case Op.PLUS:
00131
case Op.MINUS:
00132
case Op.TIMES:
00133
case Op.DIVIDE:
00134
case Op.EXP:
00135
return r;
00136
case Op.COMPARE:
00137
return r.valCmpNull();
00138
default:
00139
throw badBinop(op, r);
00140 }
00141 }
00142
00143
00144 public static final Value binop(
int op,
ValueFloat e,
ValueFloat f)
00145
throws ValueException
00146 {
00147
switch (op) {
00148
case Op.DIVIDE:
00149
try {
00150
return new ValueFloat(e.val / f.val);
00151 }
catch (ArithmeticException ae) {
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 ValueFloat((
float)(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 ValueFloat(e.val - f.val);
00168
case Op.NE:
00169
return new ValueBoolean(e.val != f.val);
00170
case Op.PLUS:
00171
return new ValueFloat(e.val + f.val);
00172
case Op.TIMES:
00173
return new ValueFloat(e.val * f.val);
00174
case Op.COMPARE:
00175
if (e.val < f.val)
return ValueInteger.MINUS_ONE;
00176
if (e.val > f.val)
return ValueInteger.PLUS_ONE;
00177
return ValueInteger.ZERO;
00178
00179
default:
00180
throw badBinop(op, e, f);
00181 }
00182 }
00183
00184 public Object
asJavaObject() {
00185
return new Float(
val);
00186 }
00187
00188 public void fromJavaObject(Object obj)
throws ValueException {
00189
if (obj instanceof Float) {
00190
val = ((Float)obj).floatValue();
00191 }
else {
00192
throw new ValueException(
"bad type: " + obj);
00193 }
00194 }
00195
00196 public void readExternal(ObjectInput in)
throws IOException {
00197
val = in.readFloat();
00198 }
00199
00200 public void writeExternal(ObjectOutput out)
00201
throws IOException
00202 {
00203 out.writeFloat(
val);
00204 }
00205
00206 public Value convert(
TypeTinyInt type)
throws ValueException {
00207
return TypeTinyInt.convertNumber(
val);
00208 }
00209
00210 public Value convert(
TypeSmallInt type)
throws ValueException {
00211
return TypeSmallInt.convertNumber(
val);
00212 }
00213
00214 public Value convert(
TypeInt type)
throws ValueException {
00215
return TypeInt.convertNumber(
val);
00216 }
00217
00218 public Value convert(
TypeBigInt type)
throws ValueException {
00219
return TypeBigInt.convertNumber(
val);
00220 }
00221
00222 public Value convert(
TypeDecimal type)
throws ValueException {
00223
ValueScaledInteger vs =
new ValueScaledInteger(
val);
00224
int prec = type.getPrecision();
00225
if (prec > 0 && vs.
requiredPrecision() > prec) {
00226
throw new ValueException(
"Too large: " +
val +
", precision = " +
00227 vs.
requiredPrecision() +
", type precision = " +
00228 prec);
00229 }
00230
int scale = type.getScale();
00231
if (scale > 0) {
00232 vs.
setScale(scale);
00233 }
00234
return vs;
00235 }
00236
00237 public Value convert(
TypeReal type)
throws ValueException {
00238
if (type.getMaxPrecision() > 31) {
00239
return new ValueDouble(
val);
00240 }
else {
00241
return this;
00242 }
00243 }
00244
00245
00246
00247 public Type getType() {
00248
return TypeReal.typeFloat;
00249 }
00250
00251 public void serializeKey(
KeyStream out)
throws IOException {
00252 out.writeFloat(
val);
00253 }
00254
00255 public String
toString() {
00256
return String.valueOf(
val);
00257 }
00258 }