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  /**
23   * 
24   */
25  
26  import java.util.ArrayList;
27  import java.util.List;
28  import java.util.Set;
29  
30  import org.apache.maven.artifact.Artifact;
31  
32  /**
33   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
34   * @version $Id: FilterArtifacts.java 1716978 2015-11-28 14:47:04Z khmarbaise $
35   */
36  public class FilterArtifacts
37  {
38      private List<ArtifactsFilter> filters;
39  
40      /**
41       * Created new instance.
42       */
43      public FilterArtifacts()
44      {
45          filters = new ArrayList<ArtifactsFilter>();
46      }
47  
48      /**
49       * Removes all of the elements from this list. The list will be empty after this call returns.
50       */
51      public void clearFilters()
52      {
53          filters.clear();
54      }
55  
56      /**
57       * Appends the specified element to the end of this list.
58       * 
59       * @param filter element to be appended to this list.
60       */
61      public void addFilter( ArtifactsFilter filter )
62      {
63          if ( filter != null )
64          {
65              filters.add( filter );
66          }
67      }
68  
69      /**
70       * Inserts the specified element at the specified position in this list. Shifts the element currently at that
71       * position (if any) and any subsequent elements to the right (adds one to their indices).
72       * 
73       * @param index at which index the specified filter is to be inserted.
74       * @param filter the filter to be inserted.
75       * @throws IndexOutOfBoundsException if index is out of range <tt>(index &lt; 0 || index &gt; size())</tt>.
76       */
77      public void addFilter( int index, ArtifactsFilter filter )
78      {
79          if ( filter != null )
80          {
81              filters.add( index, filter );
82          }
83      }
84  
85      /**
86       * @param artifacts The {@link Artifact}s to filter. 
87       * @return The resulting artifacts set.
88       * @throws ArtifactFilterException in case of a failure.
89       */
90      public Set<Artifact> filter( Set<Artifact> artifacts )
91          throws ArtifactFilterException
92      {
93          // apply filters
94          for ( ArtifactsFilter filter : filters )
95          {
96              // log(artifacts,log);
97              try
98              {
99                  artifacts = filter.filter( artifacts );
100             }
101             catch ( NullPointerException e )
102             {
103                 // don't do anything, just skip this.
104                 continue;
105             }
106         }
107 
108         return artifacts;
109     }
110 
111     /**
112      * @return the filters.
113      */
114     public List<ArtifactsFilter> getFilters()
115     {
116         return this.filters;
117     }
118 
119     /**
120      * @param filters The filters to set.
121      */
122     public void setFilters( List<ArtifactsFilter> filters )
123     {
124         this.filters = filters;
125     }
126 }