1 package org.apache.maven.shared.dependency.graph.filter; 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.Collections; 23 import java.util.List; 24 25 import org.apache.maven.shared.dependency.graph.DependencyNode; 26 27 /** 28 * A dependency node filter than only accepts nodes that are ancestors of, or equal to, a given list of nodes. 29 * 30 * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a> 31 * @version $Id$ 32 * @since 1.1 33 */ 34 public class AncestorOrSelfDependencyNodeFilter 35 implements DependencyNodeFilter 36 { 37 // fields ----------------------------------------------------------------- 38 39 /** 40 * The list of nodes that this filter accepts ancestors-or-self of. 41 */ 42 private final List<DependencyNode> descendantNodes; 43 44 // constructors ----------------------------------------------------------- 45 46 public AncestorOrSelfDependencyNodeFilter( DependencyNode descendantNode ) 47 { 48 this( Collections.singletonList( descendantNode ) ); 49 } 50 51 /** 52 * Creates a dependency node filter that only accepts nodes that are ancestors of, or equal to, the specified list 53 * of nodes. 54 * 55 * @param descendantNodes the list of nodes to accept ancestors-or-self of 56 */ 57 public AncestorOrSelfDependencyNodeFilter( List<DependencyNode> descendantNodes ) 58 { 59 this.descendantNodes = descendantNodes; 60 } 61 62 // DependencyNodeFilter methods ------------------------------------------- 63 64 /** 65 * {@inheritDoc} 66 */ 67 @Override 68 public boolean accept( DependencyNode node ) 69 { 70 for ( DependencyNode descendantNode : descendantNodes ) 71 { 72 if ( isAncestorOrSelf( node, descendantNode ) ) 73 { 74 return true; 75 } 76 } 77 78 return false; 79 } 80 81 // private methods -------------------------------------------------------- 82 83 /** 84 * Gets whether the first dependency node is an ancestor-or-self of the second. 85 * 86 * @param ancestorNode the ancestor-or-self dependency node 87 * @param descendantNode the dependency node to test 88 * @return <code>true</code> if <code>ancestorNode</code> is an ancestor, or equal to, <code>descendantNode</code> 89 */ 90 private boolean isAncestorOrSelf( DependencyNode ancestorNode, DependencyNode descendantNode ) 91 { 92 boolean ancestor = false; 93 94 while ( !ancestor && descendantNode != null ) 95 { 96 ancestor = ancestorNode.equals( descendantNode ); 97 98 descendantNode = descendantNode.getParent(); 99 } 100 101 return ancestor; 102 } 103 }