/*
Copyright 2015 Google Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.google.security.zynamics.zylib.ZyTree;

import com.google.security.zynamics.zylib.general.Pair;

import java.util.Stack;


public class InorderIterator {
  private final Stack<Pair<IZyTreeNode, Integer>> traversalStack =
      new Stack<Pair<IZyTreeNode, Integer>>();
  private final IZyTreeNode m_root;
  private boolean m_started = false;

  public InorderIterator(final IZyTreeNode root) {
    m_root = root;
  }

  private void pushLongestPathFrom(final IZyTreeNode node) {
    IZyTreeNode current = node;

    do {
      traversalStack.push(new Pair<IZyTreeNode, Integer>(current, 0));

      if (current.getChildren().size() == 0) {
        break;
      }

      current = current.getChildren().get(0);
    } while (true);
  }

  public IZyTreeNode current() {
    return traversalStack.lastElement().first();
  }

  public boolean next() {
    if (!m_started) {
      pushLongestPathFrom(m_root);

      m_started = true;
    } else {
      if (traversalStack.empty()) {
        throw new RuntimeException("Internal Error: Traversal already finished");
      }

      final Pair<IZyTreeNode, Integer> justProcessed = traversalStack.pop();

      final IZyTreeNode justProcessedNode = justProcessed.first();
      final int justProcessedChildrenProcessed = justProcessed.second();

      if (traversalStack.empty()) {
        if (justProcessedChildrenProcessed == justProcessedNode.getChildren().size()) {
          // At this point we're done
          return false;
        } else {
          if (justProcessedNode.getChildren().size() == 0) {
            throw new RuntimeException("Error");
          } else if (justProcessedNode.getChildren().size() == 1) {
            pushLongestPathFrom(justProcessed.first().getChildren()
                .get(justProcessedChildrenProcessed));
          } else {
            traversalStack.push(new Pair<IZyTreeNode, Integer>(justProcessed.first().getChildren()
                .get(justProcessedChildrenProcessed), 0));
          }

        }
      } else {
        if (justProcessedChildrenProcessed == justProcessedNode.getChildren().size()) {
          // At this point we've handled all the children of the node. The node
          // can be removed and we continue with the next node on the stack.

          // We have to adjust the parent node though.
          final Pair<IZyTreeNode, Integer> parentProcessed = traversalStack.pop();

          traversalStack.push(new Pair<IZyTreeNode, Integer>(parentProcessed.first(),
              parentProcessed.second() + 1));
        } else {
          if (justProcessedNode.getChildren().size() == 0) {
            throw new RuntimeException("Error");
          } else if (justProcessedNode.getChildren().size() == 1) {
            pushLongestPathFrom(justProcessed.first().getChildren()
                .get(justProcessedChildrenProcessed));
          } else {
            traversalStack.push(new Pair<IZyTreeNode, Integer>(justProcessed.first().getChildren()
                .get(justProcessedChildrenProcessed), 0));
          }
        }
      }
    }

    return !traversalStack.empty();
  }

}

@@ -804,22 +804,16 @@
 lStack =
-%0A     
  new Sta
@@ -819,34 +819,8 @@
 ack%3C
-Pair%3CIZyTreeNode, Integer%3E
 %3E();
@@ -1456,17 +1456,16 @@
 _root);%0A
-%0A
       m_
@@ -2056,488 +2056,87 @@
     
-if (justProcessedNode.getChildren().size() == 0) %7B%0A            throw new RuntimeException(%22Error%22);%0A          %7D else if (justProcessedNode.getChildren().size() == 1) %7B%0A            pushLongestPathFrom(justProcessed.first().getChildren()%0A                .get(justProcessedChildrenProcessed));%0A          %7D else %7B%0A            traversalStack.push(new Pair%3CIZyTreeNode, Integer%3E(justProcessed.first().getChildren()%0A                .get(justProcessedChildrenProcessed), 0));%0A          %7D%0A
+checkAndPush(justProcessed, justProcessedNode, justProcessedChildrenProcessed);
 %0A   
@@ -2705,176 +2705,450 @@
     
-if (justProcessedNode.getChildren().size() == 0) %7B
+checkAndPush(justProcessed, justProcessedNode, justProcessedChildrenProcessed);%0A        %7D
 %0A      
+%7D%0A
     
-  throw new RuntimeException(%22Error%22
+%7D%0A%0A    return !traversalStack.empty(
 );%0A  
+%7D%0A
   
+%0A
   
-    %7D else if (justProcessedNode.getChildren().size() == 1) %7B%0A
+private void checkAndPush(Pair%3CIZyTreeNode, Integer%3E justProcessed, IZyTreeNode justProcessedNode, int justProcessedChildrenProcessed) %7B%0A        switch (justProcessedNode.getChildren().size()) %7B%0A            case 0:%0A                throw new RuntimeException(%22Error%22);%0A            case 1:%0A    
     
@@ -3272,33 +3272,62 @@
 ;%0A          
-%7D else %7B%0A
+      break;%0A            default:%0A    
             
@@ -3484,72 +3484,9 @@
     
-  %7D%0A        %7D%0A      %7D%0A    %7D%0A%0A    return !traversalStack.empty();
+%7D
 %0A  %7D

Remove verbosity