1 package com.quadcap.sql.types; 2 3 40 41 import java.io.Externalizable ; 42 import java.io.IOException ; 43 import java.io.ObjectInput ; 44 import java.io.ObjectOutput ; 45 46 51 public class ValueBoolean extends Value implements Externalizable { 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 in) throws IOException { 147 val = in.read() == 1; 148 } 149 150 public void writeExternal(ObjectOutput out) 151 throws IOException 152 { 153 out.write(val ? 1 : 0); 154 } 155 156 public boolean isTrue() { return val; } 157 158 public Object asJavaObject() { 159 return new Boolean (val); 160 } 161 162 public void fromJavaObject(Object obj) throws ValueException { 163 if (obj instanceof Boolean ) { 164 val = ((Boolean )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 public Type getType() { 217 return TypeBoolean.typeBoolean; 218 } 219 220 public String toString() { 221 return "" + val; 222 } 223 224 public void serializeKey(KeyStream out) throws IOException { 225 out.writeBoolean(val); 226 } 227 } 228 | Popular Tags |