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