KickJava   Java API By Example, From Geeks To Geeks.

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


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

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