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.apache.maven.shared.dependency.graph.internal;
20  
21  import org.eclipse.aether.collection.DependencyCollectionContext;
22  import org.eclipse.aether.collection.DependencySelector;
23  import org.eclipse.aether.graph.Dependency;
24  
25  /**
26   * A dependency selector that excludes dependencies of an specific Scope which occur beyond level one of the dependency
27   * graph.
28   *
29   * @see Dependency#getScope()
30   * @author Gabriel Belingueres
31   * @since 3.1.0
32   */
33  public class DirectScopeDependencySelector implements DependencySelector {
34  
35      private final String scope;
36  
37      private final int depth;
38  
39      public DirectScopeDependencySelector(String scope) {
40          this(scope, 0);
41      }
42  
43      private DirectScopeDependencySelector(String scope, int depth) {
44          if (scope == null) {
45              throw new IllegalArgumentException("scope is null!");
46          }
47          this.scope = scope;
48          this.depth = depth;
49      }
50  
51      /**
52       * Decides whether the specified dependency should be included in the dependency graph.
53       *
54       * @param dependency The dependency to check, must not be {@code null}.
55       * @return {@code false} if the dependency should be excluded from the children of the current node, {@code true}
56       *         otherwise.
57       */
58      @Override
59      public boolean selectDependency(Dependency dependency) {
60          return depth < 2 || !scope.equals(dependency.getScope());
61      }
62  
63      /**
64       * Derives a dependency selector for the specified collection context. When calculating the child selector,
65       * implementors are strongly advised to simply return the current instance if nothing changed to help save memory.
66       *
67       * @param context The dependency collection context, must not be {@code null}.
68       * @return The dependency selector for the target node, must not be {@code null}.
69       */
70      @Override
71      public DependencySelector deriveChildSelector(DependencyCollectionContext context) {
72          if (depth >= 2) {
73              return this;
74          }
75  
76          return new DirectScopeDependencySelector(scope, depth + 1);
77      }
78  
79      @Override
80      public int hashCode() {
81          final int prime = 31;
82          int result = 1;
83          result = prime * result + depth;
84          result = prime * result + ((scope == null) ? 0 : scope.hashCode());
85          return result;
86      }
87  
88      @Override
89      public boolean equals(Object obj) {
90          if (this == obj) {
91              return true;
92          }
93          if (obj == null) {
94              return false;
95          }
96          if (getClass() != obj.getClass()) {
97              return false;
98          }
99          DirectScopeDependencySelector other = (DirectScopeDependencySelector) obj;
100         if (depth != other.depth) {
101             return false;
102         }
103         if (scope == null) {
104             if (other.scope != null) {
105                 return false;
106             }
107         } else if (!scope.equals(other.scope)) {
108             return false;
109         }
110         return true;
111     }
112 }