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.NoSuchElementException; 24 import java.util.Stack; 25 26 /** 27 * {@link Iterator} for {@link DependencyNode} implementing a traversal from leaves to root. TODO 28 * {@link #DependencyTreeInverseIterator(DependencyNode)} is costly, a better implementation would move the cost to 29 * {@link #next()} 30 * 31 * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a> 32 * @version $Id: DependencyTreeInverseIterator.java 1595642 2014-05-18 17:32:08Z jvanzyl $ 33 */ 34 public class DependencyTreeInverseIterator 35 implements Iterator<DependencyNode> 36 { 37 private Stack<DependencyNode> nodesToProcess = new Stack<DependencyNode>(); 38 39 public DependencyTreeInverseIterator( DependencyNode rootNode ) 40 { 41 DependencyTreePreorderIterator it = new DependencyTreePreorderIterator( rootNode ); 42 while ( it.hasNext() ) 43 { 44 nodesToProcess.push( it.next() ); 45 } 46 } 47 48 public boolean hasNext() 49 { 50 return !nodesToProcess.isEmpty(); 51 } 52 53 public DependencyNode next() 54 { 55 if ( !hasNext() ) 56 { 57 throw new NoSuchElementException(); 58 } 59 return nodesToProcess.pop(); 60 } 61 62 /** 63 * @throws UnsupportedOperationException 64 */ 65 public void remove() 66 { 67 throw new UnsupportedOperationException(); 68 } 69 }