1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.internal.impl.scope;
20
21 import java.util.Objects;
22
23 import org.eclipse.aether.collection.DependencyCollectionContext;
24 import org.eclipse.aether.collection.DependencySelector;
25 import org.eclipse.aether.graph.Dependency;
26
27 import static java.util.Objects.requireNonNull;
28
29
30
31
32
33
34
35
36
37 public final class OptionalDependencySelector implements DependencySelector {
38
39
40
41 public static OptionalDependencySelector fromRoot() {
42 return from(1);
43 }
44
45
46
47
48 public static OptionalDependencySelector fromDirect() {
49 return from(2);
50 }
51
52
53
54
55 public static OptionalDependencySelector from(int applyFrom) {
56 if (applyFrom < 1) {
57 throw new IllegalArgumentException("applyFrom must be non-zero and positive");
58 }
59 return new OptionalDependencySelector(Objects.hash(applyFrom), 0, applyFrom);
60 }
61
62 private final int seed;
63 private final int depth;
64 private final int applyFrom;
65
66 private OptionalDependencySelector(int seed, int depth, int applyFrom) {
67 this.seed = seed;
68 this.depth = depth;
69 this.applyFrom = applyFrom;
70 }
71
72 @Override
73 public boolean selectDependency(Dependency dependency) {
74 requireNonNull(dependency, "dependency cannot be null");
75 return depth < applyFrom || !dependency.isOptional();
76 }
77
78 @Override
79 public DependencySelector deriveChildSelector(DependencyCollectionContext context) {
80 requireNonNull(context, "context cannot be null");
81 return new OptionalDependencySelector(seed, depth + 1, applyFrom);
82 }
83
84 @Override
85 public boolean equals(Object obj) {
86 if (this == obj) {
87 return true;
88 } else if (null == obj || !getClass().equals(obj.getClass())) {
89 return false;
90 }
91
92 OptionalDependencySelector that = (OptionalDependencySelector) obj;
93 return seed == that.seed && depth == that.depth && applyFrom == that.applyFrom;
94 }
95
96 @Override
97 public int hashCode() {
98 int hash = getClass().hashCode();
99 hash = hash * 31 + seed;
100 hash = hash * 31 + depth;
101 hash = hash * 31 + applyFrom;
102 return hash;
103 }
104
105 @Override
106 public String toString() {
107 return String.format("%s(applied: %s)", this.getClass().getSimpleName(), depth >= applyFrom);
108 }
109 }