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.graph.selector;
20  
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.Collection;
24  import java.util.HashSet;
25  import java.util.Objects;
26  import java.util.TreeSet;
27  
28  import org.eclipse.aether.collection.DependencyCollectionContext;
29  import org.eclipse.aether.collection.DependencySelector;
30  import org.eclipse.aether.graph.Dependency;
31  
32  import static java.util.Objects.requireNonNull;
33  
34  /**
35   * A dependency selector that filters transitive dependencies based on their scope. Direct dependencies are always
36   * included regardless of their scope. <em>Note:</em> This filter does not assume any relationships between the scopes.
37   * In particular, the filter is not aware of scopes that logically include other scopes.
38   *
39   * @see Dependency#getScope()
40   * @deprecated Scope semantics belong to the consumer project since Resolver 2.0.0. Obtain the selector from
41   * {@link org.eclipse.aether.scope.ScopeManager#getDependencySelector(org.eclipse.aether.RepositorySystemSession,
42   * org.eclipse.aether.scope.ResolutionScope)}, or use the session created by the {@code SessionBuilderSupplier} of
43   * the {@code maven-resolver-supplier-mvn3} or {@code maven-resolver-supplier-mvn4} module, which already carries the
44   * Maven scope-aware selectors.
45   */
46  @Deprecated
47  public final class ScopeDependencySelector implements DependencySelector {
48  
49      private final boolean transitive;
50  
51      private final Collection<String> included;
52  
53      private final Collection<String> excluded;
54  
55      /**
56       * Creates a new selector using the specified includes and excludes.
57       *
58       * @param included the set of scopes to include, may be {@code null} or empty to include any scope
59       * @param excluded the set of scopes to exclude, may be {@code null} or empty to exclude no scope
60       */
61      public ScopeDependencySelector(Collection<String> included, Collection<String> excluded) {
62          transitive = false;
63          this.included = clone(included);
64          this.excluded = clone(excluded);
65      }
66  
67      private static Collection<String> clone(Collection<String> scopes) {
68          Collection<String> copy;
69          if (scopes == null || scopes.isEmpty()) {
70              // checking for null is faster than isEmpty()
71              copy = null;
72          } else {
73              copy = new HashSet<>(scopes);
74              if (copy.size() <= 2) {
75                  // contains() is faster for smallish array (sorted for equals()!)
76                  copy = new ArrayList<>(new TreeSet<>(copy));
77              }
78          }
79          return copy;
80      }
81  
82      /**
83       * Creates a new selector using the specified excludes.
84       *
85       * @param excluded the set of scopes to exclude, may be {@code null} or empty to exclude no scope
86       */
87      public ScopeDependencySelector(String... excluded) {
88          this(null, (excluded != null) ? Arrays.asList(excluded) : null);
89      }
90  
91      private ScopeDependencySelector(boolean transitive, Collection<String> included, Collection<String> excluded) {
92          this.transitive = transitive;
93          this.included = included;
94          this.excluded = excluded;
95      }
96  
97      public boolean selectDependency(Dependency dependency) {
98          requireNonNull(dependency, "dependency cannot be null");
99          if (!transitive) {
100             return true;
101         }
102 
103         String scope = dependency.getScope();
104         return (included == null || included.contains(scope)) && (excluded == null || !excluded.contains(scope));
105     }
106 
107     public DependencySelector deriveChildSelector(DependencyCollectionContext context) {
108         requireNonNull(context, "context cannot be null");
109         if (this.transitive || context.getDependency() == null) {
110             return this;
111         }
112 
113         return new ScopeDependencySelector(true, included, excluded);
114     }
115 
116     @Override
117     public boolean equals(Object obj) {
118         if (this == obj) {
119             return true;
120         } else if (null == obj || !getClass().equals(obj.getClass())) {
121             return false;
122         }
123 
124         ScopeDependencySelector that = (ScopeDependencySelector) obj;
125         return transitive == that.transitive
126                 && Objects.equals(included, that.included)
127                 && Objects.equals(excluded, that.excluded);
128     }
129 
130     @Override
131     public int hashCode() {
132         int hash = 17;
133         hash = hash * 31 + (transitive ? 1 : 0);
134         hash = hash * 31 + (included != null ? included.hashCode() : 0);
135         hash = hash * 31 + (excluded != null ? excluded.hashCode() : 0);
136         return hash;
137     }
138 
139     @Override
140     public String toString() {
141         return String.format(
142                 "%s(included: %s, excluded: %s, transitive: %s)",
143                 getClass().getSimpleName(), included, excluded, transitive);
144     }
145 }