Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6473148

TreePath.iterator() should document the iteration order

    XMLWordPrintable

Details

    • b98
    • generic
    • generic
    • Not verified

    Description

      As of b99, TreePath.iterator() is implemented as follows, and as you see it's guaranteed to cause NPE because curr is not initialized to this. Also, it fails to iterate the leaf node correctly:

          public Iterator<Tree> iterator() {
              return new Iterator<Tree>() {
                  public boolean hasNext() {
                      return curr.parent != null;
                  }
                  
                  public Tree next() {
                      curr = curr.parent;
                      return curr.leaf;
                  }
                  
                  public void remove() {
                      throw new UnsupportedOperationException();
                  }
                  
                  private TreePath curr;
              };
          }

      The correct implementation is:

          public Iterator<Tree> iterator() {
              return new Iterator<Tree>() {
                  public boolean hasNext() {
                      return curr != null;
                  }
                  
                  public Tree next() {
                      TreePath t = curr;
                      curr = curr.parent;
                      return t.leaf;
                  }
                  
                  public void remove() {
                      throw new UnsupportedOperationException();
                  }
                  
                  private TreePath curr = TreePath.this;
              };
          }

      The iterator method should also document in which order Trees are iterated. I find it more useful to be able to iterate from root to leaf, but no matter which way it goes, it should be documented.

      Attachments

        Issue Links

          Activity

            People

              vromero Vicente Arturo Romero Zaldivar
              kkawagucsunw Kohsuke Kawaguchi (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: