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