001: package com.quadcap.sql.types;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.Externalizable;
042: import java.io.IOException;
043: import java.io.ObjectInput;
044: import java.io.ObjectOutput;
045:
046: /**
047: * A <b>short</b> value.
048: *
049: * @author Stan Bailes
050: */
051: public class ValueShort extends Value implements Externalizable {
052: short val;
053:
054: public ValueShort() {
055: }
056:
057: public ValueShort(short val) {
058: this .val = val;
059: }
060:
061: public ValueShort(int v) throws ValueException {
062: if (v < Short.MIN_VALUE || v > Short.MAX_VALUE) {
063: throw new ValueException("Can't convert to SMALLINT: " + v);
064: }
065: this .val = (short) v;
066: }
067:
068: public final int intValue() {
069: return val;
070: }
071:
072: public final short shortValue() {
073: return val;
074: }
075:
076: public Value unop(int op) throws ValueException {
077: switch (op) {
078: case Op.NULL:
079: return ValueBoolean.falseBoolean;
080: case Op.PLUS:
081: return this ;
082: case Op.MINUS:
083: return new ValueShort(0 - val);
084: default:
085: throw new ValueException("Unary op: " + Op.toString(op)
086: + " not implemented for this type");
087: }
088: }
089:
090: public Value binop(int op, Value l) throws ValueException {
091: return l.binop(op, this );
092: }
093:
094: public Value binop(int op, ValueNull r) throws ValueException {
095: switch (op) {
096: case Op.EQ:
097: case Op.NE:
098: case Op.LT:
099: 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, ValueDouble r) throws ValueException {
117: return ValueDouble.binop(op, new ValueDouble(val), r);
118: }
119:
120: public Value binop(int op, ValueFloat r) throws ValueException {
121: return ValueFloat.binop(op, new ValueFloat(val), r);
122: }
123:
124: public Value binop(int op, ValueInteger r) throws ValueException {
125: return ValueInteger.binop(op, new ValueInteger(val), r);
126: }
127:
128: public Value binop(int op, ValueLong r) throws ValueException {
129: return ValueLong.binop(op, new ValueLong(val), r);
130: }
131:
132: public Value binop(int op, ValueByte r) throws ValueException {
133: return ValueShort.binop(op, this , new ValueShort(r.val));
134: }
135:
136: public Value binop(int op, ValueScaledInteger r)
137: throws ValueException {
138: return ValueScaledInteger.binop(op,
139: new ValueScaledInteger(val), r);
140: }
141:
142: public Value binop(int op, ValueShort r) throws ValueException {
143: return ValueShort.binop(op, this , r);
144: }
145:
146: public static final Value binop(int op, ValueShort e, ValueShort f)
147: throws ValueException {
148: switch (op) {
149: case Op.DIVIDE:
150: try {
151: return new ValueShort(e.val / f.val);
152: } catch (ArithmeticException ae) {
153: throw new ValueException("divide by zero");
154: }
155: case Op.EQ:
156: return new ValueBoolean(e.val == f.val);
157: case Op.EXP:
158: return new ValueShort((int) (Math.pow(e.val, f.val)));
159: case Op.GE:
160: return new ValueBoolean(e.val >= f.val);
161: case Op.GT:
162: return new ValueBoolean(e.val > f.val);
163: case Op.LE:
164: return new ValueBoolean(e.val <= f.val);
165: case Op.LT:
166: return new ValueBoolean(e.val < f.val);
167: case Op.MINUS:
168: return new ValueShort(e.val - f.val);
169: case Op.NE:
170: return new ValueBoolean(e.val != f.val);
171: case Op.PLUS:
172: return new ValueShort(e.val + f.val);
173: case Op.TIMES:
174: return new ValueShort(e.val * f.val);
175: case Op.COMPARE:
176: if (e.val < f.val)
177: return ValueInteger.MINUS_ONE;
178: if (e.val > f.val)
179: return ValueInteger.PLUS_ONE;
180: return ValueInteger.ZERO;
181:
182: default:
183: throw badBinop(op, e, f);
184: }
185: }
186:
187: public void readExternal(ObjectInput in) throws IOException {
188: val = in.readShort();
189: }
190:
191: public void writeExternal(ObjectOutput out) throws IOException {
192: out.writeShort(val);
193: }
194:
195: public Object asJavaObject() {
196: return new Short(val);
197: }
198:
199: public void fromJavaObject(Object obj) throws ValueException {
200: if (obj instanceof Short) {
201: val = ((Short) obj).shortValue();
202: } else {
203: throw new ValueException("bad type: " + obj);
204: }
205: }
206:
207: public String toString() {
208: return Short.toString(val);
209: }
210:
211: public Value convert(TypeBoolean type) throws ValueException {
212: return TypeBoolean.convertNumber(val);
213: }
214:
215: public Value convert(TypeTinyInt type) throws ValueException {
216: return TypeTinyInt.convertNumber(val);
217: }
218:
219: public Value convert(TypeSmallInt type) throws ValueException {
220: return this ;
221: }
222:
223: public Value convert(TypeInt type) throws ValueException {
224: return new ValueInteger(val);
225: }
226:
227: public Value convert(TypeBigInt type) throws ValueException {
228: return new ValueLong(val);
229: }
230:
231: public Value convert(TypeDecimal type) throws ValueException {
232: return new ValueScaledInteger((long) val);
233: }
234:
235: public Value convert(TypeReal type) throws ValueException {
236: if (type.getMaxPrecision() >= 32) {
237: return new ValueDouble((double) val);
238: } else {
239: return new ValueFloat((float) val);
240: }
241: }
242:
243: public Type getType() {
244: return TypeSmallInt.typeSmallInt;
245: }
246:
247: public void serializeKey(KeyStream out) throws IOException {
248: out.writeShort(val);
249: }
250: }
|