1 23 24 package org.objectweb.medor.expression.lib; 25 26 import org.objectweb.jorm.type.api.PType; 27 import org.objectweb.jorm.type.api.PTypeSpace; 28 import org.objectweb.medor.expression.api.ExpressionException; 29 import org.objectweb.medor.expression.api.Operand; 30 import org.objectweb.medor.expression.api.ParameterOperand; 31 import org.objectweb.medor.expression.api.TypingException; 32 33 import java.math.BigDecimal ; 34 import java.math.BigInteger ; 35 import java.util.Date ; 36 37 41 public class BasicOperand extends BasicExpression implements Operand { 42 43 protected long longValue; 44 protected double doubleValue; 45 protected Object objectValue; 46 47 protected boolean isDefined = true; 48 49 public BasicOperand() { 50 } 51 52 public BasicOperand(BasicOperand bo) { 53 super(bo); 54 longValue = bo.longValue; 55 doubleValue = bo.doubleValue; 56 objectValue = bo.objectValue; 57 isDefined = bo.isDefined; 58 } 59 protected BasicOperand(PType p) { 60 super(p); 61 } 62 63 public BasicOperand(boolean p) { 64 super(PTypeSpace.BOOLEAN); 65 longValue = (p ? 1 : 0); 66 doubleValue = (p ? 1 : 0); 67 } 68 69 public BasicOperand(char p) { 70 super(PTypeSpace.CHAR); 71 longValue = p; 72 doubleValue = p; 73 } 74 75 public BasicOperand(char[] p) { 76 super(PTypeSpace.CHARARRAY); 77 objectValue = p; 78 } 79 80 public BasicOperand(byte p) { 81 super(PTypeSpace.BYTE); 82 longValue = p; 83 doubleValue = p; 84 } 85 86 public BasicOperand(byte[] p) { 87 super(PTypeSpace.BYTEARRAY); 88 objectValue = p; 89 } 90 91 public BasicOperand(short p) { 92 super(PTypeSpace.SHORT); 93 longValue = p; 94 doubleValue = p; 95 } 96 97 public BasicOperand(int p) { 98 super(PTypeSpace.INT); 99 longValue = p; 100 doubleValue = p; 101 } 102 103 public BasicOperand(long p) { 104 super(PTypeSpace.LONG); 105 longValue = p; 106 doubleValue = p; 107 } 108 109 public BasicOperand(float p) { 110 super(PTypeSpace.FLOAT); 111 doubleValue = p; 112 } 113 114 public BasicOperand(double p) { 115 super(PTypeSpace.DOUBLE); 116 doubleValue = p; 117 } 118 119 public BasicOperand(String p) { 120 super(PTypeSpace.STRING); 121 objectValue = p; 122 } 123 124 public BasicOperand(Date p) { 125 super(PTypeSpace.DATE); 126 objectValue = p; 127 } 128 129 public BasicOperand(Object p, PType type) { 130 super(type); 131 objectValue = p; 132 switch (type.getTypeCode()) { 133 case PType.TYPECODE_BOOLEAN: 134 longValue = ((Boolean ) p).booleanValue() ? 1 : 0; 135 doubleValue = longValue; 136 break; 137 case PType.TYPECODE_BYTE: 138 longValue = ((Byte ) p).byteValue(); 139 doubleValue = longValue; 140 break; 141 case PType.TYPECODE_SHORT: 142 longValue = ((Short ) p).shortValue(); 143 doubleValue = longValue; 144 break; 145 case PType.TYPECODE_INT: 146 longValue = ((Integer ) p).intValue(); 147 doubleValue = longValue; 148 break; 149 case PType.TYPECODE_LONG: 150 longValue = ((Long ) p).intValue(); 151 doubleValue = longValue; 152 break; 153 case PType.TYPECODE_CHAR: 154 longValue = ((Character ) p).charValue(); 155 doubleValue = longValue; 156 break; 157 case PType.TYPECODE_FLOAT: 158 doubleValue = ((Float ) p).floatValue(); 159 break; 160 case PType.TYPECODE_DOUBLE: 161 doubleValue = ((Double ) p).doubleValue(); 162 break; 163 } 164 } 165 166 public Object clone(Object clone, java.util.Map obj2clone) throws CloneNotSupportedException { 167 clone = super.clone(clone, obj2clone); 168 BasicOperand bo = (BasicOperand) clone; 169 bo.longValue = longValue; 170 bo.doubleValue = doubleValue; 171 bo.objectValue = objectValue; 172 bo.type = type; 173 bo.isDefined = isDefined; 174 return clone; 175 } 176 177 180 public String getValueAsString() { 181 switch (type.getTypeCode()) { 182 case PType.TYPECODE_BOOLEAN: 183 return "" + (longValue == 1); 184 case PType.TYPECODE_BYTE: 185 case PType.TYPECODE_SHORT: 186 case PType.TYPECODE_INT: 187 case PType.TYPECODE_LONG: 188 return "" + longValue; 189 case PType.TYPECODE_CHAR: 190 return new Character ((char) longValue).toString(); 191 case PType.TYPECODE_FLOAT: 192 case PType.TYPECODE_DOUBLE: 193 return "" + doubleValue; 194 default: 195 return (objectValue==null ? null : objectValue.toString()); 196 } 197 } 198 199 public String toString() { 200 return getValueAsString(); 201 } 202 203 206 public org.objectweb.medor.expression.api.Operand 207 evaluate(ParameterOperand[] pos, Object o) throws ExpressionException { 208 return this; 209 } 210 211 public PType getType() { 212 return type; 213 } 214 215 218 public boolean isDefined() { 219 return isDefined; 220 } 221 222 public void setIsDefined(boolean isdefined) { 223 isDefined = isdefined; 224 } 225 226 public boolean getBoolean() throws TypingException { 227 if (type.getTypeCode() == PType.TYPECODE_BOOLEAN) { 228 return longValue == 1; 229 } if (type.getTypeCode() == PType.TYPECODE_OBJBOOLEAN 230 && objectValue instanceof Boolean ) { 231 return ((Boolean ) objectValue).booleanValue(); 232 } else 233 throw new TypingException("Can not get a boolean value"); 234 } 235 236 public int getInt() throws TypingException { 237 if (type.getTypeCode() == PType.TYPECODE_INT) { 238 return (int) longValue; 239 } else if (type.getTypeCode() == PType.TYPECODE_OBJINT 240 && objectValue instanceof Integer ) { 241 return ((Integer ) objectValue).intValue(); 242 } else 243 throw new TypingException("Can not get an integer value " + type.getTypeCode()); 244 } 245 246 public byte getByte() throws TypingException { 247 if (type.getTypeCode() == PType.TYPECODE_BYTE) { 248 return (byte) longValue; 249 } else if (type.getTypeCode() == PType.TYPECODE_OBJBYTE 250 && objectValue instanceof Byte ) { 251 return ((Byte ) objectValue).byteValue(); 252 } else 253 throw new TypingException("Can not get a byte value"); 254 } 255 256 public byte[] getByteArray() throws TypingException { 257 if (type.getTypeCode() == PType.TYPECODE_BYTEARRAY) { 258 return (byte[]) objectValue; 259 } else 260 throw new TypingException("Can not get a byteArray value"); 261 } 262 263 public short getShort() throws TypingException { 264 if (type.getTypeCode() == PType.TYPECODE_SHORT) { 265 return (short) longValue; 266 } else if (type.getTypeCode() == PType.TYPECODE_OBJSHORT 267 && (objectValue == null || objectValue instanceof Short )) { 268 return ((Short ) objectValue).shortValue(); 269 } else 270 throw new TypingException("Can not get a short value"); 271 } 272 273 public long getLong() throws TypingException { 274 if (type.getTypeCode() == PType.TYPECODE_LONG) { 275 return longValue; 276 } else if (type.getTypeCode() == PType.TYPECODE_OBJLONG 277 && (objectValue == null || objectValue instanceof Long )) { 278 return ((Long ) objectValue).longValue(); 279 } else 280 throw new TypingException("Can not get a long value"); 281 } 282 283 public float getFloat() throws TypingException { 284 if (type.getTypeCode() == PType.TYPECODE_FLOAT) { 285 return (float) doubleValue; 286 } else if (type.getTypeCode() == PType.TYPECODE_OBJFLOAT 287 && (objectValue == null || objectValue instanceof Float )) { 288 return ((Float ) objectValue).floatValue(); 289 } else 290 throw new TypingException("Can not get a float value"); 291 } 292 293 public double getDouble() throws TypingException { 294 if (type.getTypeCode() == PType.TYPECODE_DOUBLE) { 295 return doubleValue; 296 } else if (type.getTypeCode() == PType.TYPECODE_OBJDOUBLE 297 && (objectValue == null || objectValue instanceof Double )) { 298 return ((Double ) objectValue).doubleValue(); 299 } else 300 throw new TypingException("Can not get a double value " + type.getJavaName()); 301 } 302 303 public char getChar() throws TypingException { 304 if (type.getTypeCode() == PType.TYPECODE_CHAR) { 305 return (char) longValue; 306 } else if (type.getTypeCode() == PType.TYPECODE_OBJCHAR 307 && (objectValue == null || objectValue instanceof Character )) { 308 return ((Character ) objectValue).charValue(); 309 } else 310 throw new TypingException("Can not get a char value"); 311 } 312 313 public char[] getCharArray() throws TypingException { 314 if (type.getTypeCode() == PType.TYPECODE_CHARARRAY) { 315 return (char[]) objectValue; 316 } 317 else 318 throw new TypingException("Can not get a char value"); 319 } 320 321 public String getString() throws TypingException { 322 if (type.getTypeCode() == PType.TYPECODE_STRING 323 && (objectValue == null || objectValue instanceof String )) { 324 return (String ) objectValue; 325 } else 326 throw new TypingException("Can not get a string value: " + objectValue); 327 } 328 329 public BigDecimal getBigDecimal() throws TypingException { 330 if (type.getTypeCode() == PType.TYPECODE_BIGDECIMAL 331 && (objectValue == null || objectValue instanceof BigDecimal )) { 332 return (BigDecimal ) objectValue; 333 } else 334 throw new TypingException("Can not get a BigDecimal value: " + objectValue); 335 } 336 337 public BigInteger getBigInteger() throws TypingException { 338 if (type.getTypeCode() == PType.TYPECODE_BIGINTEGER 339 && (objectValue == null || objectValue instanceof BigInteger )) { 340 return (BigInteger ) objectValue; 341 } else 342 throw new TypingException("Can not get a BigInteger value: " + objectValue); 343 } 344 345 public Date getDate() throws TypingException { 346 if (objectValue == null || objectValue instanceof Date ) { 348 return (Date ) objectValue; 349 } else 350 throw new TypingException("Can not get a Date value: " 351 + (objectValue == null ? "null" : objectValue.getClass().getName())); 352 } 353 354 public Object getObject() { 355 if (type == null) 356 return objectValue; 357 switch (type.getTypeCode()) { 358 case PType.TYPECODE_BOOLEAN: 359 return new Boolean (longValue == 1); 360 case PType.TYPECODE_BYTE: 361 return new Byte ((byte) longValue); 362 case PType.TYPECODE_CHAR: 363 return new Character ((char) longValue); 364 case PType.TYPECODE_DOUBLE: 365 return new Double ((double) longValue); 366 case PType.TYPECODE_FLOAT: 367 return new Float ((float) longValue); 368 case PType.TYPECODE_INT: 369 return new Integer ((int) longValue); 370 case PType.TYPECODE_LONG: 371 return new Long (longValue); 372 case PType.TYPECODE_SHORT: 373 return new Short ((short) longValue); 374 default: 375 return objectValue; 376 } 377 } 378 379 public Operand compileExpression() { 380 return this; 381 } 382 383 } 384 | Popular Tags |