Coverage Report - org.apache.maven.shared.dependency.tree.filter.AncestorOrSelfDependencyNodeFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
AncestorOrSelfDependencyNodeFilter
100 %
14/14
100 %
8/8
2,25
 
 1  
 package org.apache.maven.shared.dependency.tree.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.tree.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: AncestorOrSelfDependencyNodeFilter.java 1100703 2011-05-08 08:27:33Z hboutemy $
 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  6
         this( Collections.singletonList( descendantNode ) );
 49  6
     }
 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
 56  
      *            the list of nodes to accept ancestors-or-self of
 57  
      */
 58  
     public AncestorOrSelfDependencyNodeFilter( List<DependencyNode> descendantNodes )
 59  6
     {
 60  6
         this.descendantNodes = descendantNodes;
 61  6
     }
 62  
 
 63  
     // DependencyNodeFilter methods -------------------------------------------
 64  
 
 65  
     /**
 66  
      * {@inheritDoc}
 67  
      */
 68  
     public boolean accept( DependencyNode node )
 69  
     {
 70  6
         for ( DependencyNode descendantNode : descendantNodes )
 71  
         {
 72  6
             if ( isAncestorOrSelf( node, descendantNode ) )
 73  
             {
 74  3
                 return true;
 75  
             }
 76  
         }
 77  
 
 78  3
         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
 87  
      *            the ancestor-or-self dependency node
 88  
      * @param descendantNode
 89  
      *            the dependency node to test
 90  
      * @return <code>true</code> if <code>ancestorNode</code> is an ancestor, or equal to,
 91  
      *         <code>descendantNode</code>
 92  
      */
 93  
     private boolean isAncestorOrSelf( DependencyNode ancestorNode, DependencyNode descendantNode )
 94  
     {
 95  6
         boolean ancestor = false;
 96  
 
 97  16
         while ( !ancestor && descendantNode != null )
 98  
         {
 99  10
             ancestor = ancestorNode.equals( descendantNode );
 100  
 
 101  10
             descendantNode = descendantNode.getParent();
 102  
         }
 103  
 
 104  6
         return ancestor;
 105  
     }
 106  
 }