KickJava   Java API By Example, From Geeks To Geeks.

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


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.ListIterator JavaDoc;
19 import java.util.NoSuchElementException JavaDoc;
20
21 import org.apache.commons.collections.Predicate;
22
23 /**
24  * A proxy {@link ListIterator ListIterator} which
25  * takes a {@link Predicate Predicate} instance to filter
26  * out objects from an underlying <code>ListIterator</code>
27  * instance. Only objects for which the specified
28  * <code>Predicate</code> evaluates to <code>true</code> are
29  * returned by the iterator.
30  *
31  * @since Commons Collections 2.0
32  * @version $Revision: 1.7 $ $Date: 2004/02/18 00:59:50 $
33  *
34  * @author Rodney Waldhoff
35  */

36 public class FilterListIterator implements ListIterator JavaDoc {
37
38     /** The iterator being used */
39     private ListIterator JavaDoc iterator;
40     
41     /** The predicate being used */
42     private Predicate predicate;
43
44     /**
45      * The value of the next (matching) object, when
46      * {@link #nextObjectSet} is true.
47      */

48     private Object JavaDoc nextObject;
49
50     /**
51      * Whether or not the {@link #nextObject} has been set
52      * (possibly to <code>null</code>).
53      */

54     private boolean nextObjectSet = false;
55
56     /**
57      * The value of the previous (matching) object, when
58      * {@link #previousObjectSet} is true.
59      */

60     private Object JavaDoc previousObject;
61
62     /**
63      * Whether or not the {@link #previousObject} has been set
64      * (possibly to <code>null</code>).
65      */

66     private boolean previousObjectSet = false;
67
68     /**
69      * The index of the element that would be returned by {@link #next}.
70      */

71     private int nextIndex = 0;
72     
73     //-----------------------------------------------------------------------
74
/**
75      * Constructs a new <code>FilterListIterator</code> that will not
76      * function until
77      * {@link ProxyListIterator#setListIterator(ListIterator) setListIterator}
78      * and {@link #setPredicate(Predicate) setPredicate} are invoked.
79      */

80     public FilterListIterator() {
81         super();
82     }
83
84     /**
85      * Constructs a new <code>FilterListIterator</code> that will not
86      * function until {@link #setPredicate(Predicate) setPredicate} is invoked.
87      *
88      * @param iterator the iterator to use
89      */

90     public FilterListIterator(ListIterator JavaDoc iterator ) {
91         super();
92         this.iterator = iterator;
93     }
94
95     /**
96      * Constructs a new <code>FilterListIterator</code>.
97      *
98      * @param iterator the iterator to use
99      * @param predicate the predicate to use
100      */

101     public FilterListIterator(ListIterator JavaDoc iterator, Predicate predicate) {
102         super();
103         this.iterator = iterator;
104         this.predicate = predicate;
105     }
106
107     /**
108      * Constructs a new <code>FilterListIterator</code> that will not
109      * function until
110      * {@link ProxyListIterator#setListIterator(ListIterator) setListIterator}
111      * is invoked.
112      *
113      * @param predicate the predicate to use.
114      */

115     public FilterListIterator(Predicate predicate) {
116         super();
117         this.predicate = predicate;
118     }
119
120     //-----------------------------------------------------------------------
121
/** Not supported. */
122     public void add(Object JavaDoc o) {
123         throw new UnsupportedOperationException JavaDoc("FilterListIterator.add(Object) is not supported.");
124     }
125
126     public boolean hasNext() {
127         if(nextObjectSet) {
128             return true;
129         } else {
130             return setNextObject();
131         }
132     }
133
134     public boolean hasPrevious() {
135         if(previousObjectSet) {
136             return true;
137         } else {
138             return setPreviousObject();
139         }
140     }
141
142     public Object JavaDoc next() {
143         if(!nextObjectSet) {
144             if(!setNextObject()) {
145                 throw new NoSuchElementException JavaDoc();
146             }
147         }
148         nextIndex++;
149         Object JavaDoc temp = nextObject;
150         clearNextObject();
151         return temp;
152     }
153
154     public int nextIndex() {
155         return nextIndex;
156     }
157
158     public Object JavaDoc previous() {
159         if(!previousObjectSet) {
160             if(!setPreviousObject()) {
161                 throw new NoSuchElementException JavaDoc();
162             }
163         }
164         nextIndex--;
165         Object JavaDoc temp = previousObject;
166         clearPreviousObject();
167         return temp;
168     }
169
170     public int previousIndex() {
171         return (nextIndex-1);
172     }
173
174     /** Not supported. */
175     public void remove() {
176         throw new UnsupportedOperationException JavaDoc("FilterListIterator.remove() is not supported.");
177     }
178
179     /** Not supported. */
180     public void set(Object JavaDoc o) {
181         throw new UnsupportedOperationException JavaDoc("FilterListIterator.set(Object) is not supported.");
182     }
183
184     //-----------------------------------------------------------------------
185
/**
186      * Gets the iterator this iterator is using.
187      *
188      * @return the iterator.
189      */

190     public ListIterator JavaDoc getListIterator() {
191         return iterator;
192     }
193
194     /**
195      * Sets the iterator for this iterator to use.
196      * If iteration has started, this effectively resets the iterator.
197      *
198      * @param iterator the iterator to use
199      */

200     public void setListIterator(ListIterator JavaDoc iterator) {
201         this.iterator = iterator;
202     }
203
204     //-----------------------------------------------------------------------
205
/**
206      * Gets the predicate this iterator is using.
207      *
208      * @return the predicate.
209      */

210     public Predicate getPredicate() {
211         return predicate;
212     }
213
214     /**
215      * Sets the predicate this the iterator to use.
216      *
217      * @param predicate the transformer to use
218      */

219     public void setPredicate(Predicate predicate) {
220         this.predicate = predicate;
221     }
222
223     //-----------------------------------------------------------------------
224
private void clearNextObject() {
225         nextObject = null;
226         nextObjectSet = false;
227     }
228
229     private boolean setNextObject() {
230         // if previousObjectSet,
231
// then we've walked back one step in the
232
// underlying list (due to a hasPrevious() call)
233
// so skip ahead one matching object
234
if(previousObjectSet) {
235             clearPreviousObject();
236             if(!setNextObject()) {
237                 return false;
238             } else {
239                 clearNextObject();
240             }
241         }
242
243         while(iterator.hasNext()) {
244             Object JavaDoc object = iterator.next();
245             if(predicate.evaluate(object)) {
246                 nextObject = object;
247                 nextObjectSet = true;
248                 return true;
249             }
250         }
251         return false;
252     }
253
254     private void clearPreviousObject() {
255         previousObject = null;
256         previousObjectSet = false;
257     }
258
259     private boolean setPreviousObject() {
260         // if nextObjectSet,
261
// then we've walked back one step in the
262
// underlying list (due to a hasNext() call)
263
// so skip ahead one matching object
264
if(nextObjectSet) {
265             clearNextObject();
266             if(!setPreviousObject()) {
267                 return false;
268             } else {
269                 clearPreviousObject();
270             }
271         }
272
273         while(iterator.hasPrevious()) {
274             Object JavaDoc object = iterator.previous();
275             if(predicate.evaluate(object)) {
276                 previousObject = object;
277                 previousObjectSet = true;
278                 return true;
279             }
280         }
281         return false;
282     }
283
284 }
285
Popular Tags