View Javadoc
1   package org.eclipse.aether.util.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.Collection;
23  import java.util.HashSet;
24  import java.util.List;
25  import java.util.Set;
26  
27  import org.eclipse.aether.graph.Dependency;
28  import org.eclipse.aether.graph.DependencyFilter;
29  import org.eclipse.aether.graph.DependencyNode;
30  
31  /**
32   * A simple filter to exclude artifacts based on either artifact id or group id and artifact id.
33   */
34  public final class ExclusionsDependencyFilter
35      implements DependencyFilter
36  {
37  
38      private final Set<String> excludes = new HashSet<>();
39  
40      /**
41       * Creates a new filter using the specified exclude patterns. A pattern can either be of the form
42       * {@code groupId:artifactId} (recommended) or just {@code artifactId} (deprecated).
43       * 
44       * @param excludes The exclude patterns, may be {@code null} or empty to exclude no artifacts.
45       */
46      public ExclusionsDependencyFilter( Collection<String> excludes )
47      {
48          if ( excludes != null )
49          {
50              this.excludes.addAll( excludes );
51          }
52      }
53  
54      public boolean accept( DependencyNode node, List<DependencyNode> parents )
55      {
56          Dependency dependency = node.getDependency();
57  
58          if ( dependency == null )
59          {
60              return true;
61          }
62  
63          String id = dependency.getArtifact().getArtifactId();
64  
65          if ( excludes.contains( id ) )
66          {
67              return false;
68          }
69  
70          id = dependency.getArtifact().getGroupId() + ':' + id;
71  
72          if ( excludes.contains( id ) )
73          {
74              return false;
75          }
76  
77          return true;
78      }
79  
80      @Override
81      public boolean equals( Object obj )
82      {
83          if ( this == obj )
84          {
85              return true;
86          }
87  
88          if ( obj == null || !getClass().equals( obj.getClass() ) )
89          {
90              return false;
91          }
92  
93          ExclusionsDependencyFilter that = (ExclusionsDependencyFilter) obj;
94  
95          return this.excludes.equals( that.excludes );
96      }
97  
98      @Override
99      public int hashCode()
100     {
101         int hash = 17;
102         hash = hash * 31 + excludes.hashCode();
103         return hash;
104     }
105 
106 }