1 package org.apache.maven.shared.dependency.tree;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
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 1100703 2011-05-08 08:27:33Z hboutemy $
32 */
33 public class DependencyTreePreorderIterator
34 implements Iterator<DependencyNode>
35 {
36 private Stack<DependencyNode> nodesToProcess = new Stack<DependencyNode>();
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 DependencyNode next()
49 {
50 if ( !hasNext() )
51 {
52 throw new NoSuchElementException();
53 }
54 DependencyNode currentNode = nodesToProcess.pop();
55 List<DependencyNode> 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 }