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 * @deprecated Scope semantics belong to the consumer project since Resolver 2.0.0. Obtain the selector from
32 * {@link org.eclipse.aether.scope.ScopeManager#getDependencySelector(org.eclipse.aether.RepositorySystemSession,
33 * org.eclipse.aether.scope.ResolutionScope)}, or use the session created by the {@code SessionBuilderSupplier} of
34 * the {@code maven-resolver-supplier-mvn3} or {@code maven-resolver-supplier-mvn4} module, which already carries the
35 * Maven scope-aware selectors.
36 */
37 @Deprecated
38 public final class OptionalDependencySelector implements DependencySelector {
39
40 private final int depth;
41
42 /**
43 * Creates a new selector to exclude optional transitive dependencies.
44 */
45 public OptionalDependencySelector() {
46 depth = 0;
47 }
48
49 private OptionalDependencySelector(int depth) {
50 this.depth = depth;
51 }
52
53 public boolean selectDependency(Dependency dependency) {
54 requireNonNull(dependency, "dependency cannot be null");
55 return depth < 2 || !dependency.isOptional();
56 }
57
58 public DependencySelector deriveChildSelector(DependencyCollectionContext context) {
59 requireNonNull(context, "context cannot be null");
60 if (depth >= 2) {
61 return this;
62 }
63
64 return new OptionalDependencySelector(depth + 1);
65 }
66
67 @Override
68 public boolean equals(Object obj) {
69 if (this == obj) {
70 return true;
71 } else if (null == obj || !getClass().equals(obj.getClass())) {
72 return false;
73 }
74
75 OptionalDependencySelector that = (OptionalDependencySelector) obj;
76 return depth == that.depth;
77 }
78
79 @Override
80 public int hashCode() {
81 int hash = getClass().hashCode();
82 hash = hash * 31 + depth;
83 return hash;
84 }
85
86 @Override
87 public String toString() {
88 return String.format("%s(depth: %d)", this.getClass().getSimpleName(), this.depth);
89 }
90 }