KickJava   Java API By Example, From Geeks To Geeks.

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


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 import com.quadcap.util.Debug;
49 import com.quadcap.sql.io.ExternalizeProxy;
50
51 /**
52  * A <b>double</b> value.
53  *
54  * @author Stan Bailes
55  */

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