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.Arrays;
23  import java.util.LinkedHashSet;
24  import java.util.List;
25  import java.util.Objects;
26  import java.util.Set;
27  
28  import org.apache.maven.artifact.Artifact;
29  
30  import static org.apache.maven.shared.artifact.filter.internal.Utils.isNotEmpty;
31  
32  /**
33   * This is the common base class of ClassifierFilter and TypeFilter
34   *
35   * @author <a href="richardv@mxtelecom.com">Richard van der Hoff</a>
36   */
37  public abstract class AbstractArtifactFeatureFilter
38      extends AbstractArtifactsFilter
39  {
40      /** The list of types or classifiers to include */
41      private List<String> includes;
42  
43      /**
44       * The list of types or classifiers to exclude (ignored if includes != null)
45       */
46      private List<String> excludes;
47  
48      /**
49       * <p>Constructor for AbstractArtifactFeatureFilter.</p>
50       *
51       * @param include comma separated list with includes.
52       * @param exclude comma separated list with excludes.
53       */
54      public AbstractArtifactFeatureFilter( String include, String exclude )
55      {
56          setExcludes( exclude );
57          setIncludes( include );
58      }
59  
60      /**
61       * {@inheritDoc}
62       *
63       * This function determines if filtering needs to be performed. Includes are processed before Excludes.
64       */
65      public Set<Artifact> filter( Set<Artifact> artifacts )
66      {
67          Set<Artifact> results = artifacts;
68  
69          if ( this.includes != null && !this.includes.isEmpty() )
70          {
71              results = filterIncludes( results, this.includes );
72          }
73  
74          if ( this.excludes != null && !this.excludes.isEmpty() )
75          {
76              results = filterExcludes( results, this.excludes );
77          }
78  
79          return results;
80      }
81  
82      /**
83       * Processes the dependencies list and includes the dependencies that match a filter in the list.
84       * 
85       * @param artifacts List of dependencies.
86       * @param theIncludes List of types or classifiers to include.
87       * @return a set of filtered artifacts.
88       */
89      private Set<Artifact> filterIncludes( Set<Artifact> artifacts, List<String> theIncludes )
90      {
91          Set<Artifact> result = new LinkedHashSet<>();
92  
93          for ( String include : theIncludes )
94          {
95              for ( Artifact artifact : artifacts )
96              {
97                  // if the classifier or type of the artifact
98                  // matches the feature
99                  // to include, add to the
100                 // results
101                 if ( compareFeatures( getArtifactFeature( artifact ), include ) )
102                 {
103                     result.add( artifact );
104                 }
105             }
106         }
107         return result;
108     }
109 
110     /**
111      * Processes the dependencies list and excludes the dependencies that match a filter in the list.
112      * 
113      * @param artifacts List of dependencies.
114      * @param theExcludes List of types or classifiers to exclude.
115      * @return a set of filtered artifacts.
116      */
117     private Set<Artifact> filterExcludes( Set<Artifact> artifacts, List<String> theExcludes )
118     {
119         Set<Artifact> result = new LinkedHashSet<>();
120 
121         for ( Artifact artifact : artifacts )
122         {
123             boolean exclude = false;
124             String artifactFeature = getArtifactFeature( artifact );
125 
126             // look through all types or classifiers. If no
127             // matches are found
128             // then it can be added to the results.
129             for ( String excludeFeature : theExcludes )
130             {
131                 if ( compareFeatures( artifactFeature, excludeFeature ) )
132                 {
133                     exclude = true;
134                     break;
135                 }
136             }
137 
138             if ( !exclude )
139             {
140                 result.add( artifact );
141             }
142         }
143 
144         return result;
145     }
146 
147     /**
148      * Should return the type or classifier of the given artifact, so that we can filter it
149      *
150      * @param artifact artifact to return type or classifier of
151      * @return type or classifier
152      */
153     protected abstract String getArtifactFeature( Artifact artifact );
154 
155     /**
156      * <p>Setter for the field <code>excludes</code>.</p>
157      *
158      * @param excludeString comma separated list with excludes.
159      */
160     public void setExcludes( String excludeString )
161     {
162         if ( isNotEmpty( excludeString ) )
163         {
164             this.excludes = Arrays.asList( excludeString.split( "," ) );
165         }
166     }
167 
168     /**
169      * <p>Setter for the field <code>includes</code>.</p>
170      *
171      * @param includeString comma separated list with includes.
172      */
173     public void setIncludes( String includeString )
174     {
175         if ( isNotEmpty( includeString ) )
176         {
177             this.includes = Arrays.asList( includeString.split( "," ) );
178         }
179     }
180 
181     /**
182      * <p>Getter for the field <code>excludes</code>.</p>
183      *
184      * @return Returns the excludes.
185      */
186     public List<String> getExcludes()
187     {
188         return this.excludes;
189     }
190 
191     /**
192      * <p>Getter for the field <code>includes</code>.</p>
193      *
194      * @return Returns the includes.
195      */
196     public List<String> getIncludes()
197     {
198         return this.includes;
199     }
200 
201     /**
202      * Allows Feature comparison to be customized
203      *
204      * @param lhs String artifact's feature
205      * @param rhs String feature from exclude or include list
206      * @return boolean true if features match
207      */
208     protected boolean compareFeatures( String lhs, String rhs )
209     {
210         return ( Objects.equals( lhs, rhs ) );
211     }
212 }