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 public boolean hasMoreElements() {
043 return currentChild != null;
044 }
045
046 /**
047 * returns the next element in the list iterator
048 * @return the next element in the list iterator or null
049 */
050 public TreeNode nextElement() {
051 // save the return value
052 TreeNode returnValue = currentChild;
053
054 // update the currentChild pointer, if possible
055 if (currentChild != null) {
056 currentChild = currentChild.getRightSibling();
057 } else {
058 throw new NoSuchElementException("TreeNodeChildrenEnumerator");
059 }
060
061 // return the value
062 return returnValue;
063 }
064 }
065
066
067