KickJava   Java API By Example, From Geeks To Geeks.

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


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

51 public class ValueBoolean extends Value implements Externalizable JavaDoc {
52     boolean val;
53
54     public static final ValueBoolean trueBoolean
55     = new ValueBoolean(true);
56
57     public static final ValueBoolean falseBoolean
58     = new ValueBoolean(false);
59
60     public ValueBoolean() {}
61     
62     public ValueBoolean(boolean val) { this.val = val; }
63
64     public Value binop(int op, Value l) throws ValueException {
65     return l.binop(op, this);
66     }
67
68     public Value binop(int op, ValueBoolean r) throws ValueException {
69     return ValueBoolean.binop(op, this, r);
70     }
71
72     public Value binop(int op, ValueNull r) throws ValueException {
73     switch (op) {
74     case Op.AND:
75         if (val) return r;
76         else return this;
77     case Op.OR:
78         if (!val) return r;
79         else return this;
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.COMPARE:
88         return r.valCmpNull();
89     default:
90         throw badBinop(op, r);
91     }
92     }
93
94     public Value binop(int op, ValueUnknown r) throws ValueException {
95     switch (op) {
96     case Op.AND:
97         if (val) return r;
98         else return this;
99     case Op.OR:
100         if (!val) return r;
101         else return this;
102     case Op.EQ:
103     case Op.NE:
104     case Op.LT:
105     case Op.LE:
106     case Op.GT:
107     case Op.GE:
108         return r;
109     default:
110         throw badBinop(op, r);
111     }
112     }
113
114     public static ValueBoolean binop(int op, ValueBoolean e, ValueBoolean f)
115     throws ValueException
116     {
117     switch (op) {
118         case Op.EQ:
119         return new ValueBoolean(e.val == f.val);
120         case Op.AND:
121         return new ValueBoolean(e.val & f.val);
122         case Op.OR:
123         return new ValueBoolean(e.val | f.val);
124         case Op.NE:
125         return new ValueBoolean(e.val != f.val);
126         case Op.LT:
127         return new ValueBoolean(!e.val & f.val);
128         case Op.GT:
129         return new ValueBoolean(e.val & !f.val);
130     default:
131         throw badBinop(op, e, f);
132     }
133     }
134
135     public Value unop(int op) throws ValueException {
136     switch (op) {
137     case Op.NOT:
138         return new ValueBoolean(!val);
139     case Op.NULL:
140         return falseBoolean;
141     default:
142         throw new ValueException("op: " + op + " not valid for this type");
143     }
144     }
145
146     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc {
147     val = in.read() == 1;
148     }
149     
150     public void writeExternal(ObjectOutput JavaDoc out)
151     throws IOException JavaDoc
152     {
153     out.write(val ? 1 : 0);
154     }
155
156     public boolean isTrue() { return val; }
157
158     public Object JavaDoc asJavaObject() {
159     return new Boolean JavaDoc(val);
160     }
161
162     public void fromJavaObject(Object JavaDoc obj) throws ValueException {
163     if (obj instanceof Boolean JavaDoc) {
164         val = ((Boolean JavaDoc)obj).booleanValue();
165     } else {
166         throw new ValueException("bad type: " + obj);
167     }
168     }
169
170     public Value convert(TypeBinary type) throws ValueException {
171     byte[] b = new byte[(type.getMax() + 7) / 8];
172     if (val) b[0] = 1;
173     return new ValueOctets(b, type.getMax());
174     }
175
176     public Value convert(TypeVarBinary type) throws ValueException {
177     byte[] b = new byte[1];
178     if (val) b[0] = 1;
179     return new ValueOctets(b, 1);
180     }
181
182     public Value convert(TypeBoolean type) throws ValueException {
183     return this;
184     }
185
186     public Value convert(TypeTinyInt type) throws ValueException {
187         return new ValueByte(val ? 0 : 1);
188     }
189
190     public Value convert(TypeSmallInt type) throws ValueException {
191         return new ValueShort(val ? 0 : 1);
192     }
193
194     public Value convert(TypeInt type) throws ValueException {
195         return new ValueInteger(val ? 0 : 1);
196     }
197
198     public Value convert(TypeBigInt type) throws ValueException {
199         return new ValueLong(val ? 0 : 1);
200     }
201
202     public Value convert(TypeReal type) throws ValueException {
203         if (type.getMaxPrecision() > 31) {
204             return new ValueDouble(val ? 0 : 1);
205         } else {
206             return new ValueFloat(val ? 0 : 1);
207         }
208     }
209
210     public Value convert(TypeDecimal type) throws ValueException {
211         return new ValueScaledInteger(val ? 0 : 1);
212     }
213
214     // XXX Check other getType calls to make sure they use static values
215
// XXX instead of constructing types on the fly.
216
public Type getType() {
217     return TypeBoolean.typeBoolean;
218     }
219
220     public String JavaDoc toString() {
221     return "" + val;
222     }
223
224     public void serializeKey(KeyStream out) throws IOException JavaDoc {
225     out.writeBoolean(val);
226     }
227 }
228
Popular Tags