View Javadoc
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 1595642 2014-05-18 17:32:08Z jvanzyl $
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      public boolean accept( DependencyNode node )
68      {
69          for ( DependencyNode descendantNode : descendantNodes )
70          {
71              if ( isAncestorOrSelf( node, descendantNode ) )
72              {
73                  return true;
74              }
75          }
76  
77          return false;
78      }
79  
80      // private methods --------------------------------------------------------
81  
82      /**
83       * Gets whether the first dependency node is an ancestor-or-self of the second.
84       * 
85       * @param ancestorNode the ancestor-or-self dependency node
86       * @param descendantNode the dependency node to test
87       * @return <code>true</code> if <code>ancestorNode</code> is an ancestor, or equal to, <code>descendantNode</code>
88       */
89      private boolean isAncestorOrSelf( DependencyNode ancestorNode, DependencyNode descendantNode )
90      {
91          boolean ancestor = false;
92  
93          while ( !ancestor && descendantNode != null )
94          {
95              ancestor = ancestorNode.equals( descendantNode );
96  
97              descendantNode = descendantNode.getParent();
98          }
99  
100         return ancestor;
101     }
102 }