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