KickJava   Java API By Example, From Geeks To Geeks.

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


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