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