From ac48026a8846f55dbcbbe137aa5bbbabf913018a Mon Sep 17 00:00:00 2001 From: tbreisacher Date: Thu, 12 Jan 2017 09:10:30 -0800 Subject: [PATCH] Split SiblingNodeIterable into one class that's Iterable and one that's Iterator. Error Prone has been bothering us about this for a while. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=144332348 --- src/com/google/javascript/rhino/Node.java | 36 ++++++++++------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/src/com/google/javascript/rhino/Node.java b/src/com/google/javascript/rhino/Node.java index 5b1d7fbcef4..9d8edb05b32 100644 --- a/src/com/google/javascript/rhino/Node.java +++ b/src/com/google/javascript/rhino/Node.java @@ -1468,7 +1468,9 @@ public Iterable children() { /** *

Return an iterable object that iterates over this node's siblings, - * including this Node. The iterator does not support the optional + * including this Node but not any siblings that are before this one. + * + *

The iterator does not support the optional * operation {@link Iterator#remove()}.

* *

To iterate over a node's siblings including itself, one can write

@@ -1482,32 +1484,26 @@ public Iterable siblings() { /** * @see Node#siblings() */ - private static final class SiblingNodeIterable - implements Iterable, Iterator { + private static final class SiblingNodeIterable implements Iterable { private final Node start; - @Nullable private Node current; - private boolean used; SiblingNodeIterable(Node start) { this.start = start; - this.current = start; - this.used = false; } - @Override public Iterator iterator() { - if (!used) { - used = true; - return this; - } else { - // We have already used the current object as an iterator; - // we must create a new SiblingNodeIterable based on this - // iterable's start node. - // - // Since the primary use case for Node.children is in for - // loops, this branch is extremely unlikely. - return (new SiblingNodeIterable(start)).iterator(); - } + return new SiblingNodeIterator(start); + } + } + + /** + * @see Node#siblings() + */ + private static final class SiblingNodeIterator implements Iterator { + @Nullable private Node current; + + SiblingNodeIterator(Node start) { + this.current = start; } @Override