Skip to content

Commit

Permalink
Split SiblingNodeIterable into one class that's Iterable and one that…
Browse files Browse the repository at this point in the history
…'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
  • Loading branch information
tbreisacher authored and blickly committed Jan 12, 2017
1 parent fd59e13 commit ac48026
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions src/com/google/javascript/rhino/Node.java
Expand Up @@ -1468,7 +1468,9 @@ public Iterable<Node> children() {


/** /**
* <p>Return an iterable object that iterates over this node's siblings, * <p>Return an iterable object that iterates over this node's siblings,
* <b>including this Node</b>. The iterator does not support the optional * <b>including this Node</b> but not any siblings that are before this one.
*
* <p>The iterator does not support the optional
* operation {@link Iterator#remove()}.</p> * operation {@link Iterator#remove()}.</p>
* *
* <p>To iterate over a node's siblings including itself, one can write</p> * <p>To iterate over a node's siblings including itself, one can write</p>
Expand All @@ -1482,32 +1484,26 @@ public Iterable<Node> siblings() {
/** /**
* @see Node#siblings() * @see Node#siblings()
*/ */
private static final class SiblingNodeIterable private static final class SiblingNodeIterable implements Iterable<Node> {
implements Iterable<Node>, Iterator<Node> {
private final Node start; private final Node start;
@Nullable private Node current;
private boolean used;


SiblingNodeIterable(Node start) { SiblingNodeIterable(Node start) {
this.start = start; this.start = start;
this.current = start;
this.used = false;
} }

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


@Override @Override
Expand Down

0 comments on commit ac48026

Please sign in to comment.