View Javadoc
1   package org.apache.maven.artifact.resolver.filter;
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.ArrayList;
23  import java.util.Iterator;
24  import java.util.LinkedHashSet;
25  import java.util.List;
26  import java.util.Set;
27  
28  import org.apache.maven.artifact.Artifact;
29  
30  /**
31   * Filter to include from a list of artifact patterns.
32   *
33   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
34   */
35  public class IncludesArtifactFilter
36      implements ArtifactFilter
37  {
38      private final Set<String> patterns;
39  
40      public IncludesArtifactFilter( List<String> patterns )
41      {
42          this.patterns = new LinkedHashSet<>( patterns );
43      }
44  
45      public boolean include( Artifact artifact )
46      {
47          String id = artifact.getGroupId() + ":" + artifact.getArtifactId();
48  
49          boolean matched = false;
50          for ( Iterator<String> i = patterns.iterator(); i.hasNext() & !matched; )
51          {
52              // TODO: what about wildcards? Just specifying groups? versions?
53              if ( id.equals( i.next() ) )
54              {
55                  matched = true;
56              }
57          }
58          return matched;
59      }
60  
61      public List<String> getPatterns()
62      {
63          return new ArrayList<>( patterns );
64      }
65  
66      @Override
67      public int hashCode()
68      {
69          int hash = 17;
70          hash = hash * 31 + patterns.hashCode();
71  
72          return hash;
73      }
74  
75      @Override
76      public boolean equals( Object obj )
77      {
78          if ( this == obj )
79          {
80              return true;
81          }
82  
83          // make sure IncludesArtifactFilter is not equal ExcludesArtifactFilter!
84          if ( obj == null || getClass() != obj.getClass() )
85          {
86              return false;
87          }
88  
89          IncludesArtifactFilter other = (IncludesArtifactFilter) obj;
90  
91          return patterns.equals( other.patterns );
92      }
93  }