1 package org.apache.maven.shared.dependency.tree;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.NoSuchElementException;
25 import java.util.Stack;
26
27 /**
28 * {@link Iterator} for {@link DependencyNode} implementing a preoder traversal.
29 *
30 * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
31 * @version $Id: DependencyTreePreorderIterator.java 661727 2008-05-30 14:21:49Z bentmann $
32 */
33 public class DependencyTreePreorderIterator
34 implements Iterator
35 {
36 private Stack nodesToProcess = new Stack();
37
38 public DependencyTreePreorderIterator( DependencyNode rootNode )
39 {
40 nodesToProcess.push( rootNode );
41 }
42
43 public boolean hasNext()
44 {
45 return !nodesToProcess.isEmpty();
46 }
47
48 public Object next()
49 {
50 if ( !hasNext() )
51 {
52 throw new NoSuchElementException();
53 }
54 DependencyNode currentNode = (DependencyNode) nodesToProcess.pop();
55 List children = currentNode.getChildren();
56 if ( children != null )
57 {
58 for ( int i = children.size() - 1; i >= 0; i-- )
59 {
60 nodesToProcess.push( children.get( i ) );
61 }
62 }
63 return currentNode;
64 }
65
66 /**
67 * @throws UnsupportedOperationException
68 */
69 public void remove()
70 {
71 throw new UnsupportedOperationException();
72 }
73 }