1 23 24 package org.apache.slide.search.basic.expression; 25 26 import java.util.Collection ; 27 import java.util.Iterator ; 28 29 import org.apache.slide.search.InvalidQueryException; 30 import org.apache.slide.search.SearchException; 31 import org.apache.slide.search.basic.IBasicExpression; 32 import org.apache.slide.search.basic.IBasicResultSet; 33 import org.jdom.Element; 34 35 40 public abstract class MergeExpression extends GenericBasicExpression { 41 42 43 44 private Collection expressionsToMerge = null; 45 46 52 MergeExpression (Element e, Collection expressionsToMerge) throws InvalidQueryException { 53 super (e); 54 this.expressionsToMerge = expressionsToMerge; 55 if (expressionsToMerge.size() == 0) { 56 throw new InvalidQueryException(getMustHaveMergeExpressionsMessage(e.getName())); 57 } 58 } 59 60 67 public IBasicResultSet execute () throws SearchException { 68 69 Iterator iterator = expressionsToMerge.iterator(); 70 if (iterator.hasNext()) { 71 resultSet = ((IBasicExpression)iterator.next()).execute(); 72 } 73 while (iterator.hasNext()) { 74 IBasicExpression expression = (IBasicExpression)iterator.next(); 75 merge(expression.execute()); 76 } 77 return resultSet; 78 } 79 80 81 86 protected abstract void merge (IBasicResultSet set); 87 88 89 94 protected String toString (String op) { 95 StringBuffer sb = new StringBuffer (); 96 97 Iterator it = expressionsToMerge.iterator(); 98 99 while (it.hasNext()) { 100 sb.append (((IBasicExpression) it.next()).toString()); 101 if (it.hasNext()) 102 sb.append (" ").append (op).append (" "); 103 } 104 return sb.toString(); 105 } 106 107 115 private static String getMustHaveMergeExpressionsMessage(String operationName) { 116 return "<" + operationName + "> must have at least on nested expression."; 117 } 118 } 119 120 121 | Popular Tags |