001    /*
002     *  This file is part of the Jikes RVM project (http://jikesrvm.org).
003     *
004     *  This file is licensed to You under the Eclipse Public License (EPL);
005     *  You may not use this file except in compliance with the License. You
006     *  may obtain a copy of the License at
007     *
008     *      http://www.opensource.org/licenses/eclipse-1.0.php
009     *
010     *  See the COPYRIGHT.txt file distributed with this work for information
011     *  regarding copyright ownership.
012     */
013    package org.jikesrvm.compilers.opt.util;
014    
015    import java.util.Enumeration;
016    import java.util.NoSuchElementException;
017    
018    
019    /**
020     * This class provides enumeration of all children of a TreeNode
021     */
022    final class TreeNodeChildrenEnumerator implements Enumeration<TreeNode> {
023    
024      /**
025       * the current child we are working on
026       */
027      private TreeNode currentChild;
028    
029      /**
030       * Provides iteration over a list of children tree nodes
031       * @param   node  Root of the tree to iterate over.
032       */
033      TreeNodeChildrenEnumerator(TreeNode node) {
034        // start at the first child
035        currentChild = node.getLeftChild();
036      }
037    
038      /**
039       * any elements left?
040       * @return whether there are any elements left
041       */
042      @Override
043      public boolean hasMoreElements() {
044        return currentChild != null;
045      }
046    
047      /**
048       * returns the next element in the list iterator
049       * @return the next element in the list iterator or null
050       */
051      @Override
052      public TreeNode nextElement() {
053        // save the return value
054        TreeNode returnValue = currentChild;
055    
056        // update the currentChild pointer, if possible
057        if (currentChild != null) {
058          currentChild = currentChild.getRightSibling();
059        } else {
060          throw new NoSuchElementException("TreeNodeChildrenEnumerator");
061        }
062    
063        // return the value
064        return returnValue;
065      }
066    }