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