View Javadoc
1   package org.eclipse.aether.util.graph.visitor;
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 static java.util.Objects.requireNonNull;
23  
24  import org.eclipse.aether.graph.DependencyFilter;
25  import org.eclipse.aether.graph.DependencyNode;
26  import org.eclipse.aether.graph.DependencyVisitor;
27  
28  /**
29   * A dependency visitor that delegates to another visitor if nodes match a filter. Note that in case of a mismatching
30   * node, the children of that node are still visisted and presented to the filter.
31   */
32  public final class FilteringDependencyVisitor
33      implements DependencyVisitor
34  {
35  
36      private final DependencyFilter filter;
37  
38      private final DependencyVisitor visitor;
39  
40      private final Stack<Boolean> accepts;
41  
42      private final Stack<DependencyNode> parents;
43  
44      /**
45       * Creates a new visitor that delegates traversal of nodes matching the given filter to the specified visitor.
46       *
47       * @param visitor The visitor to delegate to, must not be {@code null}.
48       * @param filter The filter to apply, may be {@code null} to not filter.
49       */
50      public FilteringDependencyVisitor( DependencyVisitor visitor, DependencyFilter filter )
51      {
52          this.visitor = requireNonNull( visitor, "dependency visitor delegate cannot be null" );
53          this.filter = filter;
54          this.accepts = new Stack<>();
55          this.parents = new Stack<>();
56      }
57  
58      /**
59       * Gets the visitor to which this visitor delegates to.
60       * 
61       * @return The visitor being delegated to, never {@code null}.
62       */
63      public DependencyVisitor getVisitor()
64      {
65          return visitor;
66      }
67  
68      /**
69       * Gets the filter being applied before delegation.
70       * 
71       * @return The filter being applied or {@code null} if none.
72       */
73      public DependencyFilter getFilter()
74      {
75          return filter;
76      }
77  
78      public boolean visitEnter( DependencyNode node )
79      {
80          boolean accept = filter == null || filter.accept( node, parents );
81  
82          accepts.push( accept );
83  
84          parents.push( node );
85  
86          if ( accept )
87          {
88              return visitor.visitEnter( node );
89          }
90          else
91          {
92              return true;
93          }
94      }
95  
96      public boolean visitLeave( DependencyNode node )
97      {
98          parents.pop();
99  
100         Boolean accept = accepts.pop();
101 
102         if ( accept )
103         {
104             return visitor.visitLeave( node );
105         }
106         else
107         {
108             return true;
109         }
110     }
111 
112 }