585a537967a1821ebceaf3f1c4ec1c770dd8e8dd
[jpim.git] / src / de / j32 / util / FilteredIterator.java
1 package de.j32.util;
2
3 import java.util.Iterator;
4 import java.util.NoSuchElementException;
5
6 public class FilteredIterator<E> implements Iterator<E>
7 {
8     Iterator<E> base_;
9     Filter<E> filter_;
10     E next_;
11     boolean hasNext_ = true;
12
13     public FilteredIterator(Iterator<E> base, Filter<E> filter)
14     {
15         base_ = base;
16         filter_ = filter;
17         advance();
18     }
19
20     public E next()
21     {
22         if (hasNext_) {
23             E rv = next_;
24             advance();
25             return rv;
26         }
27         else throw new NoSuchElementException();
28     }
29
30     public boolean hasNext()
31     {
32         return hasNext_;
33     }
34
35     public void remove()
36     {
37         throw new UnsupportedOperationException();
38     }
39
40     void advance()
41     {
42         while (base_.hasNext()) {
43             next_ = base_.next();
44             if (filter_.match(next_)) return;
45         }
46         hasNext_ = false;
47         next_ = null;
48     }
49
50 }