KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.quadcap.util.Util;
47
48 import com.quadcap.sql.io.Extern;
49 import com.quadcap.sql.io.Externable;
50 import com.quadcap.sql.io.ExternalizeProxy;
51
52 /**
53  * An <b>int</b> value.
54  *
55  * @author Stan Bailes
56  */

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