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.artifact.resolver.filter;
20  
21  import java.util.ArrayList;
22  import java.util.Iterator;
23  import java.util.LinkedHashSet;
24  import java.util.List;
25  import java.util.Set;
26  
27  import org.apache.maven.artifact.Artifact;
28  
29  /**
30   * Apply multiple filters.
31   *
32   */
33  public class AndArtifactFilter implements ArtifactFilter {
34      private Set<ArtifactFilter> filters;
35  
36      public AndArtifactFilter() {
37          this.filters = new LinkedHashSet<>();
38      }
39  
40      public AndArtifactFilter(List<ArtifactFilter> filters) {
41          this.filters = new LinkedHashSet<>(filters);
42      }
43  
44      public boolean include(Artifact artifact) {
45          boolean include = true;
46          for (Iterator<ArtifactFilter> i = filters.iterator(); i.hasNext() && include; ) {
47              ArtifactFilter filter = i.next();
48              if (!filter.include(artifact)) {
49                  include = false;
50              }
51          }
52          return include;
53      }
54  
55      public void add(ArtifactFilter artifactFilter) {
56          filters.add(artifactFilter);
57      }
58  
59      public List<ArtifactFilter> getFilters() {
60          return new ArrayList<>(filters);
61      }
62  
63      @Override
64      public int hashCode() {
65          int hash = 17;
66          hash = hash * 31 + filters.hashCode();
67          return hash;
68      }
69  
70      @Override
71      public boolean equals(Object obj) {
72          if (this == obj) {
73              return true;
74          }
75  
76          if (!(obj instanceof AndArtifactFilter)) {
77              return false;
78          }
79  
80          AndArtifactFilter other = (AndArtifactFilter) obj;
81  
82          return filters.equals(other.filters);
83      }
84  }