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 org.eclipse.aether.collection.DependencyCollectionContext;
22  import org.eclipse.aether.collection.DependencySelector;
23  import org.eclipse.aether.graph.Dependency;
24  
25  import static java.util.Objects.requireNonNull;
26  
27  /**
28   * A dependency selector that excludes optional dependencies which occur beyond level one of the dependency graph.
29   *
30   * @see Dependency#isOptional()
31   */
32  public final class OptionalDependencySelector implements DependencySelector {
33  
34      private final int depth;
35  
36      /**
37       * Creates a new selector to exclude optional transitive dependencies.
38       */
39      public OptionalDependencySelector() {
40          depth = 0;
41      }
42  
43      private OptionalDependencySelector(int depth) {
44          this.depth = depth;
45      }
46  
47      public boolean selectDependency(Dependency dependency) {
48          requireNonNull(dependency, "dependency cannot be null");
49          return depth < 2 || !dependency.isOptional();
50      }
51  
52      public DependencySelector deriveChildSelector(DependencyCollectionContext context) {
53          requireNonNull(context, "context cannot be null");
54          if (depth >= 2) {
55              return this;
56          }
57  
58          return new OptionalDependencySelector(depth + 1);
59      }
60  
61      @Override
62      public boolean equals(Object obj) {
63          if (this == obj) {
64              return true;
65          } else if (null == obj || !getClass().equals(obj.getClass())) {
66              return false;
67          }
68  
69          OptionalDependencySelector that = (OptionalDependencySelector) obj;
70          return depth == that.depth;
71      }
72  
73      @Override
74      public int hashCode() {
75          int hash = getClass().hashCode();
76          hash = hash * 31 + depth;
77          return hash;
78      }
79  
80      @Override
81      public String toString() {
82          return String.format("%s(depth: %d)", this.getClass().getSimpleName(), this.depth);
83      }
84  }