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