Skip to content

Commit

Permalink
Faster DescendantIterator
Browse files Browse the repository at this point in the history
  • Loading branch information
rolfl committed Mar 20, 2012
1 parent 5393107 commit 6c0c745
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
14 changes: 8 additions & 6 deletions core/src/java/org/jdom2/DescendantIterator.java
Expand Up @@ -68,12 +68,12 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* @author Jason Hunter * @author Jason Hunter
* @author Rolf Lear * @author Rolf Lear
*/ */
class DescendantIterator implements IteratorIterable<Content> { final class DescendantIterator implements IteratorIterable<Content> {


private final Parent parent; private final Parent parent;
private LinkedList<ListIterator<Content>> stack = new LinkedList<ListIterator<Content>>(); private LinkedList<Iterator<Content>> stack = new LinkedList<Iterator<Content>>();
ListIterator<Content> current = null; private Iterator<Content> current = null;
ListIterator<Content> following = null; private Iterator<Content> following = null;


/** /**
* Iterator for the descendants of the supplied object. * Iterator for the descendants of the supplied object.
Expand All @@ -83,7 +83,7 @@ class DescendantIterator implements IteratorIterable<Content> {
DescendantIterator(Parent parent) { DescendantIterator(Parent parent) {
this.parent = parent; this.parent = parent;
// can trust that parent is not null, DescendantIterator is package-private. // can trust that parent is not null, DescendantIterator is package-private.
current = parent.getContent().listIterator(); current = parent.getContent().iterator();
} }


@Override @Override
Expand Down Expand Up @@ -141,12 +141,14 @@ public Content next() {
} }
} }
} }

if (ret == null) { if (ret == null) {
throw new NoSuchElementException("Iterated off the end of the " + throw new NoSuchElementException("Iterated off the end of the " +
"DescendantIterator"); "DescendantIterator");
} }

if (ret instanceof Parent) { if (ret instanceof Parent) {
following = ((Parent)ret).getContent().listIterator(); following = ((Parent)ret).getContent().iterator();
} else { } else {
following = null; following = null;
} }
Expand Down
12 changes: 6 additions & 6 deletions core/src/java/org/jdom2/FilterIterator.java
Expand Up @@ -66,10 +66,10 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* @author Rolf Lear * @author Rolf Lear
* @param <T> The Generic type of content returned by this FilterIterator. * @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 final DescendantIterator iterator;
private Filter<T> filter; private final Filter<T> filter;
private T nextObject; private T nextObject;
private boolean canremove = false; private boolean canremove = false;


Expand Down Expand Up @@ -98,8 +98,8 @@ public boolean hasNext() {
} }


while (iterator.hasNext()) { while (iterator.hasNext()) {
Object obj = iterator.next(); final Object obj = iterator.next();
T f = filter.filter(obj); final T f = filter.filter(obj);
if (f != null) { if (f != null) {
nextObject = f; nextObject = f;
return true; return true;
Expand All @@ -114,7 +114,7 @@ public T next() {
throw new NoSuchElementException(); throw new NoSuchElementException();
} }


T obj = nextObject; final T obj = nextObject;
nextObject = null; nextObject = null;
canremove = true; canremove = true;
return obj; return obj;
Expand Down

0 comments on commit 6c0c745

Please sign in to comment.