View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.shared.artifact.filter.collection;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  import java.util.Set;
24  
25  import org.apache.maven.artifact.Artifact;
26  
27  /**
28   * <p>FilterArtifacts class.</p>
29   *
30   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
31   */
32  public class FilterArtifacts {
33      private List<ArtifactsFilter> filters;
34  
35      /**
36       * Created new instance.
37       */
38      public FilterArtifacts() {
39          filters = new ArrayList<>();
40      }
41  
42      /**
43       * Removes all of the elements from this list. The list will be empty after this call returns.
44       */
45      public void clearFilters() {
46          filters.clear();
47      }
48  
49      /**
50       * Appends the specified element to the end of this list.
51       *
52       * @param filter element to be appended to this list.
53       */
54      public void addFilter(ArtifactsFilter filter) {
55          if (filter != null) {
56              filters.add(filter);
57          }
58      }
59  
60      /**
61       * Inserts the specified element at the specified position in this list. Shifts the element currently at that
62       * position (if any) and any subsequent elements to the right (adds one to their indices).
63       *
64       * @param index at which index the specified filter is to be inserted.
65       * @param filter the filter to be inserted.
66       * @throws IndexOutOfBoundsException if index is out of range <code>(index &lt; 0 || index &gt; size())</code>.
67       */
68      public void addFilter(int index, ArtifactsFilter filter) {
69          if (filter != null) {
70              filters.add(index, filter);
71          }
72      }
73  
74      /**
75       * <p>filter.</p>
76       *
77       * @param artifacts The {@link org.apache.maven.artifact.Artifact}s to filter.
78       * @return The resulting artifacts set.
79       * @throws org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException in case of a failure.
80       */
81      public Set<Artifact> filter(Set<Artifact> artifacts) throws ArtifactFilterException {
82          // apply filters
83          for (ArtifactsFilter filter : filters) {
84              // log(artifacts,log);
85              try {
86                  artifacts = filter.filter(artifacts);
87              } catch (NullPointerException e) {
88                  // don't do anything, just skip this.
89              }
90          }
91  
92          return artifacts;
93      }
94  
95      /**
96       * <p>Getter for the field <code>filters</code>.</p>
97       *
98       * @return the filters.
99       */
100     public List<ArtifactsFilter> getFilters() {
101         return this.filters;
102     }
103 
104     /**
105      * <p>Setter for the field <code>filters</code>.</p>
106      *
107      * @param filters The filters to set.
108      */
109     public void setFilters(List<ArtifactsFilter> filters) {
110         this.filters = filters;
111     }
112 }