Mercurial Hosting > nabble
diff src/nabble/model/FilteredNodeIterator.java @ 0:7ecd1a4ef557
add content
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 21 Mar 2019 19:15:52 -0600 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/nabble/model/FilteredNodeIterator.java Thu Mar 21 19:15:52 2019 -0600 @@ -0,0 +1,38 @@ +package nabble.model; + +import java.util.NoSuchElementException; +import fschmidt.util.java.Filter; + + +public final class FilteredNodeIterator extends NodeIterator<Node> { + private final NodeIterator<? extends Node> iter; + private final Filter<Node> filter; + private Node next = null; + + public FilteredNodeIterator(NodeIterator<? extends Node> iter,Filter<Node> filter) { + this.iter = iter; + this.filter = filter; + } + + public boolean hasNext() { + while( next == null && iter.hasNext() ) { + Node node = iter.next(); + if( filter.ok(node) ) + next = node; + } + return next != null; + } + + public Node next() throws NoSuchElementException { + if( !hasNext() ) + throw new NoSuchElementException(); + Node node = next; + next = null; + return node; + } + + public void close() { + iter.close(); + } + +}