KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > uk > co > jezuk > mango > iterators > SelectingIterator


1 package uk.co.jezuk.mango.iterators;
2
3 /**
4  * A <code>SelectingIterator</code> enumerates only those elements of a collection
5  * that match the supplied <code>Predicate</code>.
6  *
7  * @author Jez Higgins, jez@jezuk.co.uk
8  * @version $Id: SelectingIterator.java 116 2006-09-29 09:56:09Z jez $
9  */

10 public class SelectingIterator implements java.util.Iterator JavaDoc
11 {
12   public SelectingIterator(java.util.Iterator JavaDoc iterator, uk.co.jezuk.mango.Predicate predicate)
13   {
14     iter_ = iterator;
15     pred_ = predicate;
16   } // SelectingIterator
17

18   public boolean hasNext()
19   {
20     next_ = null;
21     while(iter_.hasNext() && next_ == null)
22     {
23       Object JavaDoc candidate = iter_.next();
24       if(pred_.test(candidate))
25         next_ = candidate;
26     } // while
27
return next_ != null;
28   } // hasNext
29

30   public Object JavaDoc next()
31   {
32     return next_;
33   } // next
34

35   public void remove()
36   {
37         iter_.remove();
38   } // remove
39

40   ////////////////////////
41
private java.util.Iterator JavaDoc iter_;
42   private uk.co.jezuk.mango.Predicate pred_;
43   private Object JavaDoc next_;
44 } // SelectingIterator
45

46
47
Popular Tags