KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > data > util > FilterRowIterator


1 package prefuse.data.util;
2
3 import java.util.NoSuchElementException JavaDoc;
4
5 import prefuse.data.Table;
6 import prefuse.data.expression.Predicate;
7 import prefuse.util.collections.IntIterator;
8
9 /**
10  * Iterator over table rows that filters the output by a given predicate. For
11  * each table row, the corresponding tuple is checked against the predicate.
12  * Only rows whose tuples pass the filter are included in this iteration.
13  *
14  * @author <a HREF="http://jheer.org">jeffrey heer</a>
15  */

16 public class FilterRowIterator extends IntIterator {
17     
18     private Predicate predicate;
19     private IntIterator rows;
20     private Table t;
21     private int next;
22     
23     /**
24      * Create a new FilterRowIterator.
25      * @param rows an iterator over table rows
26      * @param t the whos rows are being iterated over
27      * @param p the filter predicate to use
28      */

29     public FilterRowIterator(IntIterator rows, Table t, Predicate p) {
30         this.predicate = p;
31         this.rows = rows;
32         this.t = t;
33         next = advance();
34     }
35     
36     private int advance() {
37         while ( rows.hasNext() ) {
38             int r = rows.nextInt();
39             if ( predicate.getBoolean(t.getTuple(r)) ) {
40                 return r;
41             }
42         }
43         rows = null;
44         next = -1;
45         return -1;
46     }
47
48     /**
49      * @see prefuse.util.collections.LiteralIterator#nextInt()
50      */

51     public int nextInt() {
52         if ( !hasNext() ) {
53             throw new NoSuchElementException JavaDoc("No more elements");
54         }
55         int retval = next;
56         next = advance();
57         return retval;
58     }
59     
60     /**
61      * @see java.util.Iterator#hasNext()
62      */

63     public boolean hasNext() {
64         return ( rows != null );
65     }
66     
67     /**
68      * Not supported.
69      * @see java.util.Iterator#remove()
70      */

71     public void remove() {
72         throw new UnsupportedOperationException JavaDoc();
73     }
74     
75 } // end of class FilterRowIterator
76
Popular Tags