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.traversal;
20
21 import org.apache.maven.shared.dependency.graph.DependencyNode;
22 import org.apache.maven.shared.dependency.graph.filter.DependencyNodeFilter;
23
24 /**
25 * A dependency node visitor that filters nodes and delegates to another visitor.
26 *
27 * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
28 * @version $Id$
29 * @since 1.1
30 */
31 public class FilteringDependencyNodeVisitor implements DependencyNodeVisitor {
32 // fields -----------------------------------------------------------------
33
34 /**
35 * The dependency node visitor to delegate to.
36 */
37 private final DependencyNodeVisitor visitor;
38
39 /**
40 * The dependency node filter to apply before delegation.
41 */
42 private final DependencyNodeFilter filter;
43
44 // constructors -----------------------------------------------------------
45
46 /**
47 * Creates a dependency node visitor that delegates nodes that are accepted by the specified filter to the specified
48 * visitor.
49 *
50 * @param visitor the dependency node visitor to delegate to
51 * @param filter the dependency node filter to apply before delegation
52 */
53 public FilteringDependencyNodeVisitor(DependencyNodeVisitor visitor, DependencyNodeFilter filter) {
54 this.visitor = visitor;
55 this.filter = filter;
56 }
57
58 // DependencyNodeVisitor methods ------------------------------------------
59
60 /**
61 * {@inheritDoc}
62 */
63 @Override
64 public boolean visit(DependencyNode node) {
65 boolean visit;
66
67 if (filter.accept(node)) {
68 visit = visitor.visit(node);
69 } else {
70 visit = true;
71 }
72
73 return visit;
74 }
75
76 /**
77 * {@inheritDoc}
78 */
79 @Override
80 public boolean endVisit(DependencyNode node) {
81 boolean visit;
82
83 if (filter.accept(node)) {
84 visit = visitor.endVisit(node);
85 } else {
86 visit = true;
87 }
88
89 return visit;
90 }
91
92 // public methods ---------------------------------------------------------
93
94 /**
95 * Gets the dependency node visitor that this visitor delegates to.
96 *
97 * @return the dependency node visitor
98 */
99 public DependencyNodeVisitor getDependencyNodeVisitor() {
100 return visitor;
101 }
102
103 /**
104 * Gets the dependency node filter that this visitor applies before delegation.
105 *
106 * @return the dependency node filter
107 */
108 public DependencyNodeFilter getDependencyNodeFilter() {
109 return filter;
110 }
111 }