View Javadoc
1   package org.apache.maven.shared.dependency.graph.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.graph.DependencyNode;
23  import org.apache.maven.shared.dependency.graph.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$
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 the dependency node visitor to delegate to
54       * @param filter the dependency node filter to apply before delegation
55       */
56      public FilteringDependencyNodeVisitor( DependencyNodeVisitor visitor, DependencyNodeFilter filter )
57      {
58          this.visitor = visitor;
59          this.filter = filter;
60      }
61  
62      // DependencyNodeVisitor methods ------------------------------------------
63  
64      /**
65       * {@inheritDoc}
66       */
67      @Override
68      public boolean visit( DependencyNode node )
69      {
70          boolean visit;
71  
72          if ( filter.accept( node ) )
73          {
74              visit = visitor.visit( node );
75          }
76          else
77          {
78              visit = true;
79          }
80  
81          return visit;
82      }
83  
84      /**
85       * {@inheritDoc}
86       */
87      @Override
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 }