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>long</b> value.
048: *
049: * @author Stan Bailes
050: */
051: public class ValueLong extends Value implements Externalizable {
052: long val;
053:
054: public ValueLong() {
055: }
056:
057: public ValueLong(long val) {
058: this .val = val;
059: }
060:
061: public final long longValue() {
062: return val;
063: }
064:
065: public Value unop(int op) throws ValueException {
066: switch (op) {
067: case Op.NULL:
068: return ValueBoolean.falseBoolean;
069: case Op.PLUS:
070: return this ;
071: case Op.MINUS:
072: return new ValueLong(0 - val);
073: default:
074: throw new ValueException("Unary op: " + Op.toString(op)
075: + " not implemented for this type");
076: }
077: }
078:
079: public Value binop(int op, Value l) throws ValueException {
080: return l.binop(op, this );
081: }
082:
083: public Value binop(int op, ValueNull r) throws ValueException {
084: switch (op) {
085: case Op.EQ:
086: case Op.NE:
087: case Op.LT:
088: case Op.LE:
089: case Op.GT:
090: case Op.GE:
091: return ValueUnknown.valueUnknown;
092: case Op.PLUS:
093: case Op.MINUS:
094: case Op.TIMES:
095: case Op.DIVIDE:
096: case Op.EXP:
097: return r;
098: case Op.COMPARE:
099: return r.valCmpNull();
100: default:
101: throw badBinop(op, r);
102: }
103: }
104:
105: public Value binop(int op, ValueDouble r) throws ValueException {
106: return ValueDouble.binop(op, new ValueDouble(val), r);
107: }
108:
109: public Value binop(int op, ValueFloat r) throws ValueException {
110: return ValueFloat.binop(op, new ValueFloat(val), r);
111: }
112:
113: public Value binop(int op, ValueInteger r) throws ValueException {
114: return ValueLong.binop(op, this , new ValueLong(r.val));
115: }
116:
117: public Value binop(int op, ValueShort r) throws ValueException {
118: return ValueLong.binop(op, this , new ValueLong(r.val));
119: }
120:
121: public Value binop(int op, ValueByte r) throws ValueException {
122: return ValueLong.binop(op, this , new ValueLong(r.val));
123: }
124:
125: public Value binop(int op, ValueLong r) throws ValueException {
126: return ValueLong.binop(op, this , r);
127: }
128:
129: public Value binop(int op, ValueScaledInteger r)
130: throws ValueException {
131: return ValueScaledInteger.binop(op,
132: new ValueScaledInteger(val), r);
133: }
134:
135: public static final Value binop(int op, ValueLong e, ValueLong f)
136: throws ValueException {
137: switch (op) {
138: case Op.DIVIDE:
139: try {
140: return new ValueLong(e.val / f.val);
141: } catch (ArithmeticException ae) {
142: throw new ValueException("divide by zero");
143: }
144: case Op.EQ:
145: return new ValueBoolean(e.val == f.val);
146: case Op.EXP:
147: return new ValueLong((int) (Math.pow(e.val, f.val)));
148: case Op.GE:
149: return new ValueBoolean(e.val >= f.val);
150: case Op.GT:
151: return new ValueBoolean(e.val > f.val);
152: case Op.LE:
153: return new ValueBoolean(e.val <= f.val);
154: case Op.LT:
155: return new ValueBoolean(e.val < f.val);
156: case Op.MINUS:
157: return new ValueLong(e.val - f.val);
158: case Op.NE:
159: return new ValueBoolean(e.val != f.val);
160: case Op.PLUS:
161: return new ValueLong(e.val + f.val);
162: case Op.TIMES:
163: return new ValueLong(e.val * f.val);
164: case Op.COMPARE:
165: if (e.val < f.val)
166: return ValueInteger.MINUS_ONE;
167: if (e.val > f.val)
168: 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 in) throws IOException {
177: val = in.readLong();
178: }
179:
180: public void writeExternal(ObjectOutput out) throws IOException {
181: out.writeLong(val);
182: }
183:
184: public Object asJavaObject() {
185: return new Long(val);
186: }
187:
188: public void fromJavaObject(Object obj) throws ValueException {
189: if (obj instanceof Long) {
190: val = ((Long) obj).longValue();
191: } else {
192: throw new ValueException("bad type: " + obj);
193: }
194: }
195:
196: public String toString() {
197: return Long.toString(val);
198: }
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 TypeSmallInt.convertNumber(val);
210: }
211:
212: public Value convert(TypeInt type) throws ValueException {
213: return TypeInt.convertNumber(val);
214: }
215:
216: public Value convert(TypeDecimal type) throws ValueException {
217: return new ValueScaledInteger(val);
218: }
219:
220: public Value convert(TypeBigInt type) throws ValueException {
221: return this ;
222: }
223:
224: public Value convert(TypeReal type) throws ValueException {
225: if (type.getMaxPrecision() > 31) {
226: return new ValueDouble((double) val);
227: } else {
228: return new ValueFloat((float) val);
229: }
230: }
231:
232: public Type getType() {
233: return TypeBigInt.typeBigInt;
234: }
235:
236: public void serializeKey(KeyStream out) throws IOException {
237: out.writeLong(val);
238: }
239: }
|