KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > types > ValueFloat


1 package com.quadcap.sql.types;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.Externalizable JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.io.ObjectInput JavaDoc;
44 import java.io.ObjectOutput JavaDoc;
45
46 import java.sql.Types JavaDoc;
47
48 /**
49  * A <b>float</b> value.
50  *
51  * @author Stan Bailes
52  */

53 public class ValueFloat extends Value implements Externalizable JavaDoc {
54     float val;
55
56     public ValueFloat() {}
57     
58     public ValueFloat(float val) { this.val = val; }
59
60     public ValueFloat(String JavaDoc s) throws ValueException {
61         try {
62 //- //#ifdef JDK11
63
//- this.val = Float.valueOf(s).floatValue();
64
//#else
65
this.val = Float.parseFloat(s);
66             //#endif
67
} catch (NumberFormatException JavaDoc ne) {
68             throw new ValueException("FLOAT format error");
69         }
70     }
71
72     public final float floatValue() { return val; }
73
74     public Value unop(int op) throws ValueException {
75     switch (op) {
76     case Op.NULL:
77         return ValueBoolean.falseBoolean;
78     case Op.PLUS:
79         return this;
80     case Op.MINUS:
81         return new ValueFloat(0.0f - val);
82     default:
83         throw new ValueException("Unary op: " + Op.toString(op) +
84                      " not implemented for this type");
85     }
86     }
87     
88     public Value binop(int op, Value l) throws ValueException {
89     return l.binop(op, this);
90     }
91
92     public Value binop(int op, ValueFloat r) throws ValueException {
93     return ValueFloat.binop(op, this, r);
94     }
95
96     public Value binop(int op, ValueDouble r) throws ValueException {
97     return ValueDouble.binop(op, new ValueDouble(val), r);
98     }
99
100     public Value binop(int op, ValueInteger r) throws ValueException {
101     return Value.binop(op, this, new ValueFloat(r.val));
102     }
103
104     public Value binop(int op, ValueShort r) throws ValueException {
105     return Value.binop(op, this, new ValueFloat(r.val));
106     }
107
108     public Value binop(int op, ValueByte r) throws ValueException {
109     return Value.binop(op, this, new ValueFloat(r.val));
110     }
111
112     public Value binop(int op, ValueLong r) throws ValueException {
113     return Value.binop(op, this, new ValueFloat(r.val));
114     }
115
116     public Value binop(int op, ValueScaledInteger r) throws ValueException {
117     return ValueDouble.binop(op, new ValueDouble(val),
118                                  new ValueDouble(r.doubleValue()));
119     }
120
121     public Value binop(int op, ValueNull r) throws ValueException {
122     switch (op) {
123     case Op.EQ:
124     case Op.NE:
125     case Op.LT:
126     case Op.LE:
127     case Op.GT:
128     case Op.GE:
129         return ValueUnknown.valueUnknown;
130     case Op.PLUS:
131     case Op.MINUS:
132     case Op.TIMES:
133     case Op.DIVIDE:
134     case Op.EXP:
135         return r;
136     case Op.COMPARE:
137         return r.valCmpNull();
138     default:
139         throw badBinop(op, r);
140     }
141     }
142
143     
144     public static final Value binop(int op, ValueFloat e, ValueFloat f)
145     throws ValueException
146     {
147     switch (op) {
148         case Op.DIVIDE:
149         try {
150         return new ValueFloat(e.val / f.val);
151         } catch (ArithmeticException JavaDoc ae) {
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 ValueFloat((float)(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 ValueFloat(e.val - f.val);
168         case Op.NE:
169         return new ValueBoolean(e.val != f.val);
170         case Op.PLUS:
171         return new ValueFloat(e.val + f.val);
172         case Op.TIMES:
173         return new ValueFloat(e.val * f.val);
174     case Op.COMPARE:
175         if (e.val < f.val) return ValueInteger.MINUS_ONE;
176         if (e.val > f.val) return ValueInteger.PLUS_ONE;
177         return ValueInteger.ZERO;
178         
179     default:
180         throw badBinop(op, e, f);
181     }
182     }
183
184     public Object JavaDoc asJavaObject() {
185     return new Float JavaDoc(val);
186     }
187
188     public void fromJavaObject(Object JavaDoc obj) throws ValueException {
189     if (obj instanceof Float JavaDoc) {
190         val = ((Float JavaDoc)obj).floatValue();
191     } else {
192         throw new ValueException("bad type: " + obj);
193     }
194     }
195
196     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc {
197     val = in.readFloat();
198     }
199     
200     public void writeExternal(ObjectOutput JavaDoc out)
201     throws IOException JavaDoc
202     {
203     out.writeFloat(val);
204     }
205
206     public Value convert(TypeTinyInt type) throws ValueException {
207         return TypeTinyInt.convertNumber(val);
208     }
209
210     public Value convert(TypeSmallInt type) throws ValueException {
211         return TypeSmallInt.convertNumber(val);
212     }
213
214     public Value convert(TypeInt type) throws ValueException {
215         return TypeInt.convertNumber(val);
216     }
217
218     public Value convert(TypeBigInt type) throws ValueException {
219         return TypeBigInt.convertNumber(val);
220     }
221
222     public Value convert(TypeDecimal type) throws ValueException {
223     ValueScaledInteger vs = new ValueScaledInteger(val);
224         int prec = type.getPrecision();
225     if (prec > 0 && vs.requiredPrecision() > prec) {
226         throw new ValueException("Too large: " + val + ", precision = " +
227         vs.requiredPrecision() + ", type precision = " +
228         prec);
229     }
230         int scale = type.getScale();
231         if (scale > 0) {
232             vs.setScale(scale);
233         }
234     return vs;
235     }
236
237     public Value convert(TypeReal type) throws ValueException {
238         if (type.getMaxPrecision() > 31) {
239             return new ValueDouble(val);
240         } else {
241             return this;
242         }
243     }
244
245     
246
247     public Type getType() {
248     return TypeReal.typeFloat;
249     }
250
251     public void serializeKey(KeyStream out) throws IOException JavaDoc {
252     out.writeFloat(val);
253     }
254
255     public String JavaDoc toString() {
256     return String.valueOf(val);
257     }
258 }
259
Popular Tags