KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > lang > jpath > adapter > JSPListUnion


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.taglibs.standard.lang.jpath.adapter;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collection JavaDoc;
21
22 import javax.servlet.jsp.PageContext JavaDoc;
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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc pageContext, Predicate predicate) throws ConversionException, EvaluationException {
78         Object JavaDoc 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 JavaDoc predicated = new ArrayList JavaDoc();
86         while (this.hasNext()) {
87             this.next();
88             result = predicate.evaluate(pageContext, new JSPListIterationContext(this));
89             if (result instanceof Double JavaDoc) {
90                 oneItem = true;
91                 predicateTrue = ((Double JavaDoc)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 JavaDoc());
102         this.position = 0;
103         first = true;
104         return oneItem;
105     }
106
107     public void remove() {
108         throw new UnsupportedOperationException JavaDoc();
109     }
110
111 }
112
Popular Tags