1 4 5 package polyglot.util; 6 7 import java.util.Iterator ; 8 import java.util.Collection ; 9 10 19 public final class FilteringIterator implements Iterator { 20 24 public FilteringIterator(Collection coll, Predicate pred) { 25 this(coll.iterator(), pred); 26 } 27 28 32 public FilteringIterator(Iterator iter, Predicate pred) { 33 backing_iterator = iter; 34 predicate = pred; 35 findNextItem(); 36 } 37 38 public Object next() { 39 Object res = next_item; 40 if (res == null) 41 throw new java.util.NoSuchElementException (); 42 findNextItem(); 43 return res; 44 } 45 46 public boolean hasNext() { 47 return next_item != null; 48 } 49 50 public void remove() { 51 throw new UnsupportedOperationException ("FilteringIterator.remove"); 52 } 53 54 private void findNextItem() { 56 while (backing_iterator.hasNext()) { 57 Object o = backing_iterator.next(); 58 if (predicate.isTrue(o)) { 59 next_item = o; 60 return; 61 } 62 } 63 next_item = null; 64 } 65 66 Object next_item; 70 Iterator backing_iterator; 71 Predicate predicate; 72 } 73 74 75 | Popular Tags |