Permalink
Browse files

Faster DescendantIterator

  • Loading branch information...
rolfl committed Mar 20, 2012
1 parent 5393107 commit 6c0c745dbc18f9248f66ce007bc4128f3893036f
Showing with 14 additions and 12 deletions.
  1. +8 −6 core/src/java/org/jdom2/DescendantIterator.java
  2. +6 −6 core/src/java/org/jdom2/FilterIterator.java
@@ -68,12 +68,12 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* @author Jason Hunter
* @author Rolf Lear
*/
-class DescendantIterator implements IteratorIterable<Content> {
+final class DescendantIterator implements IteratorIterable<Content> {
private final Parent parent;
- private LinkedList<ListIterator<Content>> stack = new LinkedList<ListIterator<Content>>();
- ListIterator<Content> current = null;
- ListIterator<Content> following = null;
+ private LinkedList<Iterator<Content>> stack = new LinkedList<Iterator<Content>>();
+ private Iterator<Content> current = null;
+ private Iterator<Content> following = null;
/**
* Iterator for the descendants of the supplied object.
@@ -83,7 +83,7 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
DescendantIterator(Parent parent) {
this.parent = parent;
// can trust that parent is not null, DescendantIterator is package-private.
- current = parent.getContent().listIterator();
+ current = parent.getContent().iterator();
}
@Override
@@ -141,12 +141,14 @@ public Content next() {
}
}
}
+
if (ret == null) {
throw new NoSuchElementException("Iterated off the end of the " +
"DescendantIterator");
}
+
if (ret instanceof Parent) {
- following = ((Parent)ret).getContent().listIterator();
+ following = ((Parent)ret).getContent().iterator();
} else {
following = null;
}
@@ -66,10 +66,10 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* @author Rolf Lear
* @param <T> The Generic type of content returned by this FilterIterator.
*/
-class FilterIterator<T> implements IteratorIterable<T> {
+final class FilterIterator<T> implements IteratorIterable<T> {
- private DescendantIterator iterator;
- private Filter<T> filter;
+ private final DescendantIterator iterator;
+ private final Filter<T> filter;
private T nextObject;
private boolean canremove = false;
@@ -98,8 +98,8 @@ public boolean hasNext() {
}
while (iterator.hasNext()) {
- Object obj = iterator.next();
- T f = filter.filter(obj);
+ final Object obj = iterator.next();
+ final T f = filter.filter(obj);
if (f != null) {
nextObject = f;
return true;
@@ -114,7 +114,7 @@ public T next() {
throw new NoSuchElementException();
}
- T obj = nextObject;
+ final T obj = nextObject;
nextObject = null;
canremove = true;
return obj;

0 comments on commit 6c0c745

Please sign in to comment.