01: package prefuse.data.util;
02:
03: import java.util.Iterator;
04: import java.util.NoSuchElementException;
05:
06: import prefuse.data.Tuple;
07: import prefuse.data.expression.Predicate;
08:
09: /**
10: * Iterator over tuples that filters the output by a given predicate.
11: *
12: * @author <a href="http://jheer.org">jeffrey heer</a>
13: */
14: public class FilterIterator implements Iterator {
15:
16: private Predicate predicate;
17: private Iterator tuples;
18: private Tuple next;
19:
20: /**
21: * Create a new FilterIterator.
22: * @param tuples an iterator over tuples
23: * @param p the filter predicate to use
24: */
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: /**
44: * @see java.util.Iterator#next()
45: */
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: /**
56: * @see java.util.Iterator#hasNext()
57: */
58: public boolean hasNext() {
59: return (tuples != null);
60: }
61:
62: /**
63: * Not supported.
64: * @see java.util.Iterator#remove()
65: */
66: public void remove() {
67: throw new UnsupportedOperationException();
68: }
69:
70: } // end of class FilterIterator
|