KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > iterators > FilterIterator


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 package org.apache.commons.collections.iterators;
17
18 import java.util.Iterator JavaDoc;
19 import java.util.NoSuchElementException JavaDoc;
20
21 import org.apache.commons.collections.Predicate;
22
23 /**
24  * Decorates an iterator such that only elements matching a predicate filter
25  * are returned.
26  *
27  * @since Commons Collections 1.0
28  * @version $Revision: 1.8 $ $Date: 2004/02/18 00:59:50 $
29  *
30  * @author James Strachan
31  * @author Jan Sorensen
32  * @author Ralph Wagner
33  * @author Stephen Colebourne
34  */

35 public class FilterIterator implements Iterator JavaDoc {
36
37     /** The iterator being used */
38     private Iterator JavaDoc iterator;
39     /** The predicate being used */
40     private Predicate predicate;
41     /** The next object in the iteration */
42     private Object JavaDoc nextObject;
43     /** Whether the next object has been calculated yet */
44     private boolean nextObjectSet = false;
45
46     //-----------------------------------------------------------------------
47
/**
48      * Constructs a new <code>FilterIterator</code> that will not function
49      * until {@link #setIterator(Iterator) setIterator} is invoked.
50      */

51     public FilterIterator() {
52         super();
53     }
54
55     /**
56      * Constructs a new <code>FilterIterator</code> that will not function
57      * until {@link #setPredicate(Predicate) setPredicate} is invoked.
58      *
59      * @param iterator the iterator to use
60      */

61     public FilterIterator(Iterator JavaDoc iterator) {
62         super();
63         this.iterator = iterator;
64     }
65
66     /**
67      * Constructs a new <code>FilterIterator</code> that will use the
68      * given iterator and predicate.
69      *
70      * @param iterator the iterator to use
71      * @param predicate the predicate to use
72      */

73     public FilterIterator(Iterator JavaDoc iterator, Predicate predicate) {
74         super();
75         this.iterator = iterator;
76         this.predicate = predicate;
77     }
78
79     //-----------------------------------------------------------------------
80
/**
81      * Returns true if the underlying iterator contains an object that
82      * matches the predicate.
83      *
84      * @return true if there is another object that matches the predicate
85      */

86     public boolean hasNext() {
87         if (nextObjectSet) {
88             return true;
89         } else {
90             return setNextObject();
91         }
92     }
93
94     /**
95      * Returns the next object that matches the predicate.
96      *
97      * @return the next object which matches the given predicate
98      * @throws NoSuchElementException if there are no more elements that
99      * match the predicate
100      */

101     public Object JavaDoc next() {
102         if (!nextObjectSet) {
103             if (!setNextObject()) {
104                 throw new NoSuchElementException JavaDoc();
105             }
106         }
107         nextObjectSet = false;
108         return nextObject;
109     }
110
111     /**
112      * Removes from the underlying collection of the base iterator the last
113      * element returned by this iterator.
114      * This method can only be called
115      * if <code>next()</code> was called, but not after
116      * <code>hasNext()</code>, because the <code>hasNext()</code> call
117      * changes the base iterator.
118      *
119      * @throws IllegalStateException if <code>hasNext()</code> has already
120      * been called.
121      */

122     public void remove() {
123         if (nextObjectSet) {
124             throw new IllegalStateException JavaDoc("remove() cannot be called");
125         }
126         iterator.remove();
127     }
128
129     //-----------------------------------------------------------------------
130
/**
131      * Gets the iterator this iterator is using.
132      *
133      * @return the iterator.
134      */

135     public Iterator JavaDoc getIterator() {
136         return iterator;
137     }
138
139     /**
140      * Sets the iterator for this iterator to use.
141      * If iteration has started, this effectively resets the iterator.
142      *
143      * @param iterator the iterator to use
144      */

145     public void setIterator(Iterator JavaDoc iterator) {
146         this.iterator = iterator;
147     }
148
149     //-----------------------------------------------------------------------
150
/**
151      * Gets the predicate this iterator is using.
152      *
153      * @return the predicate.
154      */

155     public Predicate getPredicate() {
156         return predicate;
157     }
158
159     /**
160      * Sets the predicate this the iterator to use.
161      *
162      * @param predicate the transformer to use
163      */

164     public void setPredicate(Predicate predicate) {
165         this.predicate = predicate;
166     }
167
168     //-----------------------------------------------------------------------
169
/**
170      * Set nextObject to the next object. If there are no more
171      * objects then return false. Otherwise, return true.
172      */

173     private boolean setNextObject() {
174         while (iterator.hasNext()) {
175             Object JavaDoc object = iterator.next();
176             if (predicate.evaluate(object)) {
177                 nextObject = object;
178                 nextObjectSet = true;
179                 return true;
180             }
181         }
182         return false;
183     }
184 }
185
Popular Tags