KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
47  * A <b>long</b> value.
48  *
49  * @author Stan Bailes
50  */

51 public class ValueLong extends Value implements Externalizable JavaDoc {
52     long val;
53
54     public ValueLong() {}
55     
56     public ValueLong(long val) { this.val = val; }
57
58     public final long longValue() { return val; }
59     
60     public Value unop(int op) throws ValueException {
61     switch (op) {
62     case Op.NULL:
63         return ValueBoolean.falseBoolean;
64     case Op.PLUS:
65         return this;
66     case Op.MINUS:
67         return new ValueLong(0 - val);
68     default:
69         throw new ValueException("Unary op: " + Op.toString(op) +
70                      " not implemented for this type");
71     }
72     }
73     
74     public Value binop(int op, Value l) throws ValueException {
75     return l.binop(op, this);
76     }
77
78     public Value binop(int op, ValueNull r) throws ValueException {
79     switch (op) {
80     case Op.EQ:
81     case Op.NE:
82     case Op.LT:
83     case Op.LE:
84     case Op.GT:
85     case Op.GE:
86         return ValueUnknown.valueUnknown;
87     case Op.PLUS:
88     case Op.MINUS:
89     case Op.TIMES:
90     case Op.DIVIDE:
91     case Op.EXP:
92         return r;
93     case Op.COMPARE:
94         return r.valCmpNull();
95     default:
96         throw badBinop(op, r);
97     }
98     }
99
100     public Value binop(int op, ValueDouble r) throws ValueException {
101     return ValueDouble.binop(op, new ValueDouble(val), r);
102     }
103
104     public Value binop(int op, ValueFloat r) throws ValueException {
105     return ValueFloat.binop(op, new ValueFloat(val), r);
106     }
107
108     public Value binop(int op, ValueInteger r) throws ValueException {
109     return ValueLong.binop(op, this, new ValueLong(r.val));
110     }
111
112     public Value binop(int op, ValueShort r) throws ValueException {
113     return ValueLong.binop(op, this, new ValueLong(r.val));
114     }
115
116     public Value binop(int op, ValueByte r) throws ValueException {
117     return ValueLong.binop(op, this, new ValueLong(r.val));
118     }
119
120     public Value binop(int op, ValueLong r) throws ValueException {
121     return ValueLong.binop(op, this, r);
122     }
123
124     public Value binop(int op, ValueScaledInteger r) throws ValueException {
125     return ValueScaledInteger.binop(op, new ValueScaledInteger(val), r);
126     }
127
128     public static final Value binop(int op, ValueLong e, ValueLong f)
129     throws ValueException
130     {
131     switch (op) {
132         case Op.DIVIDE:
133         try {
134         return new ValueLong(e.val / f.val);
135         } catch (ArithmeticException JavaDoc ae) {
136         throw new ValueException("divide by zero");
137         }
138         case Op.EQ:
139         return new ValueBoolean(e.val == f.val);
140         case Op.EXP:
141         return new ValueLong((int)(Math.pow(e.val, f.val)));
142         case Op.GE:
143         return new ValueBoolean(e.val >= f.val);
144         case Op.GT:
145         return new ValueBoolean(e.val > f.val);
146         case Op.LE:
147         return new ValueBoolean(e.val <= f.val);
148         case Op.LT:
149         return new ValueBoolean(e.val < f.val);
150         case Op.MINUS:
151         return new ValueLong(e.val - f.val);
152         case Op.NE:
153         return new ValueBoolean(e.val != f.val);
154         case Op.PLUS:
155         return new ValueLong(e.val + f.val);
156         case Op.TIMES:
157         return new ValueLong(e.val * f.val);
158     case Op.COMPARE:
159         if (e.val < f.val) return ValueInteger.MINUS_ONE;
160         if (e.val > f.val) return ValueInteger.PLUS_ONE;
161         return ValueInteger.ZERO;
162         
163     default:
164         throw badBinop(op, e, f);
165     }
166     }
167
168     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc {
169     val = in.readLong();
170     }
171     
172     public void writeExternal(ObjectOutput JavaDoc out)
173     throws IOException JavaDoc
174     {
175     out.writeLong(val);
176     }
177
178     public Object JavaDoc asJavaObject() {
179     return new Long JavaDoc(val);
180     }
181
182     public void fromJavaObject(Object JavaDoc obj) throws ValueException {
183     if (obj instanceof Long JavaDoc) {
184         val = ((Long JavaDoc)obj).longValue();
185     } else {
186         throw new ValueException("bad type: " + obj);
187     }
188     }
189
190     public String JavaDoc toString() { return Long.toString(val); }
191
192     public Value convert(TypeBoolean type) throws ValueException {
193         return TypeBoolean.convertNumber(val);
194     }
195
196     public Value convert(TypeTinyInt type) throws ValueException {
197         return TypeTinyInt.convertNumber(val);
198     }
199
200     public Value convert(TypeSmallInt type) throws ValueException {
201     return TypeSmallInt.convertNumber(val);
202     }
203
204     public Value convert(TypeInt type) throws ValueException {
205     return TypeInt.convertNumber(val);
206     }
207
208     public Value convert(TypeDecimal type) throws ValueException {
209     return new ValueScaledInteger(val);
210     }
211
212     public Value convert(TypeBigInt type) throws ValueException {
213     return this;
214     }
215
216     public Value convert(TypeReal type) throws ValueException {
217         if (type.getMaxPrecision() > 31) {
218             return new ValueDouble((double)val);
219         } else {
220             return new ValueFloat((float)val);
221         }
222     }
223
224     public Type getType() {
225     return TypeBigInt.typeBigInt;
226     }
227
228     public void serializeKey(KeyStream out) throws IOException JavaDoc {
229     out.writeLong(val);
230     }
231 }
232
Popular Tags