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.Collection;
22 import java.util.Objects;
23
24 import org.eclipse.aether.collection.DependencyCollectionContext;
25 import org.eclipse.aether.collection.DependencySelector;
26 import org.eclipse.aether.graph.Dependency;
27
28 import static java.util.Objects.requireNonNull;
29
30
31
32
33
34
35
36
37
38
39
40
41
42 public final class ScopeDependencySelector implements DependencySelector {
43
44
45
46
47 public static ScopeDependencySelector legacy(Collection<String> included, Collection<String> excluded) {
48 return new ScopeDependencySelector(
49 Objects.hash(true, 0, 1, Integer.MAX_VALUE, included, excluded),
50 true,
51 0,
52 1,
53 Integer.MAX_VALUE,
54 included,
55 excluded);
56 }
57
58
59
60
61 public static ScopeDependencySelector fromRoot(Collection<String> included, Collection<String> excluded) {
62 return from(1, included, excluded);
63 }
64
65
66
67
68 public static ScopeDependencySelector fromDirect(Collection<String> included, Collection<String> excluded) {
69 return from(2, included, excluded);
70 }
71
72
73
74
75 public static ScopeDependencySelector from(
76 int applyFrom, Collection<String> included, Collection<String> excluded) {
77 return fromTo(applyFrom, Integer.MAX_VALUE, included, excluded);
78 }
79
80
81
82
83
84 public static ScopeDependencySelector fromTo(
85 int applyFrom, int applyTo, Collection<String> included, Collection<String> excluded) {
86 if (applyFrom < 1) {
87 throw new IllegalArgumentException("applyFrom must be non-zero and positive");
88 }
89 if (applyFrom > applyTo) {
90 throw new IllegalArgumentException("applyTo must be greater or equal than applyFrom");
91 }
92 return new ScopeDependencySelector(
93 Objects.hash(false, 0, applyFrom, applyTo, included, excluded),
94 false,
95 0,
96 applyFrom,
97 applyTo,
98 included,
99 excluded);
100 }
101
102 private final int seed;
103 private final boolean shiftIfRootNull;
104 private final int depth;
105 private final int applyFrom;
106 private final int applyTo;
107 private final Collection<String> included;
108 private final Collection<String> excluded;
109
110 private ScopeDependencySelector(
111 int seed,
112 boolean shiftIfRootNull,
113 int depth,
114 int applyFrom,
115 int applyTo,
116 Collection<String> included,
117 Collection<String> excluded) {
118 this.seed = seed;
119 this.shiftIfRootNull = shiftIfRootNull;
120 this.depth = depth;
121 this.applyFrom = applyFrom;
122 this.applyTo = applyTo;
123 this.included = included;
124 this.excluded = excluded;
125 }
126
127 @Override
128 public boolean selectDependency(Dependency dependency) {
129 requireNonNull(dependency, "dependency cannot be null");
130 if (depth < applyFrom || depth > applyTo) {
131 return true;
132 }
133
134 String scope = dependency.getScope();
135 return (included == null || included.contains(scope)) && (excluded == null || !excluded.contains(scope));
136 }
137
138 @Override
139 public DependencySelector deriveChildSelector(DependencyCollectionContext context) {
140 requireNonNull(context, "context cannot be null");
141 if (depth == 0 && shiftIfRootNull && context.getDependency() == null) {
142 return new ScopeDependencySelector(
143 seed, shiftIfRootNull, depth + 1, applyFrom + 1, applyTo, included, excluded);
144 }
145 if (depth >= applyFrom && depth != applyTo) {
146 return this;
147 }
148 return new ScopeDependencySelector(seed, shiftIfRootNull, depth + 1, applyFrom, applyTo, included, excluded);
149 }
150
151 @Override
152 public boolean equals(Object obj) {
153 if (this == obj) {
154 return true;
155 } else if (null == obj || !getClass().equals(obj.getClass())) {
156 return false;
157 }
158
159 ScopeDependencySelector that = (ScopeDependencySelector) obj;
160 return seed == that.seed
161 && shiftIfRootNull == that.shiftIfRootNull
162 && depth == that.depth
163 && applyFrom == that.applyFrom
164 && applyTo == that.applyTo
165 && Objects.equals(included, that.included)
166 && Objects.equals(excluded, that.excluded);
167 }
168
169 @Override
170 public int hashCode() {
171 int hash = 17;
172 hash = hash * 31 + seed;
173 hash = hash * 31 + (shiftIfRootNull ? 0 : 1);
174 hash = hash * 31 + depth;
175 hash = hash * 31 + applyFrom;
176 hash = hash * 31 + applyTo;
177 hash = hash * 31 + (included != null ? included.hashCode() : 0);
178 hash = hash * 31 + (excluded != null ? excluded.hashCode() : 0);
179 return hash;
180 }
181
182 @Override
183 public String toString() {
184 return String.format(
185 "%s(included: %s, excluded: %s, applied: %s)",
186 getClass().getSimpleName(), included, excluded, depth >= applyFrom);
187 }
188 }