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.Arrays;
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 dependency filter based on dependency scopes. <em>Note:</em> This filter does not assume any relationships between
33   * the scopes. In particular, the filter is not aware of scopes that logically include other scopes.
34   *
35   * @see Dependency#getScope()
36   */
37  public final class ScopeDependencyFilter implements DependencyFilter {
38  
39      private final Set<String> included = new HashSet<>();
40  
41      private final Set<String> excluded = new HashSet<>();
42  
43      /**
44       * Creates a new filter using the specified includes and excludes.
45       *
46       * @param included The set of scopes to include, may be {@code null} or empty to include any scope.
47       * @param excluded The set of scopes to exclude, may be {@code null} or empty to exclude no scope.
48       */
49      public ScopeDependencyFilter(Collection<String> included, Collection<String> excluded) {
50          if (included != null) {
51              this.included.addAll(included);
52          }
53          if (excluded != null) {
54              this.excluded.addAll(excluded);
55          }
56      }
57  
58      /**
59       * Creates a new filter using the specified excludes.
60       *
61       * @param excluded The set of scopes to exclude, may be {@code null} or empty to exclude no scope.
62       */
63      public ScopeDependencyFilter(String... excluded) {
64          if (excluded != null) {
65              this.excluded.addAll(Arrays.asList(excluded));
66          }
67      }
68  
69      public boolean accept(DependencyNode node, List<DependencyNode> parents) {
70          Dependency dependency = node.getDependency();
71  
72          if (dependency == null) {
73              return true;
74          }
75  
76          String scope = node.getDependency().getScope();
77          return (included.isEmpty() || included.contains(scope)) && (excluded.isEmpty() || !excluded.contains(scope));
78      }
79  
80      @Override
81      public boolean equals(Object obj) {
82          if (this == obj) {
83              return true;
84          }
85  
86          if (obj == null || !getClass().equals(obj.getClass())) {
87              return false;
88          }
89  
90          ScopeDependencyFilter that = (ScopeDependencyFilter) obj;
91  
92          return this.included.equals(that.included) && this.excluded.equals(that.excluded);
93      }
94  
95      @Override
96      public int hashCode() {
97          int hash = 17;
98          hash = hash * 31 + included.hashCode();
99          hash = hash * 31 + excluded.hashCode();
100         return hash;
101     }
102 }