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 java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.HashSet;
25 import java.util.Objects;
26 import java.util.TreeSet;
27
28 import org.eclipse.aether.collection.DependencyCollectionContext;
29 import org.eclipse.aether.collection.DependencySelector;
30 import org.eclipse.aether.graph.Dependency;
31
32 import static java.util.Objects.requireNonNull;
33
34
35
36
37
38
39
40
41
42
43 @Deprecated
44 public final class ScopeDependencySelector implements DependencySelector {
45
46 private final boolean transitive;
47
48 private final Collection<String> included;
49
50 private final Collection<String> excluded;
51
52
53
54
55
56
57
58 public ScopeDependencySelector(Collection<String> included, Collection<String> excluded) {
59 transitive = false;
60 this.included = clone(included);
61 this.excluded = clone(excluded);
62 }
63
64 private static Collection<String> clone(Collection<String> scopes) {
65 Collection<String> copy;
66 if (scopes == null || scopes.isEmpty()) {
67
68 copy = null;
69 } else {
70 copy = new HashSet<>(scopes);
71 if (copy.size() <= 2) {
72
73 copy = new ArrayList<>(new TreeSet<>(copy));
74 }
75 }
76 return copy;
77 }
78
79
80
81
82
83
84 public ScopeDependencySelector(String... excluded) {
85 this(null, (excluded != null) ? Arrays.asList(excluded) : null);
86 }
87
88 private ScopeDependencySelector(boolean transitive, Collection<String> included, Collection<String> excluded) {
89 this.transitive = transitive;
90 this.included = included;
91 this.excluded = excluded;
92 }
93
94 public boolean selectDependency(Dependency dependency) {
95 requireNonNull(dependency, "dependency cannot be null");
96 if (!transitive) {
97 return true;
98 }
99
100 String scope = dependency.getScope();
101 return (included == null || included.contains(scope)) && (excluded == null || !excluded.contains(scope));
102 }
103
104 public DependencySelector deriveChildSelector(DependencyCollectionContext context) {
105 requireNonNull(context, "context cannot be null");
106 if (this.transitive || context.getDependency() == null) {
107 return this;
108 }
109
110 return new ScopeDependencySelector(true, included, excluded);
111 }
112
113 @Override
114 public boolean equals(Object obj) {
115 if (this == obj) {
116 return true;
117 } else if (null == obj || !getClass().equals(obj.getClass())) {
118 return false;
119 }
120
121 ScopeDependencySelector that = (ScopeDependencySelector) obj;
122 return transitive == that.transitive
123 && Objects.equals(included, that.included)
124 && Objects.equals(excluded, that.excluded);
125 }
126
127 @Override
128 public int hashCode() {
129 int hash = 17;
130 hash = hash * 31 + (transitive ? 1 : 0);
131 hash = hash * 31 + (included != null ? included.hashCode() : 0);
132 hash = hash * 31 + (excluded != null ? excluded.hashCode() : 0);
133 return hash;
134 }
135
136 @Override
137 public String toString() {
138 return String.format(
139 "%s(included: %s, excluded: %s, transitive: %s)",
140 getClass().getSimpleName(), included, excluded, transitive);
141 }
142 }