1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
29
30
31
32 public final class OptionalDependencySelector implements DependencySelector {
33
34 private final int depth;
35
36
37
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 }