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 This class is deprecated. Use same named class from impl module instead.
41   */
42  @Deprecated
43  public final class ScopeDependencySelector implements DependencySelector {
44  
45      private final boolean transitive;
46  
47      private final Collection<String> included;
48  
49      private final Collection<String> excluded;
50  
51      /**
52       * Creates a new selector using the specified includes and excludes.
53       *
54       * @param included The set of scopes to include, may be {@code null} or empty to include any scope.
55       * @param excluded The set of scopes to exclude, may be {@code null} or empty to exclude no scope.
56       */
57      public ScopeDependencySelector(Collection<String> included, Collection<String> excluded) {
58          transitive = false;
59          this.included = clone(included);
60          this.excluded = clone(excluded);
61      }
62  
63      private static Collection<String> clone(Collection<String> scopes) {
64          Collection<String> copy;
65          if (scopes == null || scopes.isEmpty()) {
66              // checking for null is faster than isEmpty()
67              copy = null;
68          } else {
69              copy = new HashSet<>(scopes);
70              if (copy.size() <= 2) {
71                  // contains() is faster for smallish array (sorted for equals()!)
72                  copy = new ArrayList<>(new TreeSet<>(copy));
73              }
74          }
75          return copy;
76      }
77  
78      /**
79       * Creates a new selector using the specified excludes.
80       *
81       * @param excluded The set of scopes to exclude, may be {@code null} or empty to exclude no scope.
82       */
83      public ScopeDependencySelector(String... excluded) {
84          this(null, (excluded != null) ? Arrays.asList(excluded) : null);
85      }
86  
87      private ScopeDependencySelector(boolean transitive, Collection<String> included, Collection<String> excluded) {
88          this.transitive = transitive;
89          this.included = included;
90          this.excluded = excluded;
91      }
92  
93      public boolean selectDependency(Dependency dependency) {
94          requireNonNull(dependency, "dependency cannot be null");
95          if (!transitive) {
96              return true;
97          }
98  
99          String scope = dependency.getScope();
100         return (included == null || included.contains(scope)) && (excluded == null || !excluded.contains(scope));
101     }
102 
103     public DependencySelector deriveChildSelector(DependencyCollectionContext context) {
104         requireNonNull(context, "context cannot be null");
105         if (this.transitive || context.getDependency() == null) {
106             return this;
107         }
108 
109         return new ScopeDependencySelector(true, included, excluded);
110     }
111 
112     @Override
113     public boolean equals(Object obj) {
114         if (this == obj) {
115             return true;
116         } else if (null == obj || !getClass().equals(obj.getClass())) {
117             return false;
118         }
119 
120         ScopeDependencySelector that = (ScopeDependencySelector) obj;
121         return transitive == that.transitive
122                 && Objects.equals(included, that.included)
123                 && Objects.equals(excluded, that.excluded);
124     }
125 
126     @Override
127     public int hashCode() {
128         int hash = 17;
129         hash = hash * 31 + (transitive ? 1 : 0);
130         hash = hash * 31 + (included != null ? included.hashCode() : 0);
131         hash = hash * 31 + (excluded != null ? excluded.hashCode() : 0);
132         return hash;
133     }
134 
135     @Override
136     public String toString() {
137         return String.format(
138                 "%s(included: %s, excluded: %s, transitive: %s)",
139                 getClass().getSimpleName(), included, excluded, transitive);
140     }
141 }