1 package prefuse.data.util; 2 3 import java.util.Iterator ; 4 import java.util.NoSuchElementException ; 5 6 import prefuse.data.Tuple; 7 import prefuse.data.expression.Predicate; 8 9 14 public class FilterIterator implements Iterator { 15 16 private Predicate predicate; 17 private Iterator tuples; 18 private Tuple next; 19 20 25 public FilterIterator(Iterator tuples, Predicate p) { 26 this.predicate = p; 27 this.tuples = tuples; 28 next = advance(); 29 } 30 31 private Tuple advance() { 32 while ( tuples.hasNext() ) { 33 Tuple t = (Tuple)tuples.next(); 34 if ( predicate.getBoolean(t) ) { 35 return t; 36 } 37 } 38 tuples = null; 39 next = null; 40 return null; 41 } 42 43 46 public Object next() { 47 if ( !hasNext() ) { 48 throw new NoSuchElementException ("No more elements"); 49 } 50 Tuple retval = next; 51 next = advance(); 52 return retval; 53 } 54 55 58 public boolean hasNext() { 59 return ( tuples != null ); 60 } 61 62 66 public void remove() { 67 throw new UnsupportedOperationException (); 68 } 69 70 } | Popular Tags |