View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.shared.dependency.graph.filter;
20  
21  import java.util.Collections;
22  import java.util.List;
23  
24  import org.apache.maven.shared.dependency.graph.DependencyNode;
25  
26  /**
27   * A dependency node filter than only accepts nodes that are ancestors of, or equal to, a given list of nodes.
28   *
29   * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
30   * @version $Id$
31   * @since 1.1
32   */
33  public class AncestorOrSelfDependencyNodeFilter implements DependencyNodeFilter {
34      // fields -----------------------------------------------------------------
35  
36      /**
37       * The list of nodes that this filter accepts ancestors-or-self of.
38       */
39      private final List<DependencyNode> descendantNodes;
40  
41      // constructors -----------------------------------------------------------
42  
43      public AncestorOrSelfDependencyNodeFilter(DependencyNode descendantNode) {
44          this(Collections.singletonList(descendantNode));
45      }
46  
47      /**
48       * Creates a dependency node filter that only accepts nodes that are ancestors of, or equal to, the specified list
49       * of nodes.
50       *
51       * @param descendantNodes the list of nodes to accept ancestors-or-self of
52       */
53      public AncestorOrSelfDependencyNodeFilter(List<DependencyNode> descendantNodes) {
54          this.descendantNodes = descendantNodes;
55      }
56  
57      // DependencyNodeFilter methods -------------------------------------------
58  
59      /**
60       * {@inheritDoc}
61       */
62      @Override
63      public boolean accept(DependencyNode node) {
64          for (DependencyNode descendantNode : descendantNodes) {
65              if (isAncestorOrSelf(node, descendantNode)) {
66                  return true;
67              }
68          }
69  
70          return false;
71      }
72  
73      // private methods --------------------------------------------------------
74  
75      /**
76       * Gets whether the first dependency node is an ancestor-or-self of the second.
77       *
78       * @param ancestorNode the ancestor-or-self dependency node
79       * @param descendantNode the dependency node to test
80       * @return <code>true</code> if <code>ancestorNode</code> is an ancestor, or equal to, <code>descendantNode</code>
81       */
82      private boolean isAncestorOrSelf(DependencyNode ancestorNode, DependencyNode descendantNode) {
83          boolean ancestor = false;
84  
85          while (!ancestor && descendantNode != null) {
86              ancestor = ancestorNode.equals(descendantNode);
87  
88              descendantNode = descendantNode.getParent();
89          }
90  
91          return ancestor;
92      }
93  }