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