    001    package calhoun.util;
    002
    003    import java.util.Iterator;
    004
    005    public abstract class FilterIterator<A> implements Iterator<A> {
    006            Iterator<A> baseIterator;
    007            A current = null;
    008
    009
    010            public FilterIterator(Iterator<A> base) {
    011                    baseIterator = base;
    012                    advance();
    013            }
    014
    015            void advance() {
    016                    boolean valid = false;
    017                    while(baseIterator.hasNext()) {
    018                            current = baseIterator.next();
    019                            if(test(current)) {
    020                                    valid = true;
    021                                    break;
    022                            }
    023                    }
    024                    if(!valid)
    025                            current = null;
    026            }
    027
    028            public boolean hasNext() {
    029                    return current != null;
    030            }
    031
    032            public A next() {
    033                    A ret = current;
    034                    advance();
    035                    return ret;
    036            }
    037
    038            public void remove() {
    039                    throw new UnsupportedOperationException();
    040            }
    041
    042            public abstract boolean test(A val);
    043    }