1 16 17 package org.apache.taglibs.standard.lang.jpath.adapter; 18 19 import java.util.ArrayList ; 20 import java.util.Collection ; 21 22 import javax.servlet.jsp.PageContext ; 23 24 import org.apache.taglibs.standard.lang.jpath.expression.EvaluationException; 25 import org.apache.taglibs.standard.lang.jpath.expression.Expression; 26 import org.apache.taglibs.standard.lang.jpath.expression.Predicate; 27 28 public class JSPListUnion implements JSPList { 29 30 private Expression predicate; 31 32 private JSPList left; 33 private JSPList right; 34 private int position; 35 private Object current; 36 boolean first; 37 38 public JSPListUnion(JSPList left, JSPList right) { 39 this.left = left; 40 this.right = right; 41 this.position = 0; 42 first = true; 43 } 44 45 public Object next() { 46 if (first) { 47 if (left.hasNext()) { 48 current = left.next(); 49 } else { 50 first = false; 51 current = right.next(); 52 } 53 } else { 54 current = right.next(); 55 } 56 position++; 57 return current; 58 } 59 60 public Object getCurrent() { 61 return current; 62 } 63 64 public boolean hasNext() { 65 boolean result = left.hasNext() || right.hasNext(); 66 return result; 67 } 68 69 public int getPosition() { 70 return position; 71 } 72 73 public int getLast() { 74 return (left.getLast() + right.getLast()); 75 } 76 77 public boolean applyPredicate(PageContext pageContext, Predicate predicate) throws ConversionException, EvaluationException { 78 Object result; 79 boolean oneItem = false; 80 boolean predicateTrue; 81 if (position != 0) { 82 throw new ConversionException("You cannot apply a predicate to " 83 + "a JSPList that has begun to be iterated"); 84 } 85 Collection predicated = new ArrayList (); 86 while (this.hasNext()) { 87 this.next(); 88 result = predicate.evaluate(pageContext, new JSPListIterationContext(this)); 89 if (result instanceof Double ) { 90 oneItem = true; 91 predicateTrue = ((Double )result).doubleValue() == position; 92 } else { 93 oneItem = false; 94 predicateTrue = Convert.toBoolean(result).booleanValue(); 95 } 96 if (predicateTrue) { 97 predicated.add(current); 98 } 99 } 100 this.left = Convert.toJSPList(predicated); 101 this.right = Convert.toJSPList(new ArrayList ()); 102 this.position = 0; 103 first = true; 104 return oneItem; 105 } 106 107 public void remove() { 108 throw new UnsupportedOperationException (); 109 } 110 111 } 112 | Popular Tags |