View Javadoc
1   package org.apache.maven.shared.artifact.filter.collection;
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 java.util.HashSet;
23  import java.util.Set;
24  
25  import org.apache.maven.artifact.Artifact;
26  
27  /**
28   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
29   * @version $Id: ProjectTransitivityFilter.java 1716978 2015-11-28 14:47:04Z khmarbaise $
30   */
31  public class ProjectTransitivityFilter
32      extends AbstractArtifactsFilter
33  {
34  
35      private boolean excludeTransitive;
36  
37      private Set<Artifact> directDependencies;
38  
39      /**
40       * @param directDependencies set of direct dependencies.
41       * @param excludeTransitive {@code true} exclude transitive deps {@code false} otherwise.
42       */
43      public ProjectTransitivityFilter( Set<Artifact> directDependencies, boolean excludeTransitive )
44      {
45          this.excludeTransitive = excludeTransitive;
46          this.directDependencies = directDependencies;
47      }
48  
49      /** {@inheritDoc}
50       */
51      public Set<Artifact> filter( Set<Artifact> artifacts )
52      {
53          // why not just take the directDependencies here?
54          // because if this filter is run after some other process, the
55          // set of artifacts may not be the same as the directDependencies.
56          Set<Artifact> result = artifacts;
57  
58          if ( excludeTransitive )
59          {
60              result = new HashSet<Artifact>();
61              for ( Artifact artifact : artifacts )
62              {
63                  if ( artifactIsADirectDependency( artifact ) )
64                  {
65                      result.add( artifact );
66                  }
67              }
68          }
69          return result;
70      }
71  
72      /**
73       * Compares the artifact to the list of dependencies to see if it is directly included by this project
74       * 
75       * @param artifact representing the item to compare.
76       * @return true if artifact is a direct dependency
77       */
78      public boolean artifactIsADirectDependency( Artifact artifact )
79      {
80          for ( Artifact dependency : this.directDependencies )
81          {
82              if ( dependency.equals( artifact ) )
83              {
84                  return true;
85              }
86          }
87          return false;
88      }
89  
90      /**
91       * @return Returns the excludeTransitive.
92       */
93      public boolean isExcludeTransitive()
94      {
95          return this.excludeTransitive;
96      }
97  
98      /**
99       * @param excludeTransitive The excludeTransitive to set.
100      */
101     public void setExcludeTransitive( boolean excludeTransitive )
102     {
103         this.excludeTransitive = excludeTransitive;
104     }
105 }