1 15 package org.josql.expressions; 16 17 import org.josql.Query; 18 import org.josql.QueryExecutionException; 19 import org.josql.QueryParseException; 20 21 import org.josql.internal.Utilities; 22 23 33 public class ArithmeticExpression extends ValueExpression 34 { 35 36 public static final int MULTIPLY = 0; 37 public static final int ADDITION = 1; 38 public static final int SUBTRACT = 2; 39 public static final int DIVIDE = 3; 40 public static final int MODULUS = 4; 41 42 private int type = -1; 43 44 private ValueExpression left = null; 45 private ValueExpression right = null; 46 47 private boolean fixedResult = false; 48 49 57 public Class getExpectedReturnType (Query q) 58 throws QueryParseException 59 { 60 61 return this.left.getExpectedReturnType (q); 62 63 } 64 65 92 public boolean isTrue (Object o, 93 Query q) 94 throws QueryExecutionException 95 { 96 97 o = this.evaluate (o, 98 q); 99 100 if (o == null) 101 { 102 103 return false; 104 105 } 106 107 if (o instanceof Number ) 108 { 109 110 return ((Number ) o).doubleValue () > 0; 111 112 } 113 114 return true; 115 116 } 117 118 126 public boolean hasFixedResult (Query q) 127 { 128 129 return this.fixedResult; 130 131 } 132 133 public void init (Query q) 134 throws QueryParseException 135 { 136 137 this.left.init (q); 138 this.right.init (q); 139 140 this.fixedResult = this.left.hasFixedResult (q) && this.right.hasFixedResult (q); 141 142 } 143 144 149 public ValueExpression getRight () 150 { 151 152 return this.right; 153 154 } 155 156 161 public ValueExpression getLeft () 162 { 163 164 return this.left; 165 166 } 167 168 public void setLeft (ValueExpression exp) 169 { 170 171 this.left = exp; 172 173 } 174 175 public void setRight (ValueExpression exp) 176 { 177 178 this.right = exp; 179 180 } 181 182 public int getType () 183 { 184 185 return this.type; 186 187 } 188 189 public void setType (int t) 190 { 191 192 this.type = t; 193 194 } 195 196 215 public Object evaluate (Object o, 216 Query q) 217 throws QueryExecutionException 218 { 219 220 Object l = this.left.getValue (o, 221 q); 222 223 Object r = this.right.getValue (o, 224 q); 225 226 if ((this.type == ArithmeticExpression.ADDITION) 228 && 229 (!(l instanceof Number ) 230 || 231 !(r instanceof Number ) 232 ) 233 ) 234 { 235 236 StringBuffer b = new StringBuffer (); 237 238 if (l == null) 239 { 240 241 b.append ("null"); 242 243 } else { 244 245 b.append (l); 246 247 } 248 249 if (r == null) 250 { 251 252 b.append ("null"); 253 254 } else { 255 256 b.append (r); 257 258 } 259 260 return b.toString (); 261 262 } 263 264 if (l == null) 265 { 266 267 l = new Double (0); 268 269 } 270 271 if (r == null) 272 { 273 274 r = new Double (0); 275 276 } 277 278 double ld = Utilities.getDouble (l); 279 double rd = Utilities.getDouble (r); 280 281 if (this.type == ArithmeticExpression.ADDITION) 282 { 283 284 return new Double (ld + rd); 285 286 } 287 288 if (this.type == ArithmeticExpression.SUBTRACT) 289 { 290 291 return new Double (ld - rd); 292 293 } 294 295 if (this.type == ArithmeticExpression.MULTIPLY) 296 { 297 298 return new Double (ld * rd); 299 300 } 301 302 if (this.type == ArithmeticExpression.MODULUS) 303 { 304 305 return new Double (ld % rd); 306 307 } 308 309 if (this.type == ArithmeticExpression.DIVIDE) 310 { 311 312 if (rd == 0) 313 { 314 315 return new Double (0); 316 317 } 318 319 return new Double (ld / rd); 320 321 } 322 323 return null; 324 325 } 326 327 public String toString () 328 { 329 330 String pred = "+"; 331 332 if (this.type == ArithmeticExpression.MULTIPLY) 333 { 334 335 pred = "*"; 336 337 } 338 339 if (this.type == ArithmeticExpression.SUBTRACT) 340 { 341 342 pred = "-"; 343 344 } 345 346 if (this.type == ArithmeticExpression.MODULUS) 347 { 348 349 pred = "%"; 350 351 } 352 353 if (this.type == ArithmeticExpression.DIVIDE) 354 { 355 356 pred = "/"; 357 358 } 359 360 String exp = this.left.toString () + " " + pred + " " + this.right.toString (); 361 362 if (this.isBracketed ()) 363 { 364 365 exp = "(" + exp + ")"; 366 367 } 368 369 return exp; 370 371 } 372 373 } 374
| Popular Tags
|