1 19 20 package org.apache.cayenne.exp; 21 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Map ; 27 28 import org.apache.cayenne.exp.parser.ASTAdd; 29 import org.apache.cayenne.exp.parser.ASTAnd; 30 import org.apache.cayenne.exp.parser.ASTBetween; 31 import org.apache.cayenne.exp.parser.ASTDbPath; 32 import org.apache.cayenne.exp.parser.ASTDivide; 33 import org.apache.cayenne.exp.parser.ASTEqual; 34 import org.apache.cayenne.exp.parser.ASTFalse; 35 import org.apache.cayenne.exp.parser.ASTGreater; 36 import org.apache.cayenne.exp.parser.ASTGreaterOrEqual; 37 import org.apache.cayenne.exp.parser.ASTIn; 38 import org.apache.cayenne.exp.parser.ASTLess; 39 import org.apache.cayenne.exp.parser.ASTLessOrEqual; 40 import org.apache.cayenne.exp.parser.ASTLike; 41 import org.apache.cayenne.exp.parser.ASTLikeIgnoreCase; 42 import org.apache.cayenne.exp.parser.ASTList; 43 import org.apache.cayenne.exp.parser.ASTMultiply; 44 import org.apache.cayenne.exp.parser.ASTNegate; 45 import org.apache.cayenne.exp.parser.ASTNot; 46 import org.apache.cayenne.exp.parser.ASTNotBetween; 47 import org.apache.cayenne.exp.parser.ASTNotEqual; 48 import org.apache.cayenne.exp.parser.ASTNotIn; 49 import org.apache.cayenne.exp.parser.ASTNotLike; 50 import org.apache.cayenne.exp.parser.ASTNotLikeIgnoreCase; 51 import org.apache.cayenne.exp.parser.ASTObjPath; 52 import org.apache.cayenne.exp.parser.ASTOr; 53 import org.apache.cayenne.exp.parser.ASTSubtract; 54 import org.apache.cayenne.exp.parser.ASTTrue; 55 import org.apache.cayenne.exp.parser.SimpleNode; 56 57 63 public class ExpressionFactory { 64 65 private static Class [] typeLookup; 66 67 static { 68 int[] allTypes = new int[] { 71 Expression.AND, Expression.OR, Expression.NOT, Expression.EQUAL_TO, 72 Expression.NOT_EQUAL_TO, Expression.LESS_THAN, Expression.GREATER_THAN, 73 Expression.LESS_THAN_EQUAL_TO, Expression.GREATER_THAN_EQUAL_TO, 74 Expression.BETWEEN, Expression.IN, Expression.LIKE, 75 Expression.LIKE_IGNORE_CASE, Expression.ADD, Expression.SUBTRACT, 76 Expression.MULTIPLY, Expression.DIVIDE, Expression.NEGATIVE, 77 Expression.OBJ_PATH, Expression.DB_PATH, Expression.LIST, 78 Expression.NOT_BETWEEN, Expression.NOT_IN, Expression.NOT_LIKE, 79 Expression.NOT_LIKE_IGNORE_CASE, Expression.TRUE, Expression.FALSE 80 }; 81 82 int max = 0; 83 int min = 0; 84 int allLen = allTypes.length; 85 for (int i = 0; i < allLen; i++) { 86 if (allTypes[i] > max) 87 max = allTypes[i]; 88 else if (allTypes[i] < min) 89 min = allTypes[i]; 90 } 91 92 if (max > 500) 94 throw new RuntimeException ("Types values are too big: " + max); 95 if (min < 0) 96 throw new RuntimeException ("Types values are too small: " + min); 97 98 101 typeLookup = new Class [max + 1]; 102 103 typeLookup[Expression.AND] = ASTAnd.class; 104 typeLookup[Expression.OR] = ASTOr.class; 105 typeLookup[Expression.BETWEEN] = ASTBetween.class; 106 typeLookup[Expression.NOT_BETWEEN] = ASTNotBetween.class; 107 108 typeLookup[Expression.EQUAL_TO] = ASTEqual.class; 110 typeLookup[Expression.NOT_EQUAL_TO] = ASTNotEqual.class; 111 typeLookup[Expression.LESS_THAN] = ASTLess.class; 112 typeLookup[Expression.GREATER_THAN] = ASTGreater.class; 113 typeLookup[Expression.LESS_THAN_EQUAL_TO] = ASTLessOrEqual.class; 114 typeLookup[Expression.GREATER_THAN_EQUAL_TO] = ASTGreaterOrEqual.class; 115 typeLookup[Expression.IN] = ASTIn.class; 116 typeLookup[Expression.NOT_IN] = ASTNotIn.class; 117 typeLookup[Expression.LIKE] = ASTLike.class; 118 typeLookup[Expression.LIKE_IGNORE_CASE] = ASTLikeIgnoreCase.class; 119 typeLookup[Expression.NOT_LIKE] = ASTNotLike.class; 120 typeLookup[Expression.NOT_LIKE_IGNORE_CASE] = ASTNotLikeIgnoreCase.class; 121 typeLookup[Expression.ADD] = ASTAdd.class; 122 typeLookup[Expression.SUBTRACT] = ASTSubtract.class; 123 typeLookup[Expression.MULTIPLY] = ASTMultiply.class; 124 typeLookup[Expression.DIVIDE] = ASTDivide.class; 125 126 typeLookup[Expression.NOT] = ASTNot.class; 127 typeLookup[Expression.NEGATIVE] = ASTNegate.class; 128 typeLookup[Expression.OBJ_PATH] = ASTObjPath.class; 129 typeLookup[Expression.DB_PATH] = ASTDbPath.class; 130 typeLookup[Expression.LIST] = ASTList.class; 131 132 typeLookup[Expression.TRUE] = ASTTrue.class; 133 typeLookup[Expression.FALSE] = ASTFalse.class; 134 } 135 136 140 public static Expression expressionOfType(int type) { 141 if (type < 0 || type >= typeLookup.length) { 142 throw new ExpressionException("Bad expression type: " + type); 143 } 144 145 if (typeLookup[type] == null) { 146 throw new ExpressionException("Bad expression type: " + type); 147 } 148 149 if (SimpleNode.class.isAssignableFrom(typeLookup[type])) { 151 try { 152 return (Expression) typeLookup[type].newInstance(); 153 } 154 catch (Exception ex) { 155 throw new ExpressionException("Error creating expression", ex); 156 } 157 } 158 159 throw new ExpressionException("Bad expression type: " + type); 160 } 161 162 166 protected static Object wrapPathOperand(Object op) { 167 if (op instanceof Collection ) { 168 return new ASTList((Collection ) op); 169 } 170 else if (op instanceof Object []) { 171 return new ASTList((Object []) op); 172 } 173 else { 174 return op; 175 } 176 } 177 178 186 public static Expression matchAnyDbExp(Map map, int pairType) { 187 List pairs = new ArrayList (); 188 189 Iterator it = map.entrySet().iterator(); 190 while (it.hasNext()) { 191 Map.Entry entry = (Map.Entry ) it.next(); 192 Expression exp = expressionOfType(pairType); 193 exp.setOperand(0, new ASTDbPath(entry.getKey())); 194 exp.setOperand(1, wrapPathOperand(entry.getValue())); 195 pairs.add(exp); 196 } 197 198 return joinExp(Expression.OR, pairs); 199 } 200 201 209 public static Expression matchAllDbExp(Map map, int pairType) { 210 List pairs = new ArrayList (); 211 212 Iterator it = map.entrySet().iterator(); 213 while (it.hasNext()) { 214 Map.Entry entry = (Map.Entry ) it.next(); 215 216 Expression exp = expressionOfType(pairType); 217 exp.setOperand(0, new ASTDbPath(entry.getKey())); 218 exp.setOperand(1, wrapPathOperand(entry.getValue())); 219 pairs.add(exp); 220 } 221 222 return joinExp(Expression.AND, pairs); 223 } 224 225 234 public static Expression matchAnyExp(Map map, int pairType) { 235 List pairs = new ArrayList (); 236 237 Iterator it = map.entrySet().iterator(); 238 while (it.hasNext()) { 239 Map.Entry entry = (Map.Entry ) it.next(); 240 241 Expression exp = expressionOfType(pairType); 242 exp.setOperand(0, new ASTObjPath(entry.getKey())); 243 exp.setOperand(1, wrapPathOperand(entry.getValue())); 244 pairs.add(exp); 245 } 246 247 return joinExp(Expression.OR, pairs); 248 } 249 250 258 public static Expression matchAllExp(Map map, int pairType) { 259 List pairs = new ArrayList (); 260 261 Iterator it = map.entrySet().iterator(); 262 while (it.hasNext()) { 263 Map.Entry entry = (Map.Entry ) it.next(); 264 265 Expression exp = expressionOfType(pairType); 266 exp.setOperand(0, new ASTObjPath(entry.getKey())); 267 exp.setOperand(1, wrapPathOperand(entry.getValue())); 268 pairs.add(exp); 269 } 270 271 return joinExp(Expression.AND, pairs); 272 } 273 274 277 public static Expression matchDbExp(String pathSpec, Object value) { 278 return new ASTEqual(new ASTDbPath(pathSpec), value); 279 } 280 281 284 public static Expression noMatchDbExp(String pathSpec, Object value) { 285 return new ASTNotEqual(new ASTDbPath(pathSpec), value); 286 } 287 288 291 public static Expression matchExp(String pathSpec, Object value) { 292 return new ASTEqual(new ASTObjPath(pathSpec), value); 293 } 294 295 298 public static Expression noMatchExp(String pathSpec, Object value) { 299 return new ASTNotEqual(new ASTObjPath(pathSpec), value); 300 } 301 302 305 public static Expression lessExp(String pathSpec, Object value) { 306 return new ASTLess(new ASTObjPath(pathSpec), value); 307 } 308 309 314 public static Expression lessDbExp(String pathSpec, Object value) { 315 return new ASTLess(new ASTDbPath(pathSpec), value); 316 } 317 318 321 public static Expression lessOrEqualExp(String pathSpec, Object value) { 322 return new ASTLessOrEqual(new ASTObjPath(pathSpec), value); 323 } 324 325 330 public static Expression lessOrEqualDbExp(String pathSpec, Object value) { 331 return new ASTLessOrEqual(new ASTDbPath(pathSpec), value); 332 } 333 334 337 public static Expression greaterExp(String pathSpec, Object value) { 338 return new ASTGreater(new ASTObjPath(pathSpec), value); 339 } 340 341 346 public static Expression greaterDbExp(String pathSpec, Object value) { 347 return new ASTGreater(new ASTDbPath(pathSpec), value); 348 } 349 350 353 public static Expression greaterOrEqualExp(String pathSpec, Object value) { 354 return new ASTGreaterOrEqual(new ASTObjPath(pathSpec), value); 355 } 356 357 362 public static Expression greaterOrEqualDbExp(String pathSpec, Object value) { 363 return new ASTGreaterOrEqual(new ASTDbPath(pathSpec), value); 364 } 365 366 370 public static Expression inExp(String pathSpec, Object [] values) { 371 if (values.length == 0) { 372 return new ASTFalse(); 373 } 374 return new ASTIn(new ASTObjPath(pathSpec), new ASTList(values)); 375 } 376 377 381 public static Expression inDbExp(String pathSpec, Object [] values) { 382 if (values.length == 0) { 383 return new ASTFalse(); 384 } 385 return new ASTIn(new ASTDbPath(pathSpec), new ASTList(values)); 386 } 387 388 392 public static Expression inExp(String pathSpec, Collection values) { 393 if (values.isEmpty()) { 394 return new ASTFalse(); 395 } 396 return new ASTIn(new ASTObjPath(pathSpec), new ASTList(values)); 397 } 398 399 403 public static Expression inDbExp(String pathSpec, Collection values) { 404 if (values.isEmpty()) { 405 return new ASTFalse(); 406 } 407 return new ASTIn(new ASTDbPath(pathSpec), new ASTList(values)); 408 } 409 410 414 public static Expression notInExp(String pathSpec, Collection values) { 415 if (values.isEmpty()) { 416 return new ASTTrue(); 417 } 418 return new ASTNotIn(new ASTObjPath(pathSpec), new ASTList(values)); 419 } 420 421 427 public static Expression notInDbExp(String pathSpec, Collection values) { 428 if (values.isEmpty()) { 429 return new ASTTrue(); 430 } 431 return new ASTNotIn(new ASTDbPath(pathSpec), new ASTList(values)); 432 } 433 434 435 441 public static Expression notInExp(String pathSpec, Object [] values) { 442 if (values.length == 0) { 443 return new ASTTrue(); 444 } 445 return new ASTNotIn(new ASTObjPath(pathSpec), new ASTList(values)); 446 } 447 448 454 public static Expression notInDbExp(String pathSpec, Object [] values) { 455 if (values.length == 0) { 456 return new ASTTrue(); 457 } 458 return new ASTNotIn(new ASTDbPath(pathSpec), new ASTList(values)); 459 } 460 461 464 public static Expression betweenExp(String pathSpec, Object value1, Object value2) { 465 return new ASTBetween(new ASTObjPath(pathSpec), value1, value2); 466 } 467 468 473 public static Expression betweenDbExp(String pathSpec, Object value1, Object value2) { 474 return new ASTBetween(new ASTDbPath(pathSpec), value1, value2); 475 } 476 477 480 public static Expression notBetweenExp(String pathSpec, Object value1, Object value2) { 481 return new ASTNotBetween(new ASTObjPath(pathSpec), value1, value2); 482 } 483 484 489 public static Expression notBetweenDbExp(String pathSpec, Object value1, Object value2) { 490 return new ASTNotBetween(new ASTDbPath(pathSpec), value1, value2); 491 } 492 493 496 public static Expression likeExp(String pathSpec, Object value) { 497 return new ASTLike(new ASTObjPath(pathSpec), value); 498 } 499 500 505 public static Expression likeDbExp(String pathSpec, Object value) { 506 return new ASTLike(new ASTDbPath(pathSpec), value); 507 } 508 509 512 public static Expression notLikeExp(String pathSpec, Object value) { 513 return new ASTNotLike(new ASTObjPath(pathSpec), value); 514 } 515 516 521 public static Expression notLikeDbExp(String pathSpec, Object value) { 522 return new ASTNotLike(new ASTDbPath(pathSpec), value); 523 } 524 525 528 public static Expression likeIgnoreCaseExp(String pathSpec, Object value) { 529 return new ASTLikeIgnoreCase(new ASTObjPath(pathSpec), value); 530 } 531 532 537 public static Expression likeIgnoreCaseDbExp(String pathSpec, Object value) { 538 return new ASTLikeIgnoreCase(new ASTDbPath(pathSpec), value); 539 } 540 541 544 public static Expression notLikeIgnoreCaseExp(String pathSpec, Object value) { 545 return new ASTNotLikeIgnoreCase(new ASTObjPath(pathSpec), value); 546 } 547 548 553 public static Expression notLikeIgnoreCaseDbExp(String pathSpec, Object value) { 554 return new ASTNotLikeIgnoreCase(new ASTDbPath(pathSpec), value); 555 } 556 557 562 public static Expression expTrue() { 563 return new ASTTrue(); 564 } 565 566 571 public static Expression expFalse() { 572 return new ASTFalse(); 573 } 574 575 585 public static Expression joinExp(int type, List expressions) { 586 int len = expressions.size(); 587 if (len == 0) 588 return null; 589 590 Expression currentExp = (Expression) expressions.get(0); 591 if (len == 1) { 592 return currentExp; 593 } 594 595 Expression exp = expressionOfType(type); 596 for (int i = 0; i < len; i++) { 597 exp.setOperand(i, expressions.get(i)); 598 } 599 return exp; 600 } 601 } 602 | Popular Tags |